-
Notifications
You must be signed in to change notification settings - Fork 10
/
classicpress-seo.php
434 lines (358 loc) · 10.5 KB
/
classicpress-seo.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
<?php
/**
* Plugin Name: Classic SEO
* Plugin URI: https://github.com/ClassicPress/classicpress-seo
* Description: Classic SEO is the first SEO plugin built specifically to work with ClassicPress. The plugin contains many essential SEO tools to help optimize your website.
* Version: 2.3.0
* Author: ClassicPress
* Author URI: https://github.com/ClassicPress
* License: GPL v2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
* Domain Path: /languages/
* Text Domain: cpseo
* Requires CP: 1.1
* Requires PHP: 7.4
* Update URI: https://directory.classicpress.net/wp-json/wp/v2/plugins?byslug=classicpress-seo
*
* Fork of Rank Math v1.0.33
*/
defined( 'ABSPATH' ) || exit;
/**
* Classic_SEO class.
*
* @class Main plugin class
*/
class Classic_SEO {
/**
* Plugin version.
*
* @var string
*/
public $version = '2.3.0';
/**
* Classic SEO database version.
*
* @var string
*/
public $db_version = '2';
/**
* Minimum version of ClassicPress required to run Classic SEO.
*
* @var string
*/
private $min_cp_version = '1.1.0';
/**
* Minimum version of PHP required to run Classic SEO.
*
* @var string
*/
private $php_version = '7.4';
/**
* Holds various class instances.
*
* @var array
*/
private $container = [];
/**
* Hold install error messages.
*
* @var bool
*/
private $messages = [];
/**
* The single instance of the class.
*
* @var Classic_SEO
*/
protected static $instance = null;
/**
* Magic isset to bypass referencing plugin.
*
* @param string $prop Property to check.
* @return bool
*/
public function __isset( $prop ) {
return isset( $this->{$prop} ) || isset( $this->container[ $prop ] );
}
/**
* Magic getter method.
*
* @param string $prop Property to get.
* @return mixed Property value or NULL if it does not exists.
*/
public function __get( $prop ) {
if ( array_key_exists( $prop, $this->container ) ) {
return $this->container[ $prop ];
}
if ( isset( $this->{$prop} ) ) {
return $this->{$prop};
}
return null;
}
/**
* Magic setter method.
*
* @param mixed $prop Property to set.
* @param mixed $value Value to set.
*/
public function __set( $prop, $value ) {
if ( property_exists( $this, $prop ) ) {
$this->$prop = $value;
return;
}
$this->container[ $prop ] = $value;
}
/**
* Magic call method.
*
* @param string $name Method to call.
* @param array $arguments Arguments to pass when calling.
* @return mixed Return value of the callback.
*/
public function __call( $name, $arguments ) {
$hash = [
'plugin_dir' => CPSEO_PATH,
'plugin_url' => CPSEO_PLUGIN_URL,
'includes_dir' => CPSEO_PATH . 'includes/',
'assets' => CPSEO_PLUGIN_URL . 'assets/front/',
'admin_dir' => CPSEO_PATH . 'includes/admin/',
];
if ( isset( $hash[ $name ] ) ) {
return $hash[ $name ];
}
return call_user_func_array( $name, $arguments );
}
/**
* Initialize.
*/
public function init() {
}
/**
* Retrieve main Classic_SEO instance.
*
* Ensure only one instance is loaded or can be loaded.
*
* @see Classic_SEO()
* @return Classic_SEO
*/
public static function get() {
if ( is_null( self::$instance ) && ! ( self::$instance instanceof Classic_SEO ) ) {
self::$instance = new Classic_SEO();
self::$instance->setup();
}
return self::$instance;
}
/**
* Instantiate
*/
private function setup() {
// Define plugin constants.
$this->define_constants();
if ( ! $this->requirements() ) {
return;
}
// Include required files.
$this->includes();
// Instantiate classes.
$this->instantiate();
// Initialize the action and filter hooks.
$this->init_actions();
// Loaded action.
do_action( 'cpseo/loaded' );
}
/**
* Define the plugin constants.
*/
private function define_constants() {
define( 'CPSEO_VERSION', $this->version );
define( 'CPSEO_DB_VERSION', $this->db_version );
define( 'CPSEO_MINIMUM_PHP_VERSION', $this->php_version );
define( 'CPSEO_FILE', __FILE__ );
define( 'CPSEO_PATH', plugin_dir_path( CPSEO_FILE ) );
define( 'CPSEO_BASENAME', plugin_basename( CPSEO_FILE ) );
define( 'CPSEO_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
}
/**
* Check that the ClassicPress and PHP setup meets the plugin requirements.
*
* @return bool
*/
private function requirements() {
// Check ClassicPress version.
if ( version_compare( get_bloginfo( 'version' ), $this->min_cp_version, '<' ) ) {
$this->messages[] = sprintf( esc_html__( 'Classic SEO requires ClassicPress version %s or above. Please update ClassicPress.', 'cpseo' ), $this->min_cp_version );
}
// Check PHP version.
if ( version_compare( phpversion(), $this->php_version, '<' ) ) {
$this->messages[] = sprintf( esc_html__( 'Classic SEO requires PHP version %s or above. Please update PHP.', 'cpseo' ), $this->php_version );
}
if ( empty( $this->messages ) ) {
return true;
}
if ( defined( 'WP_CLI' ) && WP_CLI && ! ( empty( $this->messages ) ) ) {
return \WP_CLI::error( implode( "\n", $this->messages ), false );
}
// Auto-deactivate plugin.
add_action( 'admin_init', [ $this, 'auto_deactivate' ] );
add_action( 'admin_notices', [ $this, 'activation_error' ] );
return false;
}
/**
* Auto-deactivate plugin if requirements are not met, and display a notice.
*/
public function auto_deactivate() {
deactivate_plugins( plugin_basename( CPSEO_FILE ) );
if ( isset( $_GET['activate'] ) ) {
unset( $_GET['activate'] );
}
}
/**
* Error notice on plugin activation.
*/
public function activation_error() {
?>
<div class="notice notice-error">
<p><?php echo join( '<br>', $this->messages ); ?></p>
</div>
<?php
}
/**
* Include the required files.
*/
private function includes() {
include dirname( __FILE__ ) . '/vendor/autoload.php';
require_once( dirname( __FILE__ ) . '/includes/class-update-client.php' );
require_once( dirname( __FILE__ ) . '/includes/class-update-client-tweaks.php' );
}
/**
* Instantiate classes.
*/
private function instantiate() {
new \Classic_SEO\Installer;
// Setting Manager.
$this->container['settings'] = new \Classic_SEO\Settings;
// JSON Manager.
$this->container['json'] = new \Classic_SEO\Json_Manager;
// Notification Manager.
$this->container['notification'] = new \Classic_SEO\Notification_Center( 'cpseo_notifications' );
$this->container['manager'] = new \Classic_SEO\Module\Manager;
$this->container['variables'] = new \Classic_SEO\Replace_Variables\Manager;
// Just init without storing it in the container.
new \Classic_SEO\Common;
$this->container['rewrite'] = new \Classic_SEO\Rewrite;
}
/**
* Initialize ClassicPress action and filter hooks.
*/
private function init_actions() {
add_action( 'init', [ $this, 'localization_setup' ] );
// Add plugin action links.
add_filter( 'plugin_row_meta', [ $this, 'plugin_row_meta' ], 10, 2 );
add_filter( 'plugin_action_links_' . plugin_basename( CPSEO_FILE ), [ $this, 'plugin_action_links' ] );
// Booting.
add_action( 'plugins_loaded', [ $this, 'init' ], 14 );
add_action( 'rest_api_init', [ $this, 'init_rest_api' ] );
// Load admin-related functionality.
if ( is_admin() ) {
add_action( 'plugins_loaded', [ $this, 'init_admin' ], 15 );
}
// Frontend-only functionality.
if ( ! is_admin() || in_array( \Classic_SEO\Helpers\Param::request( 'action' ), [ 'elementor', 'elementor_ajax' ], true ) ) {
add_action( 'plugins_loaded', [ $this, 'init_frontend' ], 15 );
}
// WP_CLI.
if ( defined( 'WP_CLI' ) && WP_CLI ) {
add_action( 'plugins_loaded', [ $this, 'init_wp_cli' ], 20 );
}
}
/**
* Load the REST API endpoints.
*/
public function init_rest_api() {
$controllers = [
new \Classic_SEO\Rest\Admin,
];
foreach ( $controllers as $controller ) {
$controller->register_routes();
}
}
/**
* Initialize the admin-related functionality.
* Runs on 'plugins_loaded'.
*/
public function init_admin() {
new \Classic_SEO\Admin\Engine;
}
/**
* Initialize the frontend functionality.
* Runs on 'plugins_loaded'.
*/
public function init_frontend() {
$this->container['frontend'] = new \Classic_SEO\Frontend\Frontend;
}
/**
* Add our custom WP-CLI commands.
*/
public function init_wp_cli() {
WP_CLI::add_command( 'cpseo sitemap generate', [ '\Classic_SEO\CLI\Commands', 'sitemap_generate' ] );
}
/**
* Show action links on the plugin screen.
*
* @param mixed $links Plugin Action links.
* @return array
*/
public function plugin_action_links( $links ) {
$plugin_links = [
'<a href="' . Classic_SEO\Helper::get_admin_url( 'options-general' ) . '">' . esc_html__( 'Settings', 'cpseo' ) . '</a>',
];
return array_merge( $links, $plugin_links );
}
/**
* Add extra links as row meta on the plugin screen.
*
* @param mixed $links Plugin Row Meta.
* @param mixed $file Plugin Base file.
* @return array
*/
public function plugin_row_meta( $links, $file ) {
if ( plugin_basename( CPSEO_FILE ) !== $file ) {
return $links;
}
return $links;
}
/**
* 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/plugins/cpseo-LOCALE.mo
* - WP_LANG_DIR/classicpress-seo/cpseo-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, 'cpseo' );
unload_textdomain( 'cpseo' );
if ( false === load_textdomain( 'cpseo', WP_LANG_DIR . '/plugins/cpseo-' . $locale . '.mo' ) ) {
load_textdomain( 'cpseo', WP_LANG_DIR . '/classicpress-seo/cpseo-' . $locale . '.mo' );
}
load_plugin_textdomain( 'cpseo', false, cpseo()->plugin_dir() . 'languages/' );
if ( is_user_logged_in() ) {
$this->container['json']->add( 'version', $this->version, 'classicSEO' );
$this->container['json']->add( 'ajaxurl', admin_url( 'admin-ajax.php' ), 'classicSEO' );
$this->container['json']->add( 'adminurl', admin_url( 'admin.php' ), 'classicSEO' );
$this->container['json']->add( 'security', wp_create_nonce( 'cpseo-ajax-nonce' ), 'classicSEO' );
}
}
}
/**
* Returns the main instance of Classic_SEO to prevent the need to use globals.
*
* @return Classic_SEO
*/
function cpseo() {
return Classic_SEO::get();
}
// Let's go.
cpseo();