PageRenderTime 31ms CodeModel.GetById 21ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

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