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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
|
<?php /** * WPSEO plugin file. * * @package WPSEO\Admin * @since 9.3.0 */
// Mark this file as deprecated. _deprecated_file( __FILE__, 'WPSEO 10.0' );
/** * Holds the logic for the recalibration beta. * * @codeCoverageIgnore Ignore, because this class has been deprecated. * * @deprecated 10.0 */ class WPSEO_Recalibration_Beta implements WPSEO_WordPress_Integration {
/** * Name of the options. * * @var string */ protected $option_name = 'recalibration_beta';
/** * The read more URL. * * @var string */ protected $read_more_url = 'https://yoa.st/recalibration-beta-explanation';
/** * The class constructor. * * @deprecated 10.0 * * @codeCoverageIgnore */ public function __construct() { _deprecated_constructor( 'WPSEO_Recalibration_Beta', 'WPSEO 10.0' ); }
/** * Shows the feature toggle. * * @codeCoverageIgnore Reason: most output is html. * * @return void */ public function show_feature_toggle() { // If the recalibration beta has been disabled you will no longer be able to enable it. // See https://github.com/Yoast/wordpress-seo/issues/12183. if ( ! self::is_enabled() ) { return; }
$values = array( 'on' => __( 'On', 'wordpress-seo' ), 'off' => __( 'Off', 'wordpress-seo' ), );
echo '<div class="switch-container">'; echo '<fieldset id="', esc_attr( $this->option_name ), '" class="fieldset-switch-toggle">'; echo '<legend><strong>', esc_html__( 'Get an even better analysis', 'wordpress-seo' ), '</strong></legend>'; echo '<p class="clear">'; printf( /* translators: 1: link opening tag, 2: link closing tag, 3: strong opening tag, 4: strong closing tag */ esc_html__( 'We have %1$srecalibrated our analysis%2$s. With the new analysis, we will get even closer to how Google sees your website. It would be %3$sawesome%4$s if you would like to %3$sbeta test this feature%4$s for us!', 'wordpress-seo' ), '<a href="' . esc_url( WPSEO_Shortlinker::get( $this->read_more_url ) ) . '" target="_blank">', '</a>', '<strong>', '</strong>' ); echo '</p>';
echo '<div class="switch-toggle switch-candy switch-yoast-seo">';
foreach ( $values as $key => $value ) { printf( '<input type="radio" id="%1$s" name="%2$s" value="%3$s" %4$s /><label for="%1$s">%5$s</label>', esc_attr( $this->option_name . '-' . $key ), 'wpseo[' . esc_attr( $this->option_name ) . ']', esc_attr( $key ), checked( $this->get_option_value( self::is_enabled() ), esc_attr( $key ), false ), esc_html( $value ) ); } echo '<a></a></div>';
echo '<p class="clear"><br/>'; esc_html_e( 'Simply switch the toggle to "on" and you\'ll be able to use the recalibrated analysis. At the same time, we\'ll add you to our specific mailing list. We\'ll only email you about your experiences with this recalibration!', 'wordpress-seo' ); echo '</p>'; echo '</fieldset><div class="clear"></div></div>' . PHP_EOL . PHP_EOL; }
/** * Registers the hook to catch option change. * * @codeCoverageIgnore Reason: because it calls a WordPress function. * * @return void */ public function register_hooks() { add_action( 'update_option_wpseo', array( $this, 'update_option' ), 10, 2 ); }
/** * Compares the logic between old and new option value and send the request. * * @param mixed $old_value The old option value. * @param mixed $new_value The new option value. * * @return void */ public function update_option( $old_value, $new_value ) { $old_option_value = false; if ( isset( $old_value[ $this->option_name ] ) ) { $old_option_value = $old_value[ $this->option_name ]; }
$new_option_value = false; if ( isset( $new_value[ $this->option_name ] ) ) { $new_option_value = $new_value[ $this->option_name ]; }
if ( $old_option_value === $new_option_value ) { return; }
if ( $new_option_value === true ) { $this->subscribe_newsletter(); } }
/** * Checks if the recalibration beta has been enabled. * * @codeCoverageIgnore Reason: It calls a dependency. * * @return bool True whether the beta has been enabled. */ public static function is_enabled() { return WPSEO_Options::get( 'recalibration_beta' ); }
/** * Checks if the user has a mailinglist subscription. * * @codeCoverageIgnore Reason: because it calls a WordPress function. * * The mailinglist subscription value will set to true when the beta is set * to enabled. This value stays true, so it's a good indicator that the user * tried the beta. * * @return bool True whether the user has a subscription. */ public function has_mailinglist_subscription() { return (bool) get_option( 'wpseo_recalibration_beta_mailinglist_subscription', false ); }
/** * Retrieves the option value based on the current setting. * * @param bool $is_enabled Is the option enabled. * * @return string On when is enabled, off when not. */ protected function get_option_value( $is_enabled ) { if ( $is_enabled === true ) { return 'on'; }
return 'off'; }
/** * Subscribes to the newsletter. * * @return void */ protected function subscribe_newsletter() { if ( $this->has_mailinglist_subscription() ) { return; }
try { $this->do_request( 'https://my.yoast.com/api/customers/newsletter/recalibration/subscribe', array( 'email' => get_option( 'admin_email' ), 'firstName' => get_option( 'blogname' ), 'lastName' => '', ) );
$this->set_mailinglist_subscription(); } catch ( Requests_Exception_HTTP $e ) { // Intentionally left blank. @todo We should offer this to a logger. return; } }
/** * Performs a request to the given url. * * @codeCoverageIgnore Reason: because it contains WordPress functions. * * @param string $url The request url. * @param array $body The request body. * * @return void * * @throws Requests_Exception_HTTP When request has failed. */ protected function do_request( $url, $body ) { $response = wp_remote_post( $url, array( 'body' => $body, ) );
if ( is_wp_error( $response ) ) { throw new Requests_Exception_HTTP( $response->get_error_message() ); } }
/** * Sets the mailing list subscription value to true. * * @codeCoverageIgnore Reason: because it calls a WordPress function. * * @return void */ protected function set_mailinglist_subscription() { update_option( 'wpseo_recalibration_beta_mailinglist_subscription', true ); } }
|