# copy-the-code/5.5.3/includes/class-updater.php

Copy Anything to Clipboard for WordPress – Copy Button, Copy Text &amp; Copy Code, version 5.5.3. 172 lines.

- Page: https://pluginprobe.com/plugins/copy-the-code/5.5.3/code/includes/class-updater.php
- Raw: https://pluginprobe.com/plugins/copy-the-code/5.5.3/raw/includes/class-updater.php
- Modified: 2026-03-08T21:17:42+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/copy-the-code/5.5.3/code/includes/class-updater.php#L10-L20`.

```php
<?php
/**
 * Plugin Updater
 *
 * Handles version updates and migrations.
 *
 * @package CTC
 * @since 5.0.0
 */

namespace CTC;

use CTC\Telemetry;

/**
 * Updater Class
 *
 * Manages plugin version updates and data migrations.
 */
class Updater {

	/**
	 * Instance
	 *
	 * @var Updater|null
	 */
	private static $instance = null;

	/**
	 * Option key for stored version.
	 *
	 * @var string
	 */
	const VERSION_OPTION = 'ctc_version';

	/**
	 * Get instance.
	 *
	 * @return Updater
	 */
	public static function get() {
		if ( null === self::$instance ) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	/**
	 * Constructor.
	 */
	private function __construct() {
		add_action( 'admin_init', [ $this, 'check_version' ] );
	}

	/**
	 * Check version and run updates if needed.
	 */
	public function check_version() {
		$saved_version   = get_option( self::VERSION_OPTION, '0.0.0' );
		$current_version = CTC_VER;

		// If versions match, no update needed.
		if ( version_compare( $saved_version, $current_version, '=' ) ) {
			return;
		}

		/**
		 * Fires before plugin update runs.
		 *
		 * @since 5.0.0
		 *
		 * @param string $saved_version   Previously saved version.
		 * @param string $current_version Current plugin version.
		 */
		do_action( 'ctc/updater/before', $saved_version, $current_version );

		// Run version-specific updates.
		$this->run_updates( $saved_version, $current_version );

		// Create ctc_analytics when upgrading to 5.3.0+ or on first install.
		if ( version_compare( $saved_version, '5.3.0', '<' ) ) {
			\CTC\Analytics\Database::create_table();
		}

		// Update stored version.
		update_option( self::VERSION_OPTION, $current_version );

		/**
		 * Fires after plugin update completes.
		 *
		 * @since 5.0.0
		 *
		 * @param string $saved_version   Previously saved version.
		 * @param string $current_version Current plugin version.
		 */
		do_action( 'ctc/updater/after', $saved_version, $current_version );
	}

	/**
	 * Run version-specific updates.
	 *
	 * @param string $from_version Version updating from.
	 * @param string $to_version   Version updating to.
	 */
	private function run_updates( $from_version, $to_version ) {
		// Update to 5.0.0.
		if ( version_compare( $from_version, '5.0.0', '<' ) ) {
			$this->update_to_5_0_0();
		}

		// Update to 5.4.0: single analytics table ctc_analytics.
		if ( version_compare( $from_version, '5.4.0', '<' ) ) {
			$this->update_to_5_4_0();
		}

		// Update to 5.4.1: migrate legacy copy_the_code_allow_tracking to ctc_telemetry_opt_in.
		if ( version_compare( $from_version, '5.4.1', '<' ) ) {
			$this->update_to_5_4_1();
		}
	}

	/**
	 * Migrate legacy tracking opt-in to new telemetry opt-in (5.4.1).
	 *
	 * Users who had "Allow tracking" set to yes in v4.x (copy_the_code_allow_tracking)
	 * are treated as opted in for ctc_telemetry_opt_in so we don't ask again.
	 *
	 * @since 5.4.1
	 */
	private function update_to_5_4_1() {
		$legacy_allow = get_option( 'copy_the_code_allow_tracking', 'no' );
		if ( 'yes' === $legacy_allow ) {
			Telemetry::set_opt_in( true );
		}
	}

	/**
	 * Migrate to single analytics table ctc_analytics.
	 * If ctc_copy_events exists, rename to ctc_analytics; otherwise create ctc_analytics.
	 *
	 * @since 5.4.0
	 * @return void
	 */
	private function update_to_5_4_0() {
		\CTC\Analytics\Database::create_table();

		// Ensure daily cleanup of old analytics events is scheduled after upgrading to 5.4.0+.
		if ( ! wp_next_scheduled( 'ctc_analytics_cleanup' ) ) {
			wp_schedule_event( time() + DAY_IN_SECONDS, 'daily', 'ctc_analytics_cleanup' );
		}
	}

	/**
	 * Update to version 5.0.0.
	 *
	 * Migrates from legacy settings to new Global Injector.
	 */
	private function update_to_5_0_0() {
		/**
		 * Fires during 5.0.0 update.
		 *
		 * Use this hook to run custom migration logic.
		 *
		 * @since 5.0.0
		 */
		do_action( 'ctc/updater/5.0.0' );

		// Migration logic for 5.0.0 can be added here.
		// For example: migrate legacy CPT settings to new format.
	}
}

```
