| 1 |
<?php |
| 2 |
|
| 3 |
namespace Upress\EzCache; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use wpdb; |
| 7 |
|
| 8 |
class Updater { |
| 9 |
/** @var string $current_version */ |
| 10 |
protected static $current_version; |
| 11 |
/** @var string $collate */ |
| 12 |
protected static $collate; |
| 13 |
/** @var wpdb $wpdb */ |
| 14 |
protected static $wpdb; |
| 15 |
|
| 16 |
/** |
| 17 |
* Run any necessary db updates, file upgrades etc. |
| 18 |
*/ |
| 19 |
public static function upgrade() { |
| 20 |
global $wpdb; |
| 21 |
|
| 22 |
self::$wpdb = $wpdb; |
| 23 |
self::$current_version = get_option( 'ezcache_version' ); |
| 24 |
self::$collate = self::$wpdb->get_charset_collate(); |
| 25 |
|
| 26 |
/** @noinspection PhpIncludeInspection */ |
| 27 |
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); |
| 28 |
|
| 29 |
try { |
| 30 |
self::update_0_1_20190811(); |
| 31 |
self::update_0_1_20190812(); |
| 32 |
|
| 33 |
// make sure we update the version in the database so we can run upgrades at later times |
| 34 |
update_option( 'ezcache_version', EZCACHE_VERSION ); |
| 35 |
} catch ( Exception $ex ) { |
| 36 |
error_log( $ex ); |
| 37 |
wp_die( $ex->getMessage() ); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Update to the 1.0 version |
| 43 |
* create the 404 database |
| 44 |
* @throws Exception |
| 45 |
*/ |
| 46 |
protected static function update_0_1_20190811() { |
| 47 |
$ver = '0.1-20190811'; |
| 48 |
if ( self::$current_version && version_compare( self::$current_version, $ver, '>=' ) ) { |
| 49 |
return; |
| 50 |
} |
| 51 |
|
| 52 |
$prefix = self::$wpdb->prefix; |
| 53 |
self::$wpdb->query( "CREATE TABLE IF NOT EXISTS `{$prefix}ezcache_webp_images` ( |
| 54 |
`id` bigint(10) UNSIGNED NOT NULL AUTO_INCREMENT, |
| 55 |
`uid` varchar(191) NOT NULL DEFAULT '', |
| 56 |
`url` text(0) NOT NULL DEFAULT '', |
| 57 |
`webp_url` text(0) NOT NULL DEFAULT '', |
| 58 |
`status` enum('pending', 'completed', 'failed') NOT NULL DEFAULT 'pending', |
| 59 |
`created_at` datetime(0) NOT NULL DEFAULT '0000-00-00 00:00:00', |
| 60 |
`updated_at` datetime(0) NOT NULL DEFAULT '0000-00-00 00:00:00', |
| 61 |
PRIMARY KEY (`id`), |
| 62 |
UNIQUE INDEX (`uid`) USING BTREE |
| 63 |
) ". ( self::$collate ) ); |
| 64 |
|
| 65 |
if ( ! empty( self::$wpdb->last_error ) ) { |
| 66 |
throw new Exception( self::$wpdb->last_error ); |
| 67 |
} |
| 68 |
|
| 69 |
self::$current_version = $ver; |
| 70 |
update_option( 'ezcache_version', self::$current_version ); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Update to the 1.0 version |
| 75 |
* create the 404 database |
| 76 |
* @throws Exception |
| 77 |
*/ |
| 78 |
protected static function update_0_1_20190812() { |
| 79 |
$ver = '0.1-20190812'; |
| 80 |
if ( self::$current_version && version_compare( self::$current_version, $ver, '>=' ) ) { |
| 81 |
return; |
| 82 |
} |
| 83 |
|
| 84 |
$prefix = self::$wpdb->prefix; |
| 85 |
self::$wpdb->query( "ALTER TABLE `{$prefix}ezcache_webp_images` |
| 86 |
ADD COLUMN `path` text(0) NOT NULL DEFAULT '' AFTER `webp_url`, |
| 87 |
ADD COLUMN `webp_path` text(0) NOT NULL DEFAULT '' AFTER `path`, |
| 88 |
ADD COLUMN `original_size` int(10) UNSIGNED NOT NULL DEFAULT 0 AFTER `webp_path`, |
| 89 |
ADD COLUMN `webp_size` int(10) UNSIGNED NOT NULL DEFAULT 0 AFTER `original_size` |
| 90 |
" ); |
| 91 |
|
| 92 |
if ( ! empty( self::$wpdb->last_error ) ) { |
| 93 |
throw new Exception( self::$wpdb->last_error ); |
| 94 |
} |
| 95 |
|
| 96 |
self::$current_version = $ver; |
| 97 |
update_option( 'ezcache_version', self::$current_version ); |
| 98 |
} |
| 99 |
|
| 100 |
} |
| 101 |
|