class-admin.php
3 years ago
class-columns.php
3 years ago
class-counter.php
3 years ago
class-crawler-detect.php
3 years ago
class-cron.php
3 years ago
class-dashboard.php
3 years ago
class-frontend.php
3 years ago
class-functions.php
3 years ago
class-query.php
3 years ago
class-settings-api.php
3 years ago
class-settings.php
3 years ago
class-update.php
3 years ago
class-widgets.php
3 years ago
functions.php
3 years ago
class-update.php
133 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( 'init', [ $this, 'check_update' ] ); |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * Check if there's a database update 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 | $general = $pvc->options['general']; |
| 41 | |
| 42 | if ( $general['reset_counts']['number'] > 0 ) { |
| 43 | // unsupported data reset in minutes/hours |
| 44 | if ( in_array( $general['reset_counts']['type'], [ 'minutes', 'hours' ], true ) ) { |
| 45 | // set type to date |
| 46 | $general['reset_counts']['type'] = 'days'; |
| 47 | |
| 48 | // new number of days |
| 49 | if ( $general['reset_counts']['type'] === 'minutes' ) |
| 50 | $general['reset_counts']['number'] = $general['reset_counts']['number'] * MINUTE_IN_SECONDS; |
| 51 | else |
| 52 | $general['reset_counts']['number'] = $general['reset_counts']['number'] * HOUR_IN_SECONDS; |
| 53 | |
| 54 | // how many days? |
| 55 | $general['reset_counts']['number'] = (int) round( ceil( $general['reset_counts']['number'] / DAY_IN_SECONDS ) ); |
| 56 | |
| 57 | // force cron to update |
| 58 | $general['cron_run'] = true; |
| 59 | $general['cron_update'] = true; |
| 60 | |
| 61 | // update settings |
| 62 | update_option( 'post_views_counter_settings_general', $general ); |
| 63 | |
| 64 | // update general options |
| 65 | $pvc->options['general'] = $general; |
| 66 | } |
| 67 | |
| 68 | // update cron job for all users |
| 69 | $pvc->cron->check_cron(); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | if ( isset( $_POST['post_view_counter_update'], $_POST['post_view_counter_number'] ) ) { |
| 74 | if ( $_POST['post_view_counter_number'] === 'update_1' ) { |
| 75 | $this->update_1(); |
| 76 | |
| 77 | // update plugin version |
| 78 | update_option( 'post_views_counter_version', $pvc->defaults['version'], false ); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | $update_1_html = ' |
| 83 | <form action="" method="post"> |
| 84 | <input type="hidden" name="post_view_counter_number" value="update_1"/> |
| 85 | <p>' . __( '<strong>Post Views Counter</strong> - this version requires a database update. Make sure to back up your database first.', 'post-views-counter' ) . '</p> |
| 86 | <p><input type="submit" class="button button-primary" name="post_view_counter_update" value="' . __( 'Run the Update', 'post-views-counter' ) . '"/></p> |
| 87 | </form>'; |
| 88 | |
| 89 | // get current database version |
| 90 | $current_db_version = get_option( 'post_views_counter_version', '1.0.0' ); |
| 91 | |
| 92 | // new version? |
| 93 | if ( version_compare( $current_db_version, $pvc->defaults['version'], '<' ) ) { |
| 94 | // is update 1 required? |
| 95 | if ( version_compare( $current_db_version, '1.2.4', '<=' ) ) |
| 96 | $pvc->add_notice( $update_1_html, 'notice notice-info', false ); |
| 97 | else |
| 98 | // update plugin version |
| 99 | update_option( 'post_views_counter_version', $pvc->defaults['version'], false ); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Database update for 1.2.4 and below. |
| 105 | * |
| 106 | * @global object $wpdb |
| 107 | * |
| 108 | * @return void |
| 109 | */ |
| 110 | public function update_1() { |
| 111 | global $wpdb; |
| 112 | |
| 113 | // get index |
| 114 | $old_index = $wpdb->query( "SHOW INDEX FROM `" . $wpdb->prefix . "post_views` WHERE Key_name = 'id_period'" ); |
| 115 | |
| 116 | // check whether index already exists |
| 117 | if ( $old_index > 0 ) { |
| 118 | // drop unwanted index which prevented saving views with indentical weeks and months |
| 119 | $wpdb->query( "ALTER TABLE `" . $wpdb->prefix . "post_views` DROP INDEX id_period" ); |
| 120 | } |
| 121 | |
| 122 | // get index |
| 123 | $new_index = $wpdb->query( "SHOW INDEX FROM `" . $wpdb->prefix . "post_views` WHERE Key_name = 'id_type_period_count'" ); |
| 124 | |
| 125 | // check whether index already exists |
| 126 | if ( $new_index === 0 ) { |
| 127 | // create new index for better performance of SQL queries |
| 128 | $wpdb->query( 'ALTER TABLE `' . $wpdb->prefix . 'post_views` ADD UNIQUE INDEX `id_type_period_count` (`id`, `type`, `period`, `count`) USING BTREE' ); |
| 129 | } |
| 130 | |
| 131 | Post_Views_Counter()->add_notice( __( 'Thank you! Datebase was successfully updated.', 'post-views-counter' ), 'updated', true ); |
| 132 | } |
| 133 | } |