Skip to content

Commit

Permalink
Merge pull request #80 from oceanwp/rc-2.2.1
Browse files Browse the repository at this point in the history
Rc 2.2.1
  • Loading branch information
eramits committed Oct 18, 2023
2 parents 927afdb + ce70d8b commit d534ffb
Show file tree
Hide file tree
Showing 10 changed files with 177 additions and 86 deletions.
7 changes: 7 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
== Changelog ==

= 2.2.1 - OCT 18 2023 =
- Fixed: Facebook Widget: PHP Notice: Undefined index: like_args, stream and force_wall.
- Fixed: Twitter Widget: PHP Notice: Undefined index: username.
- Fixed: Mailchimp Widget: Updated API.
- Fixed: LearnDash: Compatibility with new sidebar metabox.
- Fixed: LifterLMS: Compatibility with new sidebar metabox.

= 2.2.0 - SEP 6 2023 =
- NEW: Metabox Settings Interface: https://docs.oceanwp.org/category/826-oceanwp-settings
- Added: Compatibility: PHP8.2+: Menu icons.
Expand Down
105 changes: 81 additions & 24 deletions includes/client-migration/class-fs-client-migration-abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
exit;
}

if ( ! defined( 'FS_SDK__SSLVERIFY' ) ) {
define( 'FS_SDK__SSLVERIFY', false );
}

if ( class_exists( 'FS_Client_Migration_Abstract_v2' ) ) {
return;
}
Expand Down Expand Up @@ -213,9 +217,9 @@ public static function _debug_page_render() { ?>
$transient_key = "fsm_{$instance->_namespace}_{$instance->_product_id}";
$migration_uid = $instance->get_transient( $transient_key );

$last_migration_timestamp = get_transient( "fs_license_migration_{$instance->_product_id}_timestamp" );
$last_migration_response = get_transient( "fs_license_migration_{$instance->_product_id}_last_response" );
$last_migration_response_body = get_transient( "fs_license_migration_{$instance->_product_id}_last_response_body" );
$last_migration_timestamp = $instance->get_transient( "fs_license_migration_{$instance->_product_id}_timestamp" );
$last_migration_response = $instance->get_transient( "fs_license_migration_{$instance->_product_id}_last_response" );
$last_migration_response_body = $instance->get_transient( "fs_license_migration_{$instance->_product_id}_last_response_body" );

// force migration

Expand Down Expand Up @@ -305,6 +309,16 @@ public static function _debug_page_render() { ?>
'val' => ( ! empty( $last_migration_response_body ) ? $last_migration_response_body : '' ),
),
);

$last_migration_error = $instance->get_last_migration_error();

