PageRenderTime 39ms CodeModel.GetById 15ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 1ms

/wp-content/plugins/wordpress-seo/admin/class-license-page-manager.php

https://bitbucket.org/carloskikea/helpet
PHP | 211 lines | 93 code | 32 blank | 86 comment | 10 complexity | c17924b3cb541a9312f7f439dc043daa MD5 | raw file
  1<?php
  2/**
  3 * WPSEO plugin file.
  4 *
  5 * @package WPSEO\Admin
  6 */
  7
  8/**
  9 * Represents the values for a single Yoast Premium extension plugin.
 10 */
 11class WPSEO_License_Page_Manager implements WPSEO_WordPress_Integration {
 12
 13	/**
 14	 * @var string Version number for License Page Manager.
 15	 */
 16	const VERSION_LEGACY = '1';
 17
 18	/**
 19	 * @var string Version number for License Page Manager.
 20	 */
 21	const VERSION_BACKWARDS_COMPATIBILITY = '2';
 22
 23	/**
 24	 * Registers all hooks to WordPress.
 25	 */
 26	public function register_hooks() {
 27		add_filter( 'http_response', array( $this, 'handle_response' ), 10, 3 );
 28
 29		if ( $this->get_version() === self::VERSION_BACKWARDS_COMPATIBILITY ) {
 30			add_filter( 'yoast-license-valid', '__return_true' );
 31			add_filter( 'yoast-show-license-notice', '__return_false' );
 32			add_action( 'admin_init', array( $this, 'validate_extensions' ), 15 );
 33		}
 34		else {
 35			add_action( 'admin_init', array( $this, 'remove_faulty_notifications' ), 15 );
 36		}
 37	}
 38
 39	/**
 40	 * Validates the extensions and show a notice for the invalid extensions.
 41	 */
 42	public function validate_extensions() {
 43
 44		if ( filter_input( INPUT_GET, 'page' ) === WPSEO_Admin::PAGE_IDENTIFIER ) {
 45			/**
 46			 * Filter: 'yoast-active-extensions' - Collects all active extensions. This hook is implemented in the
 47			 *                                     license manager.
 48			 *
 49			 * @api array $extensions The array with extensions.
 50			 */
 51			apply_filters( 'yoast-active-extensions', array() );
 52		}
 53
 54		$extension_list = new WPSEO_Extensions();
 55		$extensions     = $extension_list->get();
 56
 57		$notification_center = Yoast_Notification_Center::get();
 58
 59		foreach ( $extensions as $product_name ) {
 60			$notification = $this->create_notification( $product_name );
 61
 62			// Add a notification when the installed plugin isn't activated in My Yoast.
 63			if ( $extension_list->is_installed( $product_name ) && ! $extension_list->is_valid( $product_name ) ) {
 64				$notification_center->add_notification( $notification );
 65
 66				continue;
 67			}
 68
 69			$notification_center->remove_notification( $notification );
 70		}
 71	}
 72
 73	/**
 74	 * Removes the faulty set notifications.
 75	 */
 76	public function remove_faulty_notifications() {
 77		$extension_list = new WPSEO_Extensions();
 78		$extensions     = $extension_list->get();
 79
 80		$notification_center = Yoast_Notification_Center::get();
 81
 82		foreach ( $extensions as $product_name ) {
 83			$notification = $this->create_notification( $product_name );
 84			$notification_center->remove_notification( $notification );
 85		}
 86	}
 87
 88	/**
 89	 * Handles the response.
 90	 *
 91	 * @param array  $response          HTTP response.
 92	 * @param array  $request_arguments HTTP request arguments. Unused.
 93	 * @param string $url               The request URL.
 94	 *
 95	 * @return array The response array.
 96	 */
 97	public function handle_response( array $response, $request_arguments, $url ) {
 98		$response_code = wp_remote_retrieve_response_code( $response );
 99
100		if ( $response_code === 200 && $this->is_expected_endpoint( $url ) ) {
101			$response_data = $this->parse_response( $response );
102			$this->detect_version( $response_data );
103		}
104
105		return $response;
106	}
107
108	/**
109	 * Returns the license page to use based on the version number.
110	 *
111	 * @return string The page to use.
112	 */
113	public function get_license_page() {
114		return 'licenses';
115	}
116
117	/**
118	 * Returns the version number of the license server.
119	 *
120	 * @return int The version number
121	 */
122	protected function get_version() {
123		return get_option( $this->get_option_name(), self::VERSION_BACKWARDS_COMPATIBILITY );
124	}
125
126	/**
127	 * Returns the option name.
128	 *
129	 * @return string The option name.
130	 */
131	protected function get_option_name() {
132		return 'wpseo_license_server_version';
133	}
134
135	/**
136	 * Sets the version when there is a value in the response.
137	 *
138	 * @param array $response The response to extract the version from.
139	 */
140	protected function detect_version( $response ) {
141		if ( ! empty( $response['serverVersion'] ) ) {
142			$this->set_version( $response['serverVersion'] );
143		}
144	}
145
146	/**
147	 * Sets the version.
148	 *
149	 * @param string $server_version The version number to save.
150	 */
151	protected function set_version( $server_version ) {
152		update_option( $this->get_option_name(), $server_version );
153	}
154
155	/**
156	 * Parses the response by getting its body and do a unserialize of it.
157	 *
158	 * @param array $response The response to parse.
159	 *
160	 * @return mixed|string|false The parsed response.
161	 */
162	protected function parse_response( $response ) {
163		$response = json_decode( wp_remote_retrieve_body( $response ), true );
164		$response = maybe_unserialize( $response );
165
166		return $response;
167	}
168
169	/**
170	 * Checks if the given url matches the expected endpoint.
171	 *
172	 * @param string $url The url to check.
173	 *
174	 * @return bool True when url matches the endpoint.
175	 */
176	protected function is_expected_endpoint( $url ) {
177		$url_parts = wp_parse_url( $url );
178
179		$is_yoast_com = ( in_array( $url_parts['host'], array( 'yoast.com', 'my.yoast.com' ), true ) );
180		$is_edd_api   = ( isset( $url_parts['path'] ) && $url_parts['path'] === '/edd-sl-api' );
181
182		return $is_yoast_com && $is_edd_api;
183	}
184
185	/**
186	 * Creates an instance of Yoast_Notification
187	 *
188	 * @param string $product_name The product to create the notification for.
189	 *
190	 * @return Yoast_Notification The created notification.
191	 */
192	protected function create_notification( $product_name ) {
193		$notification_options = array(
194			'type'         => Yoast_Notification::ERROR,
195			'id'           => 'wpseo-dismiss-' . sanitize_title_with_dashes( $product_name, null, 'save' ),
196			'capabilities' => 'wpseo_manage_options',
197		);
198
199		$notification = new Yoast_Notification(
200			sprintf(
201				/* translators: %1$s expands to the product name. %2$s expands to a link to My Yoast  */
202				__( 'You are not receiving updates or support! Fix this problem by adding this site and enabling %1$s for it in %2$s.', 'wordpress-seo' ),
203				$product_name,
204				'<a href="' . WPSEO_Shortlinker::get( 'https://yoa.st/13j' ) . '" target="_blank">My Yoast</a>'
205			),
206			$notification_options
207		);
208
209		return $notification;
210	}
211}