-
Notifications
You must be signed in to change notification settings - Fork 1
/
sc-addon-sdk.php
621 lines (532 loc) · 14.5 KB
/
sc-addon-sdk.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
<?php
/**
* Plugin Name: Sugar Calendar - Addon SDK
* Plugin URI: https://sugarcalendar.com/downloads/addon-sdk
* Description: A Software Development Kit for Sugar Calendar Addons
* Author: Sandhills Development, LLC
* Author URI: https://sandhillsdev.com
* License: GNU General Public License v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: sc-addon-sdk
* Domain Path: /sc-addon-sdk/includes/languages/
* Requires PHP: 7.0.0
* Requires at least: 5.7
* Version: 0.1.0
*/
// Exit if accessed directly
defined( 'ABSPATH' ) || exit;
/**
* Hey there! If you're reading this, you've decided to use this Software
* Development Kit rather than repeat yourself for the millionth time.
*
* All you need to do is follow the steps below, then code away as usual.
*
* 0. Fill out plugin header info above
* 1. Replace "SC_Addon_SDK" with your unique PHP prefix and namespace
* 2. Replace "'sc-addon-sdk'" with your unique plugin directory & text domain
* 3. Replace strings and URLs in this file for your application
* 4. Check the $requirements array and versions for your application
* 5. Check the "Requires" headers above to make sure they match
* 6. Check the assets in the "front-end" and "admin" directories
* 7. Delete this documentation block
* 8. Start coding!
*/
/**
* This class_exists() check avoids fatal errors when this plugin is activated
* in more than one way, and should not be removed.
*/
if ( ! class_exists( 'SC_Addon_SDK_Requirements_Check' ) ) :
/**
* The main plugin requirements checker
*
* @since 1.0.0
*/
final class SC_Addon_SDK_Requirements_Check {
/**
* Plugin file
*
* @since 1.0.0
* @var string
*/
private $file = '';
/**
* Plugin basename
*
* @since 1.0.0
* @var string
*/
private $base = '';
/**
* Plugin main class name
*
* @since 1.0.0
* @var string
*/
private $main_class = 'Sugar_Calendar\\AddOn\\SDK\\Plugin';
/**
* Public URI linking users to learn more about plugin requirements
*
* @since 1.0.0
* @var string
*/
private $url = 'https://docs.sugarcalendar.com/category/2162-add-ons';
/**
* Requirements array
*
* The minimum version numbers denoted here will be adjusted as needed.
*
* @var array
* @since 1.0.0
*/
private $requirements = array(
// PHP
'php' => array(
'minimum' => '7.0.0',
'name' => 'PHP',
'exists' => true,
'current' => false,
'checked' => false,
'met' => false
),
// WordPress
'wp' => array(
'minimum' => '5.7.0',
'name' => 'WordPress',
'exists' => true,
'current' => false,
'checked' => false,
'met' => false
),
// Sugar Calendar
'sc' => array(
'minimum' => '2.1.10',
'name' => 'Sugar Calendar',
'exists' => false,
'current' => false,
'checked' => false,
'met' => false
)
);
/** Links & Styles ********************************************************/
/**
* Plugin specific URL for an external requirements page.
*
* @since 1.0.0
* @return string
*/
private function unmet_requirements_url() {
return $this->url;
}
/**
* Plugin specific string used in CSS to identify attribute IDs and classes.
*
* @since 1.0.0
* @return string
*/
private function unmet_requirements_name() {
// Underscores are not valid
$hyphens = str_replace( '_', '-', __CLASS__ );
// Lowercase is the standard
$retval = strtolower( $hyphens );
// Return class name
return $retval;
}
/** Strings ***************************************************************/
/**
* Plugin specific text to quickly explain what's wrong.
*
* @since 1.0.0
* @return string
*/
private function unmet_requirements_text() {
esc_html_e( 'This plugin is not fully active.', 'sc-addon-sdk' );
}
/**
* Plugin specific text to describe a single unmet requirement.
*
* @since 1.0.0
* @return string
*/
private function unmet_requirements_description_text() {
return esc_html__( 'Requires %s (%s), but (%s) is installed.', 'sc-addon-sdk' );
}
/**
* Plugin specific text to describe a single missing requirement.
*
* @since 1.0.0
* @return string
*/
private function unmet_requirements_missing_text() {
return esc_html__( 'Requires %s (%s), but it appears to be missing.', 'sc-addon-sdk' );
}
/**
* Plugin specific text used to link to an external requirements page.
*
* @since 1.0.0
* @return string
*/
private function unmet_requirements_link() {
return esc_html__( 'Requirements', 'sc-addon-sdk' );
}
/**
* Plugin specific aria label text to describe the requirements link.
*
* @since 1.0.0
* @return string
*/
private function unmet_requirements_label() {
return esc_html__( 'Sugar Calendar Requirements', 'sc-addon-sdk' );
}
/**
* Plugin specific text-domain loader.
*
* @since 1.0.0
* @return void
*/
public function load_textdomain() {
load_plugin_textdomain( 'sc-addon-sdk' );
}
/**
* Check if requirements are met, then load plugin or quit.
*
* @since 1.0.0
* @return void
*/
public function load_plugin() {
$this->met()
? $this->load()
: $this->quit();
}
/** Load ******************************************************************/
/**
* Setup plugin requirements
*
* @since 1.0.0
*/
public function __construct() {
// Setup file & base
$this->file = __FILE__;
$this->base = plugin_basename( $this->file );
// Always load translations
add_action( 'plugins_loaded', array( $this, 'load_textdomain' ) );
// Always (try to) load the plugin
add_action( 'plugins_loaded', array( $this, 'load_plugin' ) );
}
/**
* Quit without loading
*
* @since 1.0.0
*/
private function quit() {
add_action( 'admin_head', array( $this, 'admin_head' ) );
add_filter( "plugin_action_links_{$this->base}", array( $this, 'plugin_row_links' ) );
add_action( "after_plugin_row_{$this->base}", array( $this, 'plugin_row_notice' ) );
}
/** Specific Methods ******************************************************/
/**
* Load normally
*
* @since 1.0.0
*/
private function load() {
// Maybe include the bundled bootstrapper
if ( ! class_exists( $this->main_class ) ) {
require_once dirname( $this->file ) . '/sc-addon-sdk/sc-addon-sdk.php';
}
// Maybe hook-in the bootstrapper
if ( ! class_exists( $this->main_class ) ) {
return;
}
// Bootstrap to plugins_loaded (priority 40, so dependencies are loaded)
add_action( 'plugins_loaded', array( $this, 'bootstrap' ), 40 );
// Register the activation hook
register_activation_hook( $this->file, array( $this, 'activate' ) );
// Register the deactivation hook
register_deactivation_hook( $this->file, array( $this, 'deactivate' ) );
}
/**
* Activate, usually on an activation hook.
*
* @since 1.0.0
*/
public function activate() {
// Bootstrap to include all of the necessary files
$this->bootstrap();
// Installer
call_user_func( array( $this->main_class, 'activate' ) );
}
/**
* Deactivate, usually on a deactivation hook.
*
* @since 1.0.0
*/
public function deactivate() {
// Bootstrap to include all of the necessary files
$this->bootstrap();
// Uninstaller
call_user_func( array( $this->main_class, 'deactivate' ) );
}
/**
* Bootstrap everything.
*
* @since 1.0.0
*/
public function bootstrap() {
call_user_func( array( $this->main_class, 'instance' ), $this->file );
}
/** Multisite *************************************************************/
/**
* Is plugin activation network wide?
*
* @since 1.0.0
* @return bool
*/
private function is_network_wide() {
return ! empty( $_GET['networkwide'] )
? (bool) $_GET['networkwide']
: false;
}
/** Helpers ***************************************************************/
/**
* Helper method to re-determine if WordPress 5.5 is showing the auto-update
* column in the Plugins list table.
*
* @since 1.0.0
*
* @return boolean
*/
private function showing_auto_updates() {
// Bail if auto updates do not exist
if ( ! function_exists( 'wp_is_auto_update_enabled_for_type' ) ) {
return false;
}
// Bail if not enabled
if ( ! wp_is_auto_update_enabled_for_type( 'plugin' ) ) {
return false;
}
// Bail if current user cannot update plugins
if ( ! current_user_can( 'update_plugins' ) ) {
return false;
}
// Bail if in blog admin
if ( is_multisite() && is_blog_admin() ) {
return false;
}
// Allowed auto-update statuses
$allowed_statuses = array(
'all',
'active',
'inactive',
'recently_activated',
'upgrade',
'search',
'paused',
'auto-update-enabled',
'auto-update-disabled'
);
// Status
$status = ! empty( $_REQUEST['plugin_status'] )
? sanitize_key( $_REQUEST['plugin_status'] )
: 'all';
// Is status allowed?
$allowed = in_array( $status, $allowed_statuses, true );
// Return if allowed
return $allowed;
}
/**
* How many columns should the description column span?
*
* For plugin_row_notice()
*
* @since 1.0.0
*/
private function description_colspan() {
return $this->showing_auto_updates()
? 2
: 1;
}
/** Agnostic Methods ******************************************************/
/**
* Plugin agnostic method to output the additional plugin row
*
* @since 1.0.0
*/
public function plugin_row_notice() {
?><tr class="active <?php echo esc_attr( $this->unmet_requirements_name() ); ?>-row">
<th class="check-column">
<span class="dashicons dashicons-warning"></span>
</th>
<td class="column-primary">
<?php $this->unmet_requirements_text(); ?>
</td>
<td class="column-description" colspan="<?php echo absint( $this->description_colspan() ); ?>">
<?php $this->unmet_requirements_description(); ?>
</td>
</tr><?php
}
/**
* Plugin agnostic method used to output all unmet requirement information
*
* @since 1.0.0
*/
private function unmet_requirements_description() {
foreach ( $this->requirements as $properties ) {
if ( empty( $properties['met'] ) ) {
$this->unmet_requirement_description( $properties );
}
}
}
/**
* Plugin agnostic method to output specific unmet requirement information
*
* @since 1.0.0
* @param array $requirement
*/
private function unmet_requirement_description( $requirement = array() ) {
// Requirement exists, but is out of date
if ( ! empty( $requirement['exists'] ) ) {
$text = sprintf(
$this->unmet_requirements_description_text(),
'<strong>' . esc_html( $requirement['name'] ) . '</strong>',
'<strong>' . esc_html( $requirement['minimum'] ) . '</strong>',
'<strong>' . esc_html( $requirement['current'] ) . '</strong>'
);
// Requirement could not be found
} else {
$text = sprintf(
$this->unmet_requirements_missing_text(),
'<strong>' . esc_html( $requirement['name'] ) . '</strong>',
'<strong>' . esc_html( $requirement['minimum'] ) . '</strong>'
);
}
// Output the description (unescaped, contains HTML)
echo wpautop( $text );
}
/**
* Plugin agnostic method to output unmet requirements styling
*
* @since 1.0.0
*/
public function admin_head() {
// Get the requirements row name
$name = $this->unmet_requirements_name(); ?>
<style id="<?php echo esc_attr( $name ); ?>">
.plugins tr[data-plugin="<?php echo esc_html( $this->base ); ?>"] th,
.plugins tr[data-plugin="<?php echo esc_html( $this->base ); ?>"] td,
.plugins .<?php echo esc_html( $name ); ?>-row th,
.plugins .<?php echo esc_html( $name ); ?>-row td {
background: #fff5f5;
}
.plugins tr[data-plugin="<?php echo esc_html( $this->base ); ?>"] th {
box-shadow: none;
}
.plugins .<?php echo esc_html( $name ); ?>-row th span {
margin-left: 6px;
color: #dc3232;
}
.plugins tr[data-plugin="<?php echo esc_html( $this->base ); ?>"] th,
.plugins .<?php echo esc_html( $name ); ?>-row th.check-column {
border-left: 4px solid #dc3232 !important;
}
.plugins .<?php echo esc_html( $name ); ?>-row .column-description p {
margin: 0;
padding: 0;
}
.plugins .<?php echo esc_html( $name ); ?>-row .column-description p:not(:last-of-type) {
margin-bottom: 8px;
}
</style>
<?php
}
/**
* Plugin agnostic method to add the "Requirements" link to row actions
*
* @since 1.0.0
* @param array $links
* @return array
*/
public function plugin_row_links( $links = array() ) {
// Add the Requirements link
$links['requirements'] =
'<a href="' . esc_url( $this->unmet_requirements_url() ) . '" aria-label="' . esc_attr( $this->unmet_requirements_label() ) . '">'
. esc_html( $this->unmet_requirements_link() )
. '</a>';
// Return links with Requirements link
return $links;
}
/** Checkers **************************************************************/
/**
* Plugin specific requirements checker
*
* @since 1.0.0
*/
private function check() {
// Loop through requirements
foreach ( $this->requirements as $dependency => $properties ) {
// Which dependency are we checking?
switch ( $dependency ) {
// PHP
case 'php' :
$version = phpversion();
$met = is_php_version_compatible( $properties['minimum'] );
break;
// WP
case 'wp' :
$version = get_bloginfo( 'version' );
$met = is_wp_version_compatible( $properties['minimum'] );
break;
// Sugar Calendar
case 'sc' :
$version = defined( 'SC_PLUGIN_VERSION' )
? SC_PLUGIN_VERSION
: false;
$met = version_compare( $version, $properties['minimum'], '>=' );
break;
/**
* Insert your own requirements checks here as needed for any
* plugin or theme code that this plugin will depend on, then
* delete this paragraph.
*/
// Unknown
default :
$version = false;
$met = false;
break;
}
// Merge to original array
if ( ! empty( $version ) ) {
$this->requirements[ $dependency ] = array_merge( $this->requirements[ $dependency ], array(
'checked' => true,
'current' => $version,
'met' => $met
) );
}
}
}
/**
* Have all requirements been met?
*
* @since 1.0.0
*
* @return boolean
*/
public function met() {
// Run the check
$this->check();
// Default to true (any false below wins)
$retval = true;
$to_meet = wp_list_pluck( $this->requirements, 'met' );
// Look for unmet dependencies, and exit if so
foreach ( $to_meet as $met ) {
if ( empty( $met ) ) {
$retval = false;
continue;
}
}
// Return
return $retval;
}
}
// Invoke the checker
new SC_Addon_SDK_Requirements_Check();
endif;