if ( ! empty( $last_migration_error ) ) {
$props[] = array(
'key' => 'Last Migration Error',
'val' => $last_migration_error,
'color' => 'red',
);
}
?>
<div style="float: left; padding: 5px 10px 5px 10px;">
<table class="widefat">
Expand Down Expand Up @@ -386,12 +400,12 @@ protected function do_license_migration( $redirect = false, $flush = false ) {
$all_licenses = $result['licenses'];

$transient_key = 'fs_license_migration_' . $this->_product_id . '_' . md5( implode( '', $all_licenses ) );
$response = $flush ? false : get_transient( $transient_key );
$response = $flush ? false : $this->get_transient_mixed( $transient_key );

if ( false !== $response ) {
$this->_logger->info( 'Response already cached and fetched directly from the 15 min transient.');
} else {
set_transient( "fs_license_migration_{$this->_product_id}_timestamp", WP_FS__SCRIPT_START_TIME, WP_FS__TIME_24_HOURS_IN_SEC * 30 );
$this->set_transient( "fs_license_migration_{$this->_product_id}_timestamp", WP_FS__SCRIPT_START_TIME, WP_FS__TIME_24_HOURS_IN_SEC * 30 );

$endpoint_url = $this->get_migration_endpoint();

Expand All @@ -401,7 +415,7 @@ protected function do_license_migration( $redirect = false, $flush = false ) {
$endpoint_url,
array(
'timeout' => 60,
'sslverify' => false,
'sslverify' => FS_SDK__SSLVERIFY,
'body' => json_encode( $migration_data ),
)
);
Expand All @@ -410,26 +424,35 @@ protected function do_license_migration( $redirect = false, $flush = false ) {
$this->set_transient( $transient_key, $response, 15 * MINUTE_IN_SECONDS );
}

set_transient( "fs_license_migration_{$this->_product_id}_last_response", $response, WP_FS__TIME_24_HOURS_IN_SEC * 30 );

$should_migrate_transient = $this->get_should_migrate_transient_key();

// make sure the response came back okay
if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
$error_message = $response->get_error_message();
$is_response_wp_error = is_wp_error( $response );

delete_transient( "fs_license_migration_{$this->_product_id}_last_response_body" );
// make sure the response came back okay
if ( $is_response_wp_error || 200 !== wp_remote_retrieve_response_code( $response ) ) {
if ( $is_response_wp_error ) {
$error_message = $response->get_error_message();
} else if (
( is_array( $response['headers'] ) || $response['headers'] instanceof ArrayAccess ) &&
! empty( $response['headers']['server'] ) &&
( 'cloudflare' === $response['headers']['server'] ) &&
! empty( $response['headers']['cf-mitigated'] ) &&
! empty( $response['headers']['cf-ray'] )
) {
$error_message = __( sprintf( "Blocked by Cloudflare (Ray ID: %s).", $response['headers']['cf-ray'] ) );
} else {
$error_message = __( 'An error has occurred, please try again.' );
}

$this->_logger->error( $error_message );
$this->delete_transient( "fs_license_migration_{$this->_product_id}_last_response_body" );

return ( is_wp_error( $response ) && ! empty( $error_message ) ) ?
$error_message :
__( 'An error occurred, please try again.' );
$this->store_last_migration_error( $error_message );

return $error_message;
} else {
$response_body = wp_remote_retrieve_body( $response );

set_transient( "fs_license_migration_{$this->_product_id}_last_response_body", $response_body, WP_FS__TIME_24_HOURS_IN_SEC * 30 );
$this->set_transient( "fs_license_migration_{$this->_product_id}_last_response_body", $response_body, WP_FS__TIME_24_HOURS_IN_SEC * 30 );

$response = json_decode( $response_body );

Expand All @@ -438,7 +461,7 @@ protected function do_license_migration( $redirect = false, $flush = false ) {
true !== $response->success
) {
if ( isset( $response->error ) ) {
$this->_logger->error( $response->error->code . ': ' . $response->error->message );
$error_message = __( sprintf( "A migration error has occurred: %s (code: %s).", $response->error->message, $response->error->code ) );

switch ( $response->error->code ) {
case 'empty_license_key':
Expand All @@ -453,13 +476,19 @@ protected function do_license_migration( $redirect = false, $flush = false ) {
}
} else {
// Unexpected error.
$this->_logger->error( 'Unexpected migration error.' );
$error_message = __( 'Unexpected migration error.' );
}

$this->_logger->error( $error_message );

$this->store_last_migration_error( $error_message );

// Failed to pull account information.
return false;
return $error_message;
}

$this->delete_last_migration_error();

// Delete transient on successful migration.
$this->delete_transient_mixed( $transient_key );

Expand All @@ -482,6 +511,11 @@ protected function do_license_migration( $redirect = false, $flush = false ) {
) {
$this->_logger->info( 'Activating bundle license after migration.' );

// Activate the license for the core product if it has a paid plan.
if ( $this->_fs->has_paid_plan() ) {
$this->_fs->activate_migrated_license( $response->data->license_key );
}

$this->_license_accessor->activate_bundle_license_after_migration(
$fs_user,
( self::TYPE_BUNDLE_TO_BUNDLE === $this->_migraiton_type ) ?
Expand Down Expand Up @@ -558,6 +592,26 @@ protected function do_license_migration( $redirect = false, $flush = false ) {
}
}

private function get_migration_error_transient_option_name() {
return "fs_license_migration_{$this->_product_id}_last_error";
}

private function store_last_migration_error( $error_message ) {
$this->set_transient( $this->get_migration_error_transient_option_name(), $error_message, WP_FS__TIME_24_HOURS_IN_SEC * 30 );
}

function get_last_migration_error() {
$error = $this->get_transient( $this->get_migration_error_transient_option_name() );

return ( ! empty( $error ) ) ?
$error :
'';
}

private function delete_last_migration_error() {
$this->delete_transient( $this->get_migration_error_transient_option_name() );
}

/**
* Initiate a non-blocking HTTP POST request to the same URL
* as the current page, with the addition of "fsm_{namespace}_{product_id}"
Expand Down Expand Up @@ -596,6 +650,10 @@ protected function spawn_license_migration() {

#endregion

if ( method_exists( $this->_fs, 'starting_migration' ) ) {
$this->_fs->starting_migration();
}

$migration_url = add_query_arg(
"fsm_{$this->_namespace}_{$this->_product_id}",
$migration_uid,
Expand All @@ -616,7 +674,7 @@ protected function spawn_license_migration() {
array(
'timeout' => 0.01,
'blocking' => false,
'sslverify' => false,
'sslverify' => FS_SDK__SSLVERIFY,
'cookies' => $cookies,
)
);
Expand Down Expand Up @@ -662,7 +720,7 @@ protected function non_blocking_license_migration( $is_blocking = false ) {
) {
$success = $this->do_license_migration();

if ( $success ) {
if ( true === $success ) {
$this->_fs->set_plugin_upgrade_complete();

return 'success';
Expand Down Expand Up @@ -707,7 +765,6 @@ protected function can_start_migration( $ignore_prev_version = false ) {
// Plugin isn't in Freemius activation mode.
return 'not_in_activation';
}

if ( ! $this->_fs->is_plugin_upgrade_mode() ) {
// Plugin isn't in plugin upgrade mode.
return 'not_in_upgrade';
Expand Down Expand Up @@ -774,7 +831,7 @@ public function try_migrate_on_activation( $response, $args ) {
$all_licenses = $result['licenses'];

$transient_key = 'fs_license_migration_' . $this->_product_id . '_' . md5( implode( '', $all_licenses ) );
$response = $this->get_transient_mixed( $transient_key );
$response = $this->get_transient( $transient_key );

$response->error->message = 'Migration error: ' . var_export( $response, true );
}
Expand Down
Loading

0 comments on commit d534ffb

Please sign in to comment.