Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move version parameters to an API endpoint #57

Merged
merged 2 commits into from Mar 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 24 additions & 8 deletions lib/admin-page.php
Expand Up @@ -325,6 +325,20 @@ function classicpress_check_can_migrate() {
return false;
}

// Get migration plugin parameters.
$parameters = classicpress_migration_parameters();
if ( is_wp_error( $parameters ) ) {
?>
<div class="notice notice-error">
<p>
<?php echo $parameters->get_error_message(); ?>
<?php echo json_encode( $parameters->get_error_data() ); ?>
</p>
</div>
<?php
return false;
}

// The first round of checks has passed. Now, run a second round related
// to conditions that the user (or at least the hosting provider) has
// control over, and display the results in a table.
Expand All @@ -350,24 +364,26 @@ function classicpress_check_can_migrate() {
// Check: Supported WP version
// More versions can be added after they are confirmed to work.
global $wp_version;
$wp_version_min = '4.9.0';
$wp_version_max = '5.1.0';
$wp_version_min = $parameters['wordpress']['min'];
$wp_version_max = $parameters['wordpress']['max'];
$wp_version_check_intro_message = sprintf( __(
/* translators: 1: minimum supported WordPress version, 2: maximum supported WordPress version */
'This plugin supports WordPress versions <strong>%1$s</strong> to <strong>%2$s</strong> (and some newer development versions).',
'switch-to-classicpress'
), $wp_version_min, $wp_version_max );
$wp_version_check_intro_message .= "<br>\n";

if (
// Version is outside of our "stable release" range...
// Version is outside of our target range of WP stable releases...
(
version_compare( $wp_version, $wp_version_min, 'lt' ) ||
version_compare( $wp_version, $wp_version_max, 'gt' )
) &&
$wp_version !== '4.9' &&
// ... and it's not a known development release.
! preg_match( '#^5\.1\.1-(alpha|beta|rc)#i', $wp_version ) &&
! preg_match( '#^5\.2-(alpha|beta|rc)#i', $wp_version )
// ... and it doesn't match any other acceptable version patterns
empty( array_filter( $parameters['wordpress']['other'], function( $pattern ) {
global $wp_version;
return preg_match( $pattern, $wp_version );
} ) )
) {
/**
* Filters whether to ignore the result of the WP version check.
Expand All @@ -387,7 +403,7 @@ function classicpress_check_can_migrate() {
);
echo "<br>\n";
_e(
'We cannot guarantee that the migration process is going to work, and may even leave your current installation partially broken.',
'We cannot guarantee that the migration process is going to work, and it may even leave your current installation partially broken.',
'switch-to-classicpress'
);
echo "<br>\n";
Expand Down
62 changes: 44 additions & 18 deletions lib/update.php
Expand Up @@ -83,31 +83,55 @@ function classicpress_override_wp_update_api( $preempt, $r, $url ) {
return $preempt;
}

if ( empty( $_POST['_build_url'] ) || empty( $_POST['version'] ) ) {
// Not sure what happened, but it's not good.
if (
! isset( $_GET['_migrate'] ) ||
! in_array( $_GET['_migrate'], array( 'classicpress', '_custom' ), true )
) {
// Not a request we're interested in; do not override.
return $preempt;
}

// TODO:
// - pull locale out of $url
// - forward to real ClassicPress API
// - POST variables are not the best place to store version & URL
switch ( $_GET['_migrate'] ) {
case 'classicpress':
$parameters = classicpress_migration_parameters();
if ( ! is_array( $parameters ) ) {
// Not sure what happened, but it's not good.
return $preempt;
}
$build_url = $parameters['classicpress']['build'];
$version = $parameters['classicpress']['version'];
break;

case '_custom':
if (
! isset( $_POST['_build_url'] ) ||
! isset( $_POST['version'] )
) {
// Not sure what happened, but it's not good.
return $preempt;
}
$build_url = $_POST['_build_url'];
$version = $_POST['version'];
break;
}

// TODO: do locales other than en_US need different handling?

$data = array(
'offers' => array(
array(
'response' => 'upgrade',
'download' => $_POST['_build_url'],
'download' => $build_url,
'locale' => 'en_US',
'packages' => array(
'full' => $_POST['_build_url'],
'full' => $build_url,
'no_content' => false,
'new_bundled' => false,
'partial' => false,
'rollback' => false,
),
'current' => $_POST['version'],
'version' => $_POST['version'],
'current' => $version,
'version' => $version,
'php_version' => '5.6.0',
'mysql_version' => '5.0',
'new_bundled' => '4.7',
Expand Down Expand Up @@ -223,6 +247,12 @@ function classicpress_override_upgrade_page() {
return;
}

$parameters = classicpress_migration_parameters();
if ( ! is_array( $parameters ) ) {
// Not sure what happened, but it's not good.
return;
}

// Save the WP version for possible later restoration by a future version
// of this plugin.
global $wp_version;
Expand All @@ -238,19 +268,15 @@ function classicpress_override_upgrade_page() {
$build_date = '20190305';

// Set `$_POST['version']` and `$_POST['locale']` with the same results
// from our update data, so that `find_core_update` will return a result.
$_POST['version'] = "$build_version+migration.$build_date";
$_POST['locale'] = 'en_US';
// Set `$_POST['_build_url']` for `classicpress_override_wp_update_api`.
$_POST['_build_url'] = 'https://github.com/ClassyBot/ClassicPress-nightly'
. "/releases/download/$build_version%2Bmigration.$build_date"
. "/ClassicPress-nightly-$build_version-migration.$build_date.zip";
// from our update data, so that `find_core_update()` will return a result.
$_POST['version'] = $parameters['classicpress']['version'];
$_POST['locale'] = 'en_US';

// Force loading a fresh response from the update API, which we will
// override with our own data.
wp_version_check( array(), true );

// Finished overriding the upgrade, now let it proceed in
// wp-admin/update-core.php (see `do_core_upgrade`).
// wp-admin/update-core.php (see `do_core_upgrade()`).
}
add_action( 'admin_head-update-core.php', 'classicpress_override_upgrade_page' );
64 changes: 64 additions & 0 deletions switch-to-classicpress.php
Expand Up @@ -152,3 +152,67 @@ function classicpress_plugin_action_links( $links ) {
'network_admin_plugin_action_links_' . plugin_basename( __FILE__ ),
'classicpress_plugin_action_links'
);

/**
* Call the ClassicPress API to determine which versions of WordPress and
* ClassicPress are supported by the migration plugin.
*
* This is handled on the ClassicPress servers, because most of the time a new
* version of WordPress or ClassicPress does not require a new version of the
* migration plugin.
*
* @since 1.0.1
*
* @return array {
* "wordpress": {
* "min": "4.9.0",
* "max": "5.x.x",
* "other": [
* "^4\\.9$",
* // patterns for development versions ...
* ]
* },
* "classicpress": {
* "build": "https://github.com/[...].zip",
* "version": "1.x.x"
* }
* }
*/
function classicpress_migration_parameters() {
$parameters = get_transient( 'classicpress_migration_parameters' );

if ( ! $parameters ) {
$response = wp_remote_get( 'https://api-v1.classicpress.net/migration/' );
$parameters = null;

if ( is_wp_error( $response ) ) {
$status = $response->get_error_message();
} else {
$status = wp_remote_retrieve_response_code( $response );
}

if ( $status === 200 ) {
$parameters = json_decode( wp_remote_retrieve_body( $response ), true );
if ( is_array( $parameters ) ) {
set_transient(
'classicpress_migration_parameters',
$parameters,
1 * HOUR_IN_SECONDS
);
}
}

if ( ! is_array( $parameters ) ) {
return new WP_Error(
'classicpress_server_error',
__(
'Could not communicate with the ClassicPress API server',
'switch-to-classicpress'
),
array( 'status' => $status )
);
}
}

return $parameters;
}