class-configuration-page.php 7.7 KB

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