Skip to content

Instantly share code, notes, and snippets.

@doiftrue
Created October 26, 2025 02:39
Show Gist options
  • Select an option

  • Save doiftrue/1d04138c2c4240b79a4735f0ab8d970f to your computer and use it in GitHub Desktop.

Select an option

Save doiftrue/1d04138c2c4240b79a4735f0ab8d970f to your computer and use it in GitHub Desktop.
Simple Upgrader Example for WP Plugin
<?php
namespace KamaClickCounter;
class Upgrader {
public const OPTION_NAME = 'kcc_version';
private string $db_ver;
private string $curr_ver;
public function __construct( string $start_from_ver = '' ) {
$this->db_ver = $start_from_ver ?: get_option( self::OPTION_NAME, '1.0' );
$this->curr_ver = plugin()->ver;
}
public function is_run_upgrade(): bool {
return $this->db_ver !== $this->curr_ver;
}
public function run_upgrade(): void {
$result = $this->run_methods( new Upgrader_Methods() );
/** @noinspection ForgottenDebugOutputInspection */
error_log( 'Kama-Click-Counter upgrade result log: ' . print_r( $result, true ) ); // TODO: better logging
update_option( self::OPTION_NAME, $this->curr_ver );
}
// TODO: wrtie unit tests
private function run_methods( Upgrader_Methods_Abstract $methods_container ): array {
$result = [];
$to_run = [];
$method_names = get_class_methods( $methods_container );
foreach( $method_names as $method_name ) {
if( preg_match( '~^v\d+~', $method_name ) ){
$to_run[ $method_name ] = strtr( $method_name, [ 'v' => '', '_' => '.' ] ); // v3_6_2 -> 3.6.2
}
}
uksort( $to_run, static fn( $a, $b ) => version_compare( $a, $b ) ); // ASC
foreach( $to_run as $method => $version ){
// process only versions greater than current db version
if( ! version_compare( $version, $this->db_ver, '>' ) ){
continue;
}
/**
* @see Upgrader_Methods::v3_6_2()
* @see Upgrader_Methods::v4_1_0()
*/
$methods_container->$method( $result );
}
return $result;
}
}
<?php
/**
* In this class you need create methods with names like vx_y_x(), that indicates
* the version for which this method will be executed during the upgrade process.
*/
namespace KamaClickCounter;
class Upgrader_Methods extends Upgrader_Methods_Abstract {
public function v4_1_0( array & $res ): void {
// Add new db columns
global $wpdb;
$exists = $wpdb->get_var( "SHOW COLUMNS FROM $wpdb->kcc_clicks LIKE 'clicks_in_month'" );
if( ! $exists ){
$up = $wpdb->query( "ALTER TABLE $wpdb->kcc_clicks
ADD COLUMN clicks_in_month bigint(20) UNSIGNED NOT NULL default 0 COMMENT 'Current month clicks count' AFTER link_clicks,
ADD COLUMN clicks_prev_month bigint(20) UNSIGNED NOT NULL default 0 COMMENT 'Previous month clicks count' AFTER clicks_in_month,
ADD COLUMN clicks_history text NOT NULL AFTER clicks_prev_month,
ADD KEY clicks_in_month (clicks_in_month)"
);
$up && $res['db_columns'] = 'New columns added to db table';
}
}
public function v3_6_2( array & $res ): void {
global $wpdb;
$wpdb->query( "UPDATE $wpdb->kcc_clicks SET link_url = REPLACE(link_url, 'http://', '//')" );
}
}
<?php
namespace KamaClickCounter;
abstract class Upgrader_Methods_Abstract {
/** @var object[] */
protected array $db_fields = [];
public function __construct() {
$this->set_db_fields();
}
private function set_db_fields(): void {
global $wpdb;
$this->db_fields = $wpdb->get_results( "SHOW COLUMNS FROM $wpdb->kcc_clicks" );
// field name to index
foreach( $this->db_fields as $k => $data ){
$this->db_fields[ $data->Field ] = $data;
unset( $this->db_fields[ $k ] );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment