| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class-based version switching functionality for the Code Snippets plugin. |
| 4 |
* |
| 5 |
* Converted from procedural `version-switch.php` to an OO class `Version_Switch`. |
| 6 |
* |
| 7 |
* @package Code_Snippets |
| 8 |
* @subpackage Settings |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace Code_Snippets\Settings; |
| 12 |
|
| 13 |
// Configuration constants for version switching |
| 14 |
const VERSION_CACHE_KEY = 'code_snippets_available_versions'; |
| 15 |
const PROGRESS_KEY = 'code_snippets_version_switch_progress'; |
| 16 |
const VERSION_CACHE_DURATION = HOUR_IN_SECONDS; |
| 17 |
const PROGRESS_TIMEOUT = 5 * MINUTE_IN_SECONDS; |
| 18 |
const WORDPRESS_API_ENDPOINT = 'https://api.wordpress.org/plugins/info/1.2/?action=plugin_information&slug=code-snippets'; |
| 19 |
|
| 20 |
class Version_Switch { |
| 21 |
/** |
| 22 |
* Initialize hook registrations. |
| 23 |
* Call this after the file is required. |
| 24 |
*/ |
| 25 |
public static function init(): void { |
| 26 |
add_action( 'wp_ajax_code_snippets_switch_version', [ __CLASS__, 'ajax_switch_version' ] ); |
| 27 |
add_action( 'wp_ajax_code_snippets_refresh_versions', [ __CLASS__, 'ajax_refresh_versions' ] ); |
| 28 |
} |
| 29 |
|
| 30 |
public static function get_available_versions(): array { |
| 31 |
$versions = get_transient( VERSION_CACHE_KEY ); |
| 32 |
|
| 33 |
if ( false === $versions ) { |
| 34 |
$response = wp_remote_get( WORDPRESS_API_ENDPOINT ); |
| 35 |
|
| 36 |
if ( is_wp_error( $response ) ) { |
| 37 |
return []; |
| 38 |
} |
| 39 |
|
| 40 |
$body = wp_remote_retrieve_body( $response ); |
| 41 |
$data = json_decode( $body, true ); |
| 42 |
|
| 43 |
if ( ! $data || ! isset( $data['versions'] ) ) { |
| 44 |
return []; |
| 45 |
} |
| 46 |
|
| 47 |
// Filter out 'trunk' and sort versions |
| 48 |
$versions = []; |
| 49 |
foreach ( $data['versions'] as $version => $download_url ) { |
| 50 |
if ( 'trunk' !== $version ) { |
| 51 |
$versions[] = [ |
| 52 |
'version' => $version, |
| 53 |
'url' => $download_url, |
| 54 |
]; |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
// Sort versions in descending order |
| 59 |
usort( $versions, function( $a, $b ) { |
| 60 |
return version_compare( $b['version'], $a['version'] ); |
| 61 |
}); |
| 62 |
|
| 63 |
// Cache for configured duration |
| 64 |
set_transient( VERSION_CACHE_KEY, $versions, VERSION_CACHE_DURATION ); |
| 65 |
} |
| 66 |
|
| 67 |
return $versions; |
| 68 |
} |
| 69 |
|
| 70 |
public static function get_current_version(): string { |
| 71 |
return defined( 'CODE_SNIPPETS_VERSION' ) ? CODE_SNIPPETS_VERSION : '0.0.0'; |
| 72 |
} |
| 73 |
|
| 74 |
public static function is_version_switch_in_progress(): bool { |
| 75 |
return get_transient( PROGRESS_KEY ) !== false; |
| 76 |
} |
| 77 |
|
| 78 |
public static function clear_version_caches(): void { |
| 79 |
delete_transient( VERSION_CACHE_KEY ); |
| 80 |
delete_transient( PROGRESS_KEY ); |
| 81 |
} |
| 82 |
|
| 83 |
public static function validate_target_version( string $target_version, array $available_versions ): array { |
| 84 |
if ( empty( $target_version ) ) { |
| 85 |
return [ |
| 86 |
'success' => false, |
| 87 |
'message' => __( 'No target version specified.', 'code-snippets' ), |
| 88 |
'download_url' => '', |
| 89 |
]; |
| 90 |
} |
| 91 |
|
| 92 |
foreach ( $available_versions as $version_info ) { |
| 93 |
if ( $version_info['version'] === $target_version ) { |
| 94 |
return [ |
| 95 |
'success' => true, |
| 96 |
'message' => '', |
| 97 |
'download_url' => $version_info['url'], |
| 98 |
]; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
return [ |
| 103 |
'success' => false, |
| 104 |
'message' => __( 'Invalid version specified.', 'code-snippets' ), |
| 105 |
'download_url' => '', |
| 106 |
]; |
| 107 |
} |
| 108 |
|
| 109 |
public static function create_error_response( string $message, string $technical_details = '' ): array { |
| 110 |
if ( ! empty( $technical_details ) ) { |
| 111 |
if ( function_exists( 'error_log' ) ) { |
| 112 |
error_log( sprintf( 'Code Snippets version switch error: %s. Details: %s', $message, $technical_details ) ); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
return [ |
| 117 |
'success' => false, |
| 118 |
'message' => $message, |
| 119 |
]; |
| 120 |
} |
| 121 |
|
| 122 |
public static function perform_version_install( string $download_url ) { |
| 123 |
if ( ! function_exists( 'wp_update_plugins' ) ) { |
| 124 |
require_once ABSPATH . 'wp-admin/includes/update.php'; |
| 125 |
} |
| 126 |
if ( ! function_exists( 'show_message' ) ) { |
| 127 |
require_once ABSPATH . 'wp-admin/includes/misc.php'; |
| 128 |
} |
| 129 |
if ( ! class_exists( 'Plugin_Upgrader' ) ) { |
| 130 |
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 131 |
} |
| 132 |
|
| 133 |
$update_handler = new \WP_Ajax_Upgrader_Skin(); |
| 134 |
$upgrader = new \Plugin_Upgrader( $update_handler ); |
| 135 |
|
| 136 |
global $code_snippets_last_update_handler, $code_snippets_last_upgrader; |
| 137 |
$code_snippets_last_update_handler = $update_handler; |
| 138 |
$code_snippets_last_upgrader = $upgrader; |
| 139 |
|
| 140 |
return $upgrader->install( $download_url, [ |
| 141 |
'overwrite_package' => true, |
| 142 |
'clear_update_cache' => true, |
| 143 |
] ); |
| 144 |
} |
| 145 |
|
| 146 |
public static function extract_handler_messages( $update_handler, $upgrader ): string { |
| 147 |
$handler_messages = ''; |
| 148 |
|
| 149 |
if ( isset( $update_handler ) ) { |
| 150 |
if ( method_exists( $update_handler, 'get_errors' ) ) { |
| 151 |
$errs = $update_handler->get_errors(); |
| 152 |
if ( $errs instanceof \WP_Error && $errs->has_errors() ) { |
| 153 |
$handler_messages .= implode( "\n", $errs->get_error_messages() ); |
| 154 |
} |
| 155 |
} |
| 156 |
if ( method_exists( $update_handler, 'get_error_messages' ) ) { |
| 157 |
$em = $update_handler->get_error_messages(); |
| 158 |
if ( $em ) { |
| 159 |
$handler_messages .= "\n" . $em; |
| 160 |
} |
| 161 |
} |
| 162 |
if ( method_exists( $update_handler, 'get_upgrade_messages' ) ) { |
| 163 |
$upgrade_msgs = $update_handler->get_upgrade_messages(); |
| 164 |
if ( is_array( $upgrade_msgs ) ) { |
| 165 |
$handler_messages .= "\n" . implode( "\n", $upgrade_msgs ); |
| 166 |
} elseif ( $upgrade_msgs ) { |
| 167 |
$handler_messages .= "\n" . (string) $upgrade_msgs; |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
if ( empty( $handler_messages ) && isset( $upgrader->result ) ) { |
| 173 |
if ( is_wp_error( $upgrader->result ) ) { |
| 174 |
$handler_messages = implode( "\n", $upgrader->result->get_error_messages() ); |
| 175 |
} else { |
| 176 |
$handler_messages = is_scalar( $upgrader->result ) ? (string) $upgrader->result : print_r( $upgrader->result, true ); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
return trim( $handler_messages ); |
| 181 |
} |
| 182 |
|
| 183 |
public static function log_version_switch_attempt( string $target_version, $result, string $details = '' ): void { |
| 184 |
if ( function_exists( 'error_log' ) ) { |
| 185 |
error_log( sprintf( 'Code Snippets version switch failed. target=%s, result=%s, details=%s', $target_version, var_export( $result, true ), $details ) ); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
public static function handle_installation_failure( string $target_version, string $download_url, $install_result ): array { |
| 190 |
global $code_snippets_last_update_handler, $code_snippets_last_upgrader; |
| 191 |
|
| 192 |
$handler_messages = self::extract_handler_messages( $code_snippets_last_update_handler, $code_snippets_last_upgrader ); |
| 193 |
self::log_version_switch_attempt( $target_version, $install_result, "URL: $download_url, Messages: $handler_messages" ); |
| 194 |
|
| 195 |
$fallback_message = __( 'Failed to switch versions. Please try again.', 'code-snippets' ); |
| 196 |
if ( ! empty( $handler_messages ) ) { |
| 197 |
$short = wp_trim_words( wp_strip_all_tags( $handler_messages ), 40, '...' ); |
| 198 |
$fallback_message = sprintf( '%s %s', $fallback_message, $short ); |
| 199 |
} |
| 200 |
|
| 201 |
return [ |
| 202 |
'success' => false, |
| 203 |
'message' => $fallback_message, |
| 204 |
]; |
| 205 |
} |
| 206 |
|
| 207 |
public static function handle_version_switch( string $target_version ): array { |
| 208 |
if ( ! current_user_can( 'update_plugins' ) ) { |
| 209 |
return self::create_error_response( __( 'You do not have permission to update plugins.', 'code-snippets' ) ); |
| 210 |
} |
| 211 |
|
| 212 |
$available_versions = self::get_available_versions(); |
| 213 |
$validation = self::validate_target_version( $target_version, $available_versions ); |
| 214 |
|
| 215 |
if ( ! $validation['success'] ) { |
| 216 |
return self::create_error_response( $validation['message'] ); |
| 217 |
} |
| 218 |
|
| 219 |
if ( self::get_current_version() === $target_version ) { |
| 220 |
return self::create_error_response( __( 'Already on the specified version.', 'code-snippets' ) ); |
| 221 |
} |
| 222 |
|
| 223 |
set_transient( PROGRESS_KEY, $target_version, PROGRESS_TIMEOUT ); |
| 224 |
|
| 225 |
$install_result = self::perform_version_install( $validation['download_url'] ); |
| 226 |
|
| 227 |
delete_transient( PROGRESS_KEY ); |
| 228 |
|
| 229 |
if ( is_wp_error( $install_result ) ) { |
| 230 |
return self::create_error_response( $install_result->get_error_message() ); |
| 231 |
} |
| 232 |
|
| 233 |
if ( $install_result ) { |
| 234 |
delete_transient( VERSION_CACHE_KEY ); |
| 235 |
|
| 236 |
return [ |
| 237 |
'success' => true, |
| 238 |
'message' => sprintf( __( 'Successfully switched to version %s. Please refresh the page to see changes.', 'code-snippets' ), $target_version ), |
| 239 |
]; |
| 240 |
} |
| 241 |
|
| 242 |
return self::handle_installation_failure( $target_version, $validation['download_url'], $install_result ); |
| 243 |
} |
| 244 |
|
| 245 |
public static function render_version_switch_field( array $args ): void { |
| 246 |
$current_version = self::get_current_version(); |
| 247 |
$available_versions = self::get_available_versions(); |
| 248 |
$is_switching = self::is_version_switch_in_progress(); |
| 249 |
|
| 250 |
?> |
| 251 |
<div class="code-snippets-version-switch"> |
| 252 |
<p> |
| 253 |
<strong><?php esc_html_e( 'Current Version:', 'code-snippets' ); ?></strong> |
| 254 |
<span class="current-version"><?php echo esc_html( $current_version ); ?></span> |
| 255 |
</p> |
| 256 |
|
| 257 |
<?php if ( $is_switching ) : ?> |
| 258 |
<div class="notice notice-info inline"> |
| 259 |
<p><?php esc_html_e( 'Version switch in progress. Please wait...', 'code-snippets' ); ?></p> |
| 260 |
</div> |
| 261 |
<?php else : ?> |
| 262 |
<p> |
| 263 |
<label for="target_version"> |
| 264 |
<?php esc_html_e( 'Switch to Version:', 'code-snippets' ); ?> |
| 265 |
</label> |
| 266 |
<select id="target_version" name="target_version" <?php disabled( empty( $available_versions ) ); ?>> |
| 267 |
<option value=""><?php esc_html_e( 'Select a version...', 'code-snippets' ); ?></option> |
| 268 |
<?php foreach ( $available_versions as $version_info ) : ?> |
| 269 |
<option value="<?php echo esc_attr( $version_info['version'] ); ?>" |
| 270 |
<?php selected( $version_info['version'], $current_version ); ?>> |
| 271 |
<?php echo esc_html( $version_info['version'] ); ?> |
| 272 |
<?php if ( $version_info['version'] === $current_version ) : ?> |
| 273 |
<?php esc_html_e( ' (Current)', 'code-snippets' ); ?> |
| 274 |
<?php endif; ?> |
| 275 |
</option> |
| 276 |
<?php endforeach; ?> |
| 277 |
</select> |
| 278 |
</p> |
| 279 |
|
| 280 |
<p> |
| 281 |
<button type="button" id="switch-version-btn" class="button button-secondary" disabled |
| 282 |
<?php disabled( empty( $available_versions ) ); ?>> |
| 283 |
<?php esc_html_e( 'Switch Version', 'code-snippets' ); ?> |
| 284 |
</button> |
| 285 |
</p> |
| 286 |
|
| 287 |
<div id="version-switch-result" class="notice" style="display: none;"></div> |
| 288 |
<?php endif; ?> |
| 289 |
</div><?php |
| 290 |
} |
| 291 |
|
| 292 |
public static function ajax_switch_version(): void { |
| 293 |
if ( ! wp_verify_nonce( $_POST['nonce'] ?? '', 'code_snippets_version_switch' ) ) { |
| 294 |
wp_die( __( 'Security check failed.', 'code-snippets' ) ); |
| 295 |
} |
| 296 |
|
| 297 |
if ( ! current_user_can( 'update_plugins' ) ) { |
| 298 |
wp_send_json_error( [ |
| 299 |
'message' => __( 'You do not have permission to update plugins.', 'code-snippets' ), |
| 300 |
] ); |
| 301 |
} |
| 302 |
|
| 303 |
$target_version = sanitize_text_field( $_POST['target_version'] ?? '' ); |
| 304 |
|
| 305 |
if ( empty( $target_version ) ) { |
| 306 |
wp_send_json_error( [ |
| 307 |
'message' => __( 'No target version specified.', 'code-snippets' ), |
| 308 |
] ); |
| 309 |
} |
| 310 |
|
| 311 |
$result = self::handle_version_switch( $target_version ); |
| 312 |
|
| 313 |
if ( $result['success'] ) { |
| 314 |
wp_send_json_success( $result ); |
| 315 |
} else { |
| 316 |
wp_send_json_error( $result ); |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
public static function render_refresh_versions_field( array $args ): void { |
| 321 |
?> |
| 322 |
<button type="button" id="refresh-versions-btn" class="button button-secondary"> |
| 323 |
<?php esc_html_e( 'Refresh Available Versions', 'code-snippets' ); ?> |
| 324 |
</button> |
| 325 |
<p class="description"> |
| 326 |
<?php esc_html_e( 'Check for the latest available plugin versions from WordPress.org.', 'code-snippets' ); ?> |
| 327 |
</p><?php |
| 328 |
} |
| 329 |
|
| 330 |
public static function ajax_refresh_versions(): void { |
| 331 |
if ( ! wp_verify_nonce( $_POST['nonce'] ?? '', 'code_snippets_refresh_versions' ) ) { |
| 332 |
wp_die( __( 'Security check failed.', 'code-snippets' ) ); |
| 333 |
} |
| 334 |
|
| 335 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 336 |
wp_send_json_error( [ |
| 337 |
'message' => __( 'You do not have permission to manage options.', 'code-snippets' ), |
| 338 |
] ); |
| 339 |
} |
| 340 |
|
| 341 |
delete_transient( VERSION_CACHE_KEY ); |
| 342 |
self::get_available_versions(); |
| 343 |
|
| 344 |
wp_send_json_success( [ |
| 345 |
'message' => __( 'Available versions updated successfully.', 'code-snippets' ), |
| 346 |
] ); |
| 347 |
} |
| 348 |
|
| 349 |
public static function render_version_switch_warning(): void { |
| 350 |
?> |
| 351 |
<div id="version-switch-warning" class="notice notice-warning" style="display: none; margin-block-start: 20px;"> |
| 352 |
<p> |
| 353 |
<strong><?php esc_html_e( 'Warning:', 'code-snippets' ); ?></strong> |
| 354 |
<?php esc_html_e( 'Switching versions may cause compatibility issues. Always backup your site before switching versions.', 'code-snippets' ); ?> |
| 355 |
</p> |
| 356 |
</div> |
| 357 |
<?php |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
// Initialize hooks when the file is loaded. |
| 362 |
Version_Switch::init(); |
| 363 |
|