generated from peroks/wp-plugin-template-v1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwp-guzzle.php
387 lines (345 loc) · 12.4 KB
/
wp-guzzle.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
<?php namespace peroks\plugins\guzzle;
/*
* Plugin Name: Guzzle for WordPress
* Plugin URI: https://github.com/peroks/wp-guzzle
* Description: Guzzle packed as a WordPress Plugin
*
* Text Domain: wp-guzzle
*
* Author: Per Egil Roksvaag
* Author URI: https://codeable.io/developers/per-egil-roksvaag/
*
* Version: 7.3.3
* Stable tag: 7.3.3
* Requires at least: 5.0
* Tested up to: 5.7.2
* Requires PHP: 7.2.5
*/
/**
* The Guzzle for WordPress plugin main class.
*
* @author Per Egil Roksvaag
* @version 7.3.3
*/
class Main
{
/**
* @var string The plugin file.
*/
const FILE = __FILE__;
/**
* Should be identical to "Plugin Name" in the plugin header comment above.
*
* @var string The plugin name.
* @todo Globally search and replace this with your own plugin name.
*/
const NAME = 'Guzzle for WordPress';
/**
* Must be identical to "Domain Path" in the plugin header comment above.
* Use lowercase and hyphens as word breaker.
*
* @var string The plugin text domain (hyphen).
* @todo Globally search and replace this with your own unique text domain
*/
const DOMAIN = 'wp-guzzle';
/**
* Should be similar to self::DOMAIN, only with underscores instead of hyphens.
* Use lowercase and underscores as word breaker.
*
* @var string The plugin prefix (underscore).
* @todo Replace this constant with your own unique plugin prefix.
*/
const PREFIX = 'wp_guzzle';
/**
* Should contain the "Version" field in the plugin header comment above.
*
* @var string The plugin version.
* @todo Set your plugin version number.
*/
const VERSION = '7.3.3';
/**
* Only requirement constants > '0' will be checked.
*
* @var string The system environment requirements.
* @todo Replace this with the system requirements of your owe plugin.
* @see Main::check() below and possibly add/remove system checks and constants.
*/
const REQUIRE_PHP = '7.2.5'; // Required PHP version
const REQUIRE_WP = '5.0'; // Required WordPress version
/**
* @var string The plugin global action hooks.
*/
const ACTION_LOADED = self::PREFIX . '_loaded';
const ACTION_UPDATE = self::PREFIX . '_update';
const ACTION_ACTIVATE = self::PREFIX . '_activate';
const ACTION_DEACTIVATE = self::PREFIX . '_deactivate';
const ACTION_DELETE = self::PREFIX . '_delete';
/**
* @var string The plugin global filter hooks.
*/
const FILTER_CLASS_CREATE = self::PREFIX . '_class_create';
const FILTER_CLASS_CREATED = self::PREFIX . '_class_created';
const FILTER_CLASS_PATH = self::PREFIX . '_class_path';
const FILTER_SYSTEM_CHECK = self::PREFIX . '_system_check';
const FILTER_PLUGIN_PREFIX = self::PREFIX . '_plugin_prefix';
const FILTER_PLUGIN_PATH = self::PREFIX . '_plugin_path';
const FILTER_PLUGIN_URL = self::PREFIX . '_plugin_url';
/**
* @var string The plugin global options.
*/
const OPTION_VERSION = self::PREFIX . '_version';
/**
* @var Main The main plugin class singleton.
*/
protected static $_instance;
/**
* @return Main The main plugin class singleton.
*/
public static function instance() {
if ( is_null( static::$_instance ) && static::check() ) {
static::$_instance = false;
$class = apply_filters( self::FILTER_CLASS_CREATE, static::class );
static::$_instance = apply_filters( self::FILTER_CLASS_CREATED, new $class(), $class, static::class );
do_action( self::ACTION_LOADED, static::$_instance );
}
return static::$_instance;
}
/**
* Constructor.
*/
protected function __construct() {
$this->autoload();
$this->run();
$this->update();
}
/**
* Registers autoloading.
*
* @todo Add your own plugin classes and their file system paths here.
*/
protected function autoload() {
$classes = apply_filters( self::FILTER_CLASS_PATH, array(
__NAMESPACE__ . '\Admin' => static::plugin_path( 'includes/admin.php' ),
__NAMESPACE__ . '\Guzzle' => static::plugin_path( 'includes/tools/guzzle.php' ),
__NAMESPACE__ . '\Singleton' => static::plugin_path( 'includes/tools/singleton.php' ),
__NAMESPACE__ . '\Repository' => static::plugin_path( 'includes/tools/repository.php' ),
) );
spl_autoload_register( function ( $name ) use ( $classes ) {
if ( array_key_exists( $name, $classes ) ) {
include $classes[ $name ];
}
} );
}
/**
* Loads and runs the plugin classes.
*
* You must register your classes for autoloading (above) before you can run them here.
*
* @todo Add your own plugin classes.
*/
protected function run() {
Guzzle::instance();
if ( is_admin() ) {
Admin::instance();
Repository::instance();
}
}
/* =========================================================================
* Everything below this line is just plugin management and some very
* basic path and url handlers. You'll find the real action in the classes
* loaded above.
* ====================================================================== */
/* -------------------------------------------------------------------------
* System environment checks
* ---------------------------------------------------------------------- */
/**
* Checks if the system environment is supported.
*
* @todo Add/remove system environment checks for your plugin.
* @return bool True if the system environment is supported, false otherwise.
*/
protected static function check() {
$error = false;
if ( defined( 'self::REQUIRE_PHP' ) && self::REQUIRE_PHP ) {
if ( version_compare( PHP_VERSION, self::REQUIRE_PHP ) < 0 ) {
$error = static::error( 'PHP', self::REQUIRE_PHP ) || $error;
}
}
if ( defined( 'self::REQUIRE_WP' ) && self::REQUIRE_WP ) {
if ( version_compare( get_bloginfo( 'version' ), self::REQUIRE_WP ) < 0 ) {
$error = static::error( 'WordPress', self::REQUIRE_WP ) || $error;
}
}
return empty( $error );
}
/**
* Logs and outputs missing system requirements.
*
* @param string $require The name of the required component.
* @param string $version The minimum version required.
* @return bool True, except when overridden by filter.
*/
protected static function error( $require, $version ) {
if ( apply_filters( self::FILTER_SYSTEM_CHECK, true, $require, $version ) ) {
if ( is_admin() ) {
// Error message
$message = __( '%1$s requires %2$s version %3$s or higher, the plugin is NOT RUNNING.', 'wp-guzzle' );
$message = sprintf( $message, self::NAME, $require, $version );
// Admin notice output
$notice = function () use ( $message ) {
vprintf( '<div class="notice notice-error"><p><strong>%s: </strong>%s</p></div>', array(
esc_html__( 'Error', 'wp-guzzle' ),
esc_html( $message ),
) );
};
// Write error message to log and create admin notice.
error_log( $message );
add_action( 'admin_notices', $notice );
}
return true;
}
return false;
}
/* -------------------------------------------------------------------------
* Update, activate, deactivate and uninstall plugin.
* ---------------------------------------------------------------------- */
/**
* Checks if the plugin was updated.
*
* Notifies plugin classes to update and flushes rewrite rules.
*
* @return bool True if the plugin was updated, false otherwise.
*/
protected function update() {
$version = get_option( self::OPTION_VERSION );
if ( self::VERSION !== $version ) {
do_action( self::ACTION_UPDATE, $this, self::VERSION, $version );
update_option( self::OPTION_VERSION, self::VERSION );
add_action( 'wp_loaded', 'flush_rewrite_rules' );
add_action( 'admin_notices', function () {
$notice = __( '%s has been updated to version %s', 'wp-guzzle' );
$notice = sprintf( $notice, self::NAME, self::VERSION );
printf( '<div class="notice notice-success is-dismissible"><p>%s.</p></div>', esc_html( $notice ) );
error_log( $notice );
} );
return true;
}
return false;
}
/**
* Registers plugin activation, deactivation and uninstall hooks.
*/
public static function register() {
if ( is_admin() ) {
register_activation_hook( self::FILE, array( static::class, 'activate' ) );
register_deactivation_hook( self::FILE, array( static::class, 'deactivate' ) );
register_uninstall_hook( self::FILE, array( static::class, 'uninstall' ) );
}
}
/**
* Runs when the plugin is activated.
*
* Notifies plugin classes to activate and flushes rewrite rules.
* This hook is called AFTER all other hooks (except 'shutdown').
* WP redirects the request immediately after this hook, so we can't register any hooks to be executed later.
*/
public static function activate() {
if ( is_admin() && current_user_can( 'activate_plugins' ) ) {
do_action( self::ACTION_ACTIVATE, static::instance(), self::VERSION, get_option( self::OPTION_VERSION ) );
update_option( self::OPTION_VERSION, self::VERSION );
$message = __( '%s version %s has been activated', 'wp-guzzle' );
error_log( sprintf( $message, self::NAME, self::VERSION ) );
flush_rewrite_rules();
}
}
/**
* Runs when the plugin is deactivated.
*
* Notifies plugin classes to deactivate and flushes rewrite rules.
*/
public static function deactivate() {
if ( is_admin() && current_user_can( 'activate_plugins' ) ) {
do_action( self::ACTION_DEACTIVATE, static::instance(), self::VERSION, get_option( self::OPTION_VERSION ) );
$message = __( '%s version %s has been deactivated', 'wp-guzzle' );
error_log( sprintf( $message, self::NAME, self::VERSION ) );
flush_rewrite_rules();
}
}
/**
* Runs when the plugin is deleted.
*
* Notifies plugin classes to delete all plugin settings and flushes rewrite rules.
*/
public static function uninstall() {
if ( is_admin() && current_user_can( 'delete_plugins' ) ) {
do_action( self::ACTION_DELETE, static::instance(), self::VERSION, get_option( self::OPTION_VERSION ) );
delete_option( self::OPTION_VERSION );
$message = __( '%s version %s has been removed', 'wp-guzzle' );
error_log( sprintf( $message, self::NAME, self::VERSION ) );
flush_rewrite_rules();
}
}
/* -------------------------------------------------------------------------
* Basic prefix, path and url utils
* ---------------------------------------------------------------------- */
/**
* Gets a prefixed identifier.
*
* @param string $name The identifier to prefix.
* @param string $sep The prefix separator.
* @return string The prefixed identifier.
*/
public function plugin_prefix( $name = '', $sep = '_' ) {
$result = str_replace( '_', $sep, self::PREFIX . $sep . $name );
return apply_filters( self::FILTER_PLUGIN_PREFIX, $result, $name, $sep );
}
/**
* Gets a full filesystem path from a local path.
*
* @param string $path The local path relative to this plugin's root directory.
* @return string The full filesystem path.
*/
public static function plugin_path( $path = '' ) {
$path = ltrim( trim( $path ), '/' );
$full = plugin_dir_path( self::FILE ) . $path;
return apply_filters( self::FILTER_PLUGIN_PATH, $full, $path );
}
/**
* Gets the URL to the given local path.
*
* @param string $path The local path relative to this plugin's root directory.
* @return string The URL.
*/
public static function plugin_url( $path = '' ) {
$path = ltrim( trim( $path ), '/' );
$url = plugins_url( $path, self::FILE );
return apply_filters( self::FILTER_PLUGIN_URL, $url, $path );
}
/* -------------------------------------------------------------------------
* Debugging
* ---------------------------------------------------------------------- */
/**
* Writes an entry to the php log and adds context information.
*
* @param string $log Log entry.
*/
public static function log( $log = '' ) {
$caller = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS );
$file = empty( $caller[0]['file'] ) ? '' : ' in ' . $caller[0]['file'];
$line = empty( $caller[0]['line'] ) ? '' : ' on line ' . $caller[0]['line'];
$type = empty( $caller[1]['function'] ) ? '#Debug: ' : '#' . $caller[1]['function'] . ': ';
$entry = str_replace( "\n", ' ', var_export( $log, true ) );
error_log( $type . gettype( $log ) . ': ' . $entry . $file . $line );
}
/**
* Writes the backtrace to the php log.
*/
public static function trace() {
static::log( debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ) );
}
}
// Registers and runs the main plugin class
if ( defined( 'ABSPATH' ) ) {
Main::register();
add_action( 'plugins_loaded', array( Main::class, 'instance' ), 5 );
}