PluginProbe ʕ •ᴥ•ʔ
Post Views Counter / 1.1.1
Post Views Counter v1.1.1
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 / settings.php
post-views-counter / includes Last commit date
columns.php 10 years ago counter.php 10 years ago cron.php 10 years ago frontend.php 10 years ago functions.php 10 years ago query.php 10 years ago settings.php 10 years ago update.php 10 years ago widgets.php 10 years ago
settings.php
720 lines
1 <?php
2 if ( ! defined( 'ABSPATH' ) )
3 exit;
4
5 new Post_Views_Counter_Settings();
6
7 class Post_Views_Counter_Settings {
8
9 private $tabs;
10 private $choices;
11 private $modes;
12 private $time_types;
13 private $groups;
14 private $user_roles;
15 private $positions;
16 private $display_styles;
17 public $post_types;
18
19 public function __construct() {
20 // set instance
21 Post_Views_Counter()->add_instance( 'settings', $this );
22
23 // actions
24 add_action( 'admin_init', array( &$this, 'register_settings' ) );
25 add_action( 'admin_menu', array( &$this, 'admin_menu_options' ) );
26 add_action( 'after_setup_theme', array( &$this, 'load_defaults' ) );
27 add_action( 'wp_loaded', array( &$this, 'load_post_types' ) );
28 }
29
30 /**
31 * Load default settings.
32 */
33 public function load_defaults() {
34
35 $this->modes = array(
36 'php' => __( 'PHP', 'post-views-counter' ),
37 'js' => __( 'JavaScript', 'post-views-counter' )
38 );
39
40 $this->time_types = array(
41 'minutes' => __( 'minutes', 'post-views-counter' ),
42 'hours' => __( 'hours', 'post-views-counter' ),
43 'days' => __( 'days', 'post-views-counter' ),
44 'weeks' => __( 'weeks', 'post-views-counter' ),
45 'months' => __( 'months', 'post-views-counter' ),
46 'years' => __( 'years', 'post-views-counter' )
47 );
48
49 $this->groups = array(
50 'robots' => __( 'robots', 'post-views-counter' ),
51 'users' => __( 'logged in users', 'post-views-counter' ),
52 'guests' => __( 'guests', 'post-views-counter' ),
53 'roles' => __( 'selected user roles', 'post-views-counter' )
54 );
55
56 $this->positions = array(
57 'before' => __( 'before the content', 'post-views-counter' ),
58 'after' => __( 'after the content', 'post-views-counter' ),
59 'manual' => __( 'manual', 'post-views-counter' )
60 );
61
62 $this->display_styles = array(
63 'icon' => __( 'icon', 'post-views-counter' ),
64 'text' => __( 'label', 'post-views-counter' )
65 );
66
67 $this->tabs = array(
68 'general' => array(
69 'name' => __( 'General', 'post-views-counter' ),
70 'key' => 'post_views_counter_settings_general',
71 'submit' => 'save_pvc_general',
72 'reset' => 'reset_pvc_general'
73 ),
74 'display' => array(
75 'name' => __( 'Display', 'post-views-counter' ),
76 'key' => 'post_views_counter_settings_display',
77 'submit' => 'save_pvc_display',
78 'reset' => 'reset_pvc_display'
79 )
80 );
81
82 $this->user_roles = $this->get_user_roles();
83 }
84
85 /**
86 * Get post types avaiable for counting.
87 */
88 public function load_post_types() {
89 $post_types = array();
90
91 // built in public post types
92 foreach ( get_post_types( array( '_builtin' => true, 'public' => true ), 'objects', 'and' ) as $key => $post_type ) {
93 if ( $key !== 'attachment' )
94 $post_types[$key] = $post_type->labels->name;
95 }
96
97 // public custom post types
98 foreach ( get_post_types( array( '_builtin' => false, 'public' => true ), 'objects', 'and' ) as $key => $post_type ) {
99 $post_types[$key] = $post_type->labels->name;
100 }
101
102 // sort post types alphabetically with their keys
103 asort( $post_types, SORT_STRING );
104
105 $this->post_types = $post_types;
106 }
107
108 /**
109 * Get all user roles.
110 */
111 public function get_user_roles() {
112 global $wp_roles;
113
114 $roles = array();
115
116 foreach ( apply_filters( 'editable_roles', $wp_roles->roles ) as $role => $details ) {
117 $roles[$role] = translate_user_role( $details['name'] );
118 }
119
120 asort( $roles, SORT_STRING );
121
122 return $roles;
123 }
124
125 /**
126 * Add options page.
127 */
128 public function admin_menu_options() {
129 add_options_page(
130 __( 'Post Views Counter', 'post-views-counter' ), __( 'Post Views Counter', 'post-views-counter' ), 'manage_options', 'post-views-counter', array( &$this, 'options_page' )
131 );
132 }
133
134 /**
135 * Options page callback.
136 */
137 public function options_page() {
138 $tab_key = (isset( $_GET['tab'] ) ? $_GET['tab'] : 'general');
139
140 echo '
141 <div class="wrap">' . screen_icon() . '
142 <h2>' . __( 'Post Views Counter', 'post-views-counter' ) . '</h2>
143 <h2 class="nav-tab-wrapper">';
144
145 foreach ( $this->tabs as $key => $name ) {
146 echo '
147 <a class="nav-tab ' . ($tab_key == $key ? 'nav-tab-active' : '') . '" href="' . esc_url( admin_url( 'options-general.php?page=post-views-counter&tab=' . $key ) ) . '">' . $name['name'] . '</a>';
148 }
149
150 echo '
151 </h2>
152 <div class="post-views-counter-settings">
153 <div class="df-credits">
154 <h3 class="hndle">' . __( 'Post Views Counter', 'post-views-counter' ) . ' ' . Post_Views_Counter()->get_attribute( 'defaults', 'version' ) . '</h3>
155 <div class="inside">
156 <h4 class="inner">' . __( 'Need support?', 'post-views-counter' ) . '</h4>
157 <p class="inner">' . __( 'If you are having problems with this plugin, please talk about them in the', 'post-views-counter' ) . ' <a href="http://www.dfactory.eu/support/?utm_source=post-views-counter-settings&utm_medium=link&utm_campaign=support" target="_blank" title="' . __( 'Support forum', 'post-views-counter' ) . '">' . __( 'Support forum', 'post-views-counter' ) . '</a></p>
158 <hr />
159 <h4 class="inner">' . __( 'Do you like this plugin?', 'post-views-counter' ) . '</h4>
160 <p class="inner"><a href="http://wordpress.org/support/view/plugin-reviews/post-views-counter" target="_blank" title="' . __( 'Rate it 5', 'post-views-counter' ) . '">' . __( 'Rate it 5', 'post-views-counter' ) . '</a> ' . __( 'on WordPress.org', 'post-views-counter' ) . '<br />' .
161 __( 'Blog about it & link to the', 'post-views-counter' ) . ' <a href="http://www.dfactory.eu/plugins/post-views-counter/?utm_source=post-views-counter-settings&utm_medium=link&utm_campaign=blog-about" target="_blank" title="' . __( 'plugin page', 'post-views-counter' ) . '">' . __( 'plugin page', 'post-views-counter' ) . '</a><br/>' .
162 __( 'Check out our other', 'post-views-counter' ) . ' <a href="http://www.dfactory.eu/plugins/?utm_source=post-views-counter-settings&utm_medium=link&utm_campaign=other-plugins" target="_blank" title="' . __( 'WordPress plugins', 'post-views-counter' ) . '">' . __( 'WordPress plugins', 'post-views-counter' ) . '</a>
163 </p>
164 <hr />
165 <p class="df-link inner">' . __( 'Created by', 'post-views-counter' ) . ' <a href="http://www.dfactory.eu/?utm_source=post-views-counter-settings&utm_medium=link&utm_campaign=created-by" target="_blank" title="dFactory - Quality plugins for WordPress"><img src="' . POST_VIEWS_COUNTER_URL . '/images/logo-dfactory.png' . '" title="dFactory - Quality plugins for WordPress" alt="dFactory - Quality plugins for WordPress" /></a></p>
166 </div>
167 </div>
168 <form action="options.php" method="post">';
169
170 wp_nonce_field( 'update-options' );
171 settings_fields( $this->tabs[$tab_key]['key'] );
172 do_settings_sections( $this->tabs[$tab_key]['key'] );
173
174 echo '
175 <p class="submit">';
176
177 submit_button( '', 'primary', $this->tabs[$tab_key]['submit'], false );
178
179 echo ' ';
180
181 submit_button( __( 'Reset to defaults', 'post-views-counter' ), 'secondary reset_pvc_settings', $this->tabs[$tab_key]['reset'], false );
182
183 echo '
184 </p>
185 </form>
186 </div>
187 <div class="clear"></div>
188 </div>';
189 }
190
191 /**
192 * Register settings callback.
193 */
194 public function register_settings() {
195 // general options
196 register_setting( 'post_views_counter_settings_general', 'post_views_counter_settings_general', array( &$this, 'validate_settings' ) );
197 add_settings_section( 'post_views_counter_settings_general', __( 'General settings', 'post-views-counter' ), '', 'post_views_counter_settings_general' );
198 add_settings_field( 'pvc_post_types_count', __( 'Post Types Count', 'post-views-counter' ), array( &$this, 'post_types_count' ), 'post_views_counter_settings_general', 'post_views_counter_settings_general' );
199 add_settings_field( 'pvc_counter_mode', __( 'Counter Mode', 'post-views-counter' ), array( &$this, 'counter_mode' ), 'post_views_counter_settings_general', 'post_views_counter_settings_general' );
200 add_settings_field( 'pvc_post_views_column', __( 'Post Views Column', 'post-views-counter' ), array( &$this, 'post_views_column' ), 'post_views_counter_settings_general', 'post_views_counter_settings_general' );
201 add_settings_field( 'pvc_restrict_edit_views', __( 'Restrict Edit', 'post-views-counter' ), array( &$this, 'restrict_edit_views' ), 'post_views_counter_settings_general', 'post_views_counter_settings_general' );
202 add_settings_field( 'pvc_time_between_counts', __( 'Time Between Counts', 'post-views-counter' ), array( &$this, 'time_between_counts' ), 'post_views_counter_settings_general', 'post_views_counter_settings_general' );
203 add_settings_field( 'pvc_reset_counts', __( 'Reset Data Interval', 'post-views-counter' ), array( &$this, 'reset_counts' ), 'post_views_counter_settings_general', 'post_views_counter_settings_general' );
204 add_settings_field( 'pvc_flush_interval', __( 'Flush Object Cache Interval', 'post-views-counter' ), array( &$this, 'flush_interval' ), 'post_views_counter_settings_general', 'post_views_counter_settings_general' );
205 add_settings_field( 'pvc_exclude', __( 'Exclude Visitors', 'post-views-counter' ), array( &$this, 'exclude' ), 'post_views_counter_settings_general', 'post_views_counter_settings_general' );
206 add_settings_field( 'pvc_exclude_ips', __( 'Exclude IPs', 'post-views-counter' ), array( &$this, 'exclude_ips' ), 'post_views_counter_settings_general', 'post_views_counter_settings_general' );
207 add_settings_field( 'pvc_wp_postviews', __( 'WP-PostViews', 'post-views-counter' ), array( &$this, 'wp_postviews' ), 'post_views_counter_settings_general', 'post_views_counter_settings_general' );
208 add_settings_field( 'pvc_deactivation_delete', __( 'Deactivation', 'post-views-counter' ), array( &$this, 'deactivation_delete' ), 'post_views_counter_settings_general', 'post_views_counter_settings_general' );
209
210 // display options
211 register_setting( 'post_views_counter_settings_display', 'post_views_counter_settings_display', array( &$this, 'validate_settings' ) );
212 add_settings_section( 'post_views_counter_settings_display', __( 'Display settings', 'post-views-counter' ), '', 'post_views_counter_settings_display' );
213 add_settings_field( 'pvc_post_views_label', __( 'Post Views Label', 'post-views-counter' ), array( &$this, 'post_views_label' ), 'post_views_counter_settings_display', 'post_views_counter_settings_display' );
214 add_settings_field( 'pvc_post_types_display', __( 'Post Types Display', 'post-views-counter' ), array( &$this, 'post_types_display' ), 'post_views_counter_settings_display', 'post_views_counter_settings_display' );
215 add_settings_field( 'pvc_restrict_display', __( 'Restrict Display', 'post-views-counter' ), array( &$this, 'restrict_display' ), 'post_views_counter_settings_display', 'post_views_counter_settings_display' );
216 add_settings_field( 'pvc_position', __( 'Position', 'post-views-counter' ), array( &$this, 'position' ), 'post_views_counter_settings_display', 'post_views_counter_settings_display' );
217 add_settings_field( 'pvc_display_style', __( 'Display Style', 'post-views-counter' ), array( &$this, 'display_style' ), 'post_views_counter_settings_display', 'post_views_counter_settings_display' );
218 add_settings_field( 'pvc_icon_class', __( 'Icon Class', 'post-views-counter' ), array( &$this, 'icon_class' ), 'post_views_counter_settings_display', 'post_views_counter_settings_display' );
219 }
220
221 /**
222 * Post views label option.
223 */
224 public function post_views_label() {
225 echo '
226 <div id="pvc_post_views_label">
227 <fieldset>
228 <input type="text" class="large-text" name="post_views_counter_settings_display[label]" value="' . esc_attr( Post_Views_Counter()->get_attribute( 'options', 'display', 'label' ) ) . '" />
229 <p class="description">' . esc_html__( 'Enter the label for the post views counter field.', 'post-views-counter' ) . '</p>
230 </fieldset>
231 </div>';
232 }
233
234 /**
235 * Post types to count option.
236 */
237 public function post_types_count() {
238 echo '
239 <div id="pvc_post_types_count">';
240
241 foreach ( $this->post_types as $post_type => $post_type_name ) {
242 echo '
243 <label class="cb-checkbox"><input id="pvc_post_types_count-' . esc_attr( $post_type ) . '" type="checkbox" name="post_views_counter_settings_general[post_types_count][' . esc_attr( $post_type ) . ']" value="1" ' . checked( in_array( $post_type, Post_Views_Counter()->get_attribute( 'options', 'general', 'post_types_count' ), true ), true, false ) . ' />' . esc_html( $post_type_name ) . ' </label>';
244 }
245
246 echo '
247 <p class="description">' . esc_html__( 'Select post types for which post views will be counted.', 'post-views-counter' ) . '</p>
248 </div>';
249 }
250
251 /**
252 * Post types to display option.
253 */
254 public function post_types_display() {
255 echo '
256 <div id="pvc_post_types_display">';
257
258 foreach ( $this->post_types as $post_type => $post_type_name ) {
259 echo '
260 <label class="cb-checkbox"><input id="pvc_post_types_display-' . esc_attr( $post_type ) . '" type="checkbox" name="post_views_counter_settings_display[post_types_display][' . esc_attr( $post_type ) . ']" value="1" ' . checked( in_array( $post_type, Post_Views_Counter()->get_attribute( 'options', 'display', 'post_types_display' ), true ), true, false ) . ' />' . esc_html( $post_type_name ) . '</label>';
261 }
262
263 echo '
264 <p class="description">' . esc_html__( 'Select post types for which post views will be displayed.', 'post-views-counter' ) . '</p>
265 </div>';
266 }
267
268 /**
269 * Counter mode option.
270 */
271 public function counter_mode() {
272 echo '
273 <div id="pvc_counter_mode">';
274
275 foreach ( $this->modes as $key => $value ) {
276 $key = esc_attr( $key );
277
278 echo '
279 <label class="cb-radio"><input type="radio" name="post_views_counter_settings_general[counter_mode]" value="' . $key . '" ' . checked( $key, Post_Views_Counter()->get_attribute( 'options', 'general', 'counter_mode' ), false ) . ' />' . esc_html( $value ) . '</label>';
280 }
281
282 echo '
283 <p class="description">' . esc_html__( 'Select the method of collecting post views data. If you are using any of the caching plugins select Javascript.', 'post-views-counter' ) . '</p>
284 </div>';
285 }
286
287 /**
288 * Post views column option.
289 */
290 public function post_views_column() {
291 echo '
292 <div id="pvc_post_views_column">
293 <label class="cb-checkbox"><input id="pvc-post-views-column-enable" type="checkbox" name="post_views_counter_settings_general[post_views_column]" value="1" ' . checked( true, (bool) Post_Views_Counter()->get_attribute( 'options', 'general', 'post_views_column' ), false ) . ' />' . esc_html__( 'Enable to display post views count column for each of the selected post types.', 'post-views-counter' ) . '</label>
294 </div>';
295 }
296
297 /**
298 * Time between counts option.
299 */
300 public function time_between_counts() {
301 echo '
302 <div id="pvc_time_between_counts">
303 <input size="4" type="text" name="post_views_counter_settings_general[time_between_counts][number]" value="' . esc_attr( Post_Views_Counter()->get_attribute( 'options', 'general', 'time_between_counts', 'number' ) ) . '" />
304 <select class="pvc-chosen-short" name="post_views_counter_settings_general[time_between_counts][type]">';
305
306 foreach ( $this->time_types as $type => $type_name ) {
307 echo '
308 <option value="' . esc_attr( $type ) . '" ' . selected( $type, Post_Views_Counter()->get_attribute( 'options', 'general', 'time_between_counts', 'type' ), false ) . '>' . esc_html( $type_name ) . '</option>';
309 }
310
311 echo '
312 </select>
313 <p class="description">' . esc_html__( 'Enter the time between single user visit count.', 'post-views-counter' ) . '</p>
314 </div>';
315 }
316
317 /**
318 * Reset counts option.
319 */
320 public function reset_counts() {
321 echo '
322 <div id="pvc_reset_counts">
323 <input size="4" type="text" name="post_views_counter_settings_general[reset_counts][number]" value="' . esc_attr( Post_Views_Counter()->get_attribute( 'options', 'general', 'reset_counts', 'number' ) ) . '" />
324 <select class="pvc-chosen-short" name="post_views_counter_settings_general[reset_counts][type]">';
325
326 foreach ( $this->time_types as $type => $type_name ) {
327 echo '
328 <option value="' . esc_attr( $type ) . '" ' . selected( $type, Post_Views_Counter()->get_attribute( 'options', 'general', 'reset_counts', 'type' ), false ) . '>' . esc_html( $type_name ) . '</option>';
329 }
330
331 echo '
332 </select>
333 <p class="description">' . esc_html__( 'Delete single day post views data older than specified above. Enter 0 (number zero) if you want to preserve your data regardless of its age.', 'post-views-counter' ) . '</p>
334 </div>';
335 }
336
337 /**
338 * Flush interval option.
339 */
340 public function flush_interval() {
341 echo '
342 <div id="pvc_flush_interval">
343 <input size="4" type="text" name="post_views_counter_settings_general[flush_interval][number]" value="' . esc_attr( Post_Views_Counter()->get_attribute( 'options', 'general', 'flush_interval', 'number' ) ) . '" />
344 <select class="pvc-chosen-short" name="post_views_counter_settings_general[flush_interval][type]">';
345
346 foreach ( $this->time_types as $type => $type_name ) {
347 echo '
348 <option value="' . esc_attr( $type ) . '" ' . selected( $type, Post_Views_Counter()->get_attribute( 'options', 'general', 'flush_interval', 'type' ), false ) . '>' . esc_html( $type_name ) . '</option>';
349 }
350
351 echo '
352 </select>
353 <p class="description">' . __( 'How often to flush cached view counts from the object cache into the database. This feature is used only if a persistent object cache is detected and the interval is greater than 0 (number zero)). When used, view counts will be collected and stored in the object cache instead of the database and will then be asynchronously flushed to the database according to the specified interval.<br /><strong>Notice:</strong> Potential data loss may occur if the object cache is cleared/unavailable for the duration of the interval.', 'post-views-counter' ) . '</p>
354 </div>';
355 }
356
357 /**
358 * Exlude user groups option.
359 */
360 public function exclude() {
361 echo '
362 <div id="pvc_exclude">
363 <fieldset>';
364
365 foreach ( $this->groups as $type => $type_name ) {
366 echo '
367 <label class="cb-checkbox"><input id="pvc_exclude-' . $type . '" type="checkbox" name="post_views_counter_settings_general[exclude][groups][' . $type . ']" value="1" ' . checked( in_array( $type, Post_Views_Counter()->get_attribute( 'options', 'general', 'exclude', 'groups' ), true ), true, false ) . ' />' . esc_html( $type_name ) . '</label>';
368 }
369
370 echo ' <p class="description">' . esc_html__( 'Use it to hide the post views counter from selected type of visitors.', 'post-views-counter' ) . '</p>
371 <div class="pvc_user_roles"' . (in_array( 'roles', Post_Views_Counter()->get_attribute( 'options', 'general', 'exclude', 'groups' ), true ) ? '' : ' style="display: none;"') . '>';
372
373 foreach ( $this->user_roles as $role => $role_name ) {
374 echo ' <label class="cb-checkbox"><input type="checkbox" name="post_views_counter_settings_general[exclude][roles][' . $role . ']" value="1" ' . checked( in_array( $role, Post_Views_Counter()->get_attribute( 'options', 'general', 'exclude', 'roles' ), true ), true, false ) . '>' . esc_html( $role_name ) . '</label>';
375 }
376
377 echo ' <p class="description">' . esc_html__( 'Use it to hide the post views counter from selected user roles.', 'post-views-counter' ) . '</p>
378 </div>
379 </fieldset>
380 </div>';
381 }
382
383 /**
384 * Exclude IPs option.
385 */
386 public function exclude_ips() {
387 // lovely php 5.2 limitations
388 $ips = Post_Views_Counter()->get_attribute( 'options', 'general', 'exclude_ips' );
389
390 echo '
391 <div id="pvc_exclude_ips">';
392
393 if ( ! empty( $ips ) ) {
394 foreach ( $ips as $key => $ip ) {
395 echo '
396 <div class="ip-box">
397 <input type="text" name="post_views_counter_settings_general[exclude_ips][]" value="' . esc_attr( $ip ) . '" /> <a href="#" class="remove-exclude-ip" title="' . esc_attr__( 'Remove', 'post-views-counter' ) . '">' . esc_attr__( 'Remove', 'post-views-counter' ) . '</a>
398 </div>';
399 }
400 } else {
401 echo '
402 <div class="ip-box">
403 <input type="text" name="post_views_counter_settings_general[exclude_ips][]" value="" /> <a href="#" class="remove-exclude-ip" title="' . esc_attr__( 'Remove', 'post-views-counter' ) . '">' . esc_attr__( 'Remove', 'post-views-counter' ) . '</a>
404 </div>';
405 }
406
407 echo '
408 <p><input type="button" class="button button-secondary add-exclude-ip" value="' . esc_attr__( 'Add new', 'post-views-counter' ) . '" /> <input type="button" class="button button-secondary add-current-ip" value="' . esc_attr__( 'Add my current IP', 'post-views-counter' ) . '" data-rel="' . esc_attr( $_SERVER['REMOTE_ADDR'] ) . '" /></p>
409 <p class="description">' . esc_html__( 'Enter the IP addresses to be excluded from post views count.', 'post-views-counter' ) . '</p>
410 </div>';
411 }
412
413 /**
414 * WP-PostViews import option.
415 */
416 public function wp_postviews() {
417 echo '
418 <div id="pvc_wp_postviews">
419 <fieldset>
420 <input type="submit" class="button button-secondary" name="post_views_counter_import_wp_postviews" value="' . __( 'Import', 'post-views-counter' ) . '"/>
421 <p class="description">' . esc_html__( 'Import post views data from WP-PostViews plugin.', 'post-views-counter' ) . '</p>
422 <label class="cb-checkbox"><input id="pvc-wp-postviews" type="checkbox" name="post_views_counter_import_wp_postviews_override" value="1" />' . esc_html__( 'Override existing Post Views Counter data.', 'post-views-counter' ) . '</label>
423 </fieldset>
424 </div>';
425 }
426
427 /**
428 * Limit views edit to admins.
429 */
430 public function restrict_edit_views() {
431 echo '
432 <div id="pvc_restrict_edit_views">
433 <label class="cb-checkbox"><input type="checkbox" name="post_views_counter_settings_general[restrict_edit_views]" value="1" ' . checked( true, (bool) Post_Views_Counter()->get_attribute( 'options', 'general', 'restrict_edit_views' ), false ) . ' />' . esc_html__( 'Enable to restrict post views editing to admins only.', 'post-views-counter' ) . '</label>
434 </div>';
435 }
436
437 /**
438 * Plugin deactivation option.
439 */
440 public function deactivation_delete() {
441 echo '
442 <div id="pvc_deactivation_delete">
443 <label class="cb-checkbox"><input type="checkbox" name="post_views_counter_settings_general[deactivation_delete]" value="1" ' . checked( true, (bool) Post_Views_Counter()->get_attribute( 'options', 'general', 'deactivation_delete' ), false ) . ' />' . esc_html__( 'Enable to delete all plugin data on deactivation.', 'post-views-counter' ) . '</label>
444 </div>';
445 }
446
447 /**
448 * Counter position option.
449 */
450 public function position() {
451 echo '
452 <div id="pvc_position">
453 <select class="pvc-chosen-short" name="post_views_counter_settings_display[position]">';
454
455 foreach ( $this->positions as $position => $position_name ) {
456 echo '
457 <option value="' . esc_attr( $position ) . '" ' . selected( $position, Post_Views_Counter()->get_attribute( 'options', 'display', 'position' ), false ) . '>' . esc_html( $position_name ) . '</option>';
458 }
459
460 echo '
461 </select>
462 <p class="description">' . esc_html__( 'Select where would you like to display the post views counter. Use [post-views] shortcode for manual display.', 'post-views-counter' ) . '</p>
463 </div>';
464 }
465
466 /**
467 * Counter style option.
468 */
469 public function display_style() {
470 echo '
471 <div id="pvc_display_style">';
472
473 foreach ( $this->display_styles as $display => $style ) {
474 $display = esc_attr( $display );
475
476 echo '
477 <label class="cb-checkbox"><input type="checkbox" name="post_views_counter_settings_display[display_style][' . $display . ']" value="1" ' . checked( true, Post_Views_Counter()->get_attribute( 'options', 'display', 'display_style', $display ), false ) . ' />' . esc_html( $style ) . '</label>';
478 }
479
480 echo '
481 <p class="description">' . esc_html__( 'Choose how to display the post views counter.', 'post-views-counter' ) . '</p>
482 </div>';
483 }
484
485 /**
486 * Counter icon class option.
487 */
488 public function icon_class() {
489 echo '
490 <div id="pvc_icon_class">
491 <input type="text" name="post_views_counter_settings_display[icon_class]" class="large-text" value="' . esc_attr( Post_Views_Counter()->get_attribute( 'options', 'display', 'icon_class' ) ) . '" />
492 <p class="description">' . sprintf( __( 'Enter the post views icon class. Any of the <a href="%s" target="_blank">Dashicons</a> classes are available.', 'post-views-counter' ), 'http://melchoyce.github.io/dashicons/' ) . '</p>
493 </div>';
494 }
495
496 /**
497 * Restrict display option.
498 */
499 public function restrict_display() {
500 echo '
501 <div id="pvc_restrict_display">
502 <fieldset>';
503
504 foreach ( $this->groups as $type => $type_name ) {
505
506 if ( $type === 'robots' )
507 continue;
508
509 echo '
510 <label class="cb-checkbox"><input id="pvc_restrict_display-' . $type . '" type="checkbox" name="post_views_counter_settings_display[restrict_display][groups][' . esc_html( $type ) . ']" value="1" ' . checked( in_array( $type, Post_Views_Counter()->get_attribute( 'options', 'display', 'restrict_display', 'groups' ), true ), true, false ) . ' />' . esc_html( $type_name ) . '</label>';
511 }
512
513 echo ' <p class="description">' . esc_html__( 'Use it to hide the post views counter from selected type of visitors.', 'post-views-counter' ) . '</p>
514 <div class="pvc_user_roles"' . (in_array( 'roles', Post_Views_Counter()->get_attribute( 'options', 'display', 'restrict_display', 'groups' ), true ) ? '' : ' style="display: none;"') . '>';
515
516 foreach ( $this->user_roles as $role => $role_name ) {
517 echo ' <label class="cb-checkbox"><input type="checkbox" name="post_views_counter_settings_display[restrict_display][roles][' . $role . ']" value="1" ' . checked( in_array( $role, Post_Views_Counter()->get_attribute( 'options', 'display', 'restrict_display', 'roles' ), true ), true, false ) . ' />' . esc_html( $role_name ) . '</label>';
518 }
519
520 echo ' <p class="description">' . esc_html__( 'Use it to hide the post views counter from selected user roles.', 'post-views-counter' ) . '</p>
521 </div>
522 </fieldset>
523 </div>';
524 }
525
526 /**
527 * Validate general settings.
528 */
529 public function validate_settings( $input ) {
530 if ( isset( $_POST['post_views_counter_import_wp_postviews'] ) ) {
531 global $wpdb;
532
533 $meta_key = esc_attr( apply_filters( 'pvc_import_meta_key', 'views' ) );
534
535 $views = $wpdb->get_results(
536 "SELECT post_id, meta_value FROM " . $wpdb->postmeta . " WHERE meta_key = '" . $meta_key . "'", ARRAY_A, 0
537 );
538
539 if ( ! empty( $views ) ) {
540 $input = Post_Views_Counter()->get_attribute( 'defaults', 'general' );
541 $input['wp_postviews_import'] = true;
542
543 $sql = '';
544
545 foreach ( $views as $view ) {
546 $sql[] = "(" . $view['post_id'] . ", 4, 'total', " . $view['meta_value'] . ")";
547 }
548
549 $wpdb->query( "INSERT INTO " . $wpdb->prefix . "post_views(id, type, period, count) VALUES " . implode( ',', $sql ) . " ON DUPLICATE KEY UPDATE count = " . (isset( $_POST['post_views_counter_import_wp_postviews_override'] ) ? '' : 'count + ') . "VALUES(count)" );
550
551 add_settings_error( 'wp_postviews_import', 'wp_postviews_import', __( 'Post views data imported succesfully.', 'post-views-counter' ), 'updated' );
552 } else {
553 add_settings_error( 'wp_postviews_import', 'wp_postviews_import', __( 'There was no post views data to import.', 'post-views-counter' ), 'updated' );
554 }
555 } elseif ( isset( $_POST['save_pvc_general'] ) ) {
556 // post types count
557 if ( isset( $input['post_types_count'] ) ) {
558 $post_types = array();
559
560 foreach ( $input['post_types_count'] as $post_type => $set ) {
561 if ( isset( $this->post_types[$post_type] ) )
562 $post_types[] = $post_type;
563 }
564
565 $input['post_types_count'] = array_unique( $post_types );
566 } else
567 $input['post_types_count'] = array();
568
569 // counter mode
570 $input['counter_mode'] = (isset( $input['counter_mode'], $this->modes[$input['counter_mode']] ) ? $input['counter_mode'] : Post_Views_Counter()->get_attribute( 'defaults', 'general', 'counter_mode' ));
571
572 // post views column
573 $input['post_views_column'] = (bool) $input['post_views_column'];
574
575 // time between counts
576 $input['time_between_counts']['number'] = (int) (isset( $input['time_between_counts']['number'] ) ? $input['time_between_counts']['number'] : Post_Views_Counter()->get_attribute( 'defaults', 'general', 'time_between_counts', 'number' ));
577 $input['time_between_counts']['type'] = (isset( $input['time_between_counts']['type'], $this->time_types[$input['time_between_counts']['type']] ) ? $input['time_between_counts']['type'] : Post_Views_Counter()->get_attribute( 'defaults', 'general', 'time_between_counts', 'type' ));
578
579 // flush interval
580 $input['flush_interval']['number'] = (int) (isset( $input['flush_interval']['number'] ) ? $input['flush_interval']['number'] : Post_Views_Counter()->get_attribute( 'defaults', 'general', 'flush_interval', 'number' ));
581 $input['flush_interval']['type'] = (isset( $input['flush_interval']['type'], $this->time_types[$input['flush_interval']['type']] ) ? $input['flush_interval']['type'] : Post_Views_Counter()->get_attribute( 'defaults', 'general', 'flush_interval', 'type' ));
582
583 // Since the settings are about to be saved and cache flush interval could've changed,
584 // we want to make sure that any changes done on the settings page are in effect immediately
585 // (instead of having to wait for the previous schedule to occur).
586 // We achieve that by making sure to clear any previous cache flush schedules and
587 // schedule the new one if the specified interval is > 0
588 Post_Views_Counter()->remove_cache_flush();
589
590 if ( $input['flush_interval']['number'] > 0 ) {
591 Post_Views_Counter()->schedule_cache_flush();
592 }
593
594 // reset counts
595 $input['reset_counts']['number'] = (int) (isset( $input['reset_counts']['number'] ) ? $input['reset_counts']['number'] : Post_Views_Counter()->get_attribute( 'defaults', 'general', 'reset_counts', 'number' ));
596 $input['reset_counts']['type'] = (isset( $input['reset_counts']['type'], $this->time_types[$input['reset_counts']['type']] ) ? $input['reset_counts']['type'] : Post_Views_Counter()->get_attribute( 'defaults', 'general', 'reset_counts', 'type' ));
597
598 // run cron on next visit?
599 $input['cron_run'] = ($input['reset_counts']['number'] > 0 ? true : false);
600 $input['cron_update'] = ($input['cron_run'] && (Post_Views_Counter()->get_attribute( 'options', 'general', 'reset_counts', 'number' ) !== $input['reset_counts']['number'] || Post_Views_Counter()->get_attribute( 'options', 'general', 'reset_counts', 'type' ) !== $input['reset_counts']['type']) ? true : false);
601
602 // exclude
603 if ( isset( $input['exclude']['groups'] ) ) {
604 $groups = array();
605
606 foreach ( $input['exclude']['groups'] as $group => $set ) {
607 if ( isset( $this->groups[$group] ) )
608 $groups[] = $group;
609 }
610
611 $input['exclude']['groups'] = array_unique( $groups );
612 } else
613 $input['exclude']['groups'] = array();
614
615 if ( in_array( 'roles', $input['exclude']['groups'], true ) && isset( $input['exclude']['roles'] ) ) {
616 $roles = array();
617
618 foreach ( $input['exclude']['roles'] as $role => $set ) {
619 if ( isset( $this->user_roles[$role] ) )
620 $roles[] = $role;
621 }
622
623 $input['exclude']['roles'] = array_unique( $roles );
624 } else
625 $input['exclude']['roles'] = array();
626
627 // exclude ips
628 if ( isset( $input['exclude_ips'] ) ) {
629
630 $ips = array();
631
632 foreach ( $input['exclude_ips'] as $ip ) {
633 if ( filter_var( $ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 ) )
634 $ips[] = $ip;
635 }
636
637 $input['exclude_ips'] = array_unique( $ips );
638 }
639
640 // restrict edit viewa
641 $input['restrict_edit_views'] = (isset( $input['restrict_edit_views'] ) ? (bool) $input['restrict_edit_views'] : Post_Views_Counter()->get_attribute( 'defaults', 'general', 'restrict_edit_views' ));
642
643 // deactivation delete
644 $input['deactivation_delete'] = (bool) isset( $input['deactivation_delete'] );
645 } elseif ( isset( $_POST['save_pvc_display'] ) ) {
646
647 // post views label
648 $input['label'] = (isset( $input['label'] ) ? $input['label'] : Post_Views_Counter()->get_attribute( 'defaults', 'general', 'label' ));
649
650 if ( function_exists( 'icl_register_string' ) )
651 icl_register_string( 'Post Views Counter', 'Post Views Label', $input['label'] );
652
653 // position
654 $input['position'] = (isset( $input['position'], $this->positions[$input['position']] ) ? $input['position'] : Post_Views_Counter()->get_attribute( 'defaults', 'general', 'position' ));
655
656 // display style
657 $input['display_style']['icon'] = (bool) isset( $input['display_style']['icon'] );
658 $input['display_style']['text'] = (bool) isset( $input['display_style']['text'] );
659
660 // link to post
661 $input['link_to_post'] = (isset( $input['link_to_post'] ) ? (bool) $input['link_to_post'] : Post_Views_Counter()->get_attribute( 'defaults', 'display', 'link_to_post' ));
662
663 // icon class
664 $input['icon_class'] = (isset( $input['icon_class'] ) ? trim( $input['icon_class'] ) : Post_Views_Counter()->get_attribute( 'defaults', 'general', 'icon_class' ));
665
666 // post types display
667 if ( isset( $input['post_types_display'] ) ) {
668 $post_types = array();
669
670 foreach ( $input['post_types_display'] as $post_type => $set ) {
671 if ( isset( $this->post_types[$post_type] ) )
672 $post_types[] = $post_type;
673 }
674
675 $input['post_types_display'] = array_unique( $post_types );
676 } else
677 $input['post_types_display'] = array();
678
679 // restrict display
680 if ( isset( $input['restrict_display']['groups'] ) ) {
681 $groups = array();
682
683 foreach ( $input['restrict_display']['groups'] as $group => $set ) {
684 if ( $group === 'robots' )
685 continue;
686
687 if ( isset( $this->groups[$group] ) )
688 $groups[] = $group;
689 }
690
691 $input['restrict_display']['groups'] = array_unique( $groups );
692 } else
693 $input['restrict_display']['groups'] = array();
694
695 if ( in_array( 'roles', $input['restrict_display']['groups'], true ) && isset( $input['restrict_display']['roles'] ) ) {
696 $roles = array();
697
698 foreach ( $input['restrict_display']['roles'] as $role => $set ) {
699 if ( isset( $this->user_roles[$role] ) )
700 $roles[] = $role;
701 }
702
703 $input['restrict_display']['roles'] = array_unique( $roles );
704 } else
705 $input['restrict_display']['roles'] = array();
706 } elseif ( isset( $_POST['reset_pvc_general'] ) ) {
707 $input = Post_Views_Counter()->get_attribute( 'defaults', 'general' );
708
709 add_settings_error( 'reset_general_settings', 'settings_reset', __( 'General settings restored to defaults.', 'post-views-counter' ), 'updated' );
710 } elseif ( isset( $_POST['reset_pvc_display'] ) ) {
711 $input = Post_Views_Counter()->get_attribute( 'defaults', 'display' );
712
713 add_settings_error( 'reset_general_settings', 'settings_reset', __( 'Display settings restored to defaults.', 'post-views-counter' ), 'updated' );
714 }
715
716 return $input;
717 }
718
719 }
720