3 * @package WPSEO\Admin|Google_Search_Console
7 * Class WPSEO_GSC_Issues
9 class WPSEO_GSC_Issues {
14 private $option_name = '';
17 * List of all current issues to compare with received issues
21 private $current_issues = array();
24 * Holder for all the issues
28 private $issues = array();
31 * Setting up the properties and fetching the current issues
33 * @param string $platform
34 * @param string $category
35 * @param array|bool $fetched_issues
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();
41 if ( ! empty( $fetched_issues ) && is_array( $fetched_issues ) ) {
42 $this->save_fetched_issues( $fetched_issues );
46 * Getting the issues from the options.
50 public function get_issues() {
51 return get_option( $this->option_name, array() );
55 * Deleting the issue from the issues
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 ] );
66 $this->save_issues( $this->issues );
75 * Fetching the issues for current category and compare them with the already existing issues.
77 * @param array $fetched_issues
79 private function save_fetched_issues( array $fetched_issues ) {
80 $this->set_current_issues();
82 $crawl_issues = $this->get_issues();
84 // Walk through the issues to do the comparison.
85 foreach ( $fetched_issues as $issue ) {
86 $this->issue_compare( $crawl_issues, $issue );
89 $this->save_issues( $crawl_issues );
91 // Refresh the value of $this->issues.
92 $this->issues = $this->get_issues();
96 * Comparing the issue with the list of current existing issues
98 * @param array $crawl_issues
99 * @param stdClass $issue
101 private function issue_compare( &$crawl_issues, $issue ) {
102 $issue->pageUrl = WPSEO_Utils::format_url( (string) $issue->pageUrl );
104 if ( ! in_array( $issue->pageUrl, $this->current_issues ) ) {
107 $this->get_issue( $this->create_issue( $issue ) )
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
116 * @param stdClass $issue
118 * @return WPSEO_GSC_Issue
120 private function create_issue( $issue ) {
121 return new WPSEO_GSC_Issue(
123 new DateTime( (string) $issue->first_detected ),
124 new DateTime( (string) $issue->last_crawled ),
125 (string) ( ! empty( $issue->responseCode ) ) ? $issue->responseCode : null
130 * Returns the crawl issue as an array.
132 * @param WPSEO_GSC_Issue $crawl_issue
136 private function get_issue( WPSEO_GSC_Issue $crawl_issue ) {
137 return $crawl_issue->to_array();
141 * Saving the issues to the options. The target option is base on current platform and category.
143 * @param array $issues
145 private function save_issues( array $issues ) {
146 update_option( $this->option_name, $issues, false );
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.
153 private function set_current_issues() {
154 if ( ! empty( $this->issues ) ) {
155 $this->current_issues = wp_list_pluck( $this->issues, 'url' );
160 * Search in the issues for the given $url
166 private function get_issue_by_url( $url ) {
167 foreach ( $this->issues as $key => $issue ) {
168 if ( $url === $issue['url'] ) {