PageRenderTime 24ms CodeModel.GetById 13ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/wordpress-seo/admin/google_search_console/class-gsc-issues.php

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