-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrank-math-pro.php
550 lines (472 loc) · 15.1 KB
/
rank-math-pro.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
<?php // @codingStandardsIgnoreLine
/**
* Rank Math SEO PRO Plugin.
*
* @package RANK_MATH
* @copyright Copyright (C) 2018-2020, Rank Math - [email protected]
* @link https://rankmath.com
* @since 2.0.0
*
* @wordpress-plugin
* Plugin Name: Rank Math SEO PRO
* Version: 3.0.66
* Plugin URI: https://rankmath.com/wordpress/plugin/seo-suite/
* Description: Super-charge your website’s SEO with the Rank Math PRO options like Site Analytics, SEO Performance, Custom Schema Templates, News/Video Sitemaps, etc.
* Author: Rank Math
* Author URI: https://rankmath.com/?utm_source=Plugin&utm_medium=Readme%20Author%20URI&utm_campaign=WP
* License: GPL-3.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Text Domain: rank-math-pro
* Domain Path: /languages
*/
use RankMath\Helper;
defined( 'ABSPATH' ) || exit;
add_filter( 'rank_math/admin/sensitive_data_encryption', '__return_false' );
update_option( 'rank_math_connect_data', [
'username' => 'info',
'email' => '[email protected]',
'api_key' => '*********',
'plan' => 'business',
'connected' => true,
] );
update_option( 'rank_math_registration_skip', 1 );
add_action( 'init', function() {
add_filter( 'pre_http_request', function( $pre, $parsed_args, $url ) {
if ( strpos( $url, 'https://rankmath.com/wp-json/rankmath/v1/' ) !== false ) {
$basename = basename( parse_url( $url, PHP_URL_PATH ) );
if ( $basename == 'siteSettings' ) {
return [
'response' => [ 'code' => 200, 'message' => 'ОК' ],
'body' => json_encode( [
'error' => '',
'plan' => 'business',
'keywords' => get_option( 'rank_math_keyword_quota', [ 'available' => 10000, 'taken' => 0 ] ),
'analytics' => 'on',
] ),
];
} elseif ( $basename == 'keywordsInfo' ) {
if ( isset( $parsed_args['body']['count'] ) ) {
return [
'response' => [ 'code' => 200, 'message' => 'ОК' ],
'body' => json_encode( [ 'available' => 10000, 'taken' => $parsed_args['body']['count'] ] ),
];
}
}
return [ 'response' => [ 'code' => 200, 'message' => 'ОК' ] ];
}
return $pre;
}, 10, 3 );
} );
/**
* RankMath class.
*
* @class The class that holds the entire plugin.
*/
final class RankMathPro {
/**
* Plugin version
*
* @var string
*/
public $version = '3.0.66';
/**
* Minimum version of Rank Math SEO.
*
* @var string
*/
public $rank_math_min_version = '1.0.223';
/**
* Holds various class instances
*
* @var array
*/
private $container = [];
/**
* Holds messages.
*
* @var array
*/
private $messages = [];
/**
* Slug for the free version of the plugin.
*
* @var string
*/
private $free_version_plugin_path = 'seo-by-rank-math/rank-math.php';
/**
* The single instance of the class
*
* @var RankMath
*/
protected static $instance = null;
/**
* Main RankMathPro instance.
*
* Ensure only one instance is loaded or can be loaded.
*
* @see rank_math_pro()
* @return RankMathPro
*/
public static function get() {
if ( is_null( self::$instance ) && ! ( self::$instance instanceof RankMathPro ) ) {
self::$instance = new RankMathPro();
}
return self::$instance;
}
/**
* Class constructor.
*/
private function __construct() {
if ( ! $this->are_requirements_met() ) {
return;
}
$this->define_constants();
$this->includes();
new \RankMathPro\Installer();
add_action( 'plugins_loaded', [ $this, 'localization_setup' ] );
add_action( 'rank_math/loaded', [ $this, 'setup' ] );
add_filter( 'rank_math/license/activate_url', [ $this, 'add_query_arg' ] );
}
/**
* Instantiate the plugin.
*/
public function setup() {
if ( ! $this->is_free_version_compatible() ) {
$this->messages[] = esc_html__( 'Please update Rank Math Free to the latest version first before using Rank Math PRO.', 'rank-math-pro' );
add_action( 'admin_notices', [ $this, 'activation_error' ] );
return false;
}
// Instantiate classes.
$this->instantiate();
// Initialize the action hooks.
$this->init_actions();
// Loaded action.
do_action( 'rank_math_pro/loaded' );
}
/**
* Check that the WordPress and PHP setup meets the plugin requirements.
*
* @return bool
*/
private function are_requirements_met() {
$dont_load = false;
if ( $this->is_free_version_being_deactivated() ) {
// Todo: this message is not displayed because of a redirect.
$this->messages[] = esc_html__( 'Rank Math free version is required to run Rank Math PRO. Both plugins are now disabled.', 'rank-math-pro' );
} elseif ( $this->is_free_version_being_rolled_back() || $this->is_free_version_being_updated() || $this->is_troubleshooting() ) {
$dont_load = true;
} else {
if ( ! $this->is_free_version_installed() ) {
if ( ! $this->install_free_version() ) {
$this->messages[] = esc_html__( 'Rank Math free version is required to run Rank Math PRO, but it could not be installed automatically. Please install and activate the free version first.', 'rank-math-pro' );
}
}
if ( ! $this->is_free_version_activated() ) {
if ( ! $this->activate_free_version() ) {
$this->messages[] = esc_html__( 'Rank Math free version is required to run Rank Math PRO, but it could not be activated automatically. Please install and activate the free version first.', 'rank-math-pro' );
}
}
}
if ( $dont_load ) {
return false;
}
if ( empty( $this->messages ) ) {
return true;
}
// Auto-deactivate plugin.
add_action( 'admin_init', [ $this, 'auto_deactivate' ] );
add_action( 'admin_notices', [ $this, 'activation_error' ] );
return false;
}
/**
* Check if troubleshooting mode is enabled in Health Check plugin and if Rank Math Free version is not.
*
* @return boolean
*/
public function is_troubleshooting() {
return (bool) get_option( 'health-check-allowed-plugins' ) && ! $this->is_free_version_activated();
}
/**
* Check if rollback is in progress, so that Pro doesn't get deactivated.
*
* @return boolean
*/
public function is_free_version_being_rolled_back() {
$reactivating = isset( $_GET['action'] )
&& 'activate-plugin' === $_GET['action']
&& isset( $_GET['plugin'] )
&& 'seo-by-rank-math/rank-math.php' === $_GET['plugin'];
return $reactivating || ( function_exists( 'rank_math' ) && rank_math()->version != get_option( 'rank_math_version' ) );
}
/**
* Auto-deactivate plugin if requirement not met and display a notice.
*/
public function auto_deactivate() {
deactivate_plugins( plugin_basename( __FILE__ ) );
if ( isset( $_GET['activate'] ) ) {
unset( $_GET['activate'] );
}
// phpcs:enable
}
/**
* Plugin activation notice.
*/
public function activation_error() {
?>
<div class="rank-math-notice notice notice-error">
<p>
<?php echo join( '<br>', $this->messages ); // phpcs:ignore ?>
</p>
</div>
<?php
}
/**
* Define the plugin constants.
*/
private function define_constants() {
define( 'RANK_MATH_PRO_VERSION', $this->version );
define( 'RANK_MATH_PRO_FILE', __FILE__ );
define( 'RANK_MATH_PRO_PATH', dirname( RANK_MATH_PRO_FILE ) . '/' );
define( 'RANK_MATH_PRO_URL', plugins_url( '', RANK_MATH_PRO_FILE ) . '/' );
}
/**
* Include the required files.
*/
private function includes() {
include dirname( __FILE__ ) . '/vendor/autoload.php';
}
/**
* Instantiate classes.
*/
private function instantiate() {
new \RankMathPro\Modules();
$this->load_3rd_party();
}
/**
* Load 3rd party modules.
*/
private function load_3rd_party() {
if ( defined( 'ELEMENTOR_VERSION' ) ) {
new \RankMathPro\Elementor\Elementor();
}
add_action(
'after_setup_theme',
function() {
if ( defined( 'ET_CORE' ) ) {
new \RankMathPro\Divi\Divi();
}
},
11
);
}
/**
* Initialize WordPress action hooks.
*/
private function init_actions() {
if ( is_admin() ) {
add_action( 'rank_math/admin/loaded', [ $this, 'init_admin' ], 15 );
}
add_action( 'rest_api_init', [ $this, 'init_rest_api' ] );
add_action( 'plugins_loaded', [ $this, 'init' ], 11 );
new \RankMathPro\Common();
new \RankMathPro\Register_Vars();
}
/**
* Initialize the admin.
*/
public function init_admin() {
new \RankMathPro\Admin\Admin();
}
/**
* Load the REST API endpoints.
*/
public function init_rest_api() {
$controllers = [
new \RankMathPro\Schema\Rest(),
new \RankMathPro\Analytics\Rest(),
new \RankMathPro\Rest\Rest(),
];
foreach ( $controllers as $controller ) {
$controller->register_routes();
}
}
/**
* Initialize.
*/
public function init() {
if ( Helper::is_module_active( 'image-seo' ) ) {
new \RankMathPro\Image_Seo_Pro();
}
if ( Helper::is_module_active( 'bbpress' ) ) {
new \RankMathPro\BBPress();
}
if ( Helper::is_module_active( 'local-seo', false ) ) {
new \RankMathPro\Local_Seo\Local_Seo();
}
if ( Helper::is_module_active( 'analytics' ) ) {
new \RankMathPro\Analytics\Analytics();
}
if ( Helper::is_woocommerce_active() && Helper::is_module_active( 'woocommerce' ) ) {
new \RankMathPro\WooCommerce();
}
if ( Helper::is_module_active( '404-monitor' ) ) {
new \RankMathPro\Monitor_Pro();
}
if ( Helper::is_module_active( 'redirections' ) ) {
new \RankMathPro\Redirections\Redirections();
}
if ( Helper::is_module_active( 'seo-analysis' ) ) {
new \RankMathPro\SEO_Analysis\SEO_Analysis_Pro();
}
if ( function_exists( 'acf' ) && Helper::is_module_active( 'acf' ) ) {
new \RankMathPro\ACF\ACF();
}
if ( Helper::is_module_active( 'content-ai' ) ) {
new \RankMathPro\Content_AI();
}
new \RankMathPro\Plugin_Update\Plugin_Update();
new \RankMathPro\Thumbnail_Overlays();
}
/**
* Initialize plugin for localization.
*
* Note: the first-loaded translation file overrides any following ones if the same translation is present.
*
* Locales found in:
* - WP_LANG_DIR/rank-math/rank-math-LOCALE.mo
* - WP_LANG_DIR/plugins/rank-math-LOCALE.mo
*/
public function localization_setup() {
$locale = is_admin() && function_exists( 'get_user_locale' ) ? get_user_locale() : get_locale();
$locale = apply_filters( 'plugin_locale', $locale, 'rank-math-pro' ); // phpcs:ignore
unload_textdomain( 'rank-math-pro' );
if ( false === load_textdomain( 'rank-math-pro', WP_LANG_DIR . '/plugins/seo-by-rank-math-pro-' . $locale . '.mo' ) ) {
load_textdomain( 'rank-math-pro', WP_LANG_DIR . '/seo-by-rank-math/seo-by-rank-math-pro-' . $locale . '.mo' );
}
load_plugin_textdomain( 'rank-math-pro', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
/**
* Check if Rank Math plugin is installed on the site.
*
* @return boolean Whether it's installed or not.
*/
public function is_free_version_installed() {
// First check if active, because that is less costly.
if ( $this->is_free_version_activated() ) {
return true;
}
if ( ! function_exists( 'get_plugins' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
$installed_plugins = get_plugins();
return array_key_exists( $this->free_version_plugin_path, $installed_plugins );
}
/**
* Install Rank Math free version from the wordpress.org repository.
*
* @return bool Whether install was successful.
*/
public function install_free_version() {
include_once ABSPATH . 'wp-includes/pluggable.php';
include_once ABSPATH . 'wp-admin/includes/misc.php';
include_once ABSPATH . 'wp-admin/includes/file.php';
include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
include_once ABSPATH . 'wp-admin/includes/plugin-install.php';
$skin = new Automatic_Upgrader_Skin();
$upgrader = new Plugin_Upgrader( $skin );
$plugin_file = 'https://downloads.wordpress.org/plugin/seo-by-rank-math.latest-stable.zip';
$result = $upgrader->install( $plugin_file );
return $result;
}
/**
* Check if Rank Math plugin is activated on the site.
*
* @return boolean Whether it's active or not.
*/
public function is_free_version_activated() {
$active_plugins = get_option( 'active_plugins', [] );
return in_array( $this->free_version_plugin_path, $active_plugins, true );
}
/**
* Checks if WP is in the process of updating the free one.
*
* @return boolean Whether we are in the process of updating the plugin or not.
*/
public function is_free_version_being_updated() {
$action = isset( $_POST['action'] ) && $_POST['action'] != -1 ? $_POST['action'] : '';
$plugins = isset( $_POST['plugin'] ) ? (array) $_POST['plugin'] : [];
if ( empty( $plugins ) ) {
$plugins = isset( $_POST['plugins'] ) ? (array) $_POST['plugins'] : [];
}
$update_plugin = 'update-plugin';
$update_selected = 'update-selected';
$actions = [ $update_plugin, $update_selected ];
if ( ! in_array( $action, $actions, true ) ) {
return false;
}
return in_array( $this->free_version_plugin_path, $plugins, true );
}
/**
* Checks if WP is in the process of deactivating the free one.
*
* @return boolean Whether we are in the process of deactivating the plugin or not.
*/
public function is_free_version_being_deactivated() {
if ( ! is_admin() ) {
return false;
}
$action = isset( $_REQUEST['action'] ) && $_REQUEST['action'] != -1 ? $_REQUEST['action'] : '';
if ( ! $action ) {
$action = isset( $_REQUEST['action2'] ) && $_REQUEST['action2'] != -1 ? $_REQUEST['action2'] : '';
}
$plugin = isset( $_REQUEST['plugin'] ) ? $_REQUEST['plugin'] : '';
$checked = isset( $_POST['checked'] ) && is_array( $_POST['checked'] ) ? $_POST['checked'] : [];
$deactivate = 'deactivate';
$deactivate_selected = 'deactivate-selected';
$actions = [ $deactivate, $deactivate_selected ];
if ( ! in_array( $action, $actions, true ) ) {
return false;
}
if ( $action === $deactivate && $plugin !== $this->free_version_plugin_path ) {
return false;
}
if ( $action === $deactivate_selected && ! in_array( $this->free_version_plugin_path, $checked, true ) ) {
return false;
}
return true;
}
/**
* Activate Rank Math free version.
*
* @return bool Whether activation was successful or not.
*/
public function activate_free_version() {
return activate_plugin( $this->free_version_plugin_path );
}
/**
* Is free version compatible.
*
* @return bool
*/
public function is_free_version_compatible() {
return defined( 'RANK_MATH_VERSION' ) && version_compare( RANK_MATH_VERSION, $this->rank_math_min_version, '>=' );
}
/**
* Add query arg to activate url.
*
* @param string $url Activate URL.
*/
public function add_query_arg( $url ) {
return add_query_arg( [ 'pro' => 1 ], $url );
}
}
/**
* Returns the main instance of RankMathPro to prevent the need to use globals.
*
* @return RankMathPro
*/
function rank_math_pro() {
return RankMathPro::get();
}
// Start it.
rank_math_pro();