| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPSEO plugin file. |
| 4 |
* |
| 5 |
* @package WPSEO\Admin |
| 6 |
*/ |
| 7 |
|
| 8 |
use Yoast\WP\SEO\Config\Schema_Types; |
| 9 |
|
| 10 |
/** |
| 11 |
* Class WPSEO_Admin_Pages. |
| 12 |
* |
| 13 |
* Class with functionality for the Yoast SEO admin pages. |
| 14 |
*/ |
| 15 |
class WPSEO_Admin_Pages { |
| 16 |
|
| 17 |
/** |
| 18 |
* The option in use for the current admin page. |
| 19 |
* |
| 20 |
* @var string |
| 21 |
*/ |
| 22 |
public $currentoption = 'wpseo'; |
| 23 |
|
| 24 |
/** |
| 25 |
* Holds the asset manager. |
| 26 |
* |
| 27 |
* @var WPSEO_Admin_Asset_Manager |
| 28 |
*/ |
| 29 |
private $asset_manager; |
| 30 |
|
| 31 |
/** |
| 32 |
* Class constructor, which basically only hooks the init function on the init hook. |
| 33 |
*/ |
| 34 |
public function __construct() { |
| 35 |
add_action( 'init', [ $this, 'init' ], 20 ); |
| 36 |
$this->asset_manager = new WPSEO_Admin_Asset_Manager(); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Make sure the needed scripts are loaded for admin pages. |
| 41 |
*/ |
| 42 |
public function init() { |
| 43 |
if ( filter_input( INPUT_GET, 'wpseo_reset_defaults' ) && wp_verify_nonce( filter_input( INPUT_GET, 'nonce' ), 'wpseo_reset_defaults' ) && current_user_can( 'manage_options' ) ) { |
| 44 |
WPSEO_Options::reset(); |
| 45 |
} |
| 46 |
|
| 47 |
add_action( 'admin_enqueue_scripts', [ $this, 'config_page_scripts' ] ); |
| 48 |
add_action( 'admin_enqueue_scripts', [ $this, 'config_page_styles' ] ); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Loads the required styles for the config page. |
| 53 |
*/ |
| 54 |
public function config_page_styles() { |
| 55 |
wp_enqueue_style( 'dashboard' ); |
| 56 |
wp_enqueue_style( 'thickbox' ); |
| 57 |
wp_enqueue_style( 'global' ); |
| 58 |
wp_enqueue_style( 'wp-admin' ); |
| 59 |
$this->asset_manager->enqueue_style( 'select2' ); |
| 60 |
$this->asset_manager->enqueue_style( 'admin-css' ); |
| 61 |
|
| 62 |
$page = filter_input( INPUT_GET, 'page' ); |
| 63 |
if ( $page === 'wpseo_titles' ) { |
| 64 |
$this->asset_manager->enqueue_style( 'search-appearance' ); |
| 65 |
} |
| 66 |
|
| 67 |
if ( $page === 'wpseo_social' || $page === 'wpseo_licenses' ) { |
| 68 |
$this->asset_manager->enqueue_style( 'monorepo' ); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Loads the required scripts for the config page. |
| 74 |
*/ |
| 75 |
public function config_page_scripts() { |
| 76 |
$this->asset_manager->enqueue_script( 'settings' ); |
| 77 |
wp_enqueue_script( 'dashboard' ); |
| 78 |
wp_enqueue_script( 'thickbox' ); |
| 79 |
|
| 80 |
$alert_dismissal_action = YoastSEO()->classes->get( \Yoast\WP\SEO\Actions\Alert_Dismissal_Action::class ); |
| 81 |
$dismissed_alerts = $alert_dismissal_action->all_dismissed(); |
| 82 |
|
| 83 |
$script_data = [ |
| 84 |
'userLanguageCode' => WPSEO_Language_Utils::get_language( \get_user_locale() ), |
| 85 |
'dismissedAlerts' => $dismissed_alerts, |
| 86 |
]; |
| 87 |
|
| 88 |
$page = filter_input( INPUT_GET, 'page' ); |
| 89 |
|
| 90 |
if ( $page === 'wpseo_titles' ) { |
| 91 |
$script_data['analysis'] = [ |
| 92 |
'plugins' => [ |
| 93 |
'replaceVars' => $this->get_replace_vars_script_data(), |
| 94 |
], |
| 95 |
]; |
| 96 |
|
| 97 |
$schema_types = new Schema_Types(); |
| 98 |
$script_data['searchAppearance'] = [ |
| 99 |
'isRtl' => is_rtl(), |
| 100 |
'userEditUrl' => add_query_arg( 'user_id', '{user_id}', admin_url( 'user-edit.php' ) ), |
| 101 |
'brushstrokeBackgroundURL' => plugins_url( 'images/brushstroke_background.svg', WPSEO_FILE ), |
| 102 |
'showLocalSEOUpsell' => $this->should_show_local_seo_upsell(), |
| 103 |
'localSEOUpsellURL' => WPSEO_Shortlinker::get( 'https://yoa.st/3mp' ), |
| 104 |
'knowledgeGraphCompanyInfoMissing' => WPSEO_Language_Utils::get_knowledge_graph_company_info_missing_l10n(), |
| 105 |
'schema' => [ |
| 106 |
'pageTypeOptions' => $schema_types->get_page_type_options(), |
| 107 |
'articleTypeOptions' => $schema_types->get_article_type_options(), |
| 108 |
], |
| 109 |
]; |
| 110 |
|
| 111 |
/** |
| 112 |
* Remove the emoji script as it is incompatible with both React and any |
| 113 |
* contenteditable fields. |
| 114 |
*/ |
| 115 |
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' ); |
| 116 |
} |
| 117 |
|
| 118 |
if ( in_array( $page, [ 'wpseo_social', WPSEO_Admin::PAGE_IDENTIFIER, 'wpseo_titles', 'wpseo_workouts' ], true ) ) { |
| 119 |
wp_enqueue_media(); |
| 120 |
|
| 121 |
$script_data['media'] = [ |
| 122 |
'choose_image' => __( 'Use Image', 'wordpress-seo' ), |
| 123 |
]; |
| 124 |
} |
| 125 |
|
| 126 |
if ( $page === 'wpseo_tools' ) { |
| 127 |
$this->enqueue_tools_scripts(); |
| 128 |
} |
| 129 |
|
| 130 |
if ( $page === 'wpseo_social' ) { |
| 131 |
$script_data['social'] = true; |
| 132 |
} |
| 133 |
|
| 134 |
$this->asset_manager->localize_script( 'settings', 'wpseoScriptData', $script_data ); |
| 135 |
$this->asset_manager->enqueue_user_language_script(); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Retrieves some variables that are needed for replacing variables in JS. |
| 140 |
* |
| 141 |
* @return array The replacement and recommended replacement variables. |
| 142 |
*/ |
| 143 |
public function get_replace_vars_script_data() { |
| 144 |
$replace_vars = new WPSEO_Replace_Vars(); |
| 145 |
$recommended_replace_vars = new WPSEO_Admin_Recommended_Replace_Vars(); |
| 146 |
$editor_specific_replace_vars = new WPSEO_Admin_Editor_Specific_Replace_Vars(); |
| 147 |
$replace_vars_list = $replace_vars->get_replacement_variables_list(); |
| 148 |
|
| 149 |
return [ |
| 150 |
'replace_vars' => $replace_vars_list, |
| 151 |
'recommended_replace_vars' => $recommended_replace_vars->get_recommended_replacevars(), |
| 152 |
'editor_specific_replace_vars' => $editor_specific_replace_vars->get(), |
| 153 |
'shared_replace_vars' => $editor_specific_replace_vars->get_generic( $replace_vars_list ), |
| 154 |
'hidden_replace_vars' => $replace_vars->get_hidden_replace_vars(), |
| 155 |
]; |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Determines whether the Local SEO upsell should be shown. |
| 160 |
* |
| 161 |
* The Local SEO upsell should: |
| 162 |
* - Only be shown in Free, not when Premium is active. |
| 163 |
* - Not be shown when Local SEO is active. |
| 164 |
* |
| 165 |
* @return bool Whether the Local SEO upsell should be shown. |
| 166 |
*/ |
| 167 |
private function should_show_local_seo_upsell() { |
| 168 |
return ! YoastSEO()->helpers->product->is_premium() |
| 169 |
&& ! ( defined( 'WPSEO_LOCAL_FILE' ) ); |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Enqueues and handles all the tool dependencies. |
| 174 |
*/ |
| 175 |
private function enqueue_tools_scripts() { |
| 176 |
$tool = filter_input( INPUT_GET, 'tool' ); |
| 177 |
|
| 178 |
if ( empty( $tool ) ) { |
| 179 |
$this->asset_manager->enqueue_script( 'yoast-seo' ); |
| 180 |
} |
| 181 |
|
| 182 |
if ( $tool === 'bulk-editor' ) { |
| 183 |
$this->asset_manager->enqueue_script( 'bulk-editor' ); |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
|