-
Notifications
You must be signed in to change notification settings - Fork 42
/
woocommerce-framework-plugin-loader-sample.php
408 lines (312 loc) · 9.76 KB
/
woocommerce-framework-plugin-loader-sample.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
<?php
/**
* Plugin Name: WooCommerce Framework Plugin TODO: plugin name
* Plugin URI: http://www.woocommerce.com/products/ TODO: product URL
* Description: TODO: plugin description
* Author: SkyVerge
* Author URI: http://www.woocommerce.com
* Version: 1.0.0 TODO: plugin version
* Text Domain: TODO: plugin textdomain
* Domain Path: /i18n/languages/
*
* Copyright: (c) 2011-2024, SkyVerge, Inc. ([email protected])
*
* License: GNU General Public License v3.0
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*
* @package TODO: package
* @author SkyVerge
* @copyright Copyright (c) 2011-2024, SkyVerge, Inc.
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0
*
* Woo: 99999:00000000000000000000000000000000 TODO: updater keys
* WC requires at least: 3.5
* WC tested up to: 4.7
*/
defined( 'ABSPATH' ) or exit;
/**
* The plugin loader class.
*
* TODO: Rename the class, and replace all instances of SV_WC_Framework_Plugin_Loader
* TODO: Update all version numbers and @since tags to the plugin version
*
* @since 1.0.0
*/
#[\AllowDynamicProperties]
class SV_WC_Framework_Plugin_Loader {
/** minimum PHP version required by this plugin */
public const MINIMUM_PHP_VERSION = '7.4';
/** minimum WordPress version required by this plugin */
public const MINIMUM_WP_VERSION = '5.6';
/** minimum WooCommerce version required by this plugin */
public const MINIMUM_WC_VERSION = '3.9';
/** SkyVerge plugin framework version used by this plugin */
public const FRAMEWORK_VERSION = '5.15.2'; // TODO: framework version
/** the plugin name, for displaying notices */
public const PLUGIN_NAME = 'WooCommerce Framework Plugin'; // TODO: plugin name
/** @var SV_WC_Framework_Plugin_Loader single instance of this class // TODO: replace with loader class name */
private static ?SV_WC_Framework_Plugin_Loader $instance = null;
/** @var array the admin notices to add */
private array $notices = [];
/**
* Constructs the class.
*
* @since 1.0.0
*/
protected function __construct() {
register_activation_hook( __FILE__, array( $this, 'activation_check' ) );
add_action( 'admin_init', array( $this, 'check_environment' ) );
add_action( 'admin_init', array( $this, 'add_plugin_notices' ) );
add_action( 'admin_notices', array( $this, 'admin_notices' ), 15 );
add_filter( 'extra_plugin_headers', array( $this, 'add_documentation_header') );
// if the environment check fails, initialize the plugin
if ( $this->is_environment_compatible() ) {
add_action( 'plugins_loaded', array( $this, 'init_plugin' ) );
}
}
/**
* Cloning instances is forbidden due to singleton pattern.
*
* @since 1.0.0
*/
public function __clone() {
_doing_it_wrong( __FUNCTION__, sprintf( 'You cannot clone instances of %s.', get_class( $this ) ), '1.0.0' );
}
/**
* Unserializing instances is forbidden due to singleton pattern.
*
* @since 1.0.0
*/
public function __wakeup() {
_doing_it_wrong( __FUNCTION__, sprintf( 'You cannot unserialize instances of %s.', get_class( $this ) ), '1.0.0' );
}
/**
* Initializes the plugin.
*
* @since 1.0.0
*/
public function init_plugin() {
if ( ! $this->plugins_compatible() ) {
return;
}
// autoload plugin and vendor files
$loader = require_once( plugin_dir_path( __FILE__ ) . 'vendor/autoload.php' );
/** If the plugin is structured for PSR-4, do the following:
// register plugin namespace with autoloader
$loader->addPsr4( 'SkyVerge\\WooCommerce\\Plugin_Name\\', __DIR__ . '/includes' ); // TODO: plugin namespace here
// depending on how the plugin is structured, you may need to manually load the file that contains the initial plugin function
// require_once( plugin_dir_path( __FILE__ ) . 'includes/Functions.php' ); // TODO: maybe load a file to call your initialization function
/******************/
/** Otherwise, for plugins that use the traditional WordPress class-class-name.php structure, simply include the main plugin file:
// load the main plugin class
require_once( plugin_dir_path( __FILE__ ) . 'class-wc-framework-plugin.php' ); // TODO: main plugin class file
*******************/
// fire it up!
wc_framework_plugin(); // TODO: call the main plugin method
}
/**
* Gets the framework version in namespace form.
*
* @since 1.0.0
*
* @return string
*/
public function get_framework_version_namespace() {
return 'v' . str_replace( '.', '_', $this->get_framework_version() );
}
/**
* Gets the framework version used by this plugin.
*
* @since 1.0.0
*
* @return string
*/
public function get_framework_version() {
return self::FRAMEWORK_VERSION;
}
/**
* Checks the server environment and other factors and deactivates plugins as necessary.
*
* Based on http://wptavern.com/how-to-prevent-wordpress-plugins-from-activating-on-sites-with-incompatible-hosting-environments
*
* @internal
*
* @since 1.0.0
*/
public function activation_check() {
if ( ! $this->is_environment_compatible() ) {
$this->deactivate_plugin();
wp_die( self::PLUGIN_NAME . ' could not be activated. ' . $this->get_environment_message() );
}
}
/**
* Checks the environment on loading WordPress, just in case the environment changes after activation.
*
* @internal
*
* @since 1.0.0
*/
public function check_environment() {
if ( ! $this->is_environment_compatible() && is_plugin_active( plugin_basename( __FILE__ ) ) ) {
$this->deactivate_plugin();
$this->add_admin_notice( 'bad_environment', 'error', self::PLUGIN_NAME . ' has been deactivated. ' . $this->get_environment_message() );
}
}
/**
* Adds notices for out-of-date WordPress and/or WooCommerce versions.
*
* @internal
*
* @since 1.0.0
*/
public function add_plugin_notices() {
if ( ! $this->is_wp_compatible() ) {
$this->add_admin_notice( 'update_wordpress', 'error', sprintf(
'%s requires WordPress version %s or higher. Please %supdate WordPress »%s',
'<strong>' . self::PLUGIN_NAME . '</strong>',
self::MINIMUM_WP_VERSION,
'<a href="' . esc_url( admin_url( 'update-core.php' ) ) . '">', '</a>'
) );
}
if ( ! $this->is_wc_compatible() ) {
$this->add_admin_notice( 'update_woocommerce', 'error', sprintf(
'%1$s requires WooCommerce version %2$s or higher. Please %3$supdate WooCommerce%4$s to the latest version, or %5$sdownload the minimum required version »%6$s',
'<strong>' . self::PLUGIN_NAME . '</strong>',
self::MINIMUM_WC_VERSION,
'<a href="' . esc_url( admin_url( 'update-core.php' ) ) . '">', '</a>',
'<a href="' . esc_url( 'https://downloads.wordpress.org/plugin/woocommerce.' . self::MINIMUM_WC_VERSION . '.zip' ) . '">', '</a>'
) );
}
}
/**
* Determines if the required plugins are compatible.
*
* @since 1.0.0
*
* @return bool
*/
private function plugins_compatible() {
return $this->is_wp_compatible() && $this->is_wc_compatible();
}
/**
* Determines if the WordPress compatible.
*
* @since 1.0.0
*
* @return bool
*/
private function is_wp_compatible() {
if ( ! self::MINIMUM_WP_VERSION ) {
return true;
}
return version_compare( get_bloginfo( 'version' ), self::MINIMUM_WP_VERSION, '>=' );
}
/**
* Determines if the WooCommerce compatible.
*
* @since 1.0.0
*
* @return bool
*/
private function is_wc_compatible() {
if ( ! self::MINIMUM_WC_VERSION ) {
return true;
}
return defined( 'WC_VERSION' ) && version_compare( WC_VERSION, self::MINIMUM_WC_VERSION, '>=' );
}
/**
* Deactivates the plugin.
*
* @internal
*
* @since 1.0.0
*/
protected function deactivate_plugin() {
deactivate_plugins( plugin_basename( __FILE__ ) );
if ( isset( $_GET['activate'] ) ) {
unset( $_GET['activate'] );
}
}
/**
* Adds an admin notice to be displayed.
*
* @since 1.0.0
*
* @param string $slug the slug for the notice
* @param string $class the css class for the notice
* @param string $message the notice message
*/
private function add_admin_notice( $slug, $class, $message ) {
$this->notices[ $slug ] = array(
'class' => $class,
'message' => $message
);
}
/**
* Displays any admin notices added with \SV_WC_Framework_Plugin_Loader::add_admin_notice()
*
* @internal
*
* @since 1.0.0
*/
public function admin_notices() {
foreach ( (array) $this->notices as $notice_key => $notice ) {
?>
<div class="<?php echo esc_attr( $notice['class'] ); ?>">
<p><?php echo wp_kses( $notice['message'], array( 'a' => array( 'href' => array() ) ) ); ?></p>
</div>
<?php
}
}
/**
* Adds the Documentation URI header.
*
* @internal
*
* @since 1.0.0
*
* @param string[] $headers original headers
* @return string[]
*/
public function add_documentation_header( $headers ) {
$headers[] = 'Documentation URI';
return $headers;
}
/**
* Determines if the server environment is compatible with this plugin.
*
* Override this method to add checks for more than just the PHP version.
*
* @since 1.0.0
*
* @return bool
*/
private function is_environment_compatible() {
return version_compare( PHP_VERSION, self::MINIMUM_PHP_VERSION, '>=' );
}
/**
* Gets the message for display when the environment is incompatible with this plugin.
*
* @since 1.0.0
*
* @return string
*/
private function get_environment_message() {
return sprintf( 'The minimum PHP version required for this plugin is %1$s. You are running %2$s.', self::MINIMUM_PHP_VERSION, PHP_VERSION );
}
/**
* Gets the main \SV_WC_Framework_Plugin_Loader instance.
*
* Ensures only one instance can be loaded.
*
* @since 1.0.0
*
* @return SV_WC_Framework_Plugin_Loader
*/
public static function instance() : SV_WC_Framework_Plugin_Loader
{
return self::$instance ??=new self();
}
}
// fire it up!
SV_WC_Framework_Plugin_Loader::instance();