Skip to content

Commit

Permalink
upgrade to 1.4b
Browse files Browse the repository at this point in the history
  • Loading branch information
notslang committed Jun 22, 2014
1 parent 32f9785 commit 927e82a
Show file tree
Hide file tree
Showing 11 changed files with 1,943 additions and 359 deletions.
48 changes: 28 additions & 20 deletions class/wpsdb-addon.php
Expand Up @@ -6,16 +6,26 @@ function __construct( $plugin_file_path ) {
parent::__construct( $plugin_file_path );
}

function get_wpsdb_basename() {
global $wpsdb;
return plugin_basename( $wpsdb->get_plugin_file_path() );
}

function meets_version_requirements( $version_required ) {
$wpsdb_version = $this->get_installed_version( $this->get_wpsdb_basename() );
$wpsdb_version = $GLOBALS['wpsdb_meta']['wp-sync-db']['version'];
$result = version_compare( $wpsdb_version, $version_required, '>=' );
$this->version_required = $version_required;
if( false == $result ) $this->hook_version_requirement_actions();

if ( $result ) {
// If pre-1.1.2 version of Media Files addon,
// then it's not supported by this version of core
if ( empty( $this->plugin_version ) ) {
$result = false;
}
// Check that this version of core supports the addon version
else {
$plugin_basename = sprintf( '%1$s/%1$s.php', $this->plugin_slug );
$required_addon_version = $this->addons[$plugin_basename]['required_version'];
$result = version_compare( $this->plugin_version, $required_addon_version, '>=' );
}
}

return $result;
}

