/wp-content/plugins/wordpress-seo/admin/google_search_console/class-gsc-ajax.php

https://bitbucket.org/carloskikea/helpet · PHP · 110 lines · 52 code · 19 blank · 39 comment · 4 complexity · 699a8a51b04f8b64b199b67ea9f873b4 MD5 · raw file

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