WPSEO_Configuration_Page

The Yoast SEO WPSEO Configuration Page class.

Defined (1)

The class is defined in the following location(s).

/admin/config-ui/class-configuration-page.php  
  1. class WPSEO_Configuration_Page { 
  2.  
  3. const PAGE_IDENTIFIER = 'wpseo_configurator'; 
  4.  
  5. /** 
  6. * WPSEO_Configuration_Wizard constructor. 
  7. */ 
  8. public function __construct() { 
  9.  
  10. if ( $this->should_add_notification() ) { 
  11. $this->add_notification(); 
  12.  
  13. if ( filter_input( INPUT_GET, 'page' ) !== self::PAGE_IDENTIFIER ) { 
  14. return; 
  15.  
  16. // Register the page for the wizard. 
  17. add_action( 'admin_menu', array( $this, 'add_wizard_page' ) ); 
  18. add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) ); 
  19. add_action( 'admin_init', array( $this, 'render_wizard_page' ) ); 
  20.  
  21. /** 
  22. * Check if the configuration is finished. If so, just remove the notification. 
  23. */ 
  24. public function catch_configuration_request() { 
  25. $configuration_page = filter_input( INPUT_GET, 'configuration' ); 
  26. $page = filter_input( INPUT_GET, 'page' ); 
  27.  
  28. if ( ! ( $configuration_page === 'finished' && ( $page === WPSEO_Admin::PAGE_IDENTIFIER ) ) ) { 
  29. return; 
  30.  
  31. $this->remove_notification(); 
  32. $this->remove_notification_option(); 
  33.  
  34. wp_redirect( admin_url( 'admin.php?page=' . WPSEO_Admin::PAGE_IDENTIFIER ) ); 
  35. exit; 
  36.  
  37.  
  38. /** 
  39. * Registers the page for the wizard. 
  40. */ 
  41. public function add_wizard_page() { 
  42. add_dashboard_page( '', '', 'manage_options', self::PAGE_IDENTIFIER, '' ); 
  43.  
  44. /** 
  45. * Renders the wizard page and exits to prevent the wordpress UI from loading. 
  46. */ 
  47. public function render_wizard_page() { 
  48. $this->show_wizard(); 
  49. exit; 
  50.  
  51. /** 
  52. * Enqueues the assets needed for the wizard. 
  53. */ 
  54. public function enqueue_assets() { 
  55. wp_enqueue_media(); 
  56.  
  57. /** 
  58. * Print the `forms.css` WP stylesheet before any Yoast style, this way 
  59. * it's easier to override selectors with the same specificity later. 
  60. */ 
  61. wp_enqueue_style( 'forms' ); 
  62. $asset_manager = new WPSEO_Admin_Asset_Manager(); 
  63. $asset_manager->register_assets(); 
  64. $asset_manager->enqueue_script( 'configuration-wizard' ); 
  65. $asset_manager->enqueue_style( 'yoast-components' ); 
  66.  
  67. $config = $this->get_config(); 
  68.  
  69. wp_localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'configuration-wizard', 'yoastWizardConfig', $config ); 
  70.  
  71. /** 
  72. * Setup Wizard Header. 
  73. */ 
  74. public function show_wizard() { 
  75. $this->enqueue_assets(); 
  76. $dashboard_url = admin_url( '/admin.php?page=wpseo_dashboard' ); 
  77. ?> 
  78. <!DOCTYPE html> 
  79. <!--[if IE 9]> 
  80. <html class="ie9" <?php language_attributes(); ?> > 
  81. <![endif]--> 
  82. <!--[if !(IE 9) ]><!--> 
  83. <html <?php language_attributes(); ?>> 
  84. <!--<![endif]--> 
  85. <head> 
  86. <meta name="viewport" content="width=device-width"/> 
  87. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 
  88. <title><?php 
  89. printf( 
  90. /** translators: %s expands to Yoast SEO. */ 
  91. __( '%s › Configuration Wizard', 'wordpress-seo' ),  
  92. 'Yoast SEO' 
  93. ); 
  94. ?></title> 
  95. <?php 
  96. wp_print_head_scripts(); 
  97. wp_print_styles( 'yoast-seo-yoast-components' ); 
  98.  
  99. /** 
  100. * Is called before the closing </head> tag in the Yoast Configuration wizard. 
  101. * Allows users to add their own scripts or styles. 
  102. * @since 4.0 
  103. */ 
  104. do_action( 'wpseo_configuration_wizard_head' ); 
  105. ?> 
  106. </head> 
  107. <body class="wp-admin"> 
  108. <div id="wizard"></div> 
  109. <a class="yoast-wizard-return-link" href="<?php echo $dashboard_url ?>"> 
  110. <?php 
  111. printf( 
  112. /** translators: %s expands to Yoast SEO. */ 
  113. __( 'Go back to the %s dashboard.', 'wordpress-seo' ),  
  114. 'Yoast SEO' 
  115. ); 
  116. ?> 
  117. </a> 
  118. <?php 
  119. wp_print_media_templates(); 
  120. wp_print_footer_scripts(); 
  121.  
  122. /** 
  123. * Is called before the closing </body> tag in the Yoast Configuration wizard. 
  124. * Allows users to add their own scripts or content. 
  125. * @since 4.0 
  126. */ 
  127. do_action( 'wpseo_configuration_wizard_footer' ); 
  128.  
  129. wp_print_scripts( 'yoast-seo-configuration-wizard' ); 
  130. ?> 
  131. </body> 
  132. </html> 
  133. <?php 
  134.  
  135.  
  136. /** 
  137. * Get the API config for the wizard. 
  138. * @return array The API endpoint config. 
  139. */ 
  140. public function get_config() { 
  141. $translations = $this->get_translations(); 
  142. $service = new WPSEO_GSC_Service(); 
  143. $config = array( 
  144. 'namespace' => WPSEO_Configuration_Endpoint::REST_NAMESPACE,  
  145. 'endpoint_retrieve' => WPSEO_Configuration_Endpoint::ENDPOINT_RETRIEVE,  
  146. 'endpoint_store' => WPSEO_Configuration_Endpoint::ENDPOINT_STORE,  
  147. 'nonce' => wp_create_nonce( 'wp_rest' ),  
  148. 'root' => esc_url_raw( rest_url() ),  
  149. 'ajaxurl' => admin_url( 'admin-ajax.php' ),  
  150. 'finishUrl' => admin_url( 'admin.php?page=wpseo_dashboard&configuration=finished' ),  
  151. 'gscAuthURL' => $service->get_client()->createAuthUrl(),  
  152. 'gscProfiles' => $service->get_sites(),  
  153. 'gscNonce' => wp_create_nonce( 'wpseo-gsc-ajax-security' ),  
  154. 'translations' => $translations,  
  155. ); 
  156.  
  157. return $config; 
  158.  
  159. /** 
  160. * Returns the translations necessary for the configuration wizard. 
  161. * @returns array The translations for the configuration wizard. 
  162. */ 
  163. public function get_translations() { 
  164. $file = plugin_dir_path( WPSEO_FILE ) . 'languages/yoast-components-' . WPSEO_Utils::get_user_locale() . '.json'; 
  165. if ( file_exists( $file ) && $file = file_get_contents( $file ) ) { 
  166. return json_decode( $file, true ); 
  167.  
  168. return array(); 
  169.  
  170. /** 
  171. * Adds a notification to the notification center. 
  172. */ 
  173. private function add_notification() { 
  174. $notification_center = Yoast_Notification_Center::get(); 
  175. $notification_center->add_notification( self::get_notification() ); 
  176.  
  177. /** 
  178. * Removes the notification from the notification center. 
  179. */ 
  180. private function remove_notification() { 
  181. $notification_center = Yoast_Notification_Center::get(); 
  182. $notification_center->remove_notification( self::get_notification() ); 
  183.  
  184. /** 
  185. * Gets the notification. 
  186. * @return Yoast_Notification 
  187. */ 
  188. private static function get_notification() { 
  189. $message = __( 'The configuration wizard helps you to easily configure your site to have the optimal SEO settings.', 'wordpress-seo' ); 
  190. $message .= '<br/>'; 
  191. $message .= sprintf( 
  192. /** translators: %1$s resolves to Yoast SEO, %2$s resolves to the starting tag of the link to the wizard, %3$s resolves to the closing link tag */ 
  193. __( 'We have detected that you have not finished this wizard yet, so we recommend you to %2$sstart the configuration wizard to configure %1$s%3$s.', 'wordpress-seo' ),  
  194. 'Yoast SEO',  
  195. '<a href="' . admin_url( '?page=' . self::PAGE_IDENTIFIER ) . '">',  
  196. '</a>' 
  197. ); 
  198.  
  199. $notification = new Yoast_Notification( 
  200. $message,  
  201. array( 
  202. 'type' => Yoast_Notification::WARNING,  
  203. 'id' => 'wpseo-dismiss-onboarding-notice',  
  204. 'capabilities' => 'manage_options',  
  205. 'priority' => 0.8,  
  206. ); 
  207.  
  208. return $notification; 
  209.  
  210. /** 
  211. * When the notice should be shown. 
  212. * @return bool 
  213. */ 
  214. private function should_add_notification() { 
  215. $options = $this->get_options(); 
  216.  
  217. return $options['show_onboarding_notice'] === true; 
  218.  
  219. /** 
  220. * Remove the options that triggers the notice for the configuration wizard. 
  221. */ 
  222. private function remove_notification_option() { 
  223. $options = $this->get_options(); 
  224.  
  225. $options['show_onboarding_notice'] = false; 
  226.  
  227. update_option( 'wpseo', $options ); 
  228.  
  229. /** 
  230. * Returns the set options 
  231. * @return mixed|void 
  232. */ 
  233. private function get_options() { 
  234. return get_option( 'wpseo' );