]> _ Git - couzy.git/blob
0516bd896864d7d0e5317967c7cf24cc5b016a4b
[couzy.git] /
1 <?php
2 /**
3  * @package WPSEO\Admin|Google_Search_Console
4  */
5
6 /**
7  * Class WPSEO_GSC_Issues
8  */
9 class WPSEO_GSC_Issues {
10
11         /**
12          * @var string
13          */
14         private $option_name = '';
15
16         /**
17          * List of all current issues to compare with received issues
18          *
19          * @var array
20          */
21         private $current_issues = array();
22
23         /**
24          * Holder for all the issues
25          *
26          * @var array
27          */
28         private $issues = array();
29
30         /**
31          * Setting up the properties and fetching the current issues
32          *
33          * @param string     $platform
34          * @param string     $category
35          * @param array|bool $fetched_issues
36          */
37         public function __construct( $platform, $category, $fetched_issues = false ) {
38                 $this->option_name = strtolower( 'wpseo-gsc-issues-' . $platform . '-' . $category );
39                 $this->issues      = $this->get_issues();
40
41                 if ( ! empty( $fetched_issues ) && is_array( $fetched_issues ) ) {
42                         $this->save_fetched_issues( $fetched_issues );
43                 }
44         }
45         /**
46          * Getting the issues from the options.
47          *
48          * @return array
49          */
50         public function get_issues() {
51                 return get_option( $this->option_name, array() );
52         }
53
54         /**
55          * Deleting the issue from the issues
56          *
57          * @param string $url
58          *
59          * @return bool
60          */
61         public function delete_issue( $url ) {
62                 $target_issue = $this->get_issue_by_url( $url );
63                 if ( $target_issue !== false ) {
64                         unset( $this->issues[ $target_issue ] );
65
66                         $this->save_issues( $this->issues );
67
68                         return true;
69                 }
70
71                 return false;
72         }
73
74         /**
75          * Fetching the issues for current category and compare them with the already existing issues.
76          *
77          * @param array $fetched_issues
78          */
79         private function save_fetched_issues( array $fetched_issues ) {
80                 $this->set_current_issues();
81
82                 $crawl_issues = $this->get_issues();
83
84                 // Walk through the issues to do the comparison.
85                 foreach ( $fetched_issues as $issue ) {
86                         $this->issue_compare( $crawl_issues, $issue );
87                 }
88
89                 $this->save_issues( $crawl_issues );
90
91                 // Refresh the value of $this->issues.
92                 $this->issues = $this->get_issues();
93         }
94
95         /**
96          * Comparing the issue with the list of current existing issues
97          *
98          * @param array    $crawl_issues
99          * @param stdClass $issue
100          */
101         private function issue_compare( &$crawl_issues, $issue ) {
102                 $issue->pageUrl = WPSEO_Utils::format_url( (string) $issue->pageUrl );
103
104                 if ( ! in_array( $issue->pageUrl, $this->current_issues ) ) {
105                         array_push(
106                                 $crawl_issues,
107                                 $this->get_issue( $this->create_issue( $issue ) )
108                         );
109                 }
110         }
111
112         /**
113          * The fetched issue from the API will be parsed as an WPSEO_Crawl_Issue object. After initializing the issue as an
114          * object, the object will be returned
115          *
116          * @param stdClass $issue
117          *
118          * @return WPSEO_GSC_Issue
119          */
120         private function create_issue( $issue ) {
121                 return new WPSEO_GSC_Issue(
122                         $issue->pageUrl,
123                         new DateTime( (string) $issue->first_detected ),
124                         new DateTime( (string) $issue->last_crawled ),
125                         (string) ( ! empty( $issue->responseCode ) ) ? $issue->responseCode : null
126                 );
127         }
128
129         /**
130          * Returns the crawl issue as an array.
131          *
132          * @param WPSEO_GSC_Issue $crawl_issue
133          *
134          * @return array()
135          */
136         private function get_issue( WPSEO_GSC_Issue $crawl_issue ) {
137                 return $crawl_issue->to_array();
138         }
139
140         /**
141          * Saving the issues to the options. The target option is base on current platform and category.
142          *
143          * @param array $issues
144          */
145         private function save_issues( array $issues ) {
146                 update_option( $this->option_name, $issues, false );
147         }
148
149         /**
150          * Getting the issues from the options and get only the URL out of it. This is because there will be a comparison
151          * with the issues from the API.
152          */
153         private function set_current_issues() {
154                 if ( ! empty( $this->issues ) ) {
155                         $this->current_issues = wp_list_pluck( $this->issues, 'url' );
156                 }
157         }
158
159         /**
160          * Search in the issues for the given $url
161          *
162          * @param string $url
163          *
164          * @return int|string
165          */
166         private function get_issue_by_url( $url ) {
167                 foreach ( $this->issues as $key => $issue ) {
168                         if ( $url === $issue['url'] ) {
169                                 return $key;
170                         }
171                 }
172
173                 return false;
174         }
175
176 }