-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
NetlifyDeployPlugin.php
275 lines (242 loc) · 6.78 KB
/
NetlifyDeployPlugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
<?php
/**
* Plugin Name: Netlify Deploy
* Plugin URI: https://github.com/pixelcollective/netlify-deploy
* Description: Run netlify builds when site content is updated
* Version: 0.1.0
* Author: Tiny Pixel Collective
* Author URI: https://tinypixel.dev
* License: MIT License
* Text Domain: netlify-deploy
* Domain Path: /languages
*/
namespace TinyPixel;
class NetlifyDeployPlugin
{
/**
* Instance
* @static object
*/
public static $instance;
/**
* Composer autoload location
* @var string
*/
public $dependencies = __DIR__ . '/vendor/autoload.php';
/**
* Error handler in case of autoload failure
* @var string
*/
public $errorHandler = __DIR__ . '/src/Error.php';
/**
* Default PostTypes
* @var array
*/
public $postTypes = [
'post',
'page',
];
/**
* Containerized classes
* @var array
*/
public $services = [
'netlify' => '\TinyPixel\NetlifyDeploy\Netlify',
'error' => '\TinyPixel\NetlifyDeploy\Error',
];
/**
* Setup class parameters
*/
public function __construct()
{
/**
* Plugin name
* @var string
*/
$this->name = basename(dirname(__FILE__));
/**
* Plugin service container
* @var object
*/
$this->container = (object) [];
/**
* Plugin requirements
* @var object
*/
$this->requires = (object) [
'php' => '7.2',
'wp' => '5.2',
];
/**
* Runtime
* @var string
*/
$this->runtime = (object) [
'env' => strtoupper(env('WP_ENV')),
'php' => phpversion(),
];
/**
* WordPress profile & references
* @var object
*/
$this->wordpressVersion = get_bloginfo('version');
/**
* Location of plugin translation files
* @var string
*/
$this->i18nHandle = "{$this->name}/languages";
}
/**
* Returns singleton construct
* @return self $instance
*/
public static function getInstance()
{
if (self::$instance == null) {
self::$instance = new NetlifyDeployPlugin();
}
return self::$instance;
}
/**
* Does compatibility checks
*/
public function preflight()
{
$this
->loadTextDomain()
->checkDependencies()
->checkPHPVersion()
->checkWPVersion()
->checkEnv();
return $this;
}
/**
* Executes plugin runtime
*/
public function run()
{
$this->container->netlify
->setupClient()
->setWebhooks()
->filterPostTypes()
->filterWebhooks()
->filterTransitions()
->filterOverrides()
->setTargetHook()
->addActions();
}
/**
* Checks autoload viability
*/
private function checkDependencies()
{
// If there is an issue with the autoloader this manually
// loads the error handler class and uses it to throw an early
// error
!file_exists($this->dependencies) && $this->throwEarlyError([
'body' => __('Netlify Deploy needs to be installed in order to be run.<br />Run <code>composer install</code> from the plugin directory.', 'netlify-deploy'),
'subtitle' => __('Autoloader not found.', 'netlify-deploy'),
]);
// load dependencies
require $this->dependencies;
// bind services to container
$this->bindServices();
return $this;
}
/**
* Binds service classes
*/
private function bindServices()
{
foreach ($this->services as $handle => $class) {
$this->container->$handle = new $class(self::getInstance());
}
}
/**
* Loads plugin i18n texts
* @return self $this
*/
private function loadTextDomain()
{
load_plugin_textdomain(
'netlify-deploy',
false,
$this->i18nHandle
);
return $this;
}
/**
* Checks for minimum PHP version compatibility
* @return self $this
*/
private function checkPHPVersion()
{
version_compare($this->requires->php, $this->runtime->php, '>') &&
$this->container->error::throw([
'body' => sprintf(
__('You must be using PHP %s or greater.', 'netlify-deploy'),
$this->requires->php,
),
'subtitle' => sprintf(
__('Invalid PHP version (%s)', 'netlify-deploy'),
$this->runtime->php,
),
]);
return $this;
}
/**
* Checks for minimum WordPress version compatibility
* @return self $this
*/
private function checkWPVersion()
{
version_compare($this->requires->wp, $this->wordpressVersion, '>') &&
$this->container->error::throw([
'body' => sprintf(__(
'You must be using WordPress %s or greater',
'netlify-deploy'
), $this->requires->wp),
'subtitle' => sprintf(
__('Invalid WordPress version (%s)', 'netlify-deploy'),
$this->wordpressVersion,
),
]);
return $this;
}
/**
* Checks environment variables
* @return self $this
*/
private function checkEnv()
{
$webhook = $this->container->netlify::getNetlifyHook(
$this->runtime->env
);
if (!$webhook->value && !(has_filter('netlify_webhooks') || has_filter('netlify_env_override'))) {
$this->container->error::throw([
'body' => sprintf(__(
"The <code>%s</code> variable must be present.",
'netlify-deploy'
), $webhook->key, $this->runtime->env),
'subtitle' => __('Netlify webhook not found.', 'netlify-deploy'),
]);
}
return $this;
}
/**
* Throws an error before autoload
* @param array $error
*/
private function throwEarlyError($error)
{
require $this->errorHandler;
\TinyPixel\NetlifyDeploy\Error::throw([
'body' => __('Netlify Deploy needs to be installed in order to be run.<br />Run <code>composer install</code> from the plugin directory.', 'netlify-deploy'),
'subtitle' => __('Autoloader not found.', 'netlify-deploy'),
]);
}
}
NetlifyDeployPlugin
::getInstance()
->preflight()
->run();