]> _ Git - couzy.git/blob
4831364867eb81511441fca042f072c3675dff39
[couzy.git] /
1 <?php
2 /**
3  * @package WPSEO\Admin|Google_Search_Console
4  */
5
6 /**
7  * Class WPSEO_GSC_Ajax
8  */
9 class WPSEO_GSC_Ajax {
10
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_gsc_create_redirect_url', array( $this, 'ajax_create_redirect' ) );
17                 add_action( 'wp_ajax_wpseo_dismiss_gsc', array( $this, 'dismiss_notice' ) );
18         }
19
20         /**
21          * This method will be access by an AJAX request and will mark an issue as fixed.
22          *
23          * First it will do a request to the Google API
24          */
25         public function ajax_mark_as_fixed( ) {
26                 if ( $this->valid_nonce() ) {
27                         $marker = new WPSEO_GSC_Marker( filter_input( INPUT_POST, 'url' ) );
28
29                         wp_die( $marker->get_response() );
30                 }
31
32                 wp_die( 'false' );
33         }
34
35         /**
36          * Handling the request to create a new redirect from the issued URL
37          */
38         public function ajax_create_redirect() {
39                 if ( $this->valid_nonce() && class_exists( 'WPSEO_URL_Redirect_Manager' ) && defined( 'WPSEO_PREMIUM_PATH' ) ) {
40                         $redirect_manager = new WPSEO_URL_Redirect_Manager();
41
42                         $old_url = filter_input( INPUT_POST, 'old_url' );
43
44                         // Creates the redirect.
45                         if ( $redirect_manager->create_redirect( $old_url, filter_input( INPUT_POST, 'new_url' ), filter_input( INPUT_POST, 'type' ) ) ) {
46                                 if ( filter_input( INPUT_POST, 'mark_as_fixed' ) === 'true' ) {
47                                         new WPSEO_GSC_Marker( $old_url );
48                                 }
49
50                                 wp_die( 'true' );
51                         }
52                 }
53
54                 wp_die( 'false' );
55         }
56
57         /**
58          * Handle the AJAX request and dismiss the GSC notice
59          */
60         public function dismiss_notice() {
61                 check_ajax_referer( 'dismiss-gsc-notice' );
62
63                 update_user_meta( get_current_user_id(), 'wpseo_dismissed_gsc_notice', true );
64
65                 wp_die( 'true' );
66         }
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 }