PluginProbe ʕ •ᴥ•ʔ
Post Views Counter / 1.7.0
Post Views Counter v1.7.0
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-settings-display.php
post-views-counter / includes Last commit date
class-admin.php 6 months ago class-columns.php 6 months ago class-counter.php 6 months ago class-crawler-detect.php 6 months ago class-cron.php 6 months ago class-dashboard.php 6 months ago class-frontend.php 6 months ago class-functions.php 6 months ago class-import.php 6 months ago class-integration-gutenberg.php 6 months ago class-integrations.php 6 months ago class-query.php 6 months ago class-settings-api.php 6 months ago class-settings-display.php 6 months ago class-settings-general.php 6 months ago class-settings-integrations.php 6 months ago class-settings-other.php 6 months ago class-settings-reports.php 6 months ago class-settings.php 6 months ago class-toolbar.php 6 months ago class-update.php 6 months ago class-widgets.php 6 months ago functions.php 6 months ago
class-settings-display.php
560 lines
1 <?php
2 // exit if accessed directly
3 if ( ! defined( 'ABSPATH' ) )
4 exit;
5
6 /**
7 * Post_Views_Counter_Settings_Display class.
8 *
9 * @class Post_Views_Counter_Settings_Display
10 */
11 class Post_Views_Counter_Settings_Display {
12
13 private $pvc;
14 private static $syncing_menu_position = false;
15
16 /**
17 * Class constructor.
18 *
19 * @return void
20 */
21 public function __construct() {
22 $this->pvc = Post_Views_Counter();
23
24 // actions
25 add_action( 'update_option_post_views_counter_settings_display', [ $this, 'sync_menu_position_option' ], 10, 3 );
26 add_action( 'add_option_post_views_counter_settings_display', [ $this, 'sync_menu_position_option_on_add' ], 10, 2 );
27
28 // filters
29 add_filter( 'pre_update_option_post_views_counter_settings_other', [ $this, 'sync_menu_position_from_other' ], 10, 2 );
30 }
31
32 /**
33 * Get sections for display tab.
34 *
35 * @return array
36 */
37 public function get_sections() {
38 return [
39 'post_views_counter_display_appearance' => [
40 'tab' => 'display',
41 'title' => __( 'Counter Appearance', 'post-views-counter' ),
42 'callback' => [ $this, 'section_display_appearance' ],
43 ],
44 'post_views_counter_display_locations' => [
45 'tab' => 'display',
46 'title' => __( 'Display Targets', 'post-views-counter' ),
47 'callback' => [ $this, 'section_display_targets' ],
48 ],
49 'post_views_counter_display_visibility' => [
50 'tab' => 'display',
51 'title' => __( 'Display Audience', 'post-views-counter' ),
52 'callback' => [ $this, 'section_display_audience' ],
53 ],
54 'post_views_counter_display_admin' => [
55 'tab' => 'display',
56 'title' => __( 'Admin Interface', 'post-views-counter' ),
57 'callback' => [ $this, 'section_display_admin' ],
58 ]
59 ];
60 }
61
62 /**
63 * Get fields for display tab.
64 *
65 * @return array
66 */
67 public function get_fields() {
68 // get post types
69 $post_types = $this->pvc->functions->get_post_types();
70
71 // user groups
72 $groups = [
73 'robots' => __( 'crawlers', 'post-views-counter' ),
74 'ai_bots' => __( 'AI bots', 'post-views-counter' ),
75 'users' => __( 'logged in users', 'post-views-counter' ),
76 'guests' => __( 'guests', 'post-views-counter' ),
77 'roles' => __( 'selected user roles', 'post-views-counter' )
78 ];
79
80 // get user roles
81 $user_roles = $this->pvc->functions->get_user_roles();
82
83 return [
84 'label' => [
85 'tab' => 'display',
86 'title' => __( 'Views Label', 'post-views-counter' ),
87 'section' => 'post_views_counter_display_appearance',
88 'type' => 'input',
89 'description' => __( 'Text shown next to the view count.', 'post-views-counter' ),
90 'subclass' => 'regular-text',
91 'validate' => [ $this, 'validate_label' ],
92 'reset' => [ $this, 'reset_label' ]
93 ],
94 'display_period' => [
95 'tab' => 'display',
96 'title' => __( 'Views Period', 'post-views-counter' ),
97 'section' => 'post_views_counter_display_appearance',
98 'type' => 'select',
99 'class' => 'pvc-pro',
100 'disabled' => true,
101 'skip_saving' => true,
102 'description' => __( 'Time range used when displaying the number of views.', 'post-views-counter' ),
103 'options' => [
104 'total' => __( 'Total Views', 'post-views-counter' )
105 ]
106 ],
107 'display_style' => [
108 'tab' => 'display',
109 'title' => __( 'Display Style', 'post-views-counter' ),
110 'section' => 'post_views_counter_display_appearance',
111 'type' => 'custom',
112 'description' => __( 'Choose whether to show an icon, label text, or both.', 'post-views-counter' ),
113 'callback' => [ $this, 'setting_display_style' ],
114 'validate' => [ $this, 'validate_display_style' ],
115 'options' => [
116 'icon' => __( 'Icon', 'post-views-counter' ),
117 'text' => __( 'Label', 'post-views-counter' )
118 ]
119 ],
120 'icon_class' => [
121 'tab' => 'display',
122 'title' => __( 'Icon Class', 'post-views-counter' ),
123 'section' => 'post_views_counter_display_appearance',
124 'type' => 'class',
125 'default' => '',
126 'description' => sprintf( __( 'Enter the CSS class for the views icon. Any Dashicons class is supported.', 'post-views-counter' ), 'https://developer.wordpress.org/resource/dashicons/' ),
127 'subclass' => 'regular-text'
128 ],
129 'position' => [
130 'tab' => 'display',
131 'title' => __( 'Position', 'post-views-counter' ),
132 'section' => 'post_views_counter_display_locations',
133 'type' => 'select',
134 'description' => sprintf( __( 'Where to insert the counter automatically. Use %s shortcode for manual placement.', 'post-views-counter' ), '<code>[post-views]</code>' ),
135 'options' => [
136 'before' => __( 'Before the content', 'post-views-counter' ),
137 'after' => __( 'After the content', 'post-views-counter' ),
138 'manual' => __( 'Manual only', 'post-views-counter' )
139 ]
140 ],
141 'post_views_column' => [
142 'tab' => 'display',
143 'title' => __( 'Admin Column', 'post-views-counter' ),
144 'section' => 'post_views_counter_display_admin',
145 'type' => 'boolean',
146 'description' => '',
147 'label' => __( 'Show a "Views" column on post and page list screens.', 'post-views-counter' )
148 ],
149 'restrict_edit_views' => [
150 'tab' => 'display',
151 'title' => __( 'Admin Edit', 'post-views-counter' ),
152 'section' => 'post_views_counter_display_admin',
153 'type' => 'boolean',
154 'description' => '',
155 'label' => __( 'Allow editing the view count on the post edit screen.', 'post-views-counter' )
156 ],
157 'dynamic_loading' => [
158 'tab' => 'display',
159 'title' => __( 'Dynamic Loading', 'post-views-counter' ),
160 'section' => 'post_views_counter_display_appearance',
161 'type' => 'boolean',
162 'class' => 'pvc-pro',
163 'disabled' => true,
164 'skip_saving' => true,
165 'value' => false,
166 'label' => __( 'Load the view count dynamically to avoid caching the displayed value.', 'post-views-counter' )
167 ],
168 'use_format' => [
169 'tab' => 'display',
170 'title' => __( 'Format Number', 'post-views-counter' ),
171 'section' => 'post_views_counter_display_appearance',
172 'type' => 'boolean',
173 'label' => __( 'Format the view count according to the site locale (uses the WordPress number_format_i18n function).', 'post-views-counter' )
174 ],
175 'taxonomies_display' => [
176 'tab' => 'display',
177 'title' => __( 'Taxonomies', 'post-views-counter' ),
178 'section' => 'post_views_counter_display_locations',
179 'type' => 'custom',
180 'class' => 'pvc-pro',
181 'skip_saving' => true,
182 'options' => $this->pvc->functions->get_taxonomies( 'labels' ),
183 'callback' => [ $this, 'setting_taxonomies_display' ]
184 ],
185 'user_display' => [
186 'tab' => 'display',
187 'title' => __( 'Author Archives', 'post-views-counter' ),
188 'section' => 'post_views_counter_display_locations',
189 'type' => 'boolean',
190 'class' => 'pvc-pro',
191 'disabled' => true,
192 'skip_saving' => true,
193 'value' => false,
194 'label' => __( 'Display the view count on author archive pages.', 'post-views-counter' )
195 ],
196 'post_types_display' => [
197 'tab' => 'display',
198 'title' => __( 'Post Types', 'post-views-counter' ),
199 'section' => 'post_views_counter_display_locations',
200 'type' => 'checkbox',
201 'display_type' => 'horizontal',
202 'description' => __( 'Select post types where the view counter will be displayed.', 'post-views-counter' ),
203 'options' => $post_types
204 ],
205 'page_types_display' => [
206 'tab' => 'display',
207 'title' => __( 'Page Type', 'post-views-counter' ),
208 'section' => 'post_views_counter_display_locations',
209 'type' => 'checkbox',
210 'display_type' => 'horizontal',
211 'description' => __( 'Select page contexts where the view counter will be displayed.', 'post-views-counter' ),
212 'options' => apply_filters(
213 'pvc_page_types_display_options',
214 [
215 'home' => __( 'Home', 'post-views-counter' ),
216 'archive' => __( 'Archives', 'post-views-counter' ),
217 'singular' => __( 'Single posts and pages', 'post-views-counter' ),
218 'search' => __( 'Search results', 'post-views-counter' ),
219 ]
220 )
221 ],
222 'restrict_display' => [
223 'tab' => 'display',
224 'title' => __( 'User Type', 'post-views-counter' ),
225 'section' => 'post_views_counter_display_visibility',
226 'type' => 'custom',
227 'description' => '',
228 'options' => [
229 'groups' => $groups,
230 'roles' => $user_roles
231 ],
232 'callback' => [ $this, 'setting_restrict_display' ],
233 'validate' => [ $this, 'validate_restrict_display' ]
234 ],
235 'toolbar_statistics' => [
236 'tab' => 'display',
237 'title' => __( 'Toolbar Chart', 'post-views-counter' ),
238 'section' => 'post_views_counter_display_admin',
239 'type' => 'boolean',
240 'description' => __( 'A views chart is shown for content types that are being counted.', 'post-views-counter' ),
241 'label' => __( 'Show a views chart in the admin toolbar.', 'post-views-counter' )
242 ],
243 'menu_position' => [
244 'tab' => 'display',
245 'title' => __( 'Menu Position', 'post-views-counter' ),
246 'section' => 'post_views_counter_display_admin',
247 'type' => 'radio',
248 'options' => [
249 'top' => __( 'Top menu', 'post-views-counter' ),
250 'sub' => __( 'Settings submenu', 'post-views-counter' )
251 ],
252 'description' => __( 'Choose where the plugin menu appears in the admin sidebar.', 'post-views-counter' ),
253 ]
254 ];
255 }
256
257 /**
258 * Validate label.
259 *
260 * @param array $input
261 * @param array $field
262 * @return array
263 */
264 public function validate_label( $input, $field ) {
265 if ( ! isset( $input ) )
266 $input = $this->pvc->defaults['display']['label'];
267
268 // use internal settings API to validate settings first
269 $input = $this->pvc->settings_api->validate_field( $input, 'input', $field );
270
271 if ( function_exists( 'icl_register_string' ) )
272 icl_register_string( 'Post Views Counter', 'Post Views Label', $input );
273
274 return $input;
275 }
276
277 /**
278 * Restore post views label to default value.
279 *
280 * @param array $default
281 * @param array $field
282 * @return array
283 */
284 public function reset_label( $default, $field ) {
285 if ( function_exists( 'icl_register_string' ) )
286 icl_register_string( 'Post Views Counter', 'Post Views Label', $default );
287
288 return $default;
289 }
290
291 /**
292 * Setting: display style.
293 *
294 * @param array $field
295 * @return string
296 */
297 public function setting_display_style( $field ) {
298 $html = '<div class="pvc-field-group pvc-checkbox-group">';
299 $html .= '<input type="hidden" name="post_views_counter_settings_display[display_style]" value="empty" />';
300
301 foreach ( $field['options'] as $key => $label ) {
302 $html .= '
303 <label><input id="post_views_counter_display_display_style_' . esc_attr( $key ) . '" type="checkbox" name="post_views_counter_settings_display[display_style][]" value="' . esc_attr( $key ) . '" ' . checked( ! empty( $this->pvc->options['display']['display_style'][$key] ), true, false ) . ' />' . esc_html( $label ) . '</label> ';
304 }
305 $html .= '</div>';
306
307 return $html;
308 }
309
310 /**
311 * Validate display style.
312 *
313 * @param array $input
314 * @param array $field
315 * @return array
316 */
317 public function validate_display_style( $input, $field ) {
318 $data = [];
319
320 foreach ( $field['options'] as $value => $label ) {
321 $data[$value] = false;
322 }
323
324 // any data?
325 if ( ! empty( $input['display_style'] && $input['display_style'] !== 'empty' && is_array( $input['display_style'] ) ) ) {
326 foreach ( $input['display_style'] as $value ) {
327 if ( array_key_exists( $value, $field['options'] ) )
328 $data[$value] = true;
329 }
330 }
331
332 $input['display_style'] = $data;
333
334 return $input;
335 }
336
337 /**
338 * Setting: taxonomies display.
339 *
340 * @param array $field
341 * @return string
342 */
343 public function setting_taxonomies_display( $field ) {
344 $html = '<div class="pvc-field-group pvc-checkbox-group">';
345
346 foreach ( $field['options'] as $taxonomy => $label ) {
347 $html .= '
348 <label><input type="checkbox" name="" value="" disabled />' . esc_html( $label ) . '</label>';
349 }
350
351 $html .= '</div>';
352
353 $html .= '
354 <p class="description">' . esc_html__( 'Select taxonomies where the view counter will be displayed.', 'post-views-counter' ) . '</p>';
355
356 return $html;
357 }
358
359 /**
360 * Setting: user type.
361 *
362 * @param array $field
363 *
364 * @return string
365 */
366 public function setting_restrict_display( $field ) {
367 $html = '<div class="pvc-field-group pvc-checkbox-group">';
368
369 foreach ( $field['options']['groups'] as $type => $type_name ) {
370 if ( $type === 'robots' || $type === 'ai_bots' )
371 continue;
372
373 $html .= '
374 <label><input id="pvc_restrict_display-' . esc_attr( $type ) . '" type="checkbox" name="post_views_counter_settings_display[restrict_display][groups][' . esc_attr( $type ) . ']" value="1" ' . checked( in_array( $type, $this->pvc->options['display']['restrict_display']['groups'], true ), true, false ) . ' />' . esc_html( $type_name ) . '</label>';
375 }
376
377 $html .= '</div>';
378
379 $html .= '
380 <p class="description">' . __( 'Hide the view counter for selected visitor groups.', 'post-views-counter' ) . '</p>
381 <div class="pvc_user_roles pvc-subfield pvc-field-group pvc-checkbox-group"' . ( in_array( 'roles', $this->pvc->options['display']['restrict_display']['groups'], true ) ? '' : ' style="display: none;"' ) . '>';
382
383 foreach ( $field['options']['roles'] as $role => $role_name ) {
384 $html .= '
385 <label><input type="checkbox" name="post_views_counter_settings_display[restrict_display][roles][' . esc_attr( $role ) . ']" value="1" ' . checked( in_array( $role, $this->pvc->options['display']['restrict_display']['roles'], true ), true, false ) . ' />' . esc_html( $role_name ) . '</label>';
386 }
387
388 $html .= '
389 <p class="description">' . __( 'Hide the view counter for selected user roles.', 'post-views-counter' ) . '</p>
390 </div>';
391
392 return $html;
393 }
394
395 /**
396 * Validate user type.
397 *
398 * @param array $input
399 * @param array $field
400 *
401 * @return array
402 */
403 public function validate_restrict_display( $input, $field ) {
404 // any groups?
405 if ( isset( $input['restrict_display']['groups'] ) ) {
406 $groups = [];
407
408 foreach ( $input['restrict_display']['groups'] as $group => $set ) {
409 if ( $group === 'robots' || $group === 'ai_bots' )
410 continue;
411
412 if ( isset( $field['options']['groups'][$group] ) )
413 $groups[] = $group;
414 }
415
416 $input['restrict_display']['groups'] = array_unique( $groups );
417 } else
418 $input['restrict_display']['groups'] = [];
419
420 // any roles?
421 if ( in_array( 'roles', $input['restrict_display']['groups'], true ) && isset( $input['restrict_display']['roles'] ) ) {
422 $roles = [];
423
424 foreach ( $input['restrict_display']['roles'] as $role => $set ) {
425 if ( isset( $field['options']['roles'][$role] ) )
426 $roles[] = $role;
427 }
428
429 $input['restrict_display']['roles'] = array_unique( $roles );
430 } else
431 $input['restrict_display']['roles'] = [];
432
433 return $input;
434 }
435
436 /**
437 * Section description: display targets.
438 *
439 * @return void
440 */
441 public function section_display_targets() {
442 echo '<p class="description">' . esc_html__( 'Choose where the counter is inserted and which content types it attaches to.', 'post-views-counter' ) . '</p>';
443 }
444
445 /**
446 * Section description: display audience.
447 *
448 * @return void
449 */
450 public function section_display_audience() {
451 echo '<p class="description">' . esc_html__( 'Control which visitor groups can see the counter. These rules apply on top of the display targets.', 'post-views-counter' ) . '</p>';
452 }
453
454 /**
455 * Section description: counter appearance.
456 *
457 * @return void
458 */
459 public function section_display_appearance() {
460 echo '<p class="description">' . esc_html__( 'Adjust the label, period, icon, and formatting used when the counter is rendered.', 'post-views-counter' ) . '</p>';
461 }
462
463 /**
464 * Section description: admin interface.
465 *
466 * @return void
467 */
468 public function section_display_admin() {
469 echo '<p class="description">' . esc_html__( 'Control how view counts are shown and managed in WordPress admin (columns, edit permissions, toolbar chart).', 'post-views-counter' ) . '</p>';
470 }
471
472 /**
473 * Mirror the saved menu position to legacy storage.
474 *
475 * @param mixed $old_value
476 * @param mixed $value
477 * @param string $option
478 * @return void
479 */
480 public function sync_menu_position_option( $old_value, $value, $option ) {
481 if ( self::$syncing_menu_position )
482 return;
483
484 self::$syncing_menu_position = true;
485 $this->mirror_menu_position_value( $value );
486 self::$syncing_menu_position = false;
487 }
488
489 /**
490 * Mirror the saved menu position when the option is added.
491 *
492 * @param string $option
493 * @param mixed $value
494 * @return void
495 */
496 public function sync_menu_position_option_on_add( $option, $value ) {
497 if ( self::$syncing_menu_position )
498 return;
499
500 self::$syncing_menu_position = true;
501 $this->mirror_menu_position_value( $value );
502 self::$syncing_menu_position = false;
503 }
504
505 /**
506 * Update the legacy menu position value stored under "Other" settings.
507 *
508 * @param mixed $value
509 * @return void
510 */
511 private function mirror_menu_position_value( $value ) {
512 if ( ! is_array( $value ) )
513 return;
514
515 $menu_position = isset( $value['menu_position'] ) && in_array( $value['menu_position'], [ 'top', 'sub' ], true ) ? $value['menu_position'] : 'top';
516 $other_options = get_option( 'post_views_counter_settings_other', [] );
517
518 if ( ! is_array( $other_options ) )
519 $other_options = [];
520
521 if ( ! isset( $other_options['menu_position'] ) || $other_options['menu_position'] !== $menu_position ) {
522 $other_options['menu_position'] = $menu_position;
523 update_option( 'post_views_counter_settings_other', $other_options );
524 }
525
526 $this->pvc->options['other']['menu_position'] = $menu_position;
527 }
528
529 /**
530 * Sync menu position from legacy writes to "Other" settings.
531 *
532 * @param mixed $value
533 * @param mixed $old_value
534 * @return mixed
535 */
536 public function sync_menu_position_from_other( $value, $old_value ) {
537 if ( self::$syncing_menu_position || ! is_array( $value ) || ! isset( $value['menu_position'] ) )
538 return $value;
539
540 $menu_position = in_array( $value['menu_position'], [ 'top', 'sub' ], true ) ? $value['menu_position'] : 'top';
541
542 self::$syncing_menu_position = true;
543
544 $display_options = get_option( 'post_views_counter_settings_display', [] );
545
546 if ( ! is_array( $display_options ) )
547 $display_options = [];
548
549 if ( ! isset( $display_options['menu_position'] ) || $display_options['menu_position'] !== $menu_position ) {
550 $display_options['menu_position'] = $menu_position;
551 update_option( 'post_views_counter_settings_display', $display_options );
552 }
553
554 $this->pvc->options['display']['menu_position'] = $menu_position;
555
556 self::$syncing_menu_position = false;
557
558 return $value;
559 }
560 }