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