| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Updater |
| 4 |
* |
| 5 |
* Handles version updates and migrations. |
| 6 |
* |
| 7 |
* @package CTC |
| 8 |
* @since 5.0.0 |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace CTC; |
| 12 |
|
| 13 |
/** |
| 14 |
* Updater Class |
| 15 |
* |
| 16 |
* Manages plugin version updates and data migrations. |
| 17 |
*/ |
| 18 |
class Updater { |
| 19 |
|
| 20 |
/** |
| 21 |
* Instance |
| 22 |
* |
| 23 |
* @var Updater|null |
| 24 |
*/ |
| 25 |
private static $instance = null; |
| 26 |
|
| 27 |
/** |
| 28 |
* Option key for stored version. |
| 29 |
* |
| 30 |
* @var string |
| 31 |
*/ |
| 32 |
const VERSION_OPTION = 'ctc_version'; |
| 33 |
|
| 34 |
/** |
| 35 |
* Get instance. |
| 36 |
* |
| 37 |
* @return Updater |
| 38 |
*/ |
| 39 |
public static function get() { |
| 40 |
if ( null === self::$instance ) { |
| 41 |
self::$instance = new self(); |
| 42 |
} |
| 43 |
return self::$instance; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Constructor. |
| 48 |
*/ |
| 49 |
private function __construct() { |
| 50 |
add_action( 'admin_init', [ $this, 'check_version' ] ); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Check version and run updates if needed. |
| 55 |
*/ |
| 56 |
public function check_version() { |
| 57 |
$saved_version = get_option( self::VERSION_OPTION, '0.0.0' ); |
| 58 |
$current_version = CTC_VER; |
| 59 |
|
| 60 |
// If versions match, no update needed. |
| 61 |
if ( version_compare( $saved_version, $current_version, '=' ) ) { |
| 62 |
return; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Fires before plugin update runs. |
| 67 |
* |
| 68 |
* @since 5.0.0 |
| 69 |
* |
| 70 |
* @param string $saved_version Previously saved version. |
| 71 |
* @param string $current_version Current plugin version. |
| 72 |
*/ |
| 73 |
do_action( 'ctc/updater/before', $saved_version, $current_version ); |
| 74 |
|
| 75 |
// Run version-specific updates. |
| 76 |
$this->run_updates( $saved_version, $current_version ); |
| 77 |
|
| 78 |
// Update stored version. |
| 79 |
update_option( self::VERSION_OPTION, $current_version ); |
| 80 |
|
| 81 |
/** |
| 82 |
* Fires after plugin update completes. |
| 83 |
* |
| 84 |
* @since 5.0.0 |
| 85 |
* |
| 86 |
* @param string $saved_version Previously saved version. |
| 87 |
* @param string $current_version Current plugin version. |
| 88 |
*/ |
| 89 |
do_action( 'ctc/updater/after', $saved_version, $current_version ); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Run version-specific updates. |
| 94 |
* |
| 95 |
* @param string $from_version Version updating from. |
| 96 |
* @param string $to_version Version updating to. |
| 97 |
*/ |
| 98 |
private function run_updates( $from_version, $to_version ) { |
| 99 |
// Update to 5.0.0. |
| 100 |
if ( version_compare( $from_version, '5.0.0', '<' ) ) { |
| 101 |
$this->update_to_5_0_0(); |
| 102 |
} |
| 103 |
|
| 104 |
// Add future version updates here. |
| 105 |
// Example: |
| 106 |
// if ( version_compare( $from_version, '5.1.0', '<' ) ) { |
| 107 |
// $this->update_to_5_1_0(); |
| 108 |
// } |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Update to version 5.0.0. |
| 113 |
* |
| 114 |
* Migrates from legacy settings to new Global Injector. |
| 115 |
*/ |
| 116 |
private function update_to_5_0_0() { |
| 117 |
/** |
| 118 |
* Fires during 5.0.0 update. |
| 119 |
* |
| 120 |
* Use this hook to run custom migration logic. |
| 121 |
* |
| 122 |
* @since 5.0.0 |
| 123 |
*/ |
| 124 |
do_action( 'ctc/updater/5.0.0' ); |
| 125 |
|
| 126 |
// Migration logic for 5.0.0 can be added here. |
| 127 |
// For example: migrate legacy CPT settings to new format. |
| 128 |
} |
| 129 |
} |
| 130 |
|