WPSEO_GSC

Class WPSEO_GSC.

Defined (1)

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

/admin/google_search_console/class-gsc.php  
  1. class WPSEO_GSC { 
  2.  
  3. /** 
  4. * The option where data will be stored 
  5. */ 
  6. const OPTION_WPSEO_GSC = 'wpseo-gsc'; 
  7.  
  8. /** 
  9. * @var WPSEO_GSC_Service 
  10. */ 
  11. private $service; 
  12.  
  13. /** 
  14. * @var WPSEO_GSC_Category_Filters 
  15. */ 
  16. protected $category_filter; 
  17.  
  18. /** 
  19. * @var WPSEO_GSC_Issues 
  20. */ 
  21. protected $issue_fetch; 
  22.  
  23. /** 
  24. * @var string current platform 
  25. */ 
  26. private $platform; 
  27.  
  28. /** 
  29. * @var string current category 
  30. */ 
  31. private $category; 
  32.  
  33. /** 
  34. * Constructor for the page class. This will initialize all GSC related stuff 
  35. */ 
  36. public function __construct() { 
  37. add_action( 'init', array( $this, 'init' ) ); 
  38.  
  39. /** 
  40. * Run init logic. 
  41. */ 
  42. public function init() { 
  43.  
  44. // Setting the screen option. 
  45. if ( filter_input( INPUT_GET, 'page' ) === 'wpseo_search_console' ) { 
  46.  
  47. if ( filter_input( INPUT_GET, 'tab' ) !== 'settings' && WPSEO_GSC_Settings::get_profile() === '' ) { 
  48. wp_redirect( add_query_arg( 'tab', 'settings' ) ); 
  49. exit; 
  50.  
  51. $this->set_hooks(); 
  52. $this->set_dependencies(); 
  53. $this->request_handler(); 
  54.  
  55. add_action( 'admin_init', array( $this, 'register_gsc_notification' ) ); 
  56. add_action( 'admin_init', array( $this, 'register_settings' ) ); 
  57.  
  58. /** 
  59. * If the Google Search Console has no credentials, add a notification for the user to give him a heads up. This message is dismissable. 
  60. */ 
  61. public function register_gsc_notification() { 
  62.  
  63. $notification = $this->get_profile_notification(); 
  64. $notification_center = Yoast_Notification_Center::get(); 
  65.  
  66. if ( WPSEO_GSC_Settings::get_profile() === '' ) { 
  67. $notification_center->add_notification( $notification ); 
  68. else { 
  69. $notification_center->remove_notification( $notification ); 
  70.  
  71. /** 
  72. * Builds the notification used when GSC is not connected to a profile 
  73. * @return Yoast_Notification 
  74. */ 
  75. private function get_profile_notification() { 
  76. return new Yoast_Notification( 
  77. sprintf( 
  78. /** translators: 1: link open tag; 2: link close tag. */ 
  79. __( 'Don\'t miss your crawl errors: %1$sconnect with Google Search Console here%2$s.', 'wordpress-seo' ),  
  80. '<a href="' . admin_url( 'admin.php?page=wpseo_search_console&tab=settings' ) . '">',  
  81. '</a>' 
  82. ),  
  83. array( 
  84. 'type' => Yoast_Notification::WARNING,  
  85. 'id' => 'wpseo-dismiss-gsc',  
  86. 'capabilities' => 'manage_options',  
  87. ); 
  88.  
  89. /** 
  90. * Be sure the settings will be registered, so data can be stored 
  91. */ 
  92. public function register_settings() { 
  93. register_setting( 'yoast_wpseo_gsc_options', self::OPTION_WPSEO_GSC ); 
  94.  
  95. /** 
  96. * Function that outputs the redirect page 
  97. */ 
  98. public function display() { 
  99. require_once WPSEO_PATH . '/admin/google_search_console/views/gsc-display.php'; 
  100.  
  101. /** 
  102. * Display the table 
  103. */ 
  104. public function display_table() { 
  105. // The list table. 
  106. $list_table = new WPSEO_GSC_Table( $this->platform, $this->category, $this->issue_fetch->get_issues() ); 
  107.  
  108. // Adding filter to display the category filters. 
  109. add_filter( 'views_' . $list_table->get_screen_id(), array( $this->category_filter, 'as_array' ) ); 
  110.  
  111. // Preparing and displaying the table. 
  112. $list_table->prepare_items(); 
  113. $list_table->search_box( __( 'Search', 'wordpress-seo' ), 'wpseo-crawl-issues-search' ); 
  114. $list_table->display(); 
  115.  
  116. /** 
  117. * Load the admin redirects scripts 
  118. */ 
  119. public function page_scripts() { 
  120.  
  121. $asset_manager = new WPSEO_Admin_Asset_Manager(); 
  122. $asset_manager->enqueue_script( 'admin-gsc' ); 
  123. $asset_manager->enqueue_style( 'metabox-css' ); 
  124. add_screen_option( 'per_page', array( 
  125. 'label' => __( 'Crawl errors per page', 'wordpress-seo' ),  
  126. 'default' => 50,  
  127. 'option' => 'errors_per_page',  
  128. ) ); 
  129.  
  130. /** 
  131. * Set the screen options 
  132. * @param string $status Status string. 
  133. * @param string $option Option key. 
  134. * @param string $value Value to return. 
  135. * @return mixed 
  136. */ 
  137. public function set_screen_option( $status, $option, $value ) { 
  138. if ( 'errors_per_page' == $option ) { 
  139. return $value; 
  140.  
  141. /** 
  142. * Setting the hooks to be load on page request 
  143. */ 
  144. private function set_hooks() { 
  145. add_action( 'admin_enqueue_scripts', array( $this, 'page_scripts' ) ); 
  146. add_filter( 'set-screen-option', array( $this, 'set_screen_option' ), 11, 3 ); 
  147.  
  148. /** 
  149. * Handles the POST and GET requests 
  150. */ 
  151. private function request_handler() { 
  152.  
  153. // List the table search post to a get. 
  154. $this->list_table_search_post_to_get(); 
  155.  
  156. // Catch the authorization code POST. 
  157. $this->catch_authentication_post(); 
  158.  
  159. // Is there a reset post than we will remove the posts and data. 
  160. if ( filter_input( INPUT_GET, 'gsc_reset' ) ) { 
  161. // Clear the google data. 
  162. WPSEO_GSC_Settings::clear_data( $this->service ); 
  163.  
  164. // Adding notification to the notification center. 
  165. /** Translators: %1$s: expands to Google Search Console. */ 
  166. $this->add_notification( sprintf( __( 'The %1$s data has been removed. You will have to reauthenticate if you want to retrieve the data again.', 'wordpress-seo' ), 'Google Search Console' ), Yoast_Notification::UPDATED ); 
  167.  
  168. // Directly output the notifications. 
  169. wp_redirect( remove_query_arg( 'gsc_reset' ) ); 
  170. exit; 
  171.  
  172. // Reloads al the issues. 
  173. if ( wp_verify_nonce( filter_input( INPUT_POST, 'reload-crawl-issues-nonce' ), 'reload-crawl-issues' ) && filter_input( INPUT_POST, 'reload-crawl-issues' ) ) { 
  174. // Reloading all the issues. 
  175. WPSEO_GSC_Settings::reload_issues(); 
  176.  
  177. // Adding the notification. 
  178. $this->add_notification( __( 'The issues have been successfully reloaded!', 'wordpress-seo' ), Yoast_Notification::UPDATED ); 
  179.  
  180. // Directly output the notifications. 
  181. Yoast_Notification_Center::get()->display_notifications(); 
  182.  
  183. // Catch bulk action request. 
  184. new WPSEO_GSC_Bulk_Action(); 
  185.  
  186. /** 
  187. * Catch the redirects search post and redirect it to a search get 
  188. */ 
  189. private function list_table_search_post_to_get() { 
  190. $search_string = filter_input( INPUT_POST, 's' ); 
  191.  
  192. if ( $search_string === null ) { 
  193. return; 
  194.  
  195. // When there is nothing being search and there is no search param in the url, break this method. 
  196. if ( $search_string === '' && filter_input( INPUT_GET, 's' ) === null ) { 
  197. return; 
  198.  
  199. $url = ( $search_string !== '' ) ? add_query_arg( 's', $search_string ) : remove_query_arg( 's' ); 
  200.  
  201. // Do the redirect. 
  202. wp_redirect( $url ); 
  203. exit; 
  204.  
  205.  
  206. /** 
  207. * Catch the authentication post 
  208. */ 
  209. private function catch_authentication_post() { 
  210. $gsc_values = filter_input( INPUT_POST, 'gsc', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY ); 
  211. // Catch the authorization code POST. 
  212. if ( ! empty( $gsc_values['authorization_code'] ) && wp_verify_nonce( $gsc_values['gsc_nonce'], 'wpseo-gsc_nonce' ) ) { 
  213. if ( ! WPSEO_GSC_Settings::validate_authorization( trim( $gsc_values['authorization_code'] ), $this->service->get_client() ) ) { 
  214. $this->add_notification( __( 'Incorrect Google Authorization Code.', 'wordpress-seo' ), Yoast_Notification::ERROR ); 
  215.  
  216. // Redirect user to prevent a post resubmission which causes an oauth error. 
  217. wp_redirect( admin_url( 'admin.php' ) . '?page=' . esc_attr( filter_input( INPUT_GET, 'page' ) ) . '&tab=settings' ); 
  218. exit; 
  219.  
  220. /** 
  221. * Adding notification to the yoast notification center 
  222. * @param string $message Message string. 
  223. * @param string $type Message type. 
  224. */ 
  225. private function add_notification( $message, $type ) { 
  226. Yoast_Notification_Center::get()->add_notification( 
  227. new Yoast_Notification( $message, array( 'type' => $type ) ) 
  228. ); 
  229.  
  230. /** 
  231. * Setting dependencies which will be used one this page 
  232. */ 
  233. private function set_dependencies() { 
  234. // Setting the service object. 
  235. $this->service = new WPSEO_GSC_Service( WPSEO_GSC_Settings::get_profile() ); 
  236.  
  237. // Setting the platform. 
  238. $this->platform = WPSEO_GSC_Mapper::get_current_platform( 'tab' ); 
  239.  
  240. // Loading the issue counter. 
  241. $issue_count = new WPSEO_GSC_Count( $this->service ); 
  242. $issue_count->fetch_counts(); 
  243.  
  244. // Loading the category filters. 
  245. $this->category_filter = new WPSEO_GSC_Category_Filters( $issue_count->get_platform_counts( $this->platform ) ); 
  246.  
  247. // Setting the current category. 
  248. $this->category = $this->category_filter->get_category(); 
  249.  
  250. // Listing the issues. 
  251. $issue_count->list_issues( $this->platform, $this->category ); 
  252.  
  253. // Fetching the issues. 
  254. $this->issue_fetch = new WPSEO_GSC_Issues( $this->platform, $this->category, $issue_count->get_issues() ); 
  255.  
  256. /** 
  257. * Setting the tab help on top of the screen 
  258. */ 
  259. public function set_help() { 
  260. $screen = get_current_screen(); 
  261.  
  262. $screen->add_help_tab( 
  263. array( 
  264. 'id' => 'basic-help',  
  265. 'title' => __( 'Issue categories', 'wordpress-seo' ),  
  266. 'content' => '<p><strong>' . __( 'Desktop', 'wordpress-seo' ) . '</strong><br />' . __( 'Errors that occurred when your site was crawled by Googlebot.', 'wordpress-seo' ) . '</p>' 
  267. . '<p><strong>' . __( 'Smartphone', 'wordpress-seo' ) . '</strong><br />' . __( 'Errors that occurred only when your site was crawled by Googlebot-Mobile (errors didn\'t appear for desktop).', 'wordpress-seo' ) . '</p>' 
  268. . '<p><strong>' . __( 'Feature phone', 'wordpress-seo' ) . '</strong><br />' . __( 'Errors that only occurred when your site was crawled by Googlebot for feature phones (errors didn\'t appear for desktop).', 'wordpress-seo' ) . '</p>',  
  269. );