forked from afragen/wordpress-beta-tester
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWP_Beta_Tester.php
490 lines (428 loc) · 15.1 KB
/
WP_Beta_Tester.php
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
<?php
/**
* WordPress Beta Tester
*
* @package WordPress_Beta_Tester
* @author Andy Fragen, original author Peter Westwood.
* @license GPLv2+
* @copyright 2009-2016 Peter Westwood (email : peter.westwood@ftwr.co.uk)
*/
/**
* WP_Beta_Tester
*/
class WP_Beta_Tester {
/**
* Holds main plugin file.
*
* @var string
*/
public $file;
/**
* Holds plugin options.
*
* @var array
*/
public static $options;
/**
* Holds WP_AUTO_UPDATE_CORE if set.
*
* @var string|bool
*/
public static $core_update_stream_constant;
/**
* Holds WP_AUTO_UPDATE_CORE if set.
*
* @var string|bool
*/
public static $core_update_channel_constant;
/**
* Constructor.
*
* @param string $file Main plugin file.
* @param array $options Plugin options.
* @return void
*/
public function __construct( $file, $options ) {
$this->file = $file;
self::$options = $options;
self::$core_update_stream_constant = defined( 'WP_AUTO_UPDATE_CORE' ) && in_array( \WP_AUTO_UPDATE_CORE, array( 'beta', 'rc' ), true ) ? \WP_AUTO_UPDATE_CORE : false;
self::$core_update_channel_constant = defined( 'WP_AUTO_UPDATE_CORE' ) && in_array( \WP_AUTO_UPDATE_CORE, array( 'development', 'branch-development' ), true ) ? \WP_AUTO_UPDATE_CORE : false;
}
/**
* Rev up the engines.
*
* @return void
*/
public function run() {
$this->load_hooks();
( new WPBT_Settings( $this, self::$options ) )->run();
}
/**
* Load hooks.
*
* @return void
*/
protected function load_hooks() {
add_action(
'update_option_wp_beta_tester_stream',
array(
$this,
'action_update_option_wp_beta_tester_stream',
)
);
add_filter( 'pre_http_request', array( $this, 'filter_http_request' ), 10, 3 );
// Fixed in https://core.trac.wordpress.org/changeset/49708.
if ( version_compare( get_bloginfo( 'version' ), '5.6-RC1-49708', '<=' )
&& preg_match( '/alpha|beta|RC/', get_bloginfo( 'version' ) )
) {
// set priority to 11 so that we fire after the function core hooks into this filter.
add_filter( 'update_footer', array( $this, 'update_footer' ), 11 );
}
// Add dashboard widget.
add_action( 'wp_dashboard_setup', array( $this, 'add_dashboard_widget' ) );
add_action( 'wp_network_dashboard_setup', array( $this, 'add_dashboard_widget' ) );
// Delete development RSS feed transient for dashboard widget on core upgrade.
add_action( 'upgrader_process_complete', array( $this, 'delete_feed_transient_on_upgrade' ), 10, 2 );
}
/**
* Check and display notice if 'update' really downgrade.
*
* @return void
*/
public function action_admin_head_plugins_php() {
// Workaround the check throttling in wp_version_check().
$st = get_site_transient( 'update_core' );
if ( is_object( $st ) ) {
$st->last_checked = 0;
set_site_transient( 'update_core', $st );
}
wp_version_check();
// Can output an error here if current config drives version backwards.
if ( $this->check_if_settings_downgrade() ) {
echo '<div id="message" class="notice notice-warning"><p>';
$admin_page = is_multisite() ? network_admin_url( 'settings.php' ) : admin_url( 'tools.php' );
$admin_page = add_query_arg(
array(
'page' => 'wp-beta-tester',
'tab' => 'wp_beta_tester_core',
),
$admin_page
);
/* translators: %s: link to setting page */
printf(
/* translators: %s: WordPress Beta Tester Settings page URL */
wp_kses_post( __( '<strong>Warning:</strong> Your current <a href="%s">WordPress Beta Tester plugin configuration</a> will downgrade your installation to a previous version - please reconfigure it.', 'wordpress-beta-tester' ) ),
esc_url( $admin_page )
);
echo '</p></div>';
}
}
/**
* Filter 'pre_http_request' to add beta-tester API check.
*
* @param mixed $result $result from filter.
* @param array $args Array of filter args.
* @param string $url URL from filter.
* @return stdClass Output from wp_remote_get().
*/
public function filter_http_request( $result, $args, $url ) {
if ( $result || isset( $args['_beta_tester'] ) ) {
return $result;
}
if ( false === strpos( $url, '//api.wordpress.org/core/version-check/' ) ) {
return $result;
}
// It's a core-update request.
$args['_beta_tester'] = true;
$url = empty( self::$options['stream-option'] )
? add_query_arg( 'channel', self::$options['channel'], $url )
: add_query_arg( 'channel', self::$options['stream-option'], $url );
// Use WP_AUTO_UPDATE_CORE if set.
$url = self::$core_update_stream_constant ? add_query_arg( 'channel', self::$core_update_stream_constant, $url ) : $url;
$url = self::$core_update_channel_constant ? add_query_arg( 'channel', self::$core_update_channel_constant, $url ) : $url;
// Make adjustments for switching between channels.
$url = $this->channel_switching_modification( $url );
// phpcs:ignore Squiz.PHP.CommentedOutCode.Found
// $url = add_query_arg( 'pretend_releases', array( '5.6-beta2' ), $url );
// pretend_releases[]=5.6-beta2 query arg example.
return wp_remote_get( $url, $args );
}
/**
* Modify URL to version check to return expected API response.
*
* @param string $url Version check URL.
*
* @return string $url
*/
private function channel_switching_modification( $url ) {
$next_versions = ( new WPBT_Core( $this, static::$options ) )->calculate_next_versions();
$wp_version = get_bloginfo( 'version' );
$channel = self::$core_update_channel_constant ? self::$core_update_channel_constant : self::$options['channel'];
if ( false !== strpos( $channel, 'development' )
&& version_compare( $wp_version, $next_versions['point'], '<' )
) {
return $url;
}
switch ( $channel ) {
case 'branch-development':
$url = add_query_arg( 'version', $next_versions['point'] . '-alpha', $url );
break;
case 'development':
if ( false !== strpos( $wp_version, $next_versions['point'] )
|| version_compare( $wp_version, $next_versions['point'], '<' )
) {
$url = add_query_arg( 'version', $next_versions['release'] . '-alpha', $url );
}
break;
}
return $url;
}
/**
* Our option has changed so update the cached information pronto.
*
* @return void
*/
public function action_update_option_wp_beta_tester_stream() {
do_action( 'wp_version_check' );
}
/**
* Get preferred update version from core.
*
* @return /stdClass
*/
public function get_preferred_from_update_core() {
if ( ! function_exists( 'get_preferred_from_update_core' ) ) {
require_once ABSPATH . 'wp-admin/includes/update.php';
}
// Validate that we have api data and if not get the normal data so we always have it.
$preferred = get_preferred_from_update_core();
if ( false === $preferred ) {
wp_version_check();
$preferred = get_preferred_from_update_core();
}
return $preferred;
}
/**
* Get current WP release version.
*
* @since 3.1.0
* @return string|array $wp_version
*/
public function get_current_wp_release() {
$response = get_site_transient( 'current_wp_release' );
if ( ! $response ) {
$response = wp_remote_get( 'https://api.wordpress.org/core/stable-check/1.0/' );
$response = wp_remote_retrieve_body( $response );
if ( is_wp_error( $response ) ) {
return array();
}
$response = (array) json_decode( $response );
$response = array_keys( $response, 'latest', true );
$response = array_pop( $response );
$response = null === $response ? '' : $response;
set_site_transient( 'current_wp_release', $response, DAY_IN_SECONDS );
}
return $response;
}
/**
* Returns whether 'update' is really downgrade.
*
* @return bool
*/
public function check_if_settings_downgrade() {
$wp_version = get_bloginfo( 'version' );
$wp_real_version = explode( '-', $wp_version );
$wpbt_core = new WPBT_Core( $this, self::$options );
$next_versions = $wpbt_core->calculate_next_versions();
if ( empty( $next_versions ) ) {
return false;
}
return version_compare( $next_versions['release'], $wp_real_version[0], 'lt' );
}
/**
* Add dashboard widget for beta testing information.
*
* @since 2.2.3
*
* @return void
*/
public function add_dashboard_widget() {
$wp_version = get_bloginfo( 'version' );
$beta_rc = preg_match( '/alpha|beta|RC/', $wp_version );
if ( $beta_rc ) {
wp_add_dashboard_widget( 'beta_tester_dashboard_widget', __( 'WordPress Beta Testing', 'wordpress-beta-tester' ), array( $this, 'beta_tester_dashboard' ) );
}
}
/**
* Setup dashboard widget.
*
* @since 2.2.3
*
* @return void
*/
public function beta_tester_dashboard() {
$wp_version = get_bloginfo( 'version' );
$next_version = explode( '-', $wp_version );
$milestone = array_shift( $next_version );
$bug = '<span class="dashicons dashicons-buddicons-replies"></span>';
$preferred = $this->get_preferred_from_update_core();
$update_version = ( new WPBT_Core( $this, self::$options ) )->get_next_version( $preferred->version );
$report_url = '';
$report_url = add_query_arg(
array(
'page' => 'wp-beta-tester',
'tab' => 'wp_beta_tester_bug_report',
),
is_multisite() ? network_admin_url( 'settings.php' ) : admin_url( 'tools.php' )
);
if ( is_plugin_active( 'test-reports/test-reports.php' ) ) {
$report_url = add_query_arg(
array(
'page' => 'test-reports',
),
is_multisite() ? network_admin_url( 'settings.php' ) : admin_url( 'tools.php' )
);
}
/* translators: %s: WordPress version */
printf( wp_kses_post( '<p>' . __( 'Please help test <strong>WordPress %s</strong>.', 'wordpress-beta-tester' ) . '</p>' ), esc_attr( $milestone ) );
echo wp_kses_post( $this->add_dev_notes_field_guide_links( $milestone ) );
echo wp_kses_post( $this->parse_development_feed( $milestone ) );
/* translators: %1: link to closed and reopened trac tickets on current milestone */
printf( wp_kses_post( '<p>' . __( 'Here are the <a href="%s" target="_blank">commits for the milestone</a>.', 'wordpress-beta-tester' ) . '</p>' ), esc_url( "https://core.trac.wordpress.org/query?status=closed&status=reopened&milestone=$milestone" ) );
/* translators: %s: link to Report a Bug tab */
printf( wp_kses_post( '<p>' . " $bug " . __( 'Found a bug? <a href="%s">Report it</a>!', 'wordpress-beta-tester' ) . '</p>' ), esc_url( $report_url ) );
$capability = is_multisite() ? 'manage_network_options' : 'manage_options';
if ( current_user_can( $capability ) ) {
$parent = is_multisite() ? 'settings.php' : 'tools.php';
$wpbt_settings_page = add_query_arg( 'page', 'wp-beta-tester', network_admin_url( $parent ) );
printf(
/* translators: %s: update version */
wp_kses_post( '<p>' . __( 'Currently your site is set to update to <strong>%s</strong>.', 'wordpress-beta-tester' ) . '</p>' ),
esc_html( $update_version )
);
if ( 'beta' !== self::$options['stream-option'] ) {
/* translators: %s: WP Beta Tester settings URL */
printf( wp_kses_post( '<p>' . __( 'Head over to your <a href="%s">WordPress Beta Tester Settings</a> and make sure the <strong>beta/RC</strong> stream is selected.', 'wordpress-beta-tester' ) . '</p>' ), esc_url( $wpbt_settings_page ) );
}
}
}
/**
* Parse development RSS feed for list of milestoned items.
*
* @since 2.2.3
* @param string $milestone Milestone version.
*
* @return string HTML unordered list.
*/
private function parse_development_feed( $milestone ) {
$rss_args = array(
'show_summary' => 0,
'items' => 10,
);
$urls = array( "https://wordpress.org/news/tag/development/feed/?s=$milestone", "https://make.wordpress.org/core/tag/development/feed/?s=$milestone" );
ob_start();
wp_widget_rss_output( array( 'url' => $urls ), $rss_args );
$feed = ob_get_contents();
ob_end_clean();
$milestone = preg_quote( $milestone, '.' );
$li_regex = "#<li>.*$milestone.*?<\/li>#";
preg_match( $li_regex, $feed, $matches );
$match = array_pop( $matches );
$list = empty( $match ) ? '' : "<ul>$match</ul>";
return $list;
}
/**
* Add milestone dev notes and field guide when on RC version.
*
* @since 2.2.3
* @param string $milestone Milestone version.
*
* @return string HTML unordered list.
*/
private function add_dev_notes_field_guide_links( $milestone ) {
$wp_version = get_bloginfo( 'version' );
$beta_rc = preg_match( '/beta|RC/', $wp_version );
$rc = preg_match( '/RC/', $wp_version );
$milestone_dash = str_replace( '.', '-', $milestone );
$dev_note_link = '';
$field_guide_link = '';
if ( $beta_rc ) {
$dev_note_link = sprintf(
/* translators: %1$s Link to dev notes, %2$s: Link title */
'<a href="%1$s">%2$s</a>',
"https://make.wordpress.org/core/tag/dev-notes+$milestone_dash/",
/* translators: %s: Milestone version */
sprintf( __( 'WordPress %s Dev Notes', 'wordpress-beta-tester' ), $milestone )
);
$dev_note_link = "<li>$dev_note_link</li>";
}
if ( $rc ) {
$field_guide_link = sprintf(
/* translators: %1$s Link to field guide, %2$s: Link title */
'<a href="%1$s">%2$s</a>',
"https://make.wordpress.org/core/wordpress-$milestone_dash-field-guide/",
/* translators: %s: Milestone version */
sprintf( __( 'WordPress %s Field Guide', 'wordpress-beta-tester' ), $milestone )
);
$field_guide_link = "<li>$field_guide_link</li>";
}
$links = $beta_rc || $rc ? "<ul> $dev_note_link $field_guide_link </ul>" : '';
return $links;
}
/**
* Delete development RSS feed transient on core upgrade.
*
* @uses filter 'upgrader_process_complete'.
*
* @param \Core_Upgrader $obj \Core_Upgrader object.
* @param array $hook_extra $hook_extra array from filter.
*
* @return void
*/
public function delete_feed_transient_on_upgrade( $obj, $hook_extra ) {
if ( $obj instanceof \Core_Upgrader && 'core' === $hook_extra['type'] ) {
$transient = md5( 'https://wordpress.org/news/category/development/feed/' );
delete_transient( "feed_{$transient}" );
delete_transient( "feed_mod_{$transient}" );
}
}
/**
* Ensure core still displays "You are using a development verison..." in the admin
* footer, even if we've removed the `development` update response because the next
* beta/RC package is not available.
*
* @since 2.2.0
*
* @return string
*
* @filter update_footer
*/
public function update_footer() {
add_filter( 'pre_site_transient_update_core', array( $this, 'add_minimal_development_response' ), 10, 0 );
$content = core_update_footer();
remove_filter( 'pre_site_transient_update_core', array( $this, 'add_minimal_development_response' ) );
return $content;
}
/**
* Add a minimal development response as the preferred update.
*
* @since 2.2.0
*
* @return object
*
* @filter pre_site_transient_update_core
*/
public function add_minimal_development_response() {
$from_api = new stdClass();
$update = new stdClass();
// a "minimal" response is one with the `response`, `current` and `locale` properties.
$update->response = 'development';
$update->current = get_bloginfo( 'version' );
$update->version = $update->current;
$update->locale = get_locale();
$from_api->updates = array(
$update,
);
return $from_api;
}
}