WPSEO_GSC_Ajax

Class WPSEO_GSC_Ajax.

Defined (1)

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

/admin/google_search_console/class-gsc-ajax.php  
  1. class WPSEO_GSC_Ajax { 
  2.  
  3. /** 
  4. * Setting the AJAX hooks for GSC 
  5. */ 
  6. public function __construct() { 
  7. add_action( 'wp_ajax_wpseo_mark_fixed_crawl_issue', array( $this, 'ajax_mark_as_fixed' ) ); 
  8. add_action( 'wp_ajax_wpseo_dismiss_gsc', array( $this, 'dismiss_notice' ) ); 
  9. add_action( 'wp_ajax_wpseo_save_auth_code', array( $this, 'save_auth_code' ) ); 
  10. add_action( 'wp_ajax_wpseo_clear_auth_code', array( $this, 'clear_auth_code' ) ); 
  11. add_action( 'wp_ajax_wpseo_get_profiles', array( $this, 'get_profiles' ) ); 
  12.  
  13. /** 
  14. * This method will be access by an AJAX request and will mark an issue as fixed. 
  15. * First it will do a request to the Google API 
  16. */ 
  17. public function ajax_mark_as_fixed() { 
  18. if ( $this->valid_nonce() ) { 
  19. $marker = new WPSEO_GSC_Marker( filter_input( INPUT_POST, 'url' ) ); 
  20.  
  21. wp_die( $marker->get_response() ); 
  22.  
  23. wp_die( 'false' ); 
  24.  
  25. /** 
  26. * Handle the AJAX request and dismiss the GSC notice 
  27. */ 
  28. public function dismiss_notice() { 
  29. check_ajax_referer( 'dismiss-gsc-notice' ); 
  30.  
  31. update_user_meta( get_current_user_id(), 'wpseo_dismissed_gsc_notice', true ); 
  32.  
  33. wp_die( 'true' ); 
  34.  
  35. /** 
  36. * Saves the authorization code. 
  37. */ 
  38. public function save_auth_code() { 
  39. if ( ! $this->valid_nonce() ) { 
  40. wp_die( '0' ); 
  41.  
  42. // Validate the authorization. 
  43. $service = $this->get_service(); 
  44. $authorization_code = filter_input( INPUT_POST, 'authorization' ); 
  45. $is_authorization_valid = WPSEO_GSC_Settings::validate_authorization( $authorization_code, $service->get_client() ); 
  46. if ( ! $is_authorization_valid ) { 
  47. wp_die( '0' ); 
  48.  
  49. $this->get_profiles(); 
  50.  
  51. /** 
  52. * Clears all authorization data. 
  53. */ 
  54. public function clear_auth_code() { 
  55. if ( ! $this->valid_nonce() ) { 
  56. wp_die( '0' ); 
  57.  
  58. $service = $this->get_service(); 
  59.  
  60. WPSEO_GSC_Settings::clear_data( $service ); 
  61.  
  62. $this->get_profiles(); 
  63.  
  64. /** 
  65. * Check if posted nonce is valid and return true if it is 
  66. * @return mixed 
  67. */ 
  68. private function valid_nonce() { 
  69. return wp_verify_nonce( filter_input( INPUT_POST, 'ajax_nonce' ), 'wpseo-gsc-ajax-security' ); 
  70.  
  71. /** 
  72. * Returns an instance of the Google Search Console service. 
  73. * @return WPSEO_GSC_Service 
  74. */ 
  75. private function get_service() { 
  76. return new WPSEO_GSC_Service(); 
  77.  
  78. /** 
  79. * Prints a JSON encoded string with the current profile config. 
  80. */ 
  81. private function get_profiles() { 
  82. $component = new WPSEO_Config_Component_Connect_Google_Search_Console(); 
  83.  
  84. wp_die( wp_json_encode( $component->get_data() ) );