Activation
8 months ago
Admin
8 months ago
Block
8 months ago
Compatibility
8 months ago
Container
8 months ago
Front
8 months ago
Rest
8 months ago
Shortcode
8 months ago
Traits
8 months ago
Widget
8 months ago
Bootstrap.php
8 months ago
Cache.php
8 months ago
Helper.php
8 months ago
Image.php
8 months ago
Output.php
8 months ago
Query.php
8 months ago
Settings.php
8 months ago
Themer.php
8 months ago
Translate.php
8 months ago
Upgrader.php
8 months ago
WordPressPopularPosts.php
8 months ago
deprecated.php
8 months ago
template-tags.php
8 months ago
Upgrader.php
220 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Handles plugin upgrades. |
| 4 | * |
| 5 | * @link https://cabrerahector.com |
| 6 | * @since 7.4.0 |
| 7 | * |
| 8 | * @package WordPressPopularPosts |
| 9 | */ |
| 10 | |
| 11 | namespace WordPressPopularPosts; |
| 12 | |
| 13 | use WordPressPopularPosts\Activation\Activator; |
| 14 | use WordPressPopularPosts\Helper; |
| 15 | |
| 16 | class Upgrader { |
| 17 | |
| 18 | /** |
| 19 | * Registers class hooks. |
| 20 | * |
| 21 | * @since 7.4.0 |
| 22 | */ |
| 23 | public function hooks() |
| 24 | { |
| 25 | add_action('init', [$this, 'upgrade_check']); |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Checks whether an upgrade is required. |
| 30 | * |
| 31 | * @since 2.4.0 |
| 32 | */ |
| 33 | public function upgrade_check() |
| 34 | { |
| 35 | $this->upgrade_site(); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Upgrades single site. |
| 40 | * |
| 41 | * @since 4.0.7 |
| 42 | */ |
| 43 | private function upgrade_site() |
| 44 | { |
| 45 | // Get WPP version |
| 46 | $wpp_ver = get_option('wpp_ver'); |
| 47 | |
| 48 | if ( ! $wpp_ver ) { |
| 49 | add_option('wpp_ver', WPP_VERSION); |
| 50 | } elseif ( version_compare($wpp_ver, WPP_VERSION, '<') ) { |
| 51 | $this->upgrade(); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * On plugin upgrade, performs a number of actions: update WPP database tables structures (if needed), |
| 57 | * run the setup wizard (if needed), and some other checks. |
| 58 | * |
| 59 | * @since 2.4.0 |
| 60 | * @access private |
| 61 | */ |
| 62 | private function upgrade() |
| 63 | { |
| 64 | $now = Helper::now(); |
| 65 | |
| 66 | // Keep the upgrade process from running too many times |
| 67 | $wpp_update = get_option('wpp_update'); |
| 68 | |
| 69 | if ( $wpp_update ) { |
| 70 | $from_time = strtotime($wpp_update); |
| 71 | $to_time = strtotime($now); |
| 72 | $difference_in_minutes = round(abs($to_time - $from_time)/60, 2); |
| 73 | |
| 74 | // Upgrade flag is still valid, abort |
| 75 | if ( $difference_in_minutes <= 15 ) { |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | // Upgrade flag expired, delete it and continue |
| 80 | delete_option('wpp_update'); |
| 81 | } |
| 82 | |
| 83 | // Upgrade flag |
| 84 | add_option('wpp_update', $now); |
| 85 | |
| 86 | $this->update_db_tables($now); |
| 87 | |
| 88 | // Update WPP version |
| 89 | update_option('wpp_ver', WPP_VERSION); |
| 90 | // Remove upgrade flag |
| 91 | delete_option('wpp_update'); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Updates plugin's database tables. |
| 96 | * |
| 97 | * @since 7.3.6 |
| 98 | * @access private |
| 99 | * @global object $wpdb |
| 100 | */ |
| 101 | private function update_db_tables($now) |
| 102 | { |
| 103 | global $wpdb; |
| 104 | |
| 105 | // Set table name |
| 106 | $prefix = $wpdb->prefix . 'popularposts'; |
| 107 | |
| 108 | //phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.SchemaChange |
| 109 | |
| 110 | // Update data table structure and indexes |
| 111 | $dataFields = $wpdb->get_results( |
| 112 | $wpdb->prepare("SHOW FIELDS FROM %i;", "{$prefix}data") |
| 113 | ); |
| 114 | |
| 115 | foreach ( $dataFields as $column ) { |
| 116 | if ( 'day' == $column->Field ) { |
| 117 | $wpdb->query( |
| 118 | $wpdb->prepare("ALTER TABLE %i ALTER COLUMN day DROP DEFAULT;", "{$prefix}data") |
| 119 | ); |
| 120 | } |
| 121 | |
| 122 | if ( 'last_viewed' == $column->Field ) { |
| 123 | $wpdb->query( |
| 124 | $wpdb->prepare("ALTER TABLE %i ALTER COLUMN last_viewed DROP DEFAULT;", "{$prefix}data") |
| 125 | ); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | // Update summary table structure and indexes |
| 130 | $summaryFields = $wpdb->get_results( |
| 131 | $wpdb->prepare("SHOW FIELDS FROM %i;", "{$prefix}summary") |
| 132 | ); |
| 133 | |
| 134 | foreach ( $summaryFields as $column ) { |
| 135 | if ( 'last_viewed' == $column->Field ) { |
| 136 | $wpdb->query( |
| 137 | $wpdb->prepare("ALTER TABLE %i CHANGE last_viewed view_datetime datetime NOT NULL, ADD KEY view_datetime (view_datetime);", "{$prefix}summary") |
| 138 | ); |
| 139 | } |
| 140 | |
| 141 | if ( 'view_date' == $column->Field ) { |
| 142 | $wpdb->query( |
| 143 | $wpdb->prepare("ALTER TABLE %i ALTER COLUMN view_date DROP DEFAULT;", "{$prefix}summary") |
| 144 | ); |
| 145 | } |
| 146 | |
| 147 | if ( 'view_datetime' == $column->Field ) { |
| 148 | $wpdb->query( |
| 149 | $wpdb->prepare("ALTER TABLE %i ALTER COLUMN view_datetime DROP DEFAULT;", "{$prefix}summary") |
| 150 | ); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | $summaryIndexes = $wpdb->get_results( |
| 155 | $wpdb->prepare("SHOW INDEX FROM %i;", "{$prefix}summary") |
| 156 | ); |
| 157 | |
| 158 | foreach( $summaryIndexes as $index ) { |
| 159 | if ( 'ID_date' == $index->Key_name ) { |
| 160 | $wpdb->query( |
| 161 | $wpdb->prepare("ALTER TABLE %i DROP INDEX ID_date;", "{$prefix}summary") |
| 162 | ); |
| 163 | } |
| 164 | |
| 165 | if ( 'last_viewed' == $index->Key_name ) { |
| 166 | $wpdb->query( |
| 167 | $wpdb->prepare("ALTER TABLE %i DROP INDEX last_viewed;", "{$prefix}summary") |
| 168 | ); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | $transientsIndexes = $wpdb->get_results( |
| 173 | $wpdb->prepare("SHOW INDEX FROM %i;", "{$prefix}transients") |
| 174 | ); |
| 175 | $transientsHasTKeyIndex = false; |
| 176 | |
| 177 | foreach( $transientsIndexes as $index ) { |
| 178 | if ( 'tkey' == $index->Key_name ) { |
| 179 | $transientsHasTKeyIndex = true; |
| 180 | break; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | if ( ! $transientsHasTKeyIndex ) { |
| 185 | $wpdb->query( |
| 186 | $wpdb->prepare("TRUNCATE TABLE %i;", "{$prefix}transients") |
| 187 | ); |
| 188 | $wpdb->query( |
| 189 | $wpdb->prepare("ALTER TABLE %i ADD UNIQUE KEY tkey (tkey);", "{$prefix}transients") |
| 190 | ); |
| 191 | } |
| 192 | |
| 193 | // Validate the structure of the tables, create missing tables / fields if necessary |
| 194 | Activator::track_new_site(); |
| 195 | |
| 196 | // Check storage engine |
| 197 | $storage_engine_data = $wpdb->get_var( |
| 198 | $wpdb->prepare("SELECT `ENGINE` FROM `information_schema`.`TABLES` WHERE `TABLE_SCHEMA`=%s AND `TABLE_NAME`=%s;", $wpdb->dbname, "{$prefix}data") |
| 199 | ); |
| 200 | |
| 201 | if ( 'InnoDB' != $storage_engine_data ) { |
| 202 | $wpdb->query( |
| 203 | $wpdb->prepare("ALTER TABLE %i ENGINE=InnoDB;", "{$prefix}data") |
| 204 | ); |
| 205 | } |
| 206 | |
| 207 | $storage_engine_summary = $wpdb->get_var( |
| 208 | $wpdb->prepare("SELECT `ENGINE` FROM `information_schema`.`TABLES` WHERE `TABLE_SCHEMA`=%s AND `TABLE_NAME`=%s;", $wpdb->dbname, "{$prefix}summary") |
| 209 | ); |
| 210 | |
| 211 | if ( 'InnoDB' != $storage_engine_summary ) { |
| 212 | $wpdb->query( |
| 213 | $wpdb->prepare("ALTER TABLE %i ENGINE=InnoDB;", "{$prefix}summary") |
| 214 | ); |
| 215 | } |
| 216 | |
| 217 | //phpcs:enable |
| 218 | } |
| 219 | } |
| 220 |