| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* One-time migration of legacy WpStream storage names. |
| 5 |
* |
| 6 |
* @package Wpstream |
| 7 |
*/ |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* Moves plugin-owned state behind the wpstream_ namespace. |
| 15 |
*/ |
| 16 |
final class Wpstream_Storage_Migration { |
| 17 |
|
| 18 |
/** Migration marker option. */ |
| 19 |
const MIGRATION_OPTION = 'wpstream_storage_names_migration_31'; |
| 20 |
|
| 21 |
/** |
| 22 |
* Migrate legacy state once per site. |
| 23 |
* |
| 24 |
* @return void |
| 25 |
*/ |
| 26 |
public static function maybe_migrate() { |
| 27 |
if ( get_option( self::MIGRATION_OPTION, false ) ) { |
| 28 |
return; |
| 29 |
} |
| 30 |
|
| 31 |
$current_token_migrated = self::migrate_option( 'wp_estate_curent_token', 'wpstream_current_token' ); |
| 32 |
$token_expiry_migrated = self::migrate_option( 'wp_estate_token_expire', 'wpstream_token_expire' ); |
| 33 |
$transients_migrated = self::migrate_transients(); |
| 34 |
$user_meta_migrated = self::migrate_buddyboss_user_meta(); |
| 35 |
|
| 36 |
if ( $current_token_migrated && $token_expiry_migrated && $transients_migrated && $user_meta_migrated ) { |
| 37 |
update_option( self::MIGRATION_OPTION, 1, false ); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Move the misspelled BuddyBoss selection key for every affected user. |
| 43 |
* |
| 44 |
* @return bool Whether every affected user was migrated safely. |
| 45 |
*/ |
| 46 |
private static function migrate_buddyboss_user_meta() { |
| 47 |
global $wpdb; |
| 48 |
|
| 49 |
$legacy_name = 'budyboss_selected_channel'; |
| 50 |
$new_name = 'wpstream_buddyboss_selected_channel'; |
| 51 |
$user_ids = $wpdb->get_col( |
| 52 |
$wpdb->prepare( |
| 53 |
"SELECT DISTINCT user_id FROM {$wpdb->usermeta} WHERE meta_key = %s", |
| 54 |
$legacy_name |
| 55 |
) |
| 56 |
); |
| 57 |
if ( null === $user_ids ) { |
| 58 |
return false; |
| 59 |
} |
| 60 |
|
| 61 |
$success = true; |
| 62 |
foreach ( $user_ids as $user_id ) { |
| 63 |
$user_id = (int) $user_id; |
| 64 |
if ( ! metadata_exists( 'user', $user_id, $new_name ) ) { |
| 65 |
if ( false === update_user_meta( $user_id, $new_name, get_user_meta( $user_id, $legacy_name, true ) ) ) { |
| 66 |
$success = false; |
| 67 |
continue; |
| 68 |
} |
| 69 |
} |
| 70 |
delete_user_meta( $user_id, $legacy_name ); |
| 71 |
if ( metadata_exists( 'user', $user_id, $legacy_name ) ) { |
| 72 |
$success = false; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
return $success; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Move dynamic transient families while retaining their remaining lifetime. |
| 81 |
* |
| 82 |
* @return bool Whether every transient was copied or safely discarded. |
| 83 |
*/ |
| 84 |
private static function migrate_transients() { |
| 85 |
global $wpdb; |
| 86 |
$success = true; |
| 87 |
|
| 88 |
$prefixes = array( |
| 89 |
'free_event_streamName_' => 'wpstream_free_event_streamName_', |
| 90 |
'paid_event_streamName_' => 'wpstream_paid_event_streamName_', |
| 91 |
'vod_decryption_key_index_' => 'wpstream_vod_decryption_key_index_', |
| 92 |
'paid_vod_key_index_' => 'wpstream_paid_vod_key_index_', |
| 93 |
); |
| 94 |
|
| 95 |
foreach ( $prefixes as $legacy_prefix => $new_prefix ) { |
| 96 |
$legacy_option_prefix = '_transient_' . $legacy_prefix; |
| 97 |
$legacy_option_names = $wpdb->get_col( |
| 98 |
$wpdb->prepare( |
| 99 |
"SELECT option_name FROM {$wpdb->options} WHERE option_name LIKE %s", |
| 100 |
$wpdb->esc_like( $legacy_option_prefix ) . '%' |
| 101 |
) |
| 102 |
); |
| 103 |
|
| 104 |
if ( null === $legacy_option_names ) { |
| 105 |
$success = false; |
| 106 |
continue; |
| 107 |
} |
| 108 |
|
| 109 |
foreach ( $legacy_option_names as $legacy_option_name ) { |
| 110 |
$suffix = substr( $legacy_option_name, strlen( $legacy_option_prefix ) ); |
| 111 |
$legacy_transient = $legacy_prefix . $suffix; |
| 112 |
$new_transient = $new_prefix . $suffix; |
| 113 |
$missing = new stdClass(); |
| 114 |
$legacy_value = get_option( $legacy_option_name, $missing ); |
| 115 |
|
| 116 |
if ( $missing === $legacy_value ) { |
| 117 |
continue; |
| 118 |
} |
| 119 |
|
| 120 |
$new_option_name = '_transient_' . $new_transient; |
| 121 |
if ( $missing === get_option( $new_option_name, $missing ) ) { |
| 122 |
$timeout = (int) get_option( '_transient_timeout_' . $legacy_transient, 0 ); |
| 123 |
$expiration = $timeout > 0 ? $timeout - time() : 0; |
| 124 |
|
| 125 |
if ( 0 === $timeout || $expiration > 0 ) { |
| 126 |
if ( ! set_transient( $new_transient, $legacy_value, max( 0, $expiration ) ) ) { |
| 127 |
$success = false; |
| 128 |
continue; |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
delete_transient( $legacy_transient ); |
| 134 |
$legacy_still_present = new stdClass(); |
| 135 |
if ( $legacy_still_present !== get_option( $legacy_option_name, $legacy_still_present ) ) { |
| 136 |
$success = false; |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
return $success; |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Copy a legacy option only when the destination is absent, then clean up. |
| 146 |
* |
| 147 |
* @param string $legacy_name Legacy option name. |
| 148 |
* @param string $new_name Namespaced option name. |
| 149 |
* @return bool Whether the legacy value is absent or was migrated safely. |
| 150 |
*/ |
| 151 |
private static function migrate_option( $legacy_name, $new_name ) { |
| 152 |
$missing = new stdClass(); |
| 153 |
$legacy_value = get_option( $legacy_name, $missing ); |
| 154 |
|
| 155 |
if ( $missing === $legacy_value ) { |
| 156 |
return true; |
| 157 |
} |
| 158 |
|
| 159 |
if ( $missing === get_option( $new_name, $missing ) ) { |
| 160 |
if ( ! update_option( $new_name, $legacy_value, false ) ) { |
| 161 |
return false; |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
delete_option( $legacy_name ); |
| 166 |
return $missing === get_option( $legacy_name, $missing ); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
add_action( 'plugins_loaded', array( 'Wpstream_Storage_Migration', 'maybe_migrate' ), 5 ); |
| 171 |
|