-
Notifications
You must be signed in to change notification settings - Fork 10
/
commentpress-core.php
executable file
·574 lines (470 loc) · 13.5 KB
/
commentpress-core.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
<?php
/**
* CommentPress Core
*
* Plugin Name: CommentPress Core
* Description: CommentPress allows readers to comment in the margins of a text. You can use it to annotate, gloss, workshop, debate and more!
* Plugin URI: https://github.com/IFBook/commentpress-core
* GitHub Plugin URI: https://github.com/IFBook/commentpress-core
* Version: 4.0.3
* Author: Institute for the Future of the Book
* Author URI: https://futureofthebook.org/commentpress/
* License: GPLv2 or later
* License URI: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* Requires at least: 5.7
* Requires PHP: 7.4
* Text Domain: commentpress-core
* Domain Path: /languages
*
* Special thanks to:
*
* Eddie Tejeda for CommentPress 2.0: https://www.visudo.com
* Mark James for the icons: https://www.famfamfam.com/lab/icons/silk/
*
* @package CommentPress_Core
* @link https://github.com/IFBook/commentpress-core
* @license GPL v2 or later
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
// Set version.
define( 'COMMENTPRESS_VERSION', '4.0.3' );
// Store reference to this file.
if ( ! defined( 'COMMENTPRESS_PLUGIN_FILE' ) ) {
define( 'COMMENTPRESS_PLUGIN_FILE', __FILE__ );
}
// Store URL to this plugin's directory.
if ( ! defined( 'COMMENTPRESS_PLUGIN_URL' ) ) {
define( 'COMMENTPRESS_PLUGIN_URL', plugin_dir_url( COMMENTPRESS_PLUGIN_FILE ) );
}
// Store PATH to this plugin's directory.
if ( ! defined( 'COMMENTPRESS_PLUGIN_PATH' ) ) {
define( 'COMMENTPRESS_PLUGIN_PATH', plugin_dir_path( COMMENTPRESS_PLUGIN_FILE ) );
}
/**
* CommentPress Plugin Class.
*
* A class that handles plugin functionality.
*
* @since 4.0
*/
class CommentPress_Plugin {
/**
* Plugin context flag.
*
* May be one of "standard", "mu_sitewide" or "mu_optional".
*
* Replaces the legacy "COMMENTPRESS_PLUGIN_CONTEXT" constant.
*
* @since 4.0
* @access public
* @var string
*/
public $plugin_context;
/**
* Relative path to the common directory.
*
* @since 4.0
* @access public
* @var string
*/
public $common_path = 'includes/common/';
/**
* Relative path to the core directory.
*
* @since 4.0
* @access public
* @var string
*/
public $core_path = 'includes/core/';
/**
* Relative path to the multisite directory.
*
* @since 4.0
* @access public
* @var string
*/
public $multisite_path = 'includes/multisite/';
/**
* Constructor.
*
* @since 4.0
*/
public function __construct() {
// Initialise plugin.
$this->initialise();
}
/**
* Initialises this plugin.
*
* @since 4.0
*/
public function initialise() {
// Only do this once.
static $done;
if ( isset( $done ) && true === $done ) {
return;
}
// Include files.
$this->include_files();
// Establish context.
$this->plugin_context_set();
// Register theme directory.
$this->theme_directory_register();
// Maybe bootstrap multisite.
$this->multisite_bootstrap();
// Maybe bootstrap core.
$this->core_bootstrap();
// Register hooks.
$this->register_hooks();
/**
* Fires when CommentPress has loaded.
*
* @since 4.0
*/
do_action( 'commentpress/loaded' );
// We're done.
$done = true;
}
/**
* Includes files.
*
* @since 4.0
*/
public function include_files() {
// Include common files.
require_once COMMENTPRESS_PLUGIN_PATH . $this->common_path . 'common-functions.php';
// Include multisite and core class files.
require_once COMMENTPRESS_PLUGIN_PATH . $this->multisite_path . 'class-multisite-loader.php';
require_once COMMENTPRESS_PLUGIN_PATH . $this->core_path . 'class-core-loader.php';
}
/**
* Gets the plugin context.
*
* @since 4.0
*
* @return str $plugin_context The current plugin context.
*/
public function plugin_context_get() {
return $this->plugin_context;
}
/**
* Determines plugin context.
*
* Worth noting that during network activation, this plugin is not present
* in the "active_sitewide_plugins" array and so the plugin context will be
* set as "mu_optional" while that process runs.
*
* @since 4.0
*/
public function plugin_context_set() {
// If not Multisite, then must be Single Site install.
if ( ! is_multisite() ) {
$this->plugin_context = 'standard';
return;
}
// Is the plugin network activated?
if ( $this->is_network_activated() ) {
$this->plugin_context = 'mu_sitewide';
return;
}
// Optional activation per Site in Multisite.
$this->plugin_context = 'mu_optional';
}
// -------------------------------------------------------------------------
/**
* Registers the theme directory with WordPress.
*
* NOTE: in multisite, child themes are registered as broken if the plugin
* is not network-enabled. Make sure child themes have instructions.
*
* There are further complex issues when in Multisite:
*
* First scenario:
*
* * If the plugin is NOT initially network-enabled
* * But it IS enabled on one or more Blogs on the network
* * And the plugin in THEN network-enabled
*
* Second scenario:
*
* * If the plugin IS initially network-enabled
* * And it IS activated on one or more Blogs on the network
* * And the plugin in THEN network-disabled
*
* If installs stick to one or the other, then all works as expected.
*
* @since 4.0
*/
public function theme_directory_register() {
// Register our themes directory.
register_theme_directory( plugin_dir_path( COMMENTPRESS_PLUGIN_FILE ) . 'themes' );
}
// -------------------------------------------------------------------------
/**
* Gets a reference to the multisite loader object.
*
* @since 4.0
*
* @return CommentPress_Multisite_Loader $commentpress_mu The multisite loader reference.
*/
public function multisite() {
// Declare as global to retain backwards compatibility.
global $commentpress_mu;
// Maybe return reference.
if ( isset( $commentpress_mu ) ) {
if ( $commentpress_mu instanceof CommentPress_Multisite_Loader ) {
return $commentpress_mu;
}
}
// Not present.
return false;
}
/**
* Maybe bootstrap multisite.
*
* NOTE: This will always initialise multisite when in a multisite context.
*
* @since 4.0
*/
public function multisite_bootstrap() {
// Bail if not multisite.
if ( ! is_multisite() ) {
return;
}
// Initialise multisite.
$this->multisite_initialise();
}
/**
* Initialises multisite.
*
* @since 4.0
*
* @return CommentPress_Multisite_Loader $commentpress_mu The multisite loader reference.
*/
public function multisite_initialise() {
// Declare as global to retain backwards compatibility.
global $commentpress_mu;
// Instantiate if not yet instantiated.
if ( ! isset( $commentpress_mu ) ) {
$commentpress_mu = new CommentPress_Multisite_Loader( $this );
}
// --<
return $commentpress_mu;
}
// -------------------------------------------------------------------------
/**
* Gets a reference to the core loader object.
*
* @since 4.0
*
* @return CommentPress_Core_Loader $commentpress_core The core loader reference, or false on failure.
*/
public function core() {
// Declare as global to retain backwards compatibility.
global $commentpress_core;
// Maybe return reference.
if ( isset( $commentpress_core ) ) {
if ( $commentpress_core instanceof CommentPress_Core_Loader ) {
return $commentpress_core;
}
}
// Not present.
return false;
}
/**
* Maybe bootstrap core.
*
* NOTE: This will initialise core during plugin activation because the plugin
* context will not be registered as "mu_sitewide". This is good because core
* tests for "network_wide" during activation and acts accordingly. Subsequent
* loading will skip initialising core - this will be handled by the multisite
* class responsible for core on a per-site basis.
*
* @since 4.0
*/
public function core_bootstrap() {
// Bail if plugin is activated network-wide.
if ( 'mu_sitewide' === $this->plugin_context ) {
return;
}
// Initialise core.
$this->core_initialise();
}
/**
* Initialises core.
*
* @since 4.0
*
* @return CommentPress_Core_Loader $commentpress_core The core loader reference.
*/
public function core_initialise() {
// Declare as global to retain backwards compatibility.
global $commentpress_core;
// Instantiate if not already instantiated.
if ( ! isset( $commentpress_core ) ) {
$commentpress_core = new CommentPress_Core_Loader( $this );
}
// Return reference.
return $commentpress_core;
}
// -------------------------------------------------------------------------
/**
* Register WordPress hooks.
*
* @since 4.0
*/
public function register_hooks() {
// Check for activation.
add_action( 'activated_plugin', [ $this, 'plugin_activated' ], 10, 2 );
// Check for deactivation.
add_action( 'deactivated_plugin', [ $this, 'plugin_deactivated' ], 10, 2 );
// Add links to Settings Page.
add_filter( 'network_admin_plugin_action_links', [ $this, 'action_links' ], 20, 2 );
add_filter( 'plugin_action_links', [ $this, 'action_links' ], 20, 2 );
}
/**
* Adds "Donate" link to all CommentPress action links.
*
* @since 3.4
* @since 4.0 Moved to this class.
*
* @param array $links The existing links array.
* @param str $file The name of the plugin file.
* @return array $links The modified links array.
*/
public function action_links( $links, $file ) {
// Bail if not this plugin.
if ( plugin_basename( dirname( COMMENTPRESS_PLUGIN_FILE ) . '/commentpress-core.php' ) !== $file ) {
return $links;
}
// Add PayPal link.
$paypal = 'https://www.paypal.com/donate/?cmd=_s-xclick&hosted_button_id=PZSKM8T5ZP3SC';
$links[] = '<a href="' . esc_url( $paypal ) . '" target="_blank">' . __( 'Donate!', 'commentpress-core' ) . '</a>';
// --<
return $links;
}
// -------------------------------------------------------------------------
/**
* This plugin has been activated.
*
* @since 3.3
*
* @param str $plugin The plugin file.
* @param bool $network_wide True if network-activated, false otherwise.
*/
public function plugin_activated( $plugin, $network_wide = false ) {
// Bail if it's not our plugin.
if ( plugin_basename( COMMENTPRESS_PLUGIN_FILE ) !== $plugin ) {
return;
}
/**
* Fires when this plugin has been activated.
*
* Used internally by:
*
* * CommentPress_Multisite_Loader::plugin_activated() (Priority: 10)
* * CommentPress_Core_Loader::plugin_activated() (Priority: 20)
*
* @since 4.0
*
* @param bool $network_wide True if network-activated, false otherwise.
*/
do_action( 'commentpress/activated', $network_wide );
}
/**
* This plugin has been deactivated.
*
* @since 3.3
*
* @param str $plugin The plugin file.
* @param bool $network_wide True if network-activated, false otherwise.
*/
public function plugin_deactivated( $plugin, $network_wide = false ) {
// Bail if it's not our plugin.
if ( plugin_basename( COMMENTPRESS_PLUGIN_FILE ) !== $plugin ) {
return;
}
/**
* Fires when this plugin has been deactivated.
*
* Used internally by:
*
* * CommentPress_Core_Loader::plugin_deactivated() (Priority: 10)
* * CommentPress_Multisite_Loader::plugin_deactivated() (Priority: 20)
*
* @since 4.0
*
* @param bool $network_wide True if network-activated, false otherwise.
*/
do_action( 'commentpress/deactivated', $network_wide );
/*
* Do we want to trigger deactivation_hook for all sub-blogs?
* Or do we want to convert each instance into a self-contained
* CommentPress Core Blog?
*/
}
/**
* Checks if this plugin is network activated.
*
* @since 4.0
*
* @return bool $is_network_active True if network activated, false otherwise.
*/
public function is_network_activated() {
// Only need to test once.
static $is_network_active;
// Have we done this already?
if ( isset( $is_network_active ) ) {
return $is_network_active;
}
// If not multisite, it cannot be.
if ( ! is_multisite() ) {
$is_network_active = false;
return $is_network_active;
}
// Make sure plugin file is included when outside admin.
if ( ! function_exists( 'is_plugin_active_for_network' ) ) {
require_once ABSPATH . '/wp-admin/includes/plugin.php';
}
// Get path from "plugins" directory to this plugin.
$this_plugin = plugin_basename( COMMENTPRESS_PLUGIN_FILE );
// Test if network activated.
$is_network_active = is_plugin_active_for_network( $this_plugin );
// --<
return $is_network_active;
}
}
/**
* Bootstrap plugin if not yet loaded and returns reference.
*
* @since 4.0
*
* @return CommentPress_Plugin $plugin The plugin reference.
*/
function commentpress() {
// Maybe bootstrap plugin.
static $plugin;
if ( ! isset( $plugin ) ) {
$plugin = new CommentPress_Plugin();
}
// Return reference.
return $plugin;
}
// Bootstrap immediately.
commentpress();
/*
* Uninstall uses the 'uninstall.php' method.
*
* @see https://developer.wordpress.org/reference/functions/register_uninstall_hook/
*/