Expand All @@ -26,28 +36,26 @@ function hook_version_requirement_actions() {
function version_requirement_actions() {
$addon_requirement_check = get_option( 'wpsdb_addon_requirement_check', array() );
// we only want to delete the transients once, here we keep track of which versions we've checked
if( ! isset( $addon_requirement_check[$this->plugin_slug] ) || $addon_requirement_check[$this->plugin_slug] != $this->get_installed_version() ) {
if( ! isset( $addon_requirement_check[$this->plugin_slug] ) || $addon_requirement_check[$this->plugin_slug] != $GLOBALS['wpsdb_meta'][$this->plugin_slug]['version'] ) {
delete_site_transient( 'wpsdb_upgrade_data' );
delete_site_transient( 'update_plugins' );
$addon_requirement_check[$this->plugin_slug] = $this->get_installed_version();
$addon_requirement_check[$this->plugin_slug] = $GLOBALS['wpsdb_meta'][$this->plugin_slug]['version'];
update_option( 'wpsdb_addon_requirement_check', $addon_requirement_check );
}
$this->version_requirement_warning();
}

function version_requirement_warning() { ?>
<div class="updated warning version-requirement-warning notification-message warning-notice">
<p>
<strong>Update Required</strong> &mdash;
<?php
$wpsdb_basename = $this->get_wpsdb_basename();
$addon_name = $this->get_plugin_name();
$required = $this->version_required;
$installed = $this->get_installed_version( $wpsdb_basename );
$update = wp_nonce_url( network_admin_url( 'update.php?action=upgrade-plugin&plugin=' . urlencode( $wpsdb_basename ) ), 'upgrade-plugin_' . $wpsdb_basename );
echo sprintf( 'The version of %s you have installed, requires version %s of WP Migrate DB. You currently have %s installed. <strong><a href="%s">Update Now</a></strong>', $addon_name, $required, $installed, $update );
?>
</p>
<div class="updated warning inline-message below-h2">
<strong>Update Required</strong> &mdash;
<?php
$addon_name = $this->get_plugin_name();
$required = $this->version_required;
$installed = $GLOBALS['wpsdb_meta']['wp-sync-db']['version'];
$wpsdb_basename = sprintf( '%s/%s.php', $GLOBALS['wpsdb_meta']['wp-sync-db']['folder'], 'wp-sync-db' );
$update = wp_nonce_url( network_admin_url( 'update.php?action=upgrade-plugin&plugin=' . urlencode( $wpsdb_basename ) ), 'upgrade-plugin_' . $wpsdb_basename );
printf( __( 'The version of %1$s you have installed, requires version %2$s of WP Sync DB. You currently have %3$s installed. <strong><a href="%4$s">Update Now</a></strong>', 'wp-sync-db' ), $addon_name, $required, $installed, $update );
?>
</div>
<?php
}
Expand Down
113 changes: 79 additions & 34 deletions class/wpsdb-base.php
Expand Up @@ -4,8 +4,10 @@ class WPSDB_Base {
protected $plugin_file_path;
protected $plugin_dir_path;
protected $plugin_slug;
protected $plugin_folder_name;
protected $plugin_basename;
protected $plugin_base;
protected $plugin_version;
protected $template_dir;
protected $plugin_title;
protected $transient_timeout;
Expand All @@ -14,29 +16,36 @@ class WPSDB_Base {
protected $attempting_to_connect_to;
protected $error;
protected $temp_prefix = '_mig_';
protected $invalid_content_verification_error = 'Invalid content verification signature, please verify the connection information on the remote site and try again.';
protected $invalid_content_verification_error;
protected $addons;
protected $doing_cli_migration = false;

function __construct( $plugin_file_path ) {
$this->settings = get_option( 'wpsdb_settings' );

$this->addons = array(
'wp-sync-db-media-files/wp-sync-db-media-files.php' => array(
'name' => 'Media Files',
'required_version' => '1.0.1',
'required_version' => '1.1.4b1',
),
'wp-sync-db-cli/wp-sync-db-cli.php' => array(
'name' => 'CLI',
'required_version' => '1.0b1',
)
);

$this->invalid_content_verification_error = __( 'Invalid content verification signature, please verify the connection information on the remote site and try again.', 'wp-sync-db' );

$this->transient_timeout = 60 * 60 * 12;
$this->transient_retry_timeout = 60 * 60 * 2;

$this->plugin_file_path = $plugin_file_path;
$this->plugin_dir_path = plugin_dir_path( $plugin_file_path );
$this->plugin_slug = basename( $this->plugin_dir_path );
$this->plugin_folder_name = basename( $this->plugin_dir_path );
$this->plugin_basename = plugin_basename( $plugin_file_path );
$this->template_dir = $this->plugin_dir_path . 'template' . DS;
$this->plugin_title = ucwords( str_ireplace( '-', ' ', $this->plugin_slug ) );
$this->plugin_title = str_ireplace( array( 'db', 'wp' ), array( 'DB', 'WP' ), $this->plugin_title );
$this->plugin_title = ucwords( str_ireplace( '-', ' ', basename( $plugin_file_path ) ) );
$this->plugin_title = str_ireplace( array( 'db', 'wp', '.php' ), array( 'DB', 'WP', '' ), $this->plugin_title );

if ( is_multisite() ) {
$this->plugin_base = 'settings.php?page=wp-sync-db';
Expand All @@ -49,28 +58,10 @@ function __construct( $plugin_file_path ) {
$this->temp_prefix = apply_filters( 'wpsdb_temporary_prefix', $this->temp_prefix );
}

function printer( $debug ) {
echo '<pre>' . print_r( $debug, true ) . '</pre>';
}

function template( $template ) {
include $this->template_dir . $template . '.php';
}

function get_installed_version( $plugin = false ) {
if ( !is_admin() ) return false;

$plugin_basename = ( false !== $plugin ? $plugin : $this->plugin_basename );

$plugins = get_plugins();

if ( !isset( $plugins[$plugin_basename]['Version'] ) ) {
return false;
}

return $plugins[$plugin_basename]['Version'];
}

function open_ssl_enabled() {
if ( defined( 'OPENSSL_VERSION_TEXT' ) ) {
return true;
Expand All @@ -82,7 +73,7 @@ function open_ssl_enabled() {

function set_time_limit() {
if ( !function_exists( 'ini_get' ) || !ini_get( 'safe_mode' ) ) {
set_time_limit( 0 );
@set_time_limit( 0 );
}
}

Expand Down Expand Up @@ -117,7 +108,7 @@ function remote_post( $url, $data, $scope, $args = array(), $expecting_serial =
$args['body'] = $this->array_to_multipart( $data );
}
$args['headers']['Content-Type'] = 'multipart/form-data; boundary=' . $this->multipart_boundary;
$args['headers']['Referer'] = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
$args['headers']['Referer'] = network_admin_url( 'admin-ajax.php' );

$this->attempting_to_connect_to = $url;

Expand All @@ -132,17 +123,23 @@ function remote_post( $url, $data, $scope, $args = array(), $expecting_serial =
return $this->retry_remote_post( $url, $data, $scope, $args, $expecting_serial );
}
else if( isset( $response->errors['http_request_failed'][0] ) && strstr( $response->errors['http_request_failed'][0], 'timed out' ) ) {
$this->error = 'The connection to the remote server has timed out, no changes have been committed. (#134 - scope: ' . $scope . ')';
$this->error = sprintf( __( 'The connection to the remote server has timed out, no changes have been committed. (#134 - scope: %s)', 'wp-sync-db' ), $scope );
}
else if ( isset( $response->errors['http_request_failed'][0] ) && ( strstr( $response->errors['http_request_failed'][0], 'Could not resolve host' ) || strstr( $response->errors['http_request_failed'][0], 'couldn\'t connect to host' ) ) ) {
$this->error = 'We could not find: ' . $_POST['url'] . '. Are you sure this is the correct URL?';
$this->error = sprintf( __( 'We could not find: %s. Are you sure this is the correct URL?', 'wp-sync-db' ), $_POST['url'] );
$url_bits = parse_url( $_POST['url'] );
if( strstr( $_POST['url'], 'dev.' ) || strstr( $_POST['url'], '.dev' ) || ! strstr( $url_bits['host'], '.' ) ) {
$this->error .= '<br />It appears that you might be trying to ' . $_POST['intent'] . ( $_POST['intent'] == 'pull' ? ' from' : ' to' ) . ' a local environment. This will not work if <u>this</u> website happens to be located on a remote server, it would be impossible for this server to contact your local environment.';
$this->error .= '<br />';
if( $_POST['intent'] == 'pull' ) {
$this->error .= __( 'It appears that you might be trying to pull from a local environment. This will not work if <u>this</u> website happens to be located on a remote server, it would be impossible for this server to contact your local environment.', 'wp-sync-db' );
}
else {
$this->error .= __( 'It appears that you might be trying to push to a local environment. This will not work if <u>this</u> website happens to be located on a remote server, it would be impossible for this server to contact your local environment.', 'wp-sync-db' );
}
}
}
else {
$this->error = 'The connection failed, an unexpected error occurred, please contact support. (#121 - scope: ' . $scope . ')';
$this->error = sprintf( __( 'The connection failed, an unexpected error occurred, please contact support. (#121 - scope: %s)', 'wp-sync-db' ), $scope );
}
$this->log_error( $this->error, $response );
return false;
Expand All @@ -152,12 +149,12 @@ function remote_post( $url, $data, $scope, $args = array(), $expecting_serial =
return $this->retry_remote_post( $url, $data, $scope, $args, $expecting_serial );
}
else if( $response['response']['code'] == '401' ) {
$this->error = 'The remote site is protected with Basic Authentication. Please enter the username and password above to continue. (401 Unauthorized)';
$this->error = __( 'The remote site is protected with Basic Authentication. Please enter the username and password above to continue. (401 Unauthorized)', 'wp-sync-db' );
$this->log_error( $this->error, $response );
return false;
}
else {
$this->error = 'Unable to connect to the remote server, please check the connection details - ' . $response['response']['code'] . ' ' . $response['response']['message'] . ' (#129 - scope: ' . $scope . ')';
$this->error = sprintf( __( 'Unable to connect to the remote server, please check the connection details - %1$s %2$s (#129 - scope: %3$s)', 'wp-sync-db' ), $response['response']['code'], $response['response']['message'], $scope );
$this->log_error( $this->error, $response );
return false;
}
Expand All @@ -166,18 +163,24 @@ function remote_post( $url, $data, $scope, $args = array(), $expecting_serial =
if( strpos( $url, 'https://' ) === 0 && $scope == 'ajax_verify_connection_to_remote_site' ) {
return $this->retry_remote_post( $url, $data, $scope, $args, $expecting_serial );
}
$this->error = 'There was a problem with the AJAX request, we were expecting a serialized response, instead we received:<br />' . htmlentities( $response['body'] );
$this->error = __( 'There was a problem with the AJAX request, we were expecting a serialized response, instead we received:<br />', 'wp-sync-db' ) . htmlentities( $response['body'] );
$this->log_error( $this->error, $response );
return false;
}
elseif ( $response['body'] === '0' ) {
if( strpos( $url, 'https://' ) === 0 && $scope == 'ajax_verify_connection_to_remote_site' ) {
return $this->retry_remote_post( $url, $data, $scope, $args, $expecting_serial );
}
$this->error = 'WP Migrate DB does not seem to be installed or active on the remote site. (#131 - scope: ' . $scope . ')';
$this->error = sprintf( __( 'WP Sync DB does not seem to be installed or active on the remote site. (#131 - scope: %s)', 'wp-sync-db' ), $scope );
$this->log_error( $this->error, $response );
return false;
}
elseif ( $expecting_serial && is_serialized( $response['body'] ) == true && $scope == 'ajax_verify_connection_to_remote_site' ) {
$unserialized_response = unserialize( $response['body'] );
if ( isset( $unserialized_response['error'] ) && '1' == $unserialized_response['error'] && strpos( $url, 'https://' ) === 0 ) {
return $this->retry_remote_post( $url, $data, $scope, $args, $expecting_serial );
}
}

return $response['body'];
}
Expand Down Expand Up @@ -270,6 +273,9 @@ function display_errors() {
}

function filter_post_elements( $post_array, $accepted_elements ) {
if ( isset( $post_array['form_data'] ) ) {
$post_array['form_data'] = stripslashes( $post_array['form_data'] );
}
$accepted_elements[] = 'sig';
return array_intersect_key( $post_array, array_flip( $accepted_elements ) );
}
Expand All @@ -286,6 +292,9 @@ function verify_signature( $data, $key ) {
if( empty( $data['sig'] ) ) {
return false;
}
if ( isset( $data['nonce'] ) ) {
unset( $data['nonce'] );
}
$temp = $data;
$computed_signature = $this->create_signature( $temp, $key );
return $computed_signature === $data['sig'];
Expand Down Expand Up @@ -314,6 +323,10 @@ function get_plugin_name( $plugin = false ) {
return $plugins[$plugin_basename]['Name'];
}

function get_class_props() {
return get_object_vars( $this );
}

// Get only the table beginning with our DB prefix or temporary prefix, also skip views
function get_tables( $scope = 'regular' ) {
global $wpdb;
Expand All @@ -334,7 +347,10 @@ function plugins_dir() {
}

function is_addon_outdated( $addon_basename ) {
$installed_version = $this->get_installed_version( $addon_basename );
$addon_slug = current( explode( '/', $addon_basename ) );
// If pre-1.1.2 version of Media Files addon, then it is outdated
if ( ! isset( $GLOBALS['wpsdb_meta'][$addon_slug]['version'] ) ) return true;
$installed_version = $GLOBALS['wpsdb_meta'][$addon_slug]['version'];
$required_version = $this->addons[$addon_basename]['required_version'];
return version_compare( $installed_version, $required_version, '<' );
}
Expand All @@ -343,4 +359,33 @@ function get_plugin_file_path() {
return $this->plugin_file_path;
}

function set_cli_migration() {
$this->doing_cli_migration = true;
}

function end_ajax( $return = false ) {
if( defined( 'DOING_WPSDB_TESTS' ) || $this->doing_cli_migration ) {
return ( false === $return ) ? NULL : $return;
}

echo ( false === $return ) ? '' : $return;
exit;
}

function check_ajax_referer( $action ) {
if ( defined( 'DOING_WPSDB_TESTS' ) || $this->doing_cli_migration ) return;
$result = check_ajax_referer( $action, 'nonce', false );
if ( false === $result ) {
$return = array( 'wpsdb_error' => 1, 'body' => sprintf( __( 'Invalid nonce for: %s', 'wp-sync-db' ), $action ) );
$this->end_ajax( json_encode( $return ) );
}

$cap = ( is_multisite() ) ? 'manage_network_options' : 'export';
$cap = apply_filters( 'wpsdb_ajax_cap', $cap );
if ( !current_user_can( $cap ) ) {
$return = array( 'wpsdb_error' => 1, 'body' => sprintf( __( 'Access denied for: %s', 'wp-sync-db' ), $action ) );
$this->end_ajax( json_encode( $return ) );
}
}

}

0 comments on commit 927e82a

Please sign in to comment.