forked from reduxframework/redux-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-redux-framework-plugin.php
549 lines (463 loc) · 15.6 KB
/
class-redux-framework-plugin.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
<?php
/**
* Redux_Framework_Plugin main class
*
* @package Redux Framework
* @since 3.0.0
*/
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
if ( ! class_exists( 'Redux_Framework_Plugin', false ) ) {
/**
* Main Redux_Framework_Plugin class
*
* @since 3.0.0
*/
class Redux_Framework_Plugin {
/**
* Option array for demo mode.
*
* @access protected
* @var array $options Array of config options, used to check for demo mode
* @since 3.0.0
*/
protected $options = array();
/**
* Use this value as the text domain when translating strings from this plugin. It should match
* the Text Domain field set in the plugin header, as well as the directory name of the plugin.
* Additionally, text domains should only contain letters, number and hyphens, not underscores
* or spaces.
*
* @access protected
* @var string $plugin_slug The unique ID (slug) of this plugin
* @since 3.0.0
*/
protected $plugin_slug = 'redux-framework';
/**
* Set on network activate.
*
* @access protected
* @var string $plugin_network_activated Check for plugin network activation
* @since 3.0.0
*/
protected $plugin_network_activated = null;
/**
* Class instance.
*
* @access private
* @var Redux_Framework_Plugin $instance The one true Redux_Framework_Plugin
* @since 3.0.0
*/
private static $instance;
/**
* Crash flag.
*
* @access private
* @var Redux_Framework_Plugin $crash Crash flag if inside a crash.
* @since 4.1.15
*/
public static $crash = false;
/**
* Get active instance
*
* @access public
* @since 3.1.3
* @return self::$instance The one true Redux_Framework_Plugin
*/
public static function instance(): ?Redux_Framework_Plugin {
$path = REDUX_PLUGIN_FILE;
$res = false;
if ( function_exists( 'get_plugin_data' ) && file_exists( $path ) ) {
$data = get_plugin_data( $path );
if ( isset( $data ) && isset( $data['Version'] ) && '' !== $data['Version'] ) {
$res = version_compare( $data['Version'], '4', '<' );
}
if ( is_plugin_active( 'redux-framework/redux-framework.php' ) && true === $res ) {
echo '<div class="error"><p>' . esc_html__( 'Redux Framework version 4 is activated but not loaded. Redux Framework version 3 is still installed and activated. Please deactivate Redux Framework version 3.', 'redux-framework' ) . '</p></div>'; // phpcs:ignore WordPress.Security.EscapeOutput
return null;
}
}
if ( ! self::$instance ) {
self::$instance = new self();
if ( class_exists( 'ReduxFramework' ) ) {
self::$instance->load_first();
} else {
self::$instance->get_redux_options();
self::$instance->includes();
self::$instance->hooks();
}
}
return self::$instance;
}
/**
* Shim for getting instance
*
* @access public
* @since 4.0.1
* @return self::$instance The one true Redux_Framework_Plugin
*/
public static function get_instance(): ?Redux_Framework_Plugin {
return self::instance();
}
/**
* Get Redux options
*
* @access public
* @since 3.1.3
* @return void
*/
public function get_redux_options() {
// Setup defaults.
$defaults = array(
'demo' => false,
);
// If multisite is enabled.
if ( is_multisite() ) {
// Get network activated plugins.
$plugins = get_site_option( 'active_sitewide_plugins' );
foreach ( $plugins as $file => $plugin ) {
if ( strpos( $file, 'redux-framework.php' ) !== false ) {
$this->plugin_network_activated = true;
$this->options = get_site_option( 'ReduxFrameworkPlugin', $defaults );
}
}
}
// If options aren't set, grab them now!
if ( empty( $this->options ) ) {
$this->options = get_option( 'ReduxFrameworkPlugin', $defaults );
}
}
/**
* Include necessary files
*
* @access public
* @since 3.1.3
* @return void
*/
public function includes() {
// Include Redux_Core.
if ( file_exists( dirname( __FILE__ ) . '/redux-core/framework.php' ) ) {
require_once dirname( __FILE__ ) . '/redux-core/framework.php';
}
Redux_Core::$redux_templates_enabled = (bool) get_option( 'use_redux_templates' );
Redux_Core::$extendify_templates_enabled = (bool) get_option( 'use_extendify_templates', true );
// Including extendify sdk.
if ( true === (bool) get_option( 'use_extendify_templates', true ) ) {
if ( file_exists( dirname( __FILE__ ) . '/extendify-sdk/loader.php' ) ) {
$GLOBALS['extendify_sdk_partner'] = 'Redux';
require_once dirname( __FILE__ ) . '/extendify-sdk/loader.php';
}
}
if ( file_exists( dirname( __FILE__ ) . '/redux-templates/redux-templates.php' ) ) {
require_once dirname( __FILE__ ) . '/redux-templates/redux-templates.php';
}
if ( isset( Redux_Core::$as_plugin ) ) {
Redux_Core::$as_plugin = true;
}
add_action( 'setup_theme', array( $this, 'load_sample_config' ) );
}
/**
* Loads the sample config after everything is loaded.
*
* @access public
* @since 4.0.2
* @return void
*/
public function load_sample_config() {
// Include demo config, if demo mode is active.
if ( $this->options['demo'] && file_exists( dirname( __FILE__ ) . '/sample/sample-config.php' ) ) {
require_once dirname( __FILE__ ) . '/sample/sample-config.php';
}
}
/**
* Run action and filter hooks
*
* @access private
* @since 3.1.3
* @return void
*/
private function hooks() {
add_action( 'activated_plugin', array( $this, 'load_first' ) );
add_action( 'wp_loaded', array( $this, 'options_toggle_check' ) );
// Activate plugin when new blog is added.
add_action( 'wpmu_new_blog', array( $this, 'activate_new_site' ) );
// Display admin notices.
add_action( 'admin_notices', array( $this, 'admin_notices' ) );
// Edit plugin metalinks.
add_filter( 'plugin_row_meta', array( $this, 'plugin_metalinks' ), null, 2 );
add_filter( 'network_admin_plugin_action_links', array( $this, 'add_settings_link' ), 1, 2 );
add_filter( 'plugin_action_links', array( $this, 'add_settings_link' ), 1, 2 );
// phpcs:ignore WordPress.NamingConventions.ValidHookName
do_action( 'redux/plugin/hooks', $this );
}
/**
* Pushes Redux to top of plugin load list, so it initializes before any plugin that may use it.
*/
public function load_first() {
if ( ! class_exists( 'Redux_Functions_Ex' ) ) {
require_once dirname( __FILE__ ) . '/redux-core/inc/classes/class-redux-functions-ex.php';
}
$plugin_dir = Redux_Functions_Ex::wp_normalize_path( WP_PLUGIN_DIR ) . '/';
$self_file = Redux_Functions_Ex::wp_normalize_path( __FILE__ );
$path = str_replace( $plugin_dir, '', $self_file );
$path = str_replace( 'class-redux-framework-plugin.php', 'redux-framework.php', $path );
$plugins = get_option( 'active_plugins' );
if ( $plugins ) {
$key = array_search( $path, $plugins, true );
if ( false !== $key ) {
array_splice( $plugins, $key, 1 );
array_unshift( $plugins, $path );
update_option( 'active_plugins', $plugins );
}
if ( class_exists( 'Redux_Pro' ) ) {
$self_file = Redux_Functions_Ex::wp_normalize_path( Redux_Pro::$dir );
$path = str_replace( $plugin_dir, '', $self_file );
// phpcs:ignore WordPress.NamingConventions.ValidHookName
$basename = apply_filters( 'redux/pro/basename', 'redux-pro.php' );
$key = array_search( $path . '/' . $basename, $plugins, true );
if ( false !== $key ) {
array_splice( $plugins, $key, 1 );
array_unshift( $plugins, $path . '/' . $basename );
update_option( 'active_plugins', $plugins );
}
}
}
}
/**
* Fired on plugin activation
*
* @access public
* @return void
* @since 3.0.0
*/
public static function activate() {
delete_site_transient( 'update_plugins' );
}
/**
* Fired when plugin is deactivated
*
* @access public
* @since 3.0.0
*
* @param boolean $network_wide True if plugin is network activated, false otherwise.
*
* @return void
*/
public static function deactivate( ?bool $network_wide ) {
if ( function_exists( 'is_multisite' ) && is_multisite() ) {
if ( $network_wide ) {
// Get all blog IDs.
$blog_ids = self::get_blog_ids();
foreach ( $blog_ids as $blog_id ) {
switch_to_blog( $blog_id );
self::single_deactivate();
}
restore_current_blog();
} else {
self::single_deactivate();
}
} else {
self::single_deactivate();
}
delete_option( 'ReduxFrameworkPlugin' );
Redux_Enable_Gutenberg::cleanup_options( 'redux-framework' ); // Auto disable Gutenberg and all that.
}
/**
* Fired when a new WPMU site is activated
*
* @access public
*
* @param int $blog_id The ID of the new blog.
*
* @return void
* @since 3.0.0
*/
public function activate_new_site( int $blog_id ) {
if ( 1 !== did_action( 'wpmu_new_blog' ) ) {
return;
}
switch_to_blog( $blog_id );
self::single_activate();
restore_current_blog();
}
/**
* Get all IDs of blogs that are not activated, not spam, and not deleted
*
* @access private
* @since 3.0.0
* @global object $wpdb
* @return array|false Array of IDs or false if none are found
*/
private static function get_blog_ids() {
global $wpdb;
$var = '0';
// Get an array of IDs (We have to do it this way because WordPress says so, however redundant).
$result = wp_cache_get( 'redux-blog-ids' );
if ( false === $result ) {
// WordPress says get_col is discouraged? I found no alternative. So...ignore! - kp.
// phpcs:ignore WordPress.DB.DirectDatabaseQuery
$result = $wpdb->get_col( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE archived = %s AND spam = %s AND deleted = %s", $var, $var, $var ) );
wp_cache_set( 'redux-blog-ids', $result );
}
return $result;
}
/**
* Fired for each WPMS blog on plugin activation
*
* @access private
* @since 3.0.0
* @return void
*/
private static function single_activate() {
$nonce = wp_create_nonce( 'redux_framework_demo' );
$notices = get_option( 'ReduxFrameworkPlugin_ACTIVATED_NOTICES', array() );
$notices[] = esc_html__( 'Redux Framework has an embedded demo.', 'redux-framework' ) . ' <a href="./plugins.php?redux-framework-plugin=demo&nonce=' . $nonce . '">' . esc_html__( 'Click here to activate the sample config file.', 'redux-framework' ) . '</a>';
update_option( 'ReduxFrameworkPlugin_ACTIVATED_NOTICES', $notices );
}
/**
* Display admin notices
*
* @access public
* @since 3.0.0
* @return void
*/
public function admin_notices() {
do_action( 'redux_framework_plugin_admin_notice' );
$notices = get_option( 'ReduxFrameworkPlugin_ACTIVATED_NOTICES', '' );
if ( ! empty( $notices ) ) {
foreach ( $notices as $notice ) {
echo '<div class="updated notice is-dismissible"><p>' . $notice . '</p></div>'; // phpcs:ignore WordPress.Security.EscapeOutput
}
delete_option( 'ReduxFrameworkPlugin_ACTIVATED_NOTICES' );
}
}
/**
* Fired for each blog when the plugin is deactivated
*
* @access private
* @since 3.0.0
* @return void
*/
private static function single_deactivate() {
delete_option( 'ReduxFrameworkPlugin_ACTIVATED_NOTICES' );
}
/**
* Turn on or off
*
* @access public
* @since 3.0.0
* @return void
*/
public function options_toggle_check() {
if ( isset( $_GET['nonce'] ) && wp_verify_nonce( sanitize_key( $_GET['nonce'] ), 'redux_framework_demo' ) ) {
if ( isset( $_GET['redux-framework-plugin'] ) && 'demo' === $_GET['redux-framework-plugin'] ) {
$url = admin_url( add_query_arg( array( 'page' => 'redux-framework' ), 'options-general.php' ) );
if ( false === $this->options['demo'] ) {
$this->options['demo'] = true;
$url = admin_url( add_query_arg( array( 'page' => 'redux_demo' ), 'admin.php' ) );
} else {
$this->options['demo'] = false;
}
if ( is_multisite() && $this->plugin_network_activated ) {
update_site_option( 'ReduxFrameworkPlugin', $this->options );
} else {
update_option( 'ReduxFrameworkPlugin', $this->options );
}
wp_safe_redirect( esc_url( $url ) );
exit();
}
}
}
/**
* Add a settings link to the Redux entry in the plugin overview screen
*
* @param array $links Links array.
* @param string $file Plugin filename/slug.
*
* @return array
* @see filter:plugin_action_links
* @since 1.0
*/
public function add_settings_link( array $links, string $file ): array {
if ( strpos( REDUX_PLUGIN_FILE, $file ) === false ) {
return $links;
}
if ( ! class_exists( 'Redux_Pro' ) ) {
$links[] = sprintf(
'<a href="%s" target="_blank">%s</a>',
esc_url( $this->get_site_utm_url( '', 'upgrade' ) ),
sprintf(
'<span style="font-weight: bold;">%s</span>',
__( 'Go Pro', 'redux-framework' )
)
);
}
return $links;
}
/**
* Get the url where the Admin Columns website is hosted
*
* @param string $path Path to add to url.
*
* @return string
*/
private function get_site_url( string $path = '' ): string {
$url = 'https://redux.io';
if ( ! empty( $path ) ) {
$url .= '/' . trim( $path, '/' ) . '/';
}
return $url;
}
/**
* Url with utm tags
*
* @param string $path Path on site.
* @param string $utm_medium Medium var.
* @param string|null $utm_content Content var.
* @param bool $utm_campaign Campaign var.
*
* @return string
*/
public function get_site_utm_url( string $path, string $utm_medium, string $utm_content = null, bool $utm_campaign = false ): string {
$url = self::get_site_url( $path );
if ( ! $utm_campaign ) {
$utm_campaign = 'plugin-installation';
}
$args = array(
// Referrer: plugin.
'utm_source' => 'plugin-installation',
// Specific promotions or sales.
'utm_campaign' => $utm_campaign,
// Marketing medium: banner, documentation or email.
'utm_medium' => $utm_medium,
// Used for differentiation of medium.
'utm_content' => $utm_content,
);
$args = array_map( 'sanitize_key', array_filter( $args ) );
return add_query_arg( $args, $url );
}
/**
* Edit plugin metalinks
*
* @access public
*
* @param array $links The current array of links.
* @param string $file A specific plugin row.
*
* @return array The modified array of links
* @since 3.0.0
*/
public function plugin_metalinks( array $links, string $file ): array {
if ( strpos( $file, 'redux-framework.php' ) !== false && is_plugin_active( $file ) ) {
$links[] = '<a href="' . esc_url( admin_url( add_query_arg( array( 'page' => 'redux-framework' ), 'options-general.php' ) ) ) . '">' . esc_html__( 'What is this?', 'redux-framework' ) . '</a>';
if ( true === Redux_Core::$redux_templates_enabled ) {
$links[] = '<a href="' . esc_url( admin_url( add_query_arg( array( 'post_type' => 'page' ), 'post-new.php' ) ) ) . '#redux_templates=1">' . esc_html__( 'Template Library', 'redux-framework' ) . '</a>';
}
}
return $links;
}
}
if ( ! class_exists( 'ReduxFrameworkPlugin' ) ) {
class_alias( 'Redux_Framework_Plugin', 'ReduxFrameworkPlugin' );
}
}