PluginProbe ʕ •ᴥ•ʔ
Post Views Counter / 1.4.6
Post Views Counter v1.4.6
1.7.13 1.7.12 1.7.11 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.2.0 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3 1.3.1 1.3.10 1.3.11 1.3.12 1.3.13 1.3.2 1.3.2.1 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.7.0 1.7.1 1.7.10 1.7.2 1.7.3 1.7.4 1.7.5 1.7.6 1.7.7 1.7.8 1.7.9
post-views-counter / includes / class-update.php
post-views-counter / includes Last commit date
class-admin.php 2 years ago class-columns.php 2 years ago class-counter.php 2 years ago class-crawler-detect.php 2 years ago class-cron.php 2 years ago class-dashboard.php 2 years ago class-frontend.php 2 years ago class-functions.php 2 years ago class-query.php 2 years ago class-settings-api.php 2 years ago class-settings.php 2 years ago class-update.php 2 years ago class-widgets.php 2 years ago functions.php 2 years ago
class-update.php
182 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 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 if ( isset( $_POST['post_view_counter_update'], $_POST['post_view_counter_number'] ) ) {
122 if ( $_POST['post_view_counter_number'] === 'update_1' ) {
123 $this->update_1();
124
125 // update plugin version
126 update_option( 'post_views_counter_version', $pvc->defaults['version'], false );
127 }
128 }
129
130 // get current database version
131 $current_db_version = get_option( 'post_views_counter_version', '1.0.0' );
132
133 // new version?
134 if ( version_compare( $current_db_version, $pvc->defaults['version'], '<' ) ) {
135 // is update 1 required?
136 if ( version_compare( $current_db_version, '1.2.4', '<=' ) ) {
137 $update_1_html = '
138 <form action="" method="post">
139 <input type="hidden" name="post_view_counter_number" value="update_1"/>
140 <p>' . __( '<strong>Post Views Counter</strong> - this version requires a database update. Make sure to back up your database first.', 'post-views-counter' ) . '</p>
141 <p><input type="submit" class="button button-primary" name="post_view_counter_update" value="' . __( 'Run the Update', 'post-views-counter' ) . '"/></p>
142 </form>';
143
144 $pvc->add_notice( $update_1_html, 'notice notice-info', false );
145 } else
146 // update plugin version
147 update_option( 'post_views_counter_version', $pvc->defaults['version'], false );
148 }
149 }
150
151 /**
152 * Database update for 1.2.4 and below.
153 *
154 * @global object $wpdb
155 *
156 * @return void
157 */
158 public function update_1() {
159 global $wpdb;
160
161 // get index
162 $old_index = $wpdb->query( "SHOW INDEX FROM `" . $wpdb->prefix . "post_views` WHERE Key_name = 'id_period'" );
163
164 // check whether index already exists
165 if ( $old_index > 0 ) {
166 // drop unwanted index which prevented saving views with identical weeks and months
167 $wpdb->query( "ALTER TABLE `" . $wpdb->prefix . "post_views` DROP INDEX id_period" );
168 }
169
170 // get index
171 $new_index = $wpdb->query( "SHOW INDEX FROM `" . $wpdb->prefix . "post_views` WHERE Key_name = 'id_type_period_count'" );
172
173 // check whether index already exists
174 if ( $new_index === 0 ) {
175 // create new index for better performance of sql queries
176 $wpdb->query( 'ALTER TABLE `' . $wpdb->prefix . 'post_views` ADD UNIQUE INDEX `id_type_period_count` (`id`, `type`, `period`, `count`) USING BTREE' );
177 }
178
179 Post_Views_Counter()->add_notice( __( 'Thank you! Datebase was successfully updated.', 'post-views-counter' ), 'updated', true );
180 }
181 }
182