class-config.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. * @var string $currentoption The option in use for the current admin page.
  15. */
  16. public $currentoption = 'wpseo';
  17. /**
  18. * Holds the asset manager.
  19. *
  20. * @var WPSEO_Admin_Asset_Manager
  21. */
  22. private $asset_manager;
  23. /**
  24. * Class constructor, which basically only hooks the init function on the init hook
  25. */
  26. public function __construct() {
  27. add_action( 'init', array( $this, 'init' ), 20 );
  28. $this->asset_manager = new WPSEO_Admin_Asset_Manager();
  29. }
  30. /**
  31. * Make sure the needed scripts are loaded for admin pages
  32. */
  33. public function init() {
  34. if ( filter_input( INPUT_GET, 'wpseo_reset_defaults' ) && wp_verify_nonce( filter_input( INPUT_GET, 'nonce' ), 'wpseo_reset_defaults' ) && current_user_can( 'manage_options' ) ) {
  35. WPSEO_Options::reset();
  36. wp_redirect( admin_url( 'admin.php?page=' . WPSEO_Configuration_Page::PAGE_IDENTIFIER ) );
  37. }
  38. add_action( 'admin_init', array( $this, 'admin_init' ) );
  39. add_action( 'admin_enqueue_scripts', array( $this, 'config_page_scripts' ) );
  40. add_action( 'admin_enqueue_scripts', array( $this, 'config_page_styles' ) );
  41. }
  42. /**
  43. * Run admin-specific actions.
  44. */
  45. public function admin_init() {
  46. $page = filter_input( INPUT_GET, 'page' );
  47. $tool = filter_input( INPUT_GET, 'tool' );
  48. $export_nonce = filter_input( INPUT_POST, WPSEO_Export::NONCE_NAME );
  49. if ( 'wpseo_tools' === $page && 'import-export' === $tool && $export_nonce !== null ) {
  50. $this->do_yoast_export();
  51. }
  52. }
  53. /**
  54. * Loads the required styles for the config page.
  55. */
  56. public function config_page_styles() {
  57. wp_enqueue_style( 'dashboard' );
  58. wp_enqueue_style( 'thickbox' );
  59. wp_enqueue_style( 'global' );
  60. wp_enqueue_style( 'wp-admin' );
  61. $this->asset_manager->enqueue_style( 'select2' );
  62. $this->asset_manager->enqueue_style( 'admin-css' );
  63. }
  64. /**
  65. * Loads the required scripts for the config page.
  66. */
  67. public function config_page_scripts() {
  68. $this->asset_manager->enqueue_script( 'admin-script' );
  69. $this->asset_manager->enqueue_script( 'help-center' );
  70. $page = filter_input( INPUT_GET, 'page' );
  71. if ( $page === 'wpseo_titles' ) {
  72. wp_localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'search-appearance', 'wpseoReplaceVarsL10n', $this->localize_replace_vars_script() );
  73. wp_localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'search-appearance', 'wpseoSearchAppearance', array( 'isRtl' => is_rtl() ) );
  74. $this->asset_manager->enqueue_script( 'search-appearance' );
  75. $this->asset_manager->enqueue_style( 'search-appearance' );
  76. /**
  77. * Remove the emoji script as it is incompatible with both React and any
  78. * contenteditable fields.
  79. */
  80. remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
  81. $yoast_components_l10n = new WPSEO_Admin_Asset_Yoast_Components_L10n();
  82. $yoast_components_l10n->localize_script( 'search-appearance' );
  83. }
  84. wp_enqueue_script( 'dashboard' );
  85. wp_enqueue_script( 'thickbox' );
  86. wp_localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'admin-script', 'wpseoSelect2Locale', WPSEO_Utils::get_language( WPSEO_Utils::get_user_locale() ) );
  87. if ( in_array( $page, array( 'wpseo_social', WPSEO_Admin::PAGE_IDENTIFIER, 'wpseo_titles' ), true ) ) {
  88. wp_enqueue_media();
  89. $this->asset_manager->enqueue_script( 'admin-media' );
  90. wp_localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'admin-media', 'wpseoMediaL10n', $this->localize_media_script() );
  91. }
  92. if ( 'wpseo_tools' === $page ) {
  93. $this->enqueue_tools_scripts();
  94. }
  95. }
  96. /**
  97. * Retrieves some variables that are needed for the upload module in JS.
  98. *
  99. * @return array The upload module variables.
  100. */
  101. public function localize_media_script() {
  102. return array(
  103. 'choose_image' => __( 'Use Image', 'wordpress-seo' ),
  104. );
  105. }
  106. /**
  107. * Retrieves some variables that are needed for replacing variables in JS.
  108. *
  109. * @return array The replacement and recommended replacement variables.
  110. */
  111. public function localize_replace_vars_script() {
  112. $replace_vars = new WPSEO_Replace_Vars();
  113. $recommended_replace_vars = new WPSEO_Admin_Recommended_Replace_Vars();
  114. $editor_specific_replace_vars = new WPSEO_Admin_Editor_Specific_Replace_Vars();
  115. $replace_vars_list = $replace_vars->get_replacement_variables_list();
  116. return array(
  117. 'replace_vars' => $replace_vars_list,
  118. 'recommended_replace_vars' => $recommended_replace_vars->get_recommended_replacevars(),
  119. 'editor_specific_replace_vars' => $editor_specific_replace_vars->get(),
  120. 'shared_replace_vars' => $editor_specific_replace_vars->get_generic( $replace_vars_list ),
  121. );
  122. }
  123. /**
  124. * Enqueues and handles all the tool dependencies.
  125. */
  126. private function enqueue_tools_scripts() {
  127. $tool = filter_input( INPUT_GET, 'tool' );
  128. if ( empty( $tool ) ) {
  129. $this->asset_manager->enqueue_script( 'yoast-seo' );
  130. }
  131. if ( 'bulk-editor' === $tool ) {
  132. $this->asset_manager->enqueue_script( 'bulk-editor' );
  133. }
  134. }
  135. /**
  136. * Runs the yoast exporter class to possibly init the file download.
  137. */
  138. private function do_yoast_export() {
  139. check_admin_referer( WPSEO_Export::NONCE_ACTION, WPSEO_Export::NONCE_NAME );
  140. if ( ! WPSEO_Capability_Utils::current_user_can( 'wpseo_manage_options' ) ) {
  141. return;
  142. }
  143. $wpseo_post = filter_input( INPUT_POST, 'wpseo' );
  144. $include_taxonomy = ! empty( $wpseo_post['include_taxonomy'] );
  145. $export = new WPSEO_Export( $include_taxonomy );
  146. if ( $export->has_error() ) {
  147. add_action( 'admin_notices', array( $export, 'set_error_hook' ) );
  148. }
  149. }
  150. } /* End of class */