WPSEO_OnPage

Handle the request for getting the onpage status.

Defined (1)

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

/admin/onpage/class-onpage.php  
  1. class WPSEO_OnPage { 
  2.  
  3. /** 
  4. * The name of the user meta key for storing the dismissed status. 
  5. */ 
  6. const USER_META_KEY = 'wpseo_dismiss_onpage'; 
  7.  
  8. /** 
  9. * @var WPSEO_OnPage_Option The OnPage.org option class. 
  10. */ 
  11. private $onpage_option; 
  12.  
  13. /** 
  14. * @var boolean Is the request started by pressing the fetch button. 
  15. */ 
  16. private $is_manual_request = false; 
  17.  
  18. /** 
  19. * Constructing the object 
  20. */ 
  21. public function __construct() { 
  22. // We never want to fetch on AJAX request because doing a remote request is really slow. 
  23. if ( ! ( defined( 'DOING_AJAX' ) && DOING_AJAX === true ) ) { 
  24. $this->onpage_option = new WPSEO_OnPage_Option(); 
  25.  
  26. if ( $this->onpage_option->is_enabled() ) { 
  27. $this->set_hooks(); 
  28. $this->catch_redo_listener(); 
  29.  
  30. /** 
  31. * The hooks to run on plugin activation 
  32. */ 
  33. public function activate_hooks() { 
  34. $this->set_cron(); 
  35.  
  36. /** 
  37. * Adding a weekly schedule to the schedules array 
  38. * @param array $schedules Array with schedules. 
  39. * @return array 
  40. */ 
  41. public function add_weekly_schedule( array $schedules ) { 
  42. $schedules['weekly'] = array( 'interval' => WEEK_IN_SECONDS, 'display' => __( 'Once Weekly', 'wordpress-seo' ) ); 
  43.  
  44. return $schedules; 
  45.  
  46. /** 
  47. * Fetching the data from onpage. 
  48. * @return bool 
  49. */ 
  50. public function fetch_from_onpage() { 
  51. if ( $this->onpage_option->should_be_fetched() && false !== ( $new_status = $this->request_indexability() ) ) { 
  52.  
  53. // Updates the timestamp in the option. 
  54. $this->onpage_option->set_last_fetch( time() ); 
  55.  
  56. // The currently indexability status. 
  57. $old_status = $this->onpage_option->get_status(); 
  58.  
  59. // Saving the new status. 
  60. $this->onpage_option->set_status( $new_status ); 
  61.  
  62. // Saving the option. 
  63. $this->onpage_option->save_option(); 
  64.  
  65. // Check if the status has been changed. 
  66. if ( $old_status !== $new_status && $new_status !== WPSEO_OnPage_Option::CANNOT_FETCH ) { 
  67. $this->notify_admins(); 
  68.  
  69. return true; 
  70.  
  71. return false; 
  72.  
  73. /** 
  74. * Show a notice when the website is not indexable 
  75. */ 
  76. public function show_notice() { 
  77.  
  78. $notification = $this->get_indexability_notification(); 
  79. $notification_center = Yoast_Notification_Center::get(); 
  80.  
  81. if ( $this->should_show_notice() ) { 
  82. $notification_center->add_notification( $notification ); 
  83. else { 
  84. $notification_center->remove_notification( $notification ); 
  85.  
  86. /** 
  87. * Builds the indexability notification 
  88. * @return Yoast_Notification 
  89. */ 
  90. private function get_indexability_notification() { 
  91. $notice = sprintf( 
  92. /** translators: 1: opens a link to a related knowledge base article. 2: closes the link */ 
  93. __( '%1$sYour homepage cannot be indexed by search engines%2$s. This is very bad for SEO and should be fixed.', 'wordpress-seo' ),  
  94. '<a href="' . WPSEO_Shortlinker::get( 'https://yoa.st/onpageindexerror' ) . '" target="_blank">',  
  95. '</a>' 
  96. ); 
  97.  
  98. return new Yoast_Notification( 
  99. $notice,  
  100. array( 
  101. 'type' => Yoast_Notification::ERROR,  
  102. 'id' => 'wpseo-dismiss-onpageorg',  
  103. 'capabilities' => 'manage_options',  
  104. ); 
  105.  
  106. /** 
  107. * Send a request to OnPage.org to get the indexability 
  108. * @return int(0)|int(1)|false 
  109. */ 
  110. protected function request_indexability() { 
  111. $parameters = array(); 
  112. if ( $this->wordfence_protection_enabled() ) { 
  113. $parameters['wf_strict'] = 1; 
  114.  
  115. $request = new WPSEO_OnPage_Request(); 
  116. $response = $request->do_request( get_option( 'home' ), $parameters ); 
  117.  
  118. if ( isset( $response['is_indexable'] ) ) { 
  119. return (int) $response['is_indexable']; 
  120.  
  121. return WPSEO_OnPage_Option::CANNOT_FETCH; 
  122.  
  123. /** 
  124. * Should the notice being given? 
  125. * @return bool 
  126. */ 
  127. protected function should_show_notice() { 
  128. // If development mode is on or the blog is not public, just don't show this notice. 
  129. if ( WPSEO_Utils::is_development_mode() || ( '0' === get_option( 'blog_public' ) ) ) { 
  130. return false; 
  131.  
  132. return $this->onpage_option->get_status() === WPSEO_OnPage_Option::IS_NOT_INDEXABLE; 
  133.  
  134. /** 
  135. * Notify the admins 
  136. */ 
  137. protected function notify_admins() { 
  138. /** 
  139. * Let's start showing the notices to all admins by removing the hide-notice meta data for each admin resulting 
  140. * in popping up the notice again. 
  141. */ 
  142. delete_metadata( 'user', 0, WPSEO_OnPage::USER_META_KEY, '', true ); 
  143.  
  144. /** 
  145. * Setting up the hooks. 
  146. */ 
  147. private function set_hooks() { 
  148. // Schedule cronjob when it doesn't exists on activation. 
  149. register_activation_hook( WPSEO_FILE, array( $this, 'activate_hooks' ) ); 
  150.  
  151. // Add weekly schedule to the cron job schedules. 
  152. add_filter( 'cron_schedules', array( $this, 'add_weekly_schedule' ) ); 
  153.  
  154. // Adding admin notice if necessary. 
  155. add_filter( 'admin_init', array( $this, 'show_notice' ) ); 
  156.  
  157. // Setting the action for the OnPage fetch. 
  158. add_action( 'wpseo_onpage_fetch', array( $this, 'fetch_from_onpage' ) ); 
  159.  
  160. /** 
  161. * Setting the cronjob to get the new indexibility status. 
  162. */ 
  163. private function set_cron() { 
  164. if ( ! wp_next_scheduled( 'wpseo_onpage_fetch' ) ) { 
  165. wp_schedule_event( time(), 'weekly', 'wpseo_onpage_fetch' ); 
  166.  
  167. /** 
  168. * Redo the fetch request for onpage 
  169. */ 
  170. private function catch_redo_listener() { 
  171. if ( filter_input( INPUT_GET, 'wpseo-redo-onpage' ) === '1' ) { 
  172. $this->is_manual_request = true; 
  173.  
  174. add_action( 'admin_init', array( $this, 'fetch_from_onpage' ) ); 
  175.  
  176. /** 
  177. * Checks if WordFence protects the site against 'fake' Google crawlers. 
  178. * @return boolean 
  179. */ 
  180. private function wordfence_protection_enabled() { 
  181. if ( ! class_exists( 'wfConfig' ) ) { 
  182. return false; 
  183.  
  184. if ( ! method_exists( 'wfConfig', 'get' ) ) { 
  185. return false; 
  186.  
  187. return (bool) wfConfig::get( 'blockFakeBots' );