class-admin.php
3 months ago
class-columns-modal.php
1 month ago
class-columns.php
2 weeks ago
class-counter.php
2 months ago
class-crawler-detect.php
7 months ago
class-cron.php
1 month ago
class-dashboard.php
1 month ago
class-emails-mailer.php
1 month ago
class-emails-period.php
1 month ago
class-emails-query.php
1 month ago
class-emails-scheduler.php
1 month ago
class-emails-template.php
1 month ago
class-emails.php
1 month ago
class-frontend.php
2 weeks ago
class-functions.php
1 year ago
class-import.php
2 months ago
class-integration-gutenberg.php
6 months ago
class-integrations.php
2 months ago
class-query.php
2 months ago
class-settings-api.php
1 month ago
class-settings-display.php
4 months ago
class-settings-emails.php
1 month ago
class-settings-general.php
1 month ago
class-settings-integrations.php
5 months ago
class-settings-other.php
1 month ago
class-settings-reports.php
1 month ago
class-settings.php
1 month ago
class-toolbar.php
3 months ago
class-traffic-signals.php
2 months ago
class-update.php
1 month ago
class-widgets.php
1 month ago
functions.php
1 month ago
class-update.php
346 lines
| 1 | <?php |
| 2 | // exit if accessed directly |
| 3 | if ( ! defined( 'ABSPATH' ) ) |
| 4 | exit; |
| 5 | |
| 6 | /** |
| 7 | * Post_Views_Counter_Update class. |
| 8 | * |
| 9 | * @class Post_Views_Counter_Update |
| 10 | */ |
| 11 | class Post_Views_Counter_Update { |
| 12 | |
| 13 | /** |
| 14 | * Class constructor. |
| 15 | * |
| 16 | * @return void |
| 17 | */ |
| 18 | public function __construct() { |
| 19 | // actions |
| 20 | add_action( 'admin_init', [ $this, 'check_update' ] ); |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * Check whether update is required. |
| 25 | * |
| 26 | * @return void |
| 27 | */ |
| 28 | public function check_update() { |
| 29 | if ( ! current_user_can( 'manage_options' ) ) |
| 30 | return; |
| 31 | |
| 32 | // get main instance |
| 33 | $pvc = Post_Views_Counter(); |
| 34 | |
| 35 | // get current database version |
| 36 | $current_db_version = get_option( 'post_views_counter_version', '1.0.0' ); |
| 37 | |
| 38 | // update 1.2.4+ |
| 39 | if ( version_compare( $current_db_version, '1.2.4', '<=' ) ) { |
| 40 | // get general options |
| 41 | $general = $pvc->options['general']; |
| 42 | |
| 43 | if ( $general['reset_counts']['number'] > 0 ) { |
| 44 | // unsupported data reset in minutes/hours |
| 45 | if ( in_array( $general['reset_counts']['type'], [ 'minutes', 'hours' ], true ) ) { |
| 46 | // set type to date |
| 47 | $general['reset_counts']['type'] = 'days'; |
| 48 | |
| 49 | // new number of days |
| 50 | if ( $general['reset_counts']['type'] === 'minutes' ) |
| 51 | $general['reset_counts']['number'] = $general['reset_counts']['number'] * MINUTE_IN_SECONDS; |
| 52 | else |
| 53 | $general['reset_counts']['number'] = $general['reset_counts']['number'] * HOUR_IN_SECONDS; |
| 54 | |
| 55 | // how many days? |
| 56 | $general['reset_counts']['number'] = (int) round( ceil( $general['reset_counts']['number'] / DAY_IN_SECONDS ) ); |
| 57 | |
| 58 | // force cron to update |
| 59 | $general['cron_run'] = true; |
| 60 | $general['cron_update'] = true; |
| 61 | |
| 62 | // update settings |
| 63 | update_option( 'post_views_counter_settings_general', $general ); |
| 64 | |
| 65 | // update general options |
| 66 | $pvc->options['general'] = $general; |
| 67 | } |
| 68 | |
| 69 | // update cron job for all users |
| 70 | $pvc->cron->check_cron(); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | // update 1.3.13+ |
| 75 | if ( version_compare( $current_db_version, '1.3.13', '<=' ) ) { |
| 76 | // get general options |
| 77 | $general = $pvc->options['general']; |
| 78 | |
| 79 | // disable strict counts |
| 80 | $general['strict_counts'] = false; |
| 81 | |
| 82 | // get default other options |
| 83 | $other_options = $pvc->defaults['other']; |
| 84 | |
| 85 | // set current options |
| 86 | $other_options['deactivation_delete'] = isset( $general['deactivation_delete'] ) ? (bool) $general['deactivation_delete'] : false; |
| 87 | |
| 88 | // add other options |
| 89 | add_option( 'post_views_counter_settings_other', $other_options, null, false ); |
| 90 | |
| 91 | // update other options |
| 92 | $pvc->options['other'] = $other_options; |
| 93 | |
| 94 | // remove old setting |
| 95 | unset( $general['deactivation_delete'] ); |
| 96 | |
| 97 | // flush cache enabled? |
| 98 | if ( $general['flush_interval']['number'] > 0 ) { |
| 99 | if ( $pvc->counter->using_object_cache( true ) ) { |
| 100 | // flush data from cache |
| 101 | $pvc->counter->flush_cache_to_db(); |
| 102 | } |
| 103 | |
| 104 | // unschedule cron event |
| 105 | wp_clear_scheduled_hook( 'pvc_flush_cached_counts' ); |
| 106 | |
| 107 | // disable cache |
| 108 | $general['flush_interval'] = [ |
| 109 | 'number' => 0, |
| 110 | 'type' => 'minutes' |
| 111 | ]; |
| 112 | } |
| 113 | |
| 114 | // update general options |
| 115 | $pvc->options['general'] = $general; |
| 116 | |
| 117 | // update general options |
| 118 | update_option( 'post_views_counter_settings_general', $general ); |
| 119 | } |
| 120 | |
| 121 | // update 1.5.2+ |
| 122 | if ( version_compare( $current_db_version, '1.5.2', '<=' ) ) { |
| 123 | // get options |
| 124 | $general = $pvc->options['general']; |
| 125 | $display = $pvc->options['display']; |
| 126 | |
| 127 | // copy values |
| 128 | $display['post_views_column'] = $general['post_views_column']; |
| 129 | $display['restrict_edit_views'] = $general['restrict_edit_views']; |
| 130 | |
| 131 | // remove old values |
| 132 | unset( $general['post_views_column'] ); |
| 133 | unset( $general['restrict_edit_views'] ); |
| 134 | |
| 135 | // update settings |
| 136 | update_option( 'post_views_counter_settings_general', $general ); |
| 137 | update_option( 'post_views_counter_settings_display', $display ); |
| 138 | |
| 139 | // update options |
| 140 | $pvc->options['general'] = $general; |
| 141 | $pvc->options['display'] = $display; |
| 142 | } |
| 143 | |
| 144 | // update 1.6.0+ - migrate import settings to provider format |
| 145 | if ( version_compare( $current_db_version, '1.6.0', '<' ) ) { |
| 146 | // get other options |
| 147 | $other = $pvc->options['other']; |
| 148 | |
| 149 | // check if migration is needed |
| 150 | if ( ! isset( $other['import_provider_settings'] ) && isset( $other['import_meta_key'] ) ) { |
| 151 | $old_meta_key = $other['import_meta_key']; |
| 152 | |
| 153 | // create new provider settings structure |
| 154 | $other['import_provider_settings'] = [ |
| 155 | 'provider' => 'custom_meta_key', |
| 156 | 'strategy' => 'merge', |
| 157 | 'custom_meta_key' => [ |
| 158 | 'meta_key' => $old_meta_key |
| 159 | ] |
| 160 | ]; |
| 161 | |
| 162 | // update settings |
| 163 | update_option( 'post_views_counter_settings_other', $other ); |
| 164 | |
| 165 | // update options |
| 166 | $pvc->options['other'] = $other; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | // update 1.7.10+ - normalize legacy count interval values to canonical hours |
| 171 | if ( version_compare( $current_db_version, '1.7.10', '<' ) ) { |
| 172 | $general = get_option( 'post_views_counter_settings_general', [] ); |
| 173 | |
| 174 | if ( ! is_array( $general ) ) |
| 175 | $general = []; |
| 176 | |
| 177 | $normalized_time_between_counts = $pvc->normalize_time_between_counts( isset( $general['time_between_counts'] ) ? $general['time_between_counts'] : null, $pvc->defaults['general']['time_between_counts'] ); |
| 178 | |
| 179 | if ( ! isset( $general['time_between_counts'] ) || $general['time_between_counts'] !== $normalized_time_between_counts ) { |
| 180 | $general['time_between_counts'] = $normalized_time_between_counts; |
| 181 | |
| 182 | update_option( 'post_views_counter_settings_general', $general ); |
| 183 | } |
| 184 | |
| 185 | $pvc->options['general']['time_between_counts'] = $normalized_time_between_counts; |
| 186 | } |
| 187 | |
| 188 | // move menu position setting to display tab |
| 189 | $this->migrate_menu_position_option(); |
| 190 | $this->ensure_email_settings_option( $pvc ); |
| 191 | $this->sync_email_summary_schedule( $pvc ); |
| 192 | |
| 193 | if ( isset( $_POST['post_view_counter_update'], $_POST['post_view_counter_number'] ) ) { |
| 194 | if ( $_POST['post_view_counter_number'] === 'update_1' ) { |
| 195 | $this->update_1(); |
| 196 | |
| 197 | // update plugin version |
| 198 | update_option( 'post_views_counter_version', $pvc->defaults['version'], false ); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | // get current database version |
| 203 | $current_db_version = get_option( 'post_views_counter_version', '1.0.0' ); |
| 204 | |
| 205 | // new version? |
| 206 | if ( version_compare( $current_db_version, $pvc->defaults['version'], '<' ) ) { |
| 207 | // is update 1 required? |
| 208 | if ( version_compare( $current_db_version, '1.2.4', '<=' ) ) { |
| 209 | $update_1_html = ' |
| 210 | <form action="" method="post"> |
| 211 | <input type="hidden" name="post_view_counter_number" value="update_1"/> |
| 212 | <p>' . __( '<strong>Post Views Counter</strong> - this version requires a database update. Make sure to back up your database first.', 'post-views-counter' ) . '</p> |
| 213 | <p><input type="submit" class="button button-primary" name="post_view_counter_update" value="' . __( 'Run the Update', 'post-views-counter' ) . '"/></p> |
| 214 | </form>'; |
| 215 | |
| 216 | $pvc->add_notice( $update_1_html, 'notice notice-info', false ); |
| 217 | } else |
| 218 | // update plugin version |
| 219 | update_option( 'post_views_counter_version', $pvc->defaults['version'], false ); |
| 220 | } |
| 221 | |
| 222 | // ensure integrations option exists |
| 223 | if ( ! get_option( 'post_views_counter_settings_integrations' ) ) { |
| 224 | add_option( 'post_views_counter_settings_integrations', $pvc->defaults['integrations'], null, false ); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Ensure email settings option exists and includes current defaults. |
| 230 | * |
| 231 | * @param Post_Views_Counter $pvc |
| 232 | * @return array |
| 233 | */ |
| 234 | private function ensure_email_settings_option( $pvc ) { |
| 235 | $defaults = $pvc->get_default_emails_settings(); |
| 236 | $settings = get_option( 'post_views_counter_settings_emails', false ); |
| 237 | |
| 238 | if ( false === $settings ) { |
| 239 | add_option( 'post_views_counter_settings_emails', $defaults, null, false ); |
| 240 | |
| 241 | return $defaults; |
| 242 | } |
| 243 | |
| 244 | if ( ! is_array( $settings ) ) |
| 245 | $settings = []; |
| 246 | |
| 247 | $normalized = array_merge( $defaults, $settings ); |
| 248 | $normalized['latest_status'] = array_merge( |
| 249 | isset( $defaults['latest_status'] ) && is_array( $defaults['latest_status'] ) ? $defaults['latest_status'] : [], |
| 250 | isset( $settings['latest_status'] ) && is_array( $settings['latest_status'] ) ? $settings['latest_status'] : [] |
| 251 | ); |
| 252 | |
| 253 | if ( $normalized !== $settings ) |
| 254 | update_option( 'post_views_counter_settings_emails', $normalized ); |
| 255 | |
| 256 | return $normalized; |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * Sync the weekly email summary schedule during plugin upgrades. |
| 261 | * |
| 262 | * @param Post_Views_Counter $pvc |
| 263 | * @return void |
| 264 | */ |
| 265 | private function sync_email_summary_schedule( $pvc ) { |
| 266 | if ( ! isset( $pvc->emails_scheduler ) || ! $pvc->emails_scheduler instanceof Post_Views_Counter_Emails_Scheduler ) |
| 267 | return; |
| 268 | |
| 269 | $settings = get_option( 'post_views_counter_settings_emails', [] ); |
| 270 | |
| 271 | if ( ! is_array( $settings ) ) |
| 272 | $settings = []; |
| 273 | |
| 274 | $settings = array_merge( $pvc->get_default_emails_settings(), $settings ); |
| 275 | |
| 276 | $pvc->emails_scheduler->maybe_schedule( $settings ); |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * Database update for 1.2.4 and below. |
| 281 | * |
| 282 | * @global object $wpdb |
| 283 | * |
| 284 | * @return void |
| 285 | */ |
| 286 | public function update_1() { |
| 287 | global $wpdb; |
| 288 | |
| 289 | // get index |
| 290 | $old_index = $wpdb->query( "SHOW INDEX FROM `" . $wpdb->prefix . "post_views` WHERE Key_name = 'id_period'" ); |
| 291 | |
| 292 | // check whether index already exists |
| 293 | if ( $old_index > 0 ) { |
| 294 | // drop unwanted index which prevented saving views with identical weeks and months |
| 295 | $wpdb->query( "ALTER TABLE `" . $wpdb->prefix . "post_views` DROP INDEX id_period" ); |
| 296 | } |
| 297 | |
| 298 | // get index |
| 299 | $new_index = $wpdb->query( "SHOW INDEX FROM `" . $wpdb->prefix . "post_views` WHERE Key_name = 'id_type_period_count'" ); |
| 300 | |
| 301 | // check whether index already exists |
| 302 | if ( $new_index === 0 ) { |
| 303 | // create new index for better performance of sql queries |
| 304 | $wpdb->query( 'ALTER TABLE `' . $wpdb->prefix . 'post_views` ADD UNIQUE INDEX `id_type_period_count` (`id`, `type`, `period`, `count`) USING BTREE' ); |
| 305 | } |
| 306 | |
| 307 | Post_Views_Counter()->add_notice( __( 'Thank you! Datebase was successfully updated.', 'post-views-counter' ), 'updated', true ); |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Move menu position setting from "Other" to "Display" settings. |
| 312 | * |
| 313 | * @return void |
| 314 | */ |
| 315 | private function migrate_menu_position_option() { |
| 316 | $pvc = Post_Views_Counter(); |
| 317 | |
| 318 | // prefer legacy value if present, otherwise fall back to current display option |
| 319 | $menu_position = isset( $pvc->options['other']['menu_position'] ) && in_array( $pvc->options['other']['menu_position'], [ 'top', 'sub' ], true ) |
| 320 | ? $pvc->options['other']['menu_position'] |
| 321 | : ( isset( $pvc->options['display']['menu_position'] ) && in_array( $pvc->options['display']['menu_position'], [ 'top', 'sub' ], true ) |
| 322 | ? $pvc->options['display']['menu_position'] |
| 323 | : 'top' ); |
| 324 | |
| 325 | $display_options = get_option( 'post_views_counter_settings_display', [] ); |
| 326 | |
| 327 | if ( ! isset( $display_options['menu_position'] ) || ! in_array( $display_options['menu_position'], [ 'top', 'sub' ], true ) ) { |
| 328 | $display_options['menu_position'] = $menu_position; |
| 329 | update_option( 'post_views_counter_settings_display', $display_options ); |
| 330 | } |
| 331 | |
| 332 | $other_options = get_option( 'post_views_counter_settings_other', [] ); |
| 333 | |
| 334 | if ( ! is_array( $other_options ) ) |
| 335 | $other_options = []; |
| 336 | |
| 337 | if ( ! isset( $other_options['menu_position'] ) || ! in_array( $other_options['menu_position'], [ 'top', 'sub' ], true ) ) { |
| 338 | $other_options['menu_position'] = $display_options['menu_position']; |
| 339 | update_option( 'post_views_counter_settings_other', $other_options ); |
| 340 | } |
| 341 | |
| 342 | $pvc->options['other']['menu_position'] = $other_options['menu_position']; |
| 343 | $pvc->options['display']['menu_position'] = $display_options['menu_position']; |
| 344 | } |
| 345 | } |
| 346 |