| 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 |
use CTC\Telemetry; |
| 14 |
|
| 15 |
/** |
| 16 |
* Updater Class |
| 17 |
* |
| 18 |
* Manages plugin version updates and data migrations. |
| 19 |
*/ |
| 20 |
class Updater { |
| 21 |
|
| 22 |
/** |
| 23 |
* Instance |
| 24 |
* |
| 25 |
* @var Updater|null |
| 26 |
*/ |
| 27 |
private static $instance = null; |
| 28 |
|
| 29 |
/** |
| 30 |
* Option key for stored version. |
| 31 |
* |
| 32 |
* @var string |
| 33 |
*/ |
| 34 |
const VERSION_OPTION = 'ctc_version'; |
| 35 |
|
| 36 |
/** |
| 37 |
* Get instance. |
| 38 |
* |
| 39 |
* @return Updater |
| 40 |
*/ |
| 41 |
public static function get() { |
| 42 |
if ( null === self::$instance ) { |
| 43 |
self::$instance = new self(); |
| 44 |
} |
| 45 |
return self::$instance; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Constructor. |
| 50 |
*/ |
| 51 |
private function __construct() { |
| 52 |
add_action( 'admin_init', [ $this, 'check_version' ] ); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Check version and run updates if needed. |
| 57 |
*/ |
| 58 |
public function check_version() { |
| 59 |
$saved_version = get_option( self::VERSION_OPTION, '0.0.0' ); |
| 60 |
$current_version = CTC_VER; |
| 61 |
|
| 62 |
// If versions match, no update needed. |
| 63 |
if ( version_compare( $saved_version, $current_version, '=' ) ) { |
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Fires before plugin update runs. |
| 69 |
* |
| 70 |
* @since 5.0.0 |
| 71 |
* |
| 72 |
* @param string $saved_version Previously saved version. |
| 73 |
* @param string $current_version Current plugin version. |
| 74 |
*/ |
| 75 |
do_action( 'ctc/updater/before', $saved_version, $current_version ); |
| 76 |
|
| 77 |
// Run version-specific updates. |
| 78 |
$this->run_updates( $saved_version, $current_version ); |
| 79 |
|
| 80 |
// Create ctc_analytics when upgrading to 5.3.0+ or on first install. |
| 81 |
if ( version_compare( $saved_version, '5.3.0', '<' ) ) { |
| 82 |
\CTC\Analytics\Database::create_table(); |
| 83 |
} |
| 84 |
|
| 85 |
// Update stored version. |
| 86 |
update_option( self::VERSION_OPTION, $current_version ); |
| 87 |
|
| 88 |
/** |
| 89 |
* Fires after plugin update completes. |
| 90 |
* |
| 91 |
* @since 5.0.0 |
| 92 |
* |
| 93 |
* @param string $saved_version Previously saved version. |
| 94 |
* @param string $current_version Current plugin version. |
| 95 |
*/ |
| 96 |
do_action( 'ctc/updater/after', $saved_version, $current_version ); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Run version-specific updates. |
| 101 |
* |
| 102 |
* @param string $from_version Version updating from. |
| 103 |
* @param string $to_version Version updating to. |
| 104 |
*/ |
| 105 |
private function run_updates( $from_version, $to_version ) { |
| 106 |
// Update to 5.0.0. |
| 107 |
if ( version_compare( $from_version, '5.0.0', '<' ) ) { |
| 108 |
$this->update_to_5_0_0(); |
| 109 |
} |
| 110 |
|
| 111 |
// Update to 5.4.0: single analytics table ctc_analytics. |
| 112 |
if ( version_compare( $from_version, '5.4.0', '<' ) ) { |
| 113 |
$this->update_to_5_4_0(); |
| 114 |
} |
| 115 |
|
| 116 |
// Update to 5.4.1: migrate legacy copy_the_code_allow_tracking to ctc_telemetry_opt_in. |
| 117 |
if ( version_compare( $from_version, '5.4.1', '<' ) ) { |
| 118 |
$this->update_to_5_4_1(); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Migrate legacy tracking opt-in to new telemetry opt-in (5.4.1). |
| 124 |
* |
| 125 |
* Users who had "Allow tracking" set to yes in v4.x (copy_the_code_allow_tracking) |
| 126 |
* are treated as opted in for ctc_telemetry_opt_in so we don't ask again. |
| 127 |
* |
| 128 |
* @since 5.4.1 |
| 129 |
*/ |
| 130 |
private function update_to_5_4_1() { |
| 131 |
$legacy_allow = get_option( 'copy_the_code_allow_tracking', 'no' ); |
| 132 |
if ( 'yes' === $legacy_allow ) { |
| 133 |
Telemetry::set_opt_in( true ); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Migrate to single analytics table ctc_analytics. |
| 139 |
* If ctc_copy_events exists, rename to ctc_analytics; otherwise create ctc_analytics. |
| 140 |
* |
| 141 |
* @since 5.4.0 |
| 142 |
* @return void |
| 143 |
*/ |
| 144 |
private function update_to_5_4_0() { |
| 145 |
\CTC\Analytics\Database::create_table(); |
| 146 |
|
| 147 |
// Ensure daily cleanup of old analytics events is scheduled after upgrading to 5.4.0+. |
| 148 |
if ( ! wp_next_scheduled( 'ctc_analytics_cleanup' ) ) { |
| 149 |
wp_schedule_event( time() + DAY_IN_SECONDS, 'daily', 'ctc_analytics_cleanup' ); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Update to version 5.0.0. |
| 155 |
* |
| 156 |
* Migrates from legacy settings to new Global Injector. |
| 157 |
*/ |
| 158 |
private function update_to_5_0_0() { |
| 159 |
/** |
| 160 |
* Fires during 5.0.0 update. |
| 161 |
* |
| 162 |
* Use this hook to run custom migration logic. |
| 163 |
* |
| 164 |
* @since 5.0.0 |
| 165 |
*/ |
| 166 |
do_action( 'ctc/updater/5.0.0' ); |
| 167 |
|
| 168 |
// Migration logic for 5.0.0 can be added here. |
| 169 |
// For example: migrate legacy CPT settings to new format. |
| 170 |
} |
| 171 |
} |
| 172 |
|