| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPSEO plugin file. |
| 4 |
* |
| 5 |
* @package WPSEO\Admin |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Performs the load on admin side. |
| 10 |
*/ |
| 11 |
class WPSEO_Admin_Init { |
| 12 |
|
| 13 |
/** |
| 14 |
* Holds the global `$pagenow` variable's value. |
| 15 |
* |
| 16 |
* @var string |
| 17 |
*/ |
| 18 |
private $pagenow; |
| 19 |
|
| 20 |
/** |
| 21 |
* Holds the asset manager. |
| 22 |
* |
| 23 |
* @var WPSEO_Admin_Asset_Manager |
| 24 |
*/ |
| 25 |
private $asset_manager; |
| 26 |
|
| 27 |
/** |
| 28 |
* Class constructor. |
| 29 |
*/ |
| 30 |
public function __construct() { |
| 31 |
$GLOBALS['wpseo_admin'] = new WPSEO_Admin(); |
| 32 |
|
| 33 |
$this->pagenow = $GLOBALS['pagenow']; |
| 34 |
|
| 35 |
$this->asset_manager = new WPSEO_Admin_Asset_Manager(); |
| 36 |
|
| 37 |
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_dismissible' ] ); |
| 38 |
add_action( 'admin_init', [ $this, 'unsupported_php_notice' ], 15 ); |
| 39 |
add_action( 'admin_init', [ $this, 'remove_translations_notification' ], 15 ); |
| 40 |
add_action( 'admin_init', [ $this->asset_manager, 'register_assets' ] ); |
| 41 |
add_action( 'admin_init', [ $this, 'show_hook_deprecation_warnings' ] ); |
| 42 |
add_action( 'admin_init', [ 'WPSEO_Plugin_Conflict', 'hook_check_for_plugin_conflicts' ] ); |
| 43 |
add_action( 'admin_notices', [ $this, 'permalink_settings_notice' ] ); |
| 44 |
add_action( 'post_submitbox_misc_actions', [ $this, 'add_publish_box_section' ] ); |
| 45 |
|
| 46 |
$this->load_meta_boxes(); |
| 47 |
$this->load_taxonomy_class(); |
| 48 |
$this->load_admin_page_class(); |
| 49 |
$this->load_admin_user_class(); |
| 50 |
$this->load_xml_sitemaps_admin(); |
| 51 |
$this->load_plugin_suggestions(); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Enqueue our styling for dismissible yoast notifications. |
| 56 |
* |
| 57 |
* @return void |
| 58 |
*/ |
| 59 |
public function enqueue_dismissible() { |
| 60 |
$this->asset_manager->enqueue_style( 'dismissible' ); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Removes any notification for incomplete translations. |
| 65 |
* |
| 66 |
* @return void |
| 67 |
*/ |
| 68 |
public function remove_translations_notification() { |
| 69 |
$notification_center = Yoast_Notification_Center::get(); |
| 70 |
$notification_center->remove_notification_by_id( 'i18nModuleTranslationAssistance' ); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Creates an unsupported PHP version notification in the notification center. |
| 75 |
* |
| 76 |
* @return void |
| 77 |
*/ |
| 78 |
public function unsupported_php_notice() { |
| 79 |
$notification_center = Yoast_Notification_Center::get(); |
| 80 |
$notification_center->remove_notification_by_id( 'wpseo-dismiss-unsupported-php' ); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Gets the latest released major WordPress version from the WordPress stable-check api. |
| 85 |
* |
| 86 |
* @return float|int The latest released major WordPress version. 0 when the stable-check API doesn't respond. |
| 87 |
*/ |
| 88 |
private function get_latest_major_wordpress_version() { |
| 89 |
$core_updates = get_core_updates( [ 'dismissed' => true ] ); |
| 90 |
|
| 91 |
if ( $core_updates === false ) { |
| 92 |
return 0; |
| 93 |
} |
| 94 |
|
| 95 |
$wp_version_latest = get_bloginfo( 'version' ); |
| 96 |
foreach ( $core_updates as $update ) { |
| 97 |
if ( $update->response === 'upgrade' && version_compare( $update->version, $wp_version_latest, '>' ) ) { |
| 98 |
$wp_version_latest = $update->version; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
// Strip the patch version and convert to a float. |
| 103 |
return (float) $wp_version_latest; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Helper to verify if the user is currently visiting one of our admin pages. |
| 108 |
* |
| 109 |
* @return bool |
| 110 |
*/ |
| 111 |
private function on_wpseo_admin_page() { |
| 112 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information. |
| 113 |
if ( ! isset( $_GET['page'] ) || ! is_string( $_GET['page'] ) ) { |
| 114 |
return false; |
| 115 |
} |
| 116 |
|
| 117 |
if ( $this->pagenow !== 'admin.php' ) { |
| 118 |
return false; |
| 119 |
} |
| 120 |
|
| 121 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information. |
| 122 |
$current_page = sanitize_text_field( wp_unslash( $_GET['page'] ) ); |
| 123 |
return strpos( $current_page, 'wpseo' ) === 0; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Whether we should load the meta box classes. |
| 128 |
* |
| 129 |
* @return bool true if we should load the meta box classes, false otherwise. |
| 130 |
*/ |
| 131 |
private function should_load_meta_boxes() { |
| 132 |
/** |
| 133 |
* Filter: 'wpseo_always_register_metaboxes_on_admin' - Allow developers to change whether |
| 134 |
* the WPSEO metaboxes are only registered on the typical pages (lean loading) or always |
| 135 |
* registered when in admin. |
| 136 |
* |
| 137 |
* @param bool $register_metaboxes Whether to always register the metaboxes or not. Defaults to false. |
| 138 |
*/ |
| 139 |
if ( apply_filters( 'wpseo_always_register_metaboxes_on_admin', false ) ) { |
| 140 |
return true; |
| 141 |
} |
| 142 |
|
| 143 |
// If we are in a post editor. |
| 144 |
if ( WPSEO_Metabox::is_post_overview( $this->pagenow ) || WPSEO_Metabox::is_post_edit( $this->pagenow ) ) { |
| 145 |
return true; |
| 146 |
} |
| 147 |
|
| 148 |
// If we are doing an inline save. |
| 149 |
if ( check_ajax_referer( 'inlineeditnonce', '_inline_edit', false ) && isset( $_POST['action'] ) && sanitize_text_field( wp_unslash( $_POST['action'] ) ) === 'inline-save' ) { |
| 150 |
return true; |
| 151 |
} |
| 152 |
|
| 153 |
return false; |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Determine whether we should load the meta box class and if so, load it. |
| 158 |
* |
| 159 |
* @return void |
| 160 |
*/ |
| 161 |
private function load_meta_boxes() { |
| 162 |
if ( $this->should_load_meta_boxes() ) { |
| 163 |
$GLOBALS['wpseo_metabox'] = new WPSEO_Metabox(); |
| 164 |
$GLOBALS['wpseo_meta_columns'] = new WPSEO_Meta_Columns(); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Determine if we should load our taxonomy edit class and if so, load it. |
| 170 |
* |
| 171 |
* @return void |
| 172 |
*/ |
| 173 |
private function load_taxonomy_class() { |
| 174 |
if ( |
| 175 |
WPSEO_Taxonomy::is_term_edit( $this->pagenow ) |
| 176 |
|| WPSEO_Taxonomy::is_term_overview( $this->pagenow ) |
| 177 |
) { |
| 178 |
new WPSEO_Taxonomy(); |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Determine if we should load our admin pages class and if so, load it. |
| 184 |
* |
| 185 |
* Loads admin page class for all admin pages starting with `wpseo_`. |
| 186 |
* |
| 187 |
* @return void |
| 188 |
*/ |
| 189 |
private function load_admin_user_class() { |
| 190 |
if ( in_array( $this->pagenow, [ 'user-edit.php', 'profile.php' ], true ) |
| 191 |
&& current_user_can( 'edit_users' ) |
| 192 |
) { |
| 193 |
new WPSEO_Admin_User_Profile(); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Determine if we should load our admin pages class and if so, load it. |
| 199 |
* |
| 200 |
* Loads admin page class for all admin pages starting with `wpseo_`. |
| 201 |
* |
| 202 |
* @return void |
| 203 |
*/ |
| 204 |
private function load_admin_page_class() { |
| 205 |
|
| 206 |
if ( $this->on_wpseo_admin_page() ) { |
| 207 |
// For backwards compatabilty, this still needs a global, for now... |
| 208 |
$GLOBALS['wpseo_admin_pages'] = new WPSEO_Admin_Pages(); |
| 209 |
|
| 210 |
$page = null; |
| 211 |
|
| 212 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information. |
| 213 |
if ( isset( $_GET['page'] ) && is_string( $_GET['page'] ) ) { |
| 214 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information. |
| 215 |
$page = sanitize_text_field( wp_unslash( $_GET['page'] ) ); |
| 216 |
} |
| 217 |
|
| 218 |
// Only renders Yoast SEO Premium upsells when the page is a Yoast SEO page. |
| 219 |
if ( $page !== null && WPSEO_Utils::is_yoast_seo_free_page( $page ) ) { |
| 220 |
$this->register_premium_upsell_admin_block(); |
| 221 |
} |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* Loads the plugin suggestions. |
| 227 |
* |
| 228 |
* @return void |
| 229 |
*/ |
| 230 |
private function load_plugin_suggestions() { |
| 231 |
$suggestions = new WPSEO_Suggested_Plugins( new WPSEO_Plugin_Availability(), Yoast_Notification_Center::get() ); |
| 232 |
$suggestions->register_hooks(); |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* Registers the Premium Upsell Admin Block. |
| 237 |
* |
| 238 |
* @return void |
| 239 |
*/ |
| 240 |
private function register_premium_upsell_admin_block() { |
| 241 |
if ( ! YoastSEO()->helpers->product->is_premium() ) { |
| 242 |
$upsell_block = new WPSEO_Premium_Upsell_Admin_Block( 'wpseo_admin_promo_footer' ); |
| 243 |
$upsell_block->register_hooks(); |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
/** |
| 248 |
* See if we should start our XML Sitemaps Admin class. |
| 249 |
* |
| 250 |
* @return void |
| 251 |
*/ |
| 252 |
private function load_xml_sitemaps_admin() { |
| 253 |
if ( WPSEO_Options::get( 'enable_xml_sitemap', false, [ 'wpseo' ] ) ) { |
| 254 |
new WPSEO_Sitemaps_Admin(); |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
/** |
| 259 |
* Shows deprecation warnings to the user if a plugin has registered a filter we have deprecated. |
| 260 |
* |
| 261 |
* @return void |
| 262 |
*/ |
| 263 |
public function show_hook_deprecation_warnings() { |
| 264 |
global $wp_filter; |
| 265 |
|
| 266 |
if ( wp_doing_ajax() ) { |
| 267 |
return; |
| 268 |
} |
| 269 |
|
| 270 |
// WordPress hooks that have been deprecated since a Yoast SEO version. |
| 271 |
$deprecated_filters = [ |
| 272 |
'wpseo_genesis_force_adjacent_rel_home' => [ |
| 273 |
'version' => '9.4', |
| 274 |
'alternative' => null, |
| 275 |
], |
| 276 |
'wpseo_opengraph' => [ |
| 277 |
'version' => '14.0', |
| 278 |
'alternative' => null, |
| 279 |
], |
| 280 |
'wpseo_twitter' => [ |
| 281 |
'version' => '14.0', |
| 282 |
'alternative' => null, |
| 283 |
], |
| 284 |
'wpseo_twitter_taxonomy_image' => [ |
| 285 |
'version' => '14.0', |
| 286 |
'alternative' => null, |
| 287 |
], |
| 288 |
'wpseo_twitter_metatag_key' => [ |
| 289 |
'version' => '14.0', |
| 290 |
'alternative' => null, |
| 291 |
], |
| 292 |
'wp_seo_get_bc_ancestors' => [ |
| 293 |
'version' => '14.0', |
| 294 |
'alternative' => 'wpseo_breadcrumb_links', |
| 295 |
], |
| 296 |
'validate_facebook_app_id_api_response_code' => [ |
| 297 |
'version' => '15.5', |
| 298 |
'alternative' => null, |
| 299 |
], |
| 300 |
'validate_facebook_app_id_api_response_body' => [ |
| 301 |
'version' => '15.5', |
| 302 |
'alternative' => null, |
| 303 |
], |
| 304 |
]; |
| 305 |
|
| 306 |
// Determine which filters have been registered. |
| 307 |
$deprecated_notices = array_intersect( |
| 308 |
array_keys( $deprecated_filters ), |
| 309 |
array_keys( $wp_filter ), |
| 310 |
); |
| 311 |
|
| 312 |
// Show notice for each deprecated filter or action that has been registered. |
| 313 |
foreach ( $deprecated_notices as $deprecated_filter ) { |
| 314 |
$deprecation_info = $deprecated_filters[ $deprecated_filter ]; |
| 315 |
// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped -- Only uses the hardcoded values from above. |
| 316 |
_deprecated_hook( |
| 317 |
$deprecated_filter, |
| 318 |
'WPSEO ' . $deprecation_info['version'], |
| 319 |
$deprecation_info['alternative'], |
| 320 |
); |
| 321 |
// phpcs:enable |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
/** |
| 326 |
* Check if the permalink uses %postname%. |
| 327 |
* |
| 328 |
* @return bool |
| 329 |
*/ |
| 330 |
private function has_postname_in_permalink() { |
| 331 |
return ( strpos( get_option( 'permalink_structure' ), '%postname%' ) !== false ); |
| 332 |
} |
| 333 |
|
| 334 |
/** |
| 335 |
* Shows a notice on the permalink settings page. |
| 336 |
* |
| 337 |
* @return void |
| 338 |
*/ |
| 339 |
public function permalink_settings_notice() { |
| 340 |
global $pagenow; |
| 341 |
|
| 342 |
if ( $pagenow === 'options-permalink.php' ) { |
| 343 |
printf( |
| 344 |
'<div class="notice notice-warning"><p><strong>%1$s</strong><br>%2$s<br><a href="%3$s" target="_blank">%4$s</a></p></div>', |
| 345 |
esc_html__( 'WARNING:', 'wordpress-seo' ), |
| 346 |
sprintf( |
| 347 |
/* translators: %1$s and %2$s expand to <em> items to emphasize the word in the middle. */ |
| 348 |
esc_html__( 'Changing your permalinks settings can seriously impact your search engine visibility. It should almost %1$s never %2$s be done on a live website.', 'wordpress-seo' ), |
| 349 |
'<em>', |
| 350 |
'</em>', |
| 351 |
), |
| 352 |
esc_url( WPSEO_Shortlinker::get( 'https://yoa.st/why-permalinks/' ) ), |
| 353 |
// The link's content. |
| 354 |
esc_html__( 'Learn about why permalinks are important for SEO.', 'wordpress-seo' ), |
| 355 |
); |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
/** |
| 360 |
* Adds a custom Yoast section within the Classic Editor publish box. |
| 361 |
* |
| 362 |
* @param WP_Post $post The current post object. |
| 363 |
* |
| 364 |
* @return void |
| 365 |
*/ |
| 366 |
public function add_publish_box_section( $post ) { |
| 367 |
if ( in_array( $this->pagenow, [ 'post.php', 'post-new.php' ], true ) ) { |
| 368 |
?> |
| 369 |
<div id="yoast-seo-publishbox-section"></div> |
| 370 |
<?php |
| 371 |
/** |
| 372 |
* Fires after the post time/date setting in the Publish meta box. |
| 373 |
* |
| 374 |
* @param WP_Post $post The current post object. |
| 375 |
*/ |
| 376 |
do_action( 'wpseo_publishbox_misc_actions', $post ); |
| 377 |
} |
| 378 |
} |
| 379 |
} |
| 380 |
|