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