]> _ Git - couzy.git/blob
82e66cfb4a0132cba28304b7d44a6c727a4293fa
[couzy.git] /
1 <?php
2 /**
3  * @package WPSEO\Admin|Google_Search_Console
4  */
5
6 /**
7  * Class WPSEO_GSC_Marker
8  */
9 class WPSEO_GSC_Marker {
10
11         /**
12          * @var WPSEO_GSC_Issues
13          */
14         private $crawl_issues;
15
16         /**
17          * @var string
18          */
19         private $url = '';
20
21         /**
22          * @var string
23          */
24         private $platform;
25
26         /**
27          * @var string
28          */
29         private $category;
30
31         /**
32          * @var string
33          */
34         private $result;
35
36         /**
37          * Setting up the needed API libs and return the result
38          *
39          * If param URL is given, the request is performed by a bulk action
40          *
41          * @param string $url
42          */
43         public function __construct( $url = '' ) {
44                 $this->url    = $url;
45                 $this->result = $this->get_result();
46         }
47
48         /**
49          * Getting the response for the AJAX request
50          * @return string
51          */
52         public function get_response() {
53                 return $this->result;
54         }
55
56         /**
57          * Setting the result, this method will check if current
58          *
59          * @return string
60          */
61         private function get_result() {
62                 if ( $this->can_be_marked_as_fixed() ) {
63                         $service = new WPSEO_GSC_Service( WPSEO_GSC_Settings::get_profile() );
64
65                         if ( $this->set_crawl_issues() && $this->send_mark_as_fixed( $service ) && $this->delete_crawl_issue() ) {
66                                 $this->update_issue_count( $service );
67
68                                 return 'true';
69                         }
70                 }
71
72                 return 'false';
73         }
74
75         /**
76          * Check if request is valid by verifying the posted nonce and return the URL if this one is set
77          *
78          * @return bool|string
79          */
80         private function can_be_marked_as_fixed() {
81                 if ( $this->url !== '' ) {
82                         return $this->url;
83                 }
84
85                 return false;
86         }
87
88         /**
89          * Storing the data belonging to the current issue, this data is needed in the 'mark as fixed' flow
90          *
91          * @return bool
92          */
93         private function set_crawl_issues() {
94                 $this->platform = filter_input( INPUT_POST, 'platform' );
95                 $this->category = filter_input( INPUT_POST, 'category' );
96                 if ( $this->platform && $this->category ) {
97                         $this->crawl_issues = new WPSEO_GSC_Issues( $this->platform, $this->category );
98
99                         return true;
100                 }
101
102                 return false;
103         }
104
105         /**
106          * Sending a request to the Google Search Console API to let them know we marked an issue as fixed.
107          *
108          * @param WPSEO_GSC_Service $service
109          *
110          * @return bool
111          */
112         private function send_mark_as_fixed( WPSEO_GSC_Service $service ) {
113                 return $service->mark_as_fixed( $this->url, $this->platform, $this->category );
114         }
115
116         /**
117          * Delete the crawl issue from the database
118          *
119          * @return bool
120          */
121         private function delete_crawl_issue() {
122                 return $this->crawl_issues->delete_issue( $this->url );
123         }
124
125         /**
126          * Getting the counts for current platform - category combination and update the score of it.
127          *
128          * @param WPSEO_GSC_Service $service
129          */
130         private function update_issue_count( WPSEO_GSC_Service $service ) {
131                 $counts  = new WPSEO_GSC_Count( $service );
132
133                 // Get the issues.
134                 $total_issues = $counts->get_issue_count( $this->platform, $this->category );
135
136                 // Lower the current count with 1.
137                 $total_issues = ( $total_issues - 1 );
138
139                 // And update the count.
140                 $counts->update_issue_count( $this->platform, $this->category, $total_issues );
141         }
142
143 }