PageRenderTime 38ms CodeModel.GetById 27ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/wordpress-seo/admin/config-ui/components/class-component-connect-google-search-console.php

https://bitbucket.org/carloskikea/helpet
PHP | 151 lines | 61 code | 24 blank | 66 comment | 1 complexity | 2d276e7ededee8784b20b38096f3fc8e MD5 | raw file
  1<?php
  2/**
  3 * WPSEO plugin file.
  4 *
  5 * @package WPSEO\Admin\ConfigurationUI
  6 */
  7
  8/**
  9 * Class WPSEO_Config_Component_Connect_Google_Search_Console
 10 */
 11class WPSEO_Config_Component_Connect_Google_Search_Console implements WPSEO_Config_Component {
 12
 13	const OPTION_ACCESS_TOKEN = 'wpseo-gsc-access_token';
 14	const OPTION_REFRESH_TOKEN = 'wpseo-gsc-refresh_token';
 15
 16
 17	/** @var WPSEO_GSC_Service Service to use */
 18	protected $gsc_service;
 19
 20	/**
 21	 * WPSEO_Config_Component_Connect_Google_Search_Console constructor.
 22	 */
 23	public function __construct() {
 24		$this->gsc_service = new WPSEO_GSC_Service( $this->get_profile() );
 25	}
 26
 27	/**
 28	 * Set the Google Search Console service.
 29	 *
 30	 * @param WPSEO_GSC_Service $service Set service to use.
 31	 */
 32	public function set_gsc_service( WPSEO_GSC_Service $service ) {
 33		$this->gsc_service = $service;
 34	}
 35
 36	/**
 37	 * Gets the component identifier.
 38	 *
 39	 * @return string
 40	 */
 41	public function get_identifier() {
 42		return 'ConnectGoogleSearchConsole';
 43	}
 44
 45	/**
 46	 * Gets the field.
 47	 *
 48	 * @return WPSEO_Config_Field
 49	 */
 50	public function get_field() {
 51		return new WPSEO_Config_Field_Connect_Google_Search_Console();
 52	}
 53
 54	/**
 55	 * Get the data for the field.
 56	 *
 57	 * @return mixed
 58	 */
 59	public function get_data() {
 60
 61		$data = array(
 62			'profileList'    => $this->get_profilelist(),
 63			'profile'        => $this->get_profile(),
 64			'hasAccessToken' => $this->hasAccessToken(),
 65		);
 66
 67		return $data;
 68	}
 69
 70	/**
 71	 * Save data
 72	 *
 73	 * @param array $data Data containing changes.
 74	 *
 75	 * @return mixed
 76	 */
 77	public function set_data( $data ) {
 78
 79		$current_data = $this->get_data();
 80
 81		$this->handle_profile_change( $current_data, $data );
 82
 83		// Save profile.
 84		$has_saved = update_option(
 85			WPSEO_GSC::OPTION_WPSEO_GSC,
 86			array( 'profile' => $data['profile'] )
 87		);
 88
 89		// Collect results to return to the configurator.
 90		$results = array(
 91			'profile' => $has_saved,
 92		);
 93
 94		return $results;
 95	}
 96
 97	/**
 98	 * Remove issues when the profile has changed
 99	 *
100	 * @param array $current_data Saved data before changes.
101	 * @param array $data         Data after changes.
102	 */
103	protected function handle_profile_change( $current_data, $data ) {
104		// If the profile has been changed, remove issues.
105		if ( $current_data['profile'] === $data['profile'] ) {
106			return;
107		}
108
109		$this->reload_issues();
110	}
111
112	/**
113	 * Get the current GSC profile
114	 *
115	 * @return string
116	 */
117	protected function get_profile() {
118		return WPSEO_GSC_Settings::get_profile();
119	}
120
121	/**
122	 * Reload GSC issues
123	 */
124	protected function reload_issues() {
125		WPSEO_GSC_Settings::reload_issues();
126	}
127
128	/**
129	 * Gets a list with the profiles.
130	 *
131	 * @return array
132	 */
133	protected function get_profilelist() {
134		$profiles = array();
135		$sites    = $this->gsc_service->get_sites();
136		foreach ( $sites as $site_key => $site_value ) {
137			$profiles[ untrailingslashit( $site_key )  ] = untrailingslashit( $site_value );
138		}
139
140		return $profiles;
141	}
142
143	/**
144	 * Checks if there is an access token. If so, there is a connection.
145	 *
146	 * @return bool
147	 */
148	private function hasAccessToken() {
149		return ( null !== $this->gsc_service->get_client()->getAccessToken() );
150	}
151}