PluginProbe
ezCache / 1.3.11
ezCache v1.3.11
2.6.5 2.6.4 2.6.2 2.6.3 2.6.1 2.6.0 2.5.6 2.5.5 2.5.4 2.5.3 2.5.2 2.5.1 2.5 2.2.1 2.2.2 trunk 1.2 1.2.1 1.2.2 1.2.3 1.2.4 1.3 1.3.1 1.3.10 1.3.11 All 49 releases
ezcache / includes / Updater.php

Updater.php in ezCache 1.3.11, at includes/Updater.php

124 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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
14 protected static function wpdb() {
15 global $wpdb;
16 }
17
18 public static function uninstall() {
19 global $wpdb;
20
21 Cache::instance()->clear_cache( true );
22 $wpdb->query("DROP TABLE IF EXISTS `{$wpdb->prefix}ezcache_webp_images`");
23 delete_option( 'ezcache_version' );
24 }
25
26 /**
27 * Run any necessary db updates, file upgrades etc.
28 */
29 public static function upgrade() {
30 global $wpdb;
31
32 self::$current_version = get_option( 'ezcache_version', 0 );
33 self::$collate = $wpdb->get_charset_collate();
34
35 /** @noinspection PhpIncludeInspection */
36 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
37
38 try {
39 if ( version_compare( self::$current_version, '0.1-20190811', '<' ) ) {
40 self::update_0_1_20190811();
41 }
42
43 if ( version_compare( self::$current_version, '0.1-20190812', '<' ) ) {
44 self::update_0_1_20190812();
45 }
46
47 if ( version_compare( self::$current_version, EZCACHE_VERSION, '=' ) ) {
48 self::verify_tables();
49 }
50
51 // make sure we update the version in the database so we can run upgrades at later times
52 update_option( 'ezcache_version', EZCACHE_VERSION );
53 } catch ( Exception $ex ) {
54 error_log( 'ezCache Updater Error: ' . $ex );
55 wp_die( $ex->getMessage() );
56 }
57 }
58
59 /**
60 * Verify all tables exists and run their creation if not
61 * @throws Exception
62 */
63 protected static function verify_tables() {
64 global $wpdb;
65
66 if ( ! $wpdb->get_row( "SHOW TABLES LIKE '{$wpdb->prefix}ezcache_webp_images'" ) ) {
67 // this creates the table
68 self::update_0_1_20190811();
69 }
70
71 // and this makes sure the table has the correct columns
72 self::update_0_1_20190812();
73 }
74
75 /**
76 * Update to the 1.0 version
77 * create the 404 database
78 * @throws Exception
79 */
80 protected static function update_0_1_20190811() {
81 global $wpdb;
82
83 $wpdb->query( "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}ezcache_webp_images` (
84 `id` bigint(10) UNSIGNED NOT NULL AUTO_INCREMENT,
85 `uid` varchar(191) NOT NULL DEFAULT '',
86 `url` text(0) NOT NULL DEFAULT '',
87 `webp_url` text(0) NOT NULL DEFAULT '',
88 `status` enum('pending', 'completed', 'failed') NOT NULL DEFAULT 'pending',
89 `created_at` datetime(0) NOT NULL DEFAULT '0000-00-00 00:00:00',
90 `updated_at` datetime(0) NOT NULL DEFAULT '0000-00-00 00:00:00',
91 PRIMARY KEY (`id`),
92 UNIQUE INDEX (`uid`) USING BTREE
93 ) ". ( self::$collate ) );
94
95 if ( ! empty( $wpdb->last_error ) ) {
96 throw new Exception( $wpdb->last_error );
97 }
98 }
99
100 /**
101 * Update to the 1.0 version
102 * create the 404 database
103 * @throws Exception
104 */
105 protected static function update_0_1_20190812() {
106 global $wpdb;
107
108 $cols = $wpdb->get_col( "SHOW COLUMNS FROM `{$wpdb->prefix}ezcache_webp_images`", 0 );
109 if( ! in_array( 'path', $cols ) ) {
110 $wpdb->query( "ALTER TABLE `{$wpdb->prefix}ezcache_webp_images`
111 ADD COLUMN `path` text(0) NOT NULL DEFAULT '' AFTER `webp_url`,
112 ADD COLUMN `webp_path` text(0) NOT NULL DEFAULT '' AFTER `path`,
113 ADD COLUMN `original_size` int(10) UNSIGNED NOT NULL DEFAULT 0 AFTER `webp_path`,
114 ADD COLUMN `webp_size` int(10) UNSIGNED NOT NULL DEFAULT 0 AFTER `original_size`
115 " );
116 }
117
118 if ( ! empty( $wpdb->last_error ) ) {
119 throw new Exception( $wpdb->last_error );
120 }
121 }
122
123 }
124