class-config.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin
  6. */
  7. /**
  8. * Class WPSEO_Admin_Pages.
  9. *
  10. * Class with functionality for the Yoast SEO admin pages.
  11. */
  12. class WPSEO_Admin_Pages {
  13. /**
  14. * The option in use for the current admin page.
  15. *
  16. * @var string
  17. */
  18. public $currentoption = 'wpseo';
  19. /**
  20. * Holds the asset manager.
  21. *
  22. * @var WPSEO_Admin_Asset_Manager
  23. */
  24. private $asset_manager;
  25. /**
  26. * Class constructor, which basically only hooks the init function on the init hook.
  27. */
  28. public function __construct() {
  29. add_action( 'init', [ $this, 'init' ], 20 );
  30. $this->asset_manager = new WPSEO_Admin_Asset_Manager();
  31. }
  32. /**
  33. * Make sure the needed scripts are loaded for admin pages.
  34. */
  35. public function init() {
  36. if ( filter_input( INPUT_GET, 'wpseo_reset_defaults' ) && wp_verify_nonce( filter_input( INPUT_GET, 'nonce' ), 'wpseo_reset_defaults' ) && current_user_can( 'manage_options' ) ) {
  37. WPSEO_Options::reset();
  38. wp_redirect( admin_url( 'admin.php?page=' . WPSEO_Configuration_Page::PAGE_IDENTIFIER ) );
  39. }
  40. add_action( 'admin_enqueue_scripts', [ $this, 'config_page_scripts' ] );
  41. add_action( 'admin_enqueue_scripts', [ $this, 'config_page_styles' ] );
  42. }
  43. /**
  44. * Loads the required styles for the config page.
  45. */
  46. public function config_page_styles() {
  47. wp_enqueue_style( 'dashboard' );
  48. wp_enqueue_style( 'thickbox' );
  49. wp_enqueue_style( 'global' );
  50. wp_enqueue_style( 'wp-admin' );
  51. $this->asset_manager->enqueue_style( 'select2' );
  52. $this->asset_manager->enqueue_style( 'admin-css' );
  53. }
  54. /**
  55. * Loads the required scripts for the config page.
  56. */
  57. public function config_page_scripts() {
  58. $this->asset_manager->enqueue_script( 'admin-script' );
  59. $page = filter_input( INPUT_GET, 'page' );
  60. if ( $page === 'wpseo_titles' ) {
  61. wp_localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'search-appearance', 'wpseoReplaceVarsL10n', $this->localize_replace_vars_script() );
  62. wp_localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'search-appearance', 'wpseoSearchAppearance', $this->localize_search_appearance_script() );
  63. $this->asset_manager->enqueue_script( 'search-appearance' );
  64. $this->asset_manager->enqueue_style( 'search-appearance' );
  65. /**
  66. * Remove the emoji script as it is incompatible with both React and any
  67. * contenteditable fields.
  68. */
  69. remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
  70. $yoast_components_l10n = new WPSEO_Admin_Asset_Yoast_Components_L10n();
  71. $yoast_components_l10n->localize_script( 'search-appearance' );
  72. }
  73. wp_enqueue_script( 'dashboard' );
  74. wp_enqueue_script( 'thickbox' );
  75. wp_localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'admin-script', 'wpseoSelect2Locale', WPSEO_Language_Utils::get_language( WPSEO_Language_Utils::get_user_locale() ) );
  76. if ( in_array( $page, [ 'wpseo_social', WPSEO_Admin::PAGE_IDENTIFIER, 'wpseo_titles' ], true ) ) {
  77. wp_enqueue_media();
  78. $this->asset_manager->enqueue_script( 'admin-media' );
  79. wp_localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'admin-media', 'wpseoMediaL10n', $this->localize_media_script() );
  80. }
  81. if ( 'wpseo_tools' === $page ) {
  82. $this->enqueue_tools_scripts();
  83. }
  84. }
  85. /**
  86. * Retrieves some variables that are needed for the upload module in JS.
  87. *
  88. * @return array The upload module variables.
  89. */
  90. public function localize_media_script() {
  91. return [
  92. 'choose_image' => __( 'Use Image', 'wordpress-seo' ),
  93. ];
  94. }
  95. /**
  96. * Retrieves some variables that are needed for replacing variables in JS.
  97. *
  98. * @return array The replacement and recommended replacement variables.
  99. */
  100. public function localize_replace_vars_script() {
  101. $replace_vars = new WPSEO_Replace_Vars();
  102. $recommended_replace_vars = new WPSEO_Admin_Recommended_Replace_Vars();
  103. $editor_specific_replace_vars = new WPSEO_Admin_Editor_Specific_Replace_Vars();
  104. $replace_vars_list = $replace_vars->get_replacement_variables_list();
  105. return [
  106. 'replace_vars' => $replace_vars_list,
  107. 'recommended_replace_vars' => $recommended_replace_vars->get_recommended_replacevars(),
  108. 'editor_specific_replace_vars' => $editor_specific_replace_vars->get(),
  109. 'shared_replace_vars' => $editor_specific_replace_vars->get_generic( $replace_vars_list ),
  110. ];
  111. }
  112. /**
  113. * Retrieves some variables that are needed for the search appearance in JS.
  114. *
  115. * @return array The search appearance variables.
  116. */
  117. public function localize_search_appearance_script() {
  118. $search_appearance_l10n = [
  119. 'isRtl' => is_rtl(),
  120. 'userEditUrl' => add_query_arg( 'user_id', '{user_id}', admin_url( 'user-edit.php' ) ),
  121. 'brushstrokeBackgroundURL' => plugins_url( 'images/brushstroke_background.svg', WPSEO_FILE ),
  122. 'showLocalSEOUpsell' => $this->should_show_local_seo_upsell(),
  123. 'localSEOUpsellURL' => WPSEO_Shortlinker::get( 'https://yoa.st/3mp' ),
  124. ];
  125. $search_appearance_l10n['knowledgeGraphCompanyInfoMissing'] = WPSEO_Language_Utils::get_knowledge_graph_company_info_missing_l10n();
  126. return $search_appearance_l10n;
  127. }
  128. /**
  129. * Determines whether the Local SEO upsell should be shown.
  130. *
  131. * The Local SEO upsell should:
  132. * - Only be shown in Free, not when Premium is active.
  133. * - Not be shown when Local SEO is active.
  134. *
  135. * @return bool Whether the Local SEO upsell should be shown.
  136. */
  137. private function should_show_local_seo_upsell() {
  138. $addon_manager = new WPSEO_Addon_Manager();
  139. return ! WPSEO_Utils::is_yoast_seo_premium()
  140. && ! ( defined( 'WPSEO_LOCAL_FILE' ) );
  141. }
  142. /**
  143. * Enqueues and handles all the tool dependencies.
  144. */
  145. private function enqueue_tools_scripts() {
  146. $tool = filter_input( INPUT_GET, 'tool' );
  147. if ( empty( $tool ) ) {
  148. $this->asset_manager->enqueue_script( 'yoast-seo' );
  149. }
  150. if ( 'bulk-editor' === $tool ) {
  151. $this->asset_manager->enqueue_script( 'bulk-editor' );
  152. }
  153. }
  154. } /* End of class */