Skip to content

Instantly share code, notes, and snippets.

@hellofromtonya
Created December 8, 2022 19:57
Show Gist options
  • Save hellofromtonya/bb0aa2d4b2311c8b40fa5ae8ada4dc19 to your computer and use it in GitHub Desktop.
Save hellofromtonya/bb0aa2d4b2311c8b40fa5ae8ada4dc19 to your computer and use it in GitHub Desktop.
Testing Requests PR 3732
<?php
/**
* Plugin Name: Fake WordPress update.
* Description: Filters the value of <code>site_transient_update_core</code>.
* Author: WordPress Core Contributors
* Author URI: https://make.wordpress.org/core
* License: GPLv2 or later
* Version: 1.0.0
*/
add_filter(
'site_transient_update_core',
function ( $value ) {
if ( ! isset( $value->updates ) ) {
return $value;
}
$fake_update_location = trailingslashit( get_bloginfo( 'url' ) ) . 'wordpress-with-requests-2.zip';
$fake_update = (object) array(
'response' => 'upgrade',
'download' => $fake_update_location,
'locale' => 'en_US',
'packages' =>
(object) array(
'full' => $fake_update_location,
'no_content' => $fake_update_location,
'new_bundled' => $fake_update_location,
'partial' => '',
'rollback' => '',
),
'current' => '6.1.1-with-requests2',
'version' => '6.1.1-with-requests2',
'php_version' => '5.6.20',
'mysql_version' => '5.0',
'new_bundled' => '6.1',
'partial_version' => '',
'version_checked' => '',
);
array_unshift( $value->updates, $fake_update );
return $value;
}
);

The attached plugin lets you put your own custom WordPress update package in the root folder (aptly named wordpress-with-requests-2.zip).

Creating a custom package:

  • Create a new directory called wordpress.
  • Download a fresh copy of 6.1.1 into the wordpress directory.
  • Replace the wp-includes directory and wp-admin/includes/update_core.php file with the ones from the PR
    • Note: It's not just wp-includes/Requests/ that's changed by the PR, so it's easier to just replace the whole wp-includes directory.
  • Zip the wordpress directory to wordpress-with-requests-2.zip and put it in the root directory of your test site.

Testing:

  • Navigate to Plugins > Installed plugins and activate Fake WordPress update.
  • Navigate to Dashboard > Updates and update the site.

Expected results:

  • No errors
  • wp-includes/Requests should now have a single folder, src, containing 17 files, 7 sub-directories (in total: 64 files, 9 folders).
  • General browsing of the test site, the Site Editor, etc should all work.

If you want, you can add this to line 1575 of the PR's wp-admin/includes/update_core.php:

if ( str_ends_with( $old_file, 'Exception.php' ) ) {
    error_log( class_exists( 'Requests_Exception' ) ? 'Requests_Exception was preloaded.' : 'Requests_Exception was not preloaded.' );
}
@ironprogrammer
Copy link

If you want, you can add this to line 1575 of the PR's wp-admin/includes/update_core.php:

Adding a link here to the optional patch for logging when each old interface/class is preloaded. Thanks, @costdev!

Optional logging patch gist: https://gist.github.com/ironprogrammer/35a762d0100a7a01f4eadc37ee6b16cd

@ironprogrammer
Copy link

ironprogrammer commented Dec 9, 2022

To test auto updates, the following changes can be applied to the plugin above:

  • On line 20, change $fake_update->response from upgrade to autoupdate:
- 		'response' => 'upgrade',
+ 		'response' => 'autoupdate',
  • On lines 31-32, update the version so that the update registers as "newer" than 6.1.1 (you could also use 6.1.2 or something similar):
- 		'current' => '6.1.1-with-requests2',
- 		'version' => '6.1.1-with-requests2',
+ 		'current' => '6.1.1.1-with-requests2',
+ 		'version' => '6.1.1.1-with-requests2',
  • Add the following filters to the file:
/**
 * Allow auto updates. Overrides `WP_AUTO_UPDATE_CORE` setting.
 */
add_filter( 'auto_update_core', '__return_true' );

/**
 * Trick `WP_Automatic_Updater` into thinking there is no existing lock.
 *
 * By default, the auto-updater will only run once every hour even if
 * the cron job is manually fired.  That makes it hard to run multiple tests
 * in a short period of time.
 */
add_filter(
	'option_auto_updater.lock',
	function ($value) {
		return 1;
	}
);
delete_option('option_auto_updater.lock');

To run auto updates, wp_version_check() can be triggered in cron (e.g. using WP Crontrol). The option_auto_updater.lock filter above allows updates to be manually triggered every minute.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment