PluginProbe
WebberZone Top 10 — Popular Posts / 4.3.4
WebberZone Top 10 — Popular Posts v4.3.4
4.5.1 4.5.0 4.4.3 4.4.2 4.4.1 4.4.0 4.3.4 4.3.3 4.3.2 4.3.1 4.3.0 trunk 1.0 1.0.1 1.1 1.2 1.3 1.4 1.4.1 1.5 1.5.1 1.5.2 1.5.3 1.6 1.6.1 All 117 releases
top-10 / includes / admin / class-settings.php

class-settings.php in WebberZone Top 10 — Popular Posts 4.3.4, at includes/admin/class-settings.php

1,816 lines 67.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Settings class.
4 *
5 * @package WebberZone\Top_Ten\Admin
6 */
7
8 namespace WebberZone\Top_Ten\Admin;
9
10 use WebberZone\Top_Ten\Admin\Settings\Settings_API;
11 use WebberZone\Top_Ten\Admin\Settings\Settings_Sanitize;
12 use WebberZone\Top_Ten\Frontend\Display;
13 use WebberZone\Top_Ten\Frontend\Media_Handler;
14 use WebberZone\Top_Ten\Util\Helpers;
15 use WebberZone\Top_Ten\Util\Hook_Registry;
16
17 // If this file is called directly, abort.
18 if ( ! defined( 'WPINC' ) ) {
19 die;
20 }
21
22 /**
23 * Class to register the settings.
24 *
25 * @since 3.3.0
26 */
27 class Settings {
28
29 /**
30 * Settings API.
31 *
32 * @since 3.3.0
33 *
34 * @var Settings_API Settings API.
35 */
36 public $settings_api;
37
38 /**
39 * Statistics table.
40 *
41 * @since 3.3.0
42 *
43 * @var object Statistics table.
44 */
45 public $statistics;
46
47 /**
48 * Prefix which is used for creating the unique filters and actions.
49 *
50 * @since 3.3.0
51 *
52 * @var string Prefix.
53 */
54 public static $prefix = 'tptn';
55
56 /**
57 * Settings Key.
58 *
59 * @since 3.3.0
60 *
61 * @var string Settings Key.
62 */
63 public $settings_key = 'tptn_settings';
64
65 /**
66 * The slug name to refer to this menu by (should be unique for this menu).
67 *
68 * @since 3.3.0
69 *
70 * @var string Menu slug.
71 */
72 public $menu_slug = 'tptn_options_page';
73
74 /**
75 * Main constructor class.
76 *
77 * @since 3.3.0
78 */
79 public function __construct() {
80 Hook_Registry::add_action( 'admin_menu', array( $this, 'initialise_settings' ) );
81 Hook_Registry::add_filter( 'plugin_row_meta', array( $this, 'plugin_row_meta' ), 11, 2 );
82 Hook_Registry::add_filter( 'plugin_action_links_' . plugin_basename( TOP_TEN_PLUGIN_FILE ), array( $this, 'plugin_actions_links' ) );
83 Hook_Registry::add_filter( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ), 99 );
84 Hook_Registry::add_filter( self::$prefix . '_settings_sanitize', array( $this, 'change_settings_on_save' ), 99 );
85 Hook_Registry::add_filter( self::$prefix . '_after_setting_output', array( $this, 'display_admin_thumbnail' ), 10, 2 );
86 Hook_Registry::add_filter( self::$prefix . '_setting_field_description', array( $this, 'reset_default_thumb_setting' ), 10, 2 );
87 Hook_Registry::add_filter( self::$prefix . '_settings_page_header', array( $this, 'settings_page_header' ) );
88 Hook_Registry::add_filter( self::$prefix . '_after_setting_output', array( $this, 'after_setting_output' ), 10, 2 );
89 Hook_Registry::add_action( self::$prefix . '_settings_form_buttons', array( $this, 'add_wizard_button' ) );
90
91 Hook_Registry::add_action( 'wp_ajax_nopriv_' . self::$prefix . '_taxonomy_search_tom_select', array( __CLASS__, 'taxonomy_search_tom_select' ) );
92 Hook_Registry::add_action( 'wp_ajax_' . self::$prefix . '_taxonomy_search_tom_select', array( __CLASS__, 'taxonomy_search_tom_select' ) );
93 }
94
95 /**
96 * Initialise the settings API.
97 *
98 * @since 3.3.0
99 */
100 public function initialise_settings() {
101 $props = array(
102 'default_tab' => 'general',
103 'help_sidebar' => $this->get_help_sidebar(),
104 'help_tabs' => $this->get_help_tabs(),
105 'admin_footer_text' => $this->get_admin_footer_text(),
106 'menus' => $this->get_menus(),
107 );
108
109 $args = array(
110 'props' => $props,
111 'translation_strings' => $this->get_translation_strings(),
112 'settings_sections' => $this->get_settings_sections(),
113 'registered_settings' => $this->get_registered_settings(),
114 'upgraded_settings' => array(),
115 );
116
117 $this->settings_api = new Settings_API( $this->settings_key, self::$prefix, $args );
118 }
119
120 /**
121 * Array containing the settings' sections.
122 *
123 * @since 1.8.0
124 *
125 * @return array Settings array
126 */
127 public function get_translation_strings() {
128 $strings = array(
129 'page_header' => esc_html__( 'Top 10 Settings', 'top-10' ),
130 'reset_message' => esc_html__( 'Settings have been reset to their default values. Reload this page to view the updated settings.', 'top-10' ),
131 'success_message' => esc_html__( 'Settings updated.', 'top-10' ),
132 'save_changes' => esc_html__( 'Save Changes', 'top-10' ),
133 'reset_settings' => esc_html__( 'Reset all settings', 'top-10' ),
134 'reset_button_confirm' => esc_html__( 'Do you really want to reset all these settings to their default values?', 'top-10' ),
135 'checkbox_modified' => esc_html__( 'Modified from default setting', 'top-10' ),
136 );
137
138 /**
139 * Filter the array containing the settings' sections.
140 *
141 * @since 1.8.0
142 *
143 * @param array $strings Translation strings.
144 */
145 return apply_filters( self::$prefix . '_translation_strings', $strings );
146 }
147
148 /**
149 * Get the admin menus.
150 *
151 * @return array Admin menus.
152 */
153 public function get_menus() {
154 $menus = array();
155
156 // Settings menu.
157 $menus[] = array(
158 'settings_page' => true,
159 'type' => 'submenu',
160 'parent_slug' => 'tptn_dashboard',
161 'page_title' => esc_html__( 'Top 10 Settings', 'top-10' ),
162 'menu_title' => esc_html__( 'Settings', 'top-10' ),
163 'menu_slug' => $this->menu_slug,
164 );
165
166 return $menus;
167 }
168
169 /**
170 * Array containing the settings' sections.
171 *
172 * @since 3.3.0
173 *
174 * @return array Settings array
175 */
176 public static function get_settings_sections() {
177 $settings_sections = array(
178 'general' => __( 'General', 'top-10' ),
179 'counter' => __( 'Counter/Tracker', 'top-10' ),
180 'list' => __( 'Posts list', 'top-10' ),
181 'thumbnail' => __( 'Thumbnail', 'top-10' ),
182 'styles' => __( 'Styles', 'top-10' ),
183 'maintenance' => __( 'Maintenance', 'top-10' ),
184 'feed' => __( 'Feed', 'top-10' ),
185 );
186
187 /**
188 * Filter the array containing the settings' sections.
189 *
190 * @since 1.2.0
191 *
192 * @param array $settings_sections Settings array
193 */
194 return apply_filters( self::$prefix . '_settings_sections', $settings_sections );
195 }
196
197
198 /**
199 * Retrieve the array of plugin settings
200 *
201 * @since 3.3.0
202 *
203 * @return array Settings array
204 */
205 public static function get_registered_settings() {
206 $settings = array();
207 $sections = self::get_settings_sections();
208
209 foreach ( $sections as $section => $value ) {
210 $method_name = 'settings_' . $section;
211 if ( method_exists( __CLASS__, $method_name ) ) {
212 $settings[ $section ] = self::$method_name();
213 }
214 }
215
216 /**
217 * Filters the settings array
218 *
219 * @since 1.2.0
220 *
221 * @param array $tptn_setings Settings array
222 */
223 return apply_filters( self::$prefix . '_registered_settings', $settings );
224 }
225
226 /**
227 * Retrieve the array of General settings
228 *
229 * @since 3.3.0
230 *
231 * @return array General settings array
232 */
233 public static function settings_general() {
234 $settings = array(
235 'cache' => array(
236 'id' => 'cache',
237 'name' => esc_html__( 'Enable cache', 'top-10' ),
238 'desc' => esc_html__( 'If activated, Top 10 will use the Transients API to cache the popular posts output for the time specified below.', 'top-10' ),
239 'type' => 'checkbox',
240 'default' => true,
241 ),
242 'cache_time' => array(
243 'id' => 'cache_time',
244 'name' => esc_html__( 'Time to cache', 'top-10' ),
245 'desc' => esc_html__( 'Enter the number of seconds to cache the output.', 'top-10' ),
246 'type' => 'text',
247 'default' => HOUR_IN_SECONDS,
248 ),
249 'uninstall_clean_options' => array(
250 'id' => 'uninstall_clean_options',
251 'name' => esc_html__( 'Delete options on uninstall', 'top-10' ),
252 'desc' => esc_html__( 'If this is checked, all settings related to Top 10 are removed from the database if you choose to uninstall/delete the plugin.', 'top-10' ),
253 'type' => 'checkbox',
254 'default' => true,
255 ),
256 'uninstall_clean_tables' => array(
257 'id' => 'uninstall_clean_tables',
258 'name' => esc_html__( 'Delete counter data on uninstall', 'top-10' ),
259 'desc' => esc_html__( 'If this is checked, the tables containing the counter statistics are removed from the database if you choose to uninstall/delete the plugin. Keep this unchecked if you choose to reinstall the plugin and do not want to lose your counter data.', 'top-10' ),
260 'type' => 'checkbox',
261 'default' => false,
262 ),
263 'show_credit' => array(
264 'id' => 'show_credit',
265 'name' => esc_html__( 'Link to Top 10 plugin page', 'top-10' ),
266 'desc' => esc_html__( 'A no-follow link to the plugin homepage will be added as the last item of the popular posts.', 'top-10' ),
267 'type' => 'checkbox',
268 'default' => false,
269 ),
270 'admin_header' => array(
271 'id' => 'admin_header',
272 'name' => '<h3>' . esc_html__( 'Admin settings', 'top-10' ) . '</h3>',
273 'desc' => '',
274 'type' => 'header',
275 ),
276 'show_metabox' => array(
277 'id' => 'show_metabox',
278 'name' => esc_html__( 'Show metabox', 'top-10' ),
279 'desc' => esc_html__( 'This will add the Top 10 metabox on Edit Posts or Add New Posts screens. Also applies to Pages and Custom Post Types.', 'top-10' ),
280 'type' => 'checkbox',
281 'default' => true,
282 ),
283 'show_metabox_admins' => array(
284 'id' => 'show_metabox_admins',
285 'name' => esc_html__( 'Limit meta box to Admins', 'top-10' ),
286 'desc' => esc_html__( 'If selected, the meta box will be hidden from anyone who is not an Admin. By default, Contributors and above will be able to see the meta box. Applies only if the above option is selected.', 'top-10' ),
287 'type' => 'checkbox',
288 'default' => false,
289 ),
290 'pv_in_admin' => array(
291 'id' => 'pv_in_admin',
292 'name' => esc_html__( 'Display admin columns', 'top-10' ),
293 /* translators: %s: Custom period label (e.g. Daily, Custom (7 days)). */
294 'desc' => sprintf( esc_html__( 'Adds three columns called Total Views, %s Views and Views. You can selectively disable these by pulling down the Screen Options from the top right of the respective screens.', 'top-10' ), Helpers::get_daily_range_label() ),
295 'type' => 'checkbox',
296 'default' => true,
297 ),
298 'admin_column_post_types' => array(
299 'id' => 'admin_column_post_types',
300 'name' => esc_html__( 'Display columns on post types', 'top-10' ),
301 'desc' => esc_html__( 'Select which post types to display the admin columns. Unselect the above option if you would like to disable the columns.', 'top-10' ),
302 'type' => 'posttypes',
303 'default' => 'post,page',
304 'pro' => true,
305 ),
306 'show_count_non_admins' => array(
307 'id' => 'show_count_non_admins',
308 'name' => esc_html__( 'Show views to non-admins', 'top-10' ),
309 'desc' => esc_html__( "If you disable this then non-admins won't see the above columns or view the independent pages with the top posts.", 'top-10' ),
310 'type' => 'checkbox',
311 'default' => true,
312 ),
313 'show_dashboard_to_roles' => array(
314 'id' => 'show_dashboard_to_roles',
315 'name' => esc_html__( 'Also show dashboard to', 'top-10' ),
316 'desc' => esc_html__( 'Choose the user roles that should have access to the Top 10 dashboard, which showcases popular posts over time. These roles are linked to specific capabilities, and selecting a lower role will automatically grant access to higher roles.', 'top-10' ),
317 'type' => 'multicheck',
318 'default' => 'administrator',
319 'options' => self::get_user_roles( array( 'administrator' ) ),
320 'pro' => true,
321 ),
322 'show_admin_bar' => array(
323 'id' => 'show_admin_bar',
324 'name' => esc_html__( 'Show Admin Bar menu', 'top-10' ),
325 'desc' => esc_html__( 'Display the Top 10 menu in the WordPress admin bar with quick access to popular posts stats and tools.', 'top-10' ),
326 'type' => 'checkbox',
327 'default' => true,
328 'pro' => true,
329 ),
330 'query_optimization' => array(
331 'id' => 'query_optimization',
332 'name' => '<h3>' . esc_html__( 'Query Optimization', 'top-10' ) . '</h3>',
333 'desc' => esc_html__( 'Settings for optimizing database queries', 'top-10' ),
334 'type' => 'header',
335 ),
336 'max_execution_time' => array(
337 'id' => 'max_execution_time',
338 'name' => esc_html__( 'Max Execution Time', 'top-10' ),
339 'desc' => esc_html__( 'Maximum execution time for MySQL queries in milliseconds. Set to 0 to disable. Default is 3000 (3 seconds).', 'top-10' ),
340 'type' => 'number',
341 'default' => 3000,
342 'min' => 0,
343 'step' => 100,
344 'pro' => true,
345 ),
346 );
347
348 /**
349 * Filters the General settings array
350 *
351 * @since 3.3.0
352 *
353 * @param array $settings General settings array
354 */
355 return apply_filters( self::$prefix . '_settings_general', $settings );
356 }
357
358
359 /**
360 * Retrieve the array of Counter settings
361 *
362 * @since 3.3.0
363 *
364 * @return array Counter settings array
365 */
366 public static function settings_counter() {
367 $settings = array(
368 'counter_header' => array(
369 'id' => 'counter_header',
370 'name' => '<h3>' . esc_html__( 'Counter settings', 'top-10' ) . '</h3>',
371 'desc' => '',
372 'type' => 'header',
373 ),
374 'add_to' => array(
375 'id' => 'add_to',
376 'name' => esc_html__( 'Display number of views on', 'top-10' ) . ':',
377 /* translators: 1: Code. */
378 'desc' => sprintf( esc_html__( 'If you choose to disable this, please add the following code to your template file where you want it displayed: %1$s', 'top-10' ), "<code>&lt;?php if ( function_exists( 'echo_tptn_post_count' ) ) { echo_tptn_post_count(); } ?&gt;</code>" ),
379 'type' => 'multicheck',
380 'default' => 'single,page',
381 'options' => array(
382 'single' => esc_html__( 'Posts', 'top-10' ),
383 'page' => esc_html__( 'Pages', 'top-10' ),
384 'home' => esc_html__( 'Home page', 'top-10' ),
385 'feed' => esc_html__( 'Feeds', 'top-10' ),
386 'category_archives' => esc_html__( 'Category archives', 'top-10' ),
387 'tag_archives' => esc_html__( 'Tag archives', 'top-10' ),
388 'other_archives' => esc_html__( 'Other archives', 'top-10' ),
389 ),
390 ),
391 'count_disp_form' => array(
392 'id' => 'count_disp_form',
393 'name' => esc_html__( 'Format to display the post views', 'top-10' ),
394 /* translators: 1: Opening a tag, 2: Closing a tag, 3: Opening code tage, 4. Closing code tag. */
395 'desc' => sprintf( esc_html__( 'Use %1$s to display the total count, %2$s for daily count and %3$s for overall counts across all posts. Default display is %4$s', 'top-10' ), '<code>%totalcount%</code>', '<code>%dailycount%</code>', '<code>%overallcount%</code>', '<code>Visited %totalcount% times, %dailycount% visit(s) today</code>' ),
396 'type' => 'textarea',
397 'default' => 'Visited %totalcount% times, %dailycount% visit(s) today',
398 ),
399 'count_disp_form_zero' => array(
400 'id' => 'count_disp_form_zero',
401 'name' => esc_html__( 'No visits text', 'top-10' ),
402 'desc' => esc_html__( "This text is displayed when there are no hits for the post and it isn't a single page. For example, if you display post views on the Homepage or Archives, this text will be used. To override this, simply enter the same text as the above option.", 'top-10' ),
403 'type' => 'textarea',
404 'default' => 'No visits yet',
405 ),
406 'number_format_count' => array(
407 'id' => 'number_format_count',
408 'name' => esc_html__( 'Number format post count', 'top-10' ),
409 'desc' => esc_html__( 'Activating this option will convert the post counts into a number format based on the locale', 'top-10' ),
410 'type' => 'checkbox',
411 'default' => true,
412 ),
413 'daily_midnight' => array(
414 'id' => 'daily_midnight',
415 'name' => esc_html__( 'Start daily counts at midnight', 'top-10' ),
416 'desc' => esc_html__( 'The daily counter displays visits starting at midnight. This option is enabled by default, similar to most standard counters. If you disable this option, you can use the hourly setting in the next option.', 'top-10' ),
417 'type' => 'checkbox',
418 'default' => true,
419 ),
420 'range_desc' => array(
421 'id' => 'range_desc',
422 'name' => esc_html__( 'Default custom period range', 'top-10' ),
423 'desc' => esc_html__( 'The following two options let you set the default range for the custom period. This can be overridden in the widget settings.', 'top-10' ),
424 'type' => 'descriptive_text',
425 ),
426 'daily_range' => array(
427 'id' => 'daily_range',
428 'name' => esc_html__( 'Day(s)', 'top-10' ),
429 'desc' => '',
430 'type' => 'number',
431 'default' => '1',
432 'min' => '0',
433 'size' => 'small',
434 ),
435 'hour_range' => array(
436 'id' => 'hour_range',
437 'name' => esc_html__( 'Hour(s)', 'top-10' ),
438 'desc' => '',
439 'type' => 'number',
440 'default' => '0',
441 'min' => '0',
442 'max' => '23',
443 'size' => 'small',
444 ),
445 'dynamic_post_count' => array(
446 'id' => 'dynamic_post_count',
447 'name' => esc_html__( 'Always display latest post count', 'top-10' ),
448 'desc' => esc_html__( 'This option uses JavaScript and will increase your page load time. Turn this off if you are not using caching plugins or are OK with displaying older cached counts.', 'top-10' ),
449 'type' => 'checkbox',
450 'default' => false,
451 ),
452 'exclude_on_post_ids' => array(
453 'id' => 'exclude_on_post_ids',
454 'name' => esc_html__( 'Exclude display on these post IDs', 'top-10' ),
455 'desc' => esc_html__( 'Comma-separated list of post or page IDs to exclude displaying the top posts on. e.g. 188,320,500', 'top-10' ),
456 'type' => 'numbercsv',
457 'default' => '',
458 ),
459 'tracker_header' => array(
460 'id' => 'tracker_header',
461 'name' => '<h3>' . esc_html__( 'Tracker settings', 'top-10' ) . '</h3>',
462 'desc' => '',
463 'type' => 'header',
464 ),
465 'trackers' => array(
466 'id' => 'trackers',
467 'name' => esc_html__( 'Enable trackers', 'top-10' ),
468 /* translators: 1: Code. */
469 'desc' => esc_html__( 'Top 10 tracks hits in two tables in the database. The overall table only tracks the total hits per post. The daily table tracks hits per post on an hourly basis.', 'top-10' ),
470 'type' => 'multicheck',
471 'default' => 'overall,daily',
472 'options' => array(
473 'overall' => esc_html__( 'Overall', 'top-10' ),
474 'daily' => esc_html__( 'Daily range', 'top-10' ),
475 ),
476 ),
477 'tracker_type' => array(
478 'id' => 'tracker_type',
479 'name' => esc_html__( 'Tracker type', 'top-10' ),
480 'desc' => '',
481 'type' => 'radiodesc',
482 'default' => 'query_based',
483 'options' => self::get_tracker_types(),
484 ),
485 'tracking_method' => array(
486 'id' => 'tracking_method',
487 'name' => esc_html__( 'Tracking method', 'top-10' ),
488 'desc' => esc_html__( 'Funnel tracking buffers views in a staging table that is merged into the count tables by a background job every few minutes and is recommended for most sites. Switch to Legacy tracking if view counts are not updating on your site, e.g. when WP-Cron is disabled or unreliable. Legacy tracking does not populate the visits log table.', 'top-10' ),
489 'type' => 'radiodesc',
490 'default' => 'funnel',
491 'options' => self::get_tracking_methods(),
492 ),
493 'tracker_all_pages' => array(
494 'id' => 'tracker_all_pages',
495 'name' => esc_html__( 'Load tracker on all pages', 'top-10' ),
496 'desc' => esc_html__( 'This will load the tracker js on all pages. Helpful if you are running minification/concatenation plugins.', 'top-10' ),
497 'type' => 'checkbox',
498 'default' => false,
499 ),
500 'track_feed_views' => array(
501 'id' => 'track_feed_views',
502 'name' => esc_html__( 'Track feed views', 'top-10' ),
503 'desc' => esc_html__( 'Count post views from RSS/Atom feed readers via a tracking pixel. Views are added to the post\'s regular view count. Note: some feed readers block remote images by default; views are only counted when the reader loads images.', 'top-10' ),
504 'type' => 'checkbox',
505 'default' => false,
506 'pro' => true,
507 ),
508 'track_users' => array(
509 'id' => 'track_users',
510 'name' => esc_html__( 'Track user groups', 'top-10' ) . ':',
511 'desc' => esc_html__( 'Uncheck above to disable tracking if the current user falls into any one of these groups.', 'top-10' ),
512 'type' => 'multicheck',
513 'default' => 'authors,editors,admins',
514 'options' => array(
515 'authors' => esc_html__( 'Authors', 'top-10' ),
516 'editors' => esc_html__( 'Editors', 'top-10' ),
517 'admins' => esc_html__( 'Admins', 'top-10' ),
518 ),
519 ),
520 'logged_in' => array(
521 'id' => 'logged_in',
522 'name' => esc_html__( 'Track logged-in users', 'top-10' ),
523 'desc' => esc_html__( 'Uncheck to stop tracking logged in users. Only logged out visitors will be tracked if this is disabled. Unchecking this will override the above setting.', 'top-10' ),
524 'type' => 'checkbox',
525 'default' => true,
526 ),
527 'no_bots' => array(
528 'id' => 'no_bots',
529 'name' => esc_html__( 'Do not track bots', 'top-10' ),
530 'desc' => esc_html__( 'Enable this if you want Top 10 to attempt to stop tracking bots. The plugin includes a comprehensive set of known bot user agents but in some cases this might not be enough to stop tracking bots.', 'top-10' ),
531 'type' => 'checkbox',
532 'default' => false,
533 ),
534 'debug_mode' => array(
535 'id' => 'debug_mode',
536 'name' => esc_html__( 'Debug mode', 'top-10' ),
537 'desc' => esc_html__( 'Setting this to true will force the tracker to display an output in the browser. This is useful if you are having issues and are seeking support.', 'top-10' ),
538 'type' => 'checkbox',
539 'default' => false,
540 ),
541 );
542
543 /**
544 * Filters the Counter settings array
545 *
546 * @since 3.3.0
547 *
548 * @param array $settings Counter settings array
549 */
550 return apply_filters( self::$prefix . '_settings_counter', $settings );
551 }
552
553
554 /**
555 * Retrieve the array of List settings
556 *
557 * @since 3.3.0
558 *
559 * @return array List settings array
560 */
561 public static function settings_list() {
562 $settings = array(
563 'use_global_settings' => array(
564 'id' => 'use_global_settings',
565 'name' => esc_html__( 'Use global settings in block', 'top-10' ),
566 'desc' => esc_html__( 'If activated, the settings from this page are automatically inserted in the Popular Posts block. This also applies to existing blocks which do not have any attributes set if the post is edited.', 'top-10' ),
567 'type' => 'checkbox',
568 'default' => false,
569 'pro' => true,
570 ),
571 'limit' => array(
572 'id' => 'limit',
573 'name' => esc_html__( 'Number of posts to display', 'top-10' ),
574 'desc' => esc_html__( 'Maximum number of posts that will be displayed in the list. This option is used if you do not specify the number of posts in the widget or shortcodes', 'top-10' ),
575 'type' => 'number',
576 'default' => '10',
577 'size' => 'small',
578 ),
579 'how_old' => array(
580 'id' => 'how_old',
581 'name' => esc_html__( 'Published age of posts', 'top-10' ),
582 'desc' => esc_html__( 'This options allows you to only show posts that have been published within the above day range. Applies to both overall posts and daily posts lists. e.g. 365 days will only show posts published in the last year in the popular posts lists. Enter 0 for no restriction.', 'top-10' ),
583 'type' => 'number',
584 'default' => '0',
585 'size' => 'small',
586 ),
587 'post_types' => array(
588 'id' => 'post_types',
589 'name' => esc_html__( 'Post types to include', 'top-10' ),
590 'desc' => esc_html__( 'At least one option should be selected above. Select which post types you want to include in the list of posts. This field can be overridden using a comma separated list of post types when using the manual display.', 'top-10' ),
591 'type' => 'posttypes',
592 'default' => 'post',
593 ),
594 'exclusion_header' => array(
595 'id' => 'exclusion_header',
596 'name' => '<h3>' . esc_html__( 'Exclusion settings', 'top-10' ) . '</h3>',
597 'desc' => '',
598 'type' => 'header',
599 ),
600 'exclude_front' => array(
601 'id' => 'exclude_front',
602 'name' => esc_html__( 'Exclude Front page and Posts page', 'top-10' ),
603 'desc' => esc_html__( 'If you have designated specific pages for your Front page and Posts page via Settings > Reading, they will be tracked like any other page. Enable this option to exclude them from appearing in the popular posts lists. Note that tracking will still occur.', 'top-10' ),
604 'type' => 'checkbox',
605 'default' => false,
606 ),
607 'exclude_current_post' => array(
608 'id' => 'exclude_current_post',
609 'name' => esc_html__( 'Exclude current post', 'top-10' ),
610 'desc' => esc_html__( 'Enabling this will exclude the current post being browsed from being displayed in the popular posts list.', 'top-10' ),
611 'type' => 'checkbox',
612 'default' => false,
613 ),
614 'exclude_post_ids' => array(
615 'id' => 'exclude_post_ids',
616 'name' => esc_html__( 'Post/page IDs to exclude', 'top-10' ),
617 'desc' => esc_html__( 'Comma-separated list of post or page IDs to exclude from the list. e.g. 188,320,500', 'top-10' ),
618 'type' => 'numbercsv',
619 'default' => '',
620 'size' => 'large',
621 ),
622 'exclude_cat_slugs' => array(
623 'id' => 'exclude_cat_slugs',
624 'name' => esc_html__( 'Exclude Categories', 'top-10' ),
625 'desc' => esc_html__( 'Comma separated list of category slugs. The field above has an autocomplete so simply start typing in the starting letters and it will prompt you with options. Does not support custom taxonomies.', 'top-10' ),
626 'type' => 'csv',
627 'default' => '',
628 'size' => 'large',
629 'field_class' => 'ts_autocomplete',
630 'field_attributes' => self::get_taxonomy_search_field_attributes( 'category' ),
631 ),
632 'exclude_categories' => array(
633 'id' => 'exclude_categories',
634 'name' => esc_html__( 'Exclude category IDs', 'top-10' ),
635 'desc' => esc_html__( 'This is a readonly field that is automatically populated based on the above input when the settings are saved. These might differ from the IDs visible in the Categories page which use the term_id. Top 10 uses the term_taxonomy_id which is unique to this taxonomy.', 'top-10' ),
636 'type' => 'text',
637 'default' => '',
638 'size' => 'large',
639 'readonly' => true,
640 ),
641 'exclude_terms_include_parents' => array(
642 'id' => 'exclude_terms_include_parents',
643 'name' => esc_html__( 'Include parent categories', 'top-10' ),
644 'desc' => esc_html__( 'Exclude popular posts from parent categories or all ancestors for nested categories.', 'top-10' ),
645 'type' => 'radio',
646 'default' => 'none',
647 'options' => array(
648 'none' => esc_html__( 'None', 'top-10' ),
649 'parent' => esc_html__( 'Only parent categories', 'top-10' ),
650 'all' => esc_html__( 'All ancestors', 'top-10' ),
651 ),
652 'pro' => true,
653 ),
654 'exclude_on_cat_slugs' => array(
655 'id' => 'exclude_on_cat_slugs',
656 'name' => esc_html__( 'Exclude on Categories', 'top-10' ),
657 'desc' => esc_html__( 'Comma separated list of category slugs. The field above has an autocomplete so simply start typing in the starting letters and it will prompt you with options. Does not support custom taxonomies.', 'top-10' ),
658 'type' => 'csv',
659 'default' => '',
660 'size' => 'large',
661 'field_class' => 'ts_autocomplete',
662 'field_attributes' => self::get_taxonomy_search_field_attributes( 'category' ),
663 ),
664 'customize_output_header' => array(
665 'id' => 'customize_output_header',
666 'name' => '<h3>' . esc_html__( 'Customize the output', 'top-10' ) . '</h3>',
667 'desc' => '',
668 'type' => 'header',
669 ),
670 'title' => array(
671 'id' => 'title',
672 'name' => esc_html__( 'Heading of posts', 'top-10' ),
673 'desc' => esc_html__( 'Displayed before the list of the posts as a the master heading', 'top-10' ),
674 'type' => 'text',
675 'default' => '<h3>' . esc_html__( 'Popular posts:', 'top-10' ) . '</h3>',
676 'size' => 'large',
677 ),
678 'title_daily' => array(
679 'id' => 'title_daily',
680 'name' => esc_html__( 'Heading of posts for daily/custom period lists', 'top-10' ),
681 'desc' => esc_html__( 'Displayed before the list of the posts as a the master heading', 'top-10' ),
682 'type' => 'text',
683 'default' => '<h3>' . esc_html__( 'Currently trending:', 'top-10' ) . '</h3>',
684 'size' => 'large',
685 ),
686 'blank_output' => array(
687 'id' => 'blank_output',
688 'name' => esc_html__( 'Show when no posts are found', 'top-10' ),
689 /* translators: 1: Code. */
690 'desc' => '',
691 'type' => 'radio',
692 'default' => 'blank',
693 'options' => array(
694 'blank' => esc_html__( 'Blank output', 'top-10' ),
695 'custom_text' => esc_html__( 'Display custom text', 'top-10' ),
696 ),
697 ),
698 'blank_output_text' => array(
699 'id' => 'blank_output_text',
700 'name' => esc_html__( 'Custom text', 'top-10' ),
701 'desc' => esc_html__( 'Enter the custom text that will be displayed if the second option is selected above', 'top-10' ),
702 'type' => 'textarea',
703 'default' => esc_html__( 'No top posts yet', 'top-10' ),
704 ),
705 'show_excerpt' => array(
706 'id' => 'show_excerpt',
707 'name' => esc_html__( 'Show post excerpt', 'top-10' ),
708 'desc' => '',
709 'type' => 'checkbox',
710 'default' => false,
711 ),
712 'excerpt_length' => array(
713 'id' => 'excerpt_length',
714 'name' => esc_html__( 'Length of excerpt (in words)', 'top-10' ),
715 'desc' => '',
716 'type' => 'number',
717 'default' => '10',
718 'size' => 'small',
719 ),
720 'show_date' => array(
721 'id' => 'show_date',
722 'name' => esc_html__( 'Show date', 'top-10' ),
723 'desc' => '',
724 'type' => 'checkbox',
725 'default' => false,
726 ),
727 'show_author' => array(
728 'id' => 'show_author',
729 'name' => esc_html__( 'Show author', 'top-10' ),
730 'desc' => '',
731 'type' => 'checkbox',
732 'default' => false,
733 ),
734 'disp_list_count' => array(
735 'id' => 'disp_list_count',
736 'name' => esc_html__( 'Show number of views', 'top-10' ),
737 'desc' => '',
738 'type' => 'checkbox',
739 'default' => false,
740 ),
741 'title_length' => array(
742 'id' => 'title_length',
743 'name' => esc_html__( 'Limit post title length (in characters)', 'top-10' ),
744 'desc' => '',
745 'type' => 'number',
746 'default' => '60',
747 'size' => 'small',
748 ),
749 'link_new_window' => array(
750 'id' => 'link_new_window',
751 'name' => esc_html__( 'Open links in new window', 'top-10' ),
752 'desc' => '',
753 'type' => 'checkbox',
754 'default' => false,
755 ),
756 'link_nofollow' => array(
757 'id' => 'link_nofollow',
758 'name' => esc_html__( 'Add nofollow to links', 'top-10' ),
759 'desc' => '',
760 'type' => 'checkbox',
761 'default' => false,
762 ),
763 'html_wrapper_header' => array(
764 'id' => 'html_wrapper_header',
765 'name' => '<h3>' . esc_html__( 'HTML to display', 'top-10' ) . '</h3>',
766 'desc' => '',
767 'type' => 'header',
768 ),
769 'before_list' => array(
770 'id' => 'before_list',
771 'name' => esc_html__( 'Before the list of posts', 'top-10' ),
772 'desc' => '',
773 'type' => 'text',
774 'default' => '<ul>',
775 'size' => 'large',
776 ),
777 'after_list' => array(
778 'id' => 'after_list',
779 'name' => esc_html__( 'After the list of posts', 'top-10' ),
780 'desc' => '',
781 'type' => 'text',
782 'default' => '</ul>',
783 'size' => 'large',
784 ),
785 'before_list_item' => array(
786 'id' => 'before_list_item',
787 'name' => esc_html__( 'Before each list item', 'top-10' ),
788 'desc' => '',
789 'type' => 'text',
790 'default' => '<li>',
791 'size' => 'large',
792 ),
793 'after_list_item' => array(
794 'id' => 'after_list_item',
795 'name' => esc_html__( 'After each list item', 'top-10' ),
796 'desc' => '',
797 'type' => 'text',
798 'default' => '</li>',
799 'size' => 'large',
800 ),
801 );
802
803 /**
804 * Filters the List settings array
805 *
806 * @since 3.3.0
807 *
808 * @param array $settings List settings array
809 */
810 return apply_filters( self::$prefix . '_settings_list', $settings );
811 }
812
813
814 /**
815 * Retrieve the array of Thumbnail settings
816 *
817 * @since 3.3.0
818 *
819 * @return array Thumbnail settings array
820 */
821 public static function settings_thumbnail() {
822 $settings = array(
823 'post_thumb_op' => array(
824 'id' => 'post_thumb_op',
825 'name' => esc_html__( 'Location of the post thumbnail', 'top-10' ),
826 'desc' => '',
827 'type' => 'radio',
828 'default' => 'text_only',
829 'options' => array(
830 'inline' => esc_html__( 'Display thumbnails inline with posts, before title', 'top-10' ),
831 'after' => esc_html__( 'Display thumbnails inline with posts, after title', 'top-10' ),
832 'thumbs_only' => esc_html__( 'Display only thumbnails, no text', 'top-10' ),
833 'text_only' => esc_html__( 'Do not display thumbnails, only text', 'top-10' ),
834 ),
835 ),
836 'thumb_size' => array(
837 'id' => 'thumb_size',
838 'name' => esc_html__( 'Thumbnail size', 'top-10' ),
839 'desc' => esc_html__( 'You can choose from existing image sizes above or create a custom size (tptn_thumbnail). If you select a custom size, enter the width, height, and crop settings below. For best results, use a cropped image. Note that changing the width and/or height below will not automatically resize existing images. You will need to regenerate the images using a plugin or WP CLI: wp media regenerate.', 'top-10' ),
840 'type' => 'thumbsizes',
841 'default' => 'tptn_thumbnail',
842 'options' => Media_Handler::get_all_image_sizes(),
843 ),
844 'thumb_width' => array(
845 'id' => 'thumb_width',
846 'name' => esc_html__( 'Thumbnail width', 'top-10' ),
847 'desc' => '',
848 'type' => 'number',
849 'default' => '250',
850 'size' => 'small',
851 ),
852 'thumb_height' => array(
853 'id' => 'thumb_height',
854 'name' => esc_html__( 'Thumbnail height', 'top-10' ),
855 'desc' => '',
856 'type' => 'number',
857 'default' => '250',
858 'size' => 'small',
859 ),
860 'thumb_crop' => array(
861 'id' => 'thumb_crop',
862 'name' => esc_html__( 'Hard crop thumbnails', 'top-10' ),
863 'desc' => esc_html__( 'Check this box to hard crop the thumbnails. i.e. force the width and height above vs. maintaining proportions.', 'top-10' ),
864 'type' => 'checkbox',
865 'default' => true,
866 ),
867 'thumb_create_sizes' => array(
868 'id' => 'thumb_create_sizes',
869 'name' => esc_html__( 'Generate thumbnail sizes', 'top-10' ),
870 'desc' => esc_html__( 'If you select this option and tptn_thumbnail is chosen above, the plugin will register the image size with WordPress to create new thumbnails. Note that this does not update old images, as explained above.', 'top-10' ),
871 'type' => 'checkbox',
872 'default' => true,
873 ),
874 'thumb_html' => array(
875 'id' => 'thumb_html',
876 'name' => esc_html__( 'Thumbnail size attributes', 'top-10' ),
877 'desc' => '',
878 'type' => 'radio',
879 'default' => 'html',
880 'options' => array(
881 /* translators: %s: Code. */
882 'css' => sprintf( esc_html__( 'Use CSS to set the width and height: e.g. %s', 'top-10' ), '<code>style="max-width:250px;max-height:250px"</code>' ),
883 /* translators: %s: Code. */
884 'html' => sprintf( esc_html__( 'Use HTML attributes to set the width and height: e.g. %s', 'top-10' ), '<code>width="250" height="250"</code>' ),
885 'none' => esc_html__( 'No width or height set. You will need to use external styles to force any width or height of your choice.', 'top-10' ),
886 ),
887 ),
888 'thumb_meta' => array(
889 'id' => 'thumb_meta',
890 'name' => esc_html__( 'Thumbnail meta field name', 'top-10' ),
891 'desc' => esc_html__( 'The value of this field should contain the URL of the image and can be set in the metabox in the Edit Post screen', 'top-10' ),
892 'type' => 'text',
893 'default' => 'post-image',
894 ),
895 'scan_images' => array(
896 'id' => 'scan_images',
897 'name' => esc_html__( 'Get first image', 'top-10' ),
898 'desc' => esc_html__( 'If enabled, the plugin will fetch the first image in the post content. Note that this might slow down your page loading time if the first image in the posts is large in file size.', 'top-10' ),
899 'type' => 'checkbox',
900 'default' => true,
901 ),
902 'thumb_default_show' => array(
903 'id' => 'thumb_default_show',
904 'name' => esc_html__( 'Use default thumbnail', 'top-10' ),
905 'desc' => esc_html__( 'Check this box to show the default thumbnail from the next option if no thumbnail is found from the post.', 'top-10' ),
906 'type' => 'checkbox',
907 'default' => true,
908 ),
909 'thumb_default' => array(
910 'id' => 'thumb_default',
911 'name' => esc_html__( 'Default thumbnail', 'top-10' ),
912 'desc' => esc_html__( 'Enter the full URL of the image that you wish to display if no thumbnail is found. This image will be displayed below.', 'top-10' ),
913 'type' => 'file',
914 'default' => Display::get_default_thumbnail(),
915 'size' => 'large',
916 ),
917 );
918
919 /**
920 * Filters the Thumbnail settings array
921 *
922 * @since 3.3.0
923 *
924 * @param array $settings Thumbnail settings array
925 */
926 return apply_filters( self::$prefix . '_settings_thumbnail', $settings );
927 }
928
929
930 /**
931 * Retrieve the array of Styles settings
932 *
933 * @since 3.3.0
934 *
935 * @return array Styles settings array
936 */
937 public static function settings_styles() {
938 $settings = array(
939 'tptn_styles' => array(
940 'id' => 'tptn_styles',
941 'name' => esc_html__( 'Popular posts style', 'top-10' ),
942 'desc' => '',
943 'type' => 'select',
944 'default' => 'no_style',
945 'options' => wp_list_pluck( self::get_styles(), 'name', 'id' ),
946 ),
947 'custom_css' => array(
948 'id' => 'custom_css',
949 'name' => esc_html__( 'Custom CSS', 'top-10' ),
950 'desc' => sprintf(
951 /* translators: 1: Opening a tag, 2: Closing a tag, 3: Opening code tage, 4. Closing code tag. */
952 esc_html__( 'Do not include %3$sstyle%4$s tags. Check out %1$sthis article%2$s for available CSS classes to style.', 'top-10' ),
953 '<a href="' . esc_url( 'https://webberzone.com/support/knowledgebase/using-and-customising-top-10/' ) . '" target="_blank">',
954 '</a>',
955 '<code>',
956 '</code>'
957 ),
958 'type' => 'css',
959 'default' => '',
960 'field_class' => 'codemirror_css',
961 ),
962 );
963
964 /**
965 * Filters the Styles settings array
966 *
967 * @since 3.3.0
968 *
969 * @param array $settings Styles settings array
970 */
971 return apply_filters( self::$prefix . '_settings_styles', $settings );
972 }
973
974
975 /**
976 * Retrieve the array of Maintenance settings
977 *
978 * @since 3.3.0
979 *
980 * @return array Maintenance settings array
981 */
982 public static function settings_maintenance() {
983 $settings = array(
984 'cron_on' => array(
985 'id' => 'cron_on',
986 'name' => esc_html__( 'Enable scheduled maintenance', 'top-10' ),
987 'desc' => sprintf(
988 /* translators: 1: Constant holding number of days data is stored. */
989 esc_html__( 'Regularly cleaning the database can enhance performance, especially for high-traffic blogs. Enabling maintenance will automatically delete entries older than %s days from the daily tables.', 'top-10' ),
990 '<strong>' . (int) apply_filters( 'tptn_maintenance_days', TOP_TEN_STORE_DATA ) . '</strong>'
991 ),
992 'type' => 'checkbox',
993 'default' => false,
994 ),
995 'maintenance_days' => array(
996 'id' => 'maintenance_days',
997 'name' => esc_html__( 'Daily table retention period', 'top-10' ),
998 'desc' => sprintf(
999 /* translators: 1: Constant holding number of days data is stored. */
1000 esc_html__( 'Enter the number of days to retain data in the daily tables before scheduled maintenance removes older entries. Enter 0 to use the default value of %s days.', 'top-10' ),
1001 '<strong>' . TOP_TEN_STORE_DATA . '</strong>'
1002 ),
1003 'type' => 'number',
1004 'default' => 0,
1005 'min' => 0,
1006 'size' => 'small',
1007 'pro' => true,
1008 ),
1009 'log_retention_days' => array(
1010 'id' => 'log_retention_days',
1011 'name' => esc_html__( 'Visits log retention period', 'top-10' ),
1012 'desc' => sprintf(
1013 /* translators: 1: Default number of days for visits log retention. */
1014 esc_html__( 'Enter the number of days to retain raw visit rows in the visits log table. The log stores one row per visit and grows faster than the daily table, so a shorter window is recommended. Enter 0 to use the default value of %s days.', 'top-10' ),
1015 '<strong>' . TOP_TEN_LOG_STORE_DATA . '</strong>'
1016 ),
1017 'type' => 'number',
1018 'default' => 0,
1019 'min' => 0,
1020 'size' => 'small',
1021 'pro' => true,
1022 ),
1023 'cron_range_desc' => array(
1024 'id' => 'cron_range_desc',
1025 'name' => esc_html__( 'Time to run maintenance', 'top-10' ),
1026 'desc' => esc_html__( 'The next two options allow you to set the time to run the cron.', 'top-10' ),
1027 'type' => 'descriptive_text',
1028 ),
1029 'cron_hour' => array(
1030 'id' => 'cron_hour',
1031 'name' => esc_html__( 'Hour', 'top-10' ),
1032 'desc' => '',
1033 'type' => 'number',
1034 'default' => '0',
1035 'min' => '0',
1036 'max' => '23',
1037 'size' => 'small',
1038 ),
1039 'cron_min' => array(
1040 'id' => 'cron_min',
1041 'name' => esc_html__( 'Minute', 'top-10' ),
1042 'desc' => '',
1043 'type' => 'number',
1044 'default' => '0',
1045 'min' => '0',
1046 'max' => '59',
1047 'size' => 'small',
1048 ),
1049 'cron_recurrence' => array(
1050 'id' => 'cron_recurrence',
1051 'name' => esc_html__( 'Run maintenance', 'top-10' ),
1052 'desc' => '',
1053 'type' => 'radio',
1054 'default' => 'weekly',
1055 'options' => array(
1056 'daily' => esc_html__( 'Daily', 'top-10' ),
1057 'weekly' => esc_html__( 'Weekly', 'top-10' ),
1058 'fortnightly' => esc_html__( 'Fortnightly', 'top-10' ),
1059 'monthly' => esc_html__( 'Monthly', 'top-10' ),
1060 ),
1061 ),
1062 );
1063
1064 /**
1065 * Filters the Maintenance settings array
1066 *
1067 * @since 3.3.0
1068 *
1069 * @param array $settings Maintenance settings array
1070 */
1071 return apply_filters( self::$prefix . '_settings_maintenance', $settings );
1072 }
1073
1074
1075 /**
1076 * Retrieve the array of Feed settings
1077 *
1078 * @since 2.8.0
1079 *
1080 * @return array Feed settings array
1081 */
1082 public static function settings_feed() {
1083 $settings = array(
1084 'feed_permalink_overall' => array(
1085 'id' => 'feed_permalink_overall',
1086 'name' => esc_html__( 'Permalink - Overall', 'top-10' ),
1087 /* translators: 1: Opening link tag, 2: Closing link tag. */
1088 'desc' => sprintf( esc_html__( 'This will set the path of the custom feed generated by the plugin for overall popular posts. You might need to %1$srefresh your permalinks%2$s when changing this option.', 'top-10' ), '<a href="' . admin_url( 'options-permalink.php' ) . '" target="_blank">', '</a>' ),
1089 'type' => 'text',
1090 'default' => 'popular-posts',
1091 'size' => 'large',
1092 ),
1093 'feed_permalink_daily' => array(
1094 'id' => 'feed_permalink_daily',
1095 /* translators: %s: Custom period label (e.g. Daily, Custom (7 days)). */
1096 'name' => sprintf( esc_html__( 'Permalink - %s', 'top-10' ), Helpers::get_daily_range_label( (int) \tptn_get_option( 'feed_daily_range', 1 ) ) ),
1097 /* translators: 1: Opening link tag, 2: Closing link tag. */
1098 'desc' => sprintf( esc_html__( 'This will set the path of the custom feed generated by the plugin for daily/custom period popular posts. You might need to %1$srefresh your permalinks%2$s when changing this option.', 'top-10' ), '<a href="' . admin_url( 'options-permalink.php' ) . '" target="_blank">', '</a>' ),
1099 'type' => 'text',
1100 'default' => 'popular-posts-daily',
1101 'size' => 'large',
1102 ),
1103 'feed_limit' => array(
1104 'id' => 'feed_limit',
1105 'name' => esc_html__( 'Number of posts to display', 'top-10' ),
1106 'desc' => esc_html__( 'Maximum number of posts that will be displayed in the custom feed.', 'top-10' ),
1107 'type' => 'number',
1108 'default' => '10',
1109 'size' => 'small',
1110 ),
1111 'feed_daily_range' => array(
1112 'id' => 'feed_daily_range',
1113 'name' => esc_html__( 'Custom period in day(s)', 'top-10' ),
1114 'desc' => '',
1115 'type' => 'number',
1116 'default' => '1',
1117 'min' => '0',
1118 'size' => 'small',
1119 ),
1120 'feed_category_slugs' => array(
1121 'id' => 'feed_category_slugs',
1122 'name' => esc_html__( 'Filter Categories', 'top-10' ),
1123 'desc' => esc_html__( 'Comma separated list of category slugs to include in the feed. The field has an autocomplete so simply start typing the starting letters and it will prompt you with options. Leave blank to include all categories.', 'top-10' ),
1124 'type' => 'csv',
1125 'default' => '',
1126 'size' => 'large',
1127 'field_class' => 'ts_autocomplete',
1128 'field_attributes' => self::get_taxonomy_search_field_attributes( 'category' ),
1129 'pro' => true,
1130 ),
1131 );
1132
1133 /**
1134 * Filters the Feed settings array
1135 *
1136 * @since 2.8.0
1137 *
1138 * @param array $settings Feed settings array
1139 */
1140 return apply_filters( self::$prefix . '_settings_feed', $settings );
1141 }
1142
1143 /**
1144 * Get the various styles.
1145 *
1146 * @since 2.5.0
1147 * @return array Style options.
1148 */
1149 public static function get_styles() {
1150 $styles = array(
1151 array(
1152 'id' => 'no_style',
1153 'name' => esc_html__( 'No styles', 'top-10' ),
1154 'description' => esc_html__( 'Select this option if you plan to add your own styles', 'top-10' ) . '<br />',
1155 ),
1156 array(
1157 'id' => 'text_only',
1158 'name' => esc_html__( 'Text only', 'top-10' ),
1159 'description' => esc_html__( 'Disable thumbnails and no longer include the default style sheet included in the plugin', 'top-10' ) . '<br />',
1160 ),
1161 array(
1162 'id' => 'left_thumbs',
1163 'name' => esc_html__( 'Left thumbnails', 'top-10' ),
1164 'description' => esc_html__( 'Enabling this option will set the post thumbnail to be before text. Disabling this option will not revert any settings.', 'top-10' ),
1165 ),
1166 );
1167
1168 /**
1169 * Filter the array containing the types of styles to add your own.
1170 *
1171 * @since 2.5.0
1172 *
1173 * @param array $styles Different styles.
1174 */
1175 return apply_filters( self::$prefix . '_get_styles', $styles );
1176 }
1177
1178 /**
1179 * Get User Roles.
1180 *
1181 * @since 4.0.0
1182 *
1183 * @param array $remove_roles Roles to remove.
1184 * @return array User roles in the format 'role' => 'name'.
1185 */
1186 public static function get_user_roles( $remove_roles = array() ) {
1187 // Get the global $wp_roles object using the wp_roles() function.
1188 $wp_roles = wp_roles();
1189
1190 // Initialize the array to store roles in the desired format.
1191 $roles_array = array();
1192
1193 if ( ! empty( $wp_roles->roles ) ) {
1194 // Loop through all roles and store them in 'role' => 'name' format.
1195 foreach ( $wp_roles->roles as $role_key => $role_details ) {
1196 if ( in_array( $role_key, $remove_roles, true ) ) {
1197 continue;
1198 }
1199 $roles_array[ $role_key ] = esc_html( $role_details['name'] );
1200 }
1201 } else {
1202 // Fallback to default WordPress roles.
1203 $default_roles = array(
1204 'administrator' => esc_html__( 'Administrator' ),
1205 'editor' => esc_html__( 'Editor' ),
1206 'author' => esc_html__( 'Author' ),
1207 'contributor' => esc_html__( 'Contributor' ),
1208 'subscriber' => esc_html__( 'Subscriber' ),
1209 );
1210
1211 foreach ( $default_roles as $role_key => $role_name ) {
1212 if ( ! in_array( $role_key, $remove_roles, true ) ) {
1213 $roles_array[ $role_key ] = $role_name;
1214 }
1215 }
1216 }
1217
1218 return $roles_array;
1219 }
1220
1221 /**
1222 * Adding WordPress plugin action links.
1223 *
1224 * @since 3.3.0
1225 *
1226 * @param array $links Array of links.
1227 * @return array
1228 */
1229 public function plugin_actions_links( $links ) {
1230
1231 return array_merge(
1232 array(
1233 'settings' => '<a href="' . admin_url( 'admin.php?page=' . $this->menu_slug ) . '">' . esc_html__( 'Settings', 'top-10' ) . '</a>',
1234 ),
1235 $links
1236 );
1237 }
1238
1239 /**
1240 * Add meta links on Plugins page.
1241 *
1242 * @since 3.3.0
1243 *
1244 * @param array $links Array of Links.
1245 * @param string $file Current file.
1246 * @return array
1247 */
1248 public function plugin_row_meta( $links, $file ) {
1249
1250 if ( false !== strpos( $file, 'top-10.php' ) ) {
1251 $new_links = array(
1252 'support' => '<a href = "https://wordpress.org/support/plugin/top-10">' . esc_html__( 'Support', 'top-10' ) . '</a>',
1253 'donate' => '<a href = "https://wzn.io/donate-wz">' . esc_html__( 'Donate', 'top-10' ) . '</a>',
1254 'contribute' => '<a href = "https://github.com/WebberZone/top-10">' . esc_html__( 'Contribute', 'top-10' ) . '</a>',
1255 );
1256
1257 $links = array_merge( $links, $new_links );
1258 }
1259 return $links;
1260 }
1261
1262 /**
1263 * Get the help sidebar content to display on the plugin settings page.
1264 *
1265 * @since 1.8.0
1266 */
1267 public function get_help_sidebar() {
1268 $help_sidebar =
1269 /* translators: 1: Plugin support site link. */
1270 '<p>' . sprintf( __( 'For more information or how to get support visit the <a href="%s">support site</a>.', 'top-10' ), esc_url( 'https://webberzone.com/support/' ) ) . '</p>' .
1271 /* translators: 1: WordPress.org support forums link. */
1272 '<p>' . sprintf( __( 'Support queries should be posted in the <a href="%s">WordPress.org support forums</a>.', 'top-10' ), esc_url( 'https://wordpress.org/support/plugin/top-10' ) ) . '</p>' .
1273 '<p>' . sprintf(
1274 /* translators: 1: Github issues link, 2: Github plugin page link. */
1275 __( '<a href="%1$s">Post an issue</a> on <a href="%2$s">GitHub</a> (bug reports only).', 'top-10' ),
1276 esc_url( 'https://github.com/ajaydsouza/top-10/issues' ),
1277 esc_url( 'https://github.com/ajaydsouza/top-10' )
1278 ) . '</p>';
1279
1280 /**
1281 * Filter to modify the help sidebar content.
1282 *
1283 * @since 1.8.0
1284 *
1285 * @param string $help_sidebar Help sidebar content.
1286 */
1287 return apply_filters( self::$prefix . '_settings_help', $help_sidebar );
1288 }
1289
1290 /**
1291 * Get the help tabs to display on the plugin settings page.
1292 *
1293 * @since 1.8.0
1294 */
1295 public function get_help_tabs() {
1296 $help_tabs = array(
1297 array(
1298 'id' => 'tptn-settings-general-help',
1299 'title' => esc_html__( 'General', 'top-10' ),
1300 'content' =>
1301 '<p><strong>' . esc_html__( 'This tab provides global settings for how Top 10 works across your site.', 'top-10' ) . '</strong></p>' .
1302 '<p>' . esc_html__( 'Configure caching, uninstall behaviour, admin columns, the dashboard, and other high-level options. You must click the Save Changes button at the bottom of the screen for new settings to take effect.', 'top-10' ) . '</p>',
1303 ),
1304 array(
1305 'id' => 'tptn-settings-counter-help',
1306 'title' => esc_html__( 'Counter / Tracker', 'top-10' ),
1307 'content' =>
1308 '<p><strong>' . esc_html__( 'This tab controls how Top 10 tracks and displays view counts.', 'top-10' ) . '</strong></p>' .
1309 '<p>' . esc_html__( 'Choose where to display view counts, how they are formatted, which visits are tracked, and how the tracker works (REST API, query variables, or Ajax).', 'top-10' ) . '</p>',
1310 ),
1311 array(
1312 'id' => 'tptn-settings-list-help',
1313 'title' => esc_html__( 'Posts list', 'top-10' ),
1314 'content' =>
1315 '<p><strong>' . esc_html__( 'This tab controls the popular posts list output.', 'top-10' ) . '</strong></p>' .
1316 '<p>' . esc_html__( 'Configure the number of posts, age of posts, post types, exclusions, and what information is shown (title, date, author, views, excerpts, and HTML wrappers).', 'top-10' ) . '</p>',
1317 ),
1318 array(
1319 'id' => 'tptn-settings-thumbnail-help',
1320 'title' => esc_html__( 'Thumbnail', 'top-10' ),
1321 'content' =>
1322 '<p><strong>' . esc_html__( 'This tab manages thumbnail settings for the popular posts list.', 'top-10' ) . '</strong></p>' .
1323 '<p>' . esc_html__( 'Choose whether thumbnails are displayed, select the image size, configure custom dimensions, cropping, the default thumbnail, and fallback behaviour when no thumbnail is found.', 'top-10' ) . '</p>',
1324 ),
1325 array(
1326 'id' => 'tptn-settings-styles-help',
1327 'title' => esc_html__( 'Styles', 'top-10' ),
1328 'content' =>
1329 '<p><strong>' . esc_html__( 'This tab controls how the popular posts list is styled.', 'top-10' ) . '</strong></p>' .
1330 '<p>' . esc_html__( 'Select a built-in style or disable styles if you want to fully control the look using your own CSS.', 'top-10' ) . '</p>',
1331 ),
1332 array(
1333 'id' => 'tptn-settings-maintenance-help',
1334 'title' => esc_html__( 'Maintenance', 'top-10' ),
1335 'content' =>
1336 '<p><strong>' . esc_html__( 'This tab handles tools and maintenance-related options.', 'top-10' ) . '</strong></p>' .
1337 '<p>' . esc_html__( 'Use this screen to reset settings, clear cached popular posts, and manage database-related options such as pruning or rebuilding counts.', 'top-10' ) . '</p>',
1338 ),
1339 array(
1340 'id' => 'tptn-settings-feed-help',
1341 'title' => esc_html__( 'Feed', 'top-10' ),
1342 'content' =>
1343 '<p><strong>' . esc_html__( 'This tab controls how Top 10 content appears in your site feed.', 'top-10' ) . '</strong></p>' .
1344 '<p>' . esc_html__( 'Configure copyright text and additional HTML that is added before or after the content in your feeds.', 'top-10' ) . '</p>',
1345 ),
1346 );
1347
1348 /**
1349 * Filter to add more help tabs.
1350 *
1351 * @since 1.8.0
1352 *
1353 * @param array $help_tabs Associative array of help tabs.
1354 */
1355 return apply_filters( self::$prefix . '_settings_help', $help_tabs );
1356 }
1357
1358 /**
1359 * Function returns the different types of trackers.
1360 *
1361 * @since 3.3.0
1362 * @return array Tracker types.
1363 */
1364 public static function get_tracker_types() {
1365
1366 $trackers = array(
1367 array(
1368 'id' => 'rest_based',
1369 'name' => __( 'REST API based', 'top-10' ),
1370 'description' => __( 'Uses the REST API to record visits', 'top-10' ),
1371 ),
1372 array(
1373 'id' => 'query_based',
1374 'name' => __( 'Query variable based', 'top-10' ),
1375 'description' => __( 'Uses query variables to record visits', 'top-10' ),
1376 ),
1377 array(
1378 'id' => 'ajaxurl',
1379 'name' => __( 'Ajaxurl based', 'top-10' ),
1380 'description' => __( 'Uses admin-ajax.php which is inbuilt within WordPress to process the tracker', 'top-10' ),
1381 ),
1382 );
1383
1384 /**
1385 * Filter the array containing the types of trackers to add your own.
1386 *
1387 * @since 2.4.0
1388 *
1389 * @param array $trackers Different trackers.
1390 */
1391 return apply_filters( self::$prefix . '_get_tracker_types', $trackers );
1392 }
1393
1394 /**
1395 * Function returns the different tracking methods.
1396 *
1397 * @since 4.3.3
1398 * @return array Tracking methods.
1399 */
1400 public static function get_tracking_methods() {
1401
1402 $methods = array(
1403 array(
1404 'id' => 'funnel',
1405 'name' => __( 'Funnel tracking', 'top-10' ),
1406 'description' => __( 'Views are buffered and merged into the count tables every few minutes (default)', 'top-10' ),
1407 ),
1408 array(
1409 'id' => 'legacy',
1410 'name' => __( 'Legacy tracking', 'top-10' ),
1411 'description' => __( 'Views are written directly to the count tables on every visit (behaviour of versions before 4.3)', 'top-10' ),
1412 ),
1413 );
1414
1415 /**
1416 * Filter the array containing the tracking methods.
1417 *
1418 * @since 4.3.3
1419 *
1420 * @param array $methods Tracking methods.
1421 */
1422 return apply_filters( self::$prefix . '_get_tracking_methods', $methods );
1423 }
1424
1425
1426
1427
1428 /**
1429 * Add footer text on the plugin page.
1430 *
1431 * @since 2.0.0
1432 */
1433 public static function get_admin_footer_text() {
1434 return sprintf(
1435 /* translators: 1: Opening achor tag with Plugin page link, 2: Closing anchor tag, 3: Opening anchor tag with review link. */
1436 __( 'Thank you for using %1$sWebberZone Top 10%2$s! Please %3$srate us%2$s on WordPress.org', 'top-10' ),
1437 '<a href="https://webberzone.com/plugins/top-10/" target="_blank">',
1438 '</a>',
1439 '<a href="https://wordpress.org/support/plugin/top-10/reviews/#new-post" target="_blank">'
1440 );
1441 }
1442
1443 /**
1444 * Enqueue scripts and styles.
1445 *
1446 * @since 3.3.0
1447 *
1448 * @param string $hook Current hook.
1449 */
1450 public function admin_enqueue_scripts( $hook ) {
1451
1452 if ( ! isset( $this->settings_api->settings_page ) || $this->settings_api->settings_page !== $hook ) {
1453 return;
1454 }
1455
1456 wp_enqueue_script( 'top-ten-admin-js' );
1457 wp_enqueue_style( 'top-ten-admin-css' );
1458 wp_enqueue_style( 'wp-spinner' );
1459 wp_localize_script(
1460 'top-ten-admin-js',
1461 'tptn_admin',
1462 array(
1463 'thumb_default' => Display::get_default_thumbnail(),
1464 )
1465 );
1466 wp_localize_script(
1467 'top-ten-admin-js',
1468 'top_ten_admin_data',
1469 array(
1470 'security' => wp_create_nonce( 'tptn-admin' ),
1471 'strings' => array(
1472 'confirm_message' => esc_html__( 'Are you sure you want to clear the cache?', 'top-10' ),
1473 'clearing_text' => esc_html__( 'Clearing...', 'top-10' ),
1474 'success_message' => esc_html__( 'Cache cleared successfully!', 'top-10' ),
1475 'fail_message' => esc_html__( 'Failed to clear cache. Please try again.', 'top-10' ),
1476 'request_fail_message' => esc_html__( 'Request failed: ', 'top-10' ),
1477 'left_thumbs_message' => esc_html__( 'Note: This setting cannot be changed as the Popular posts style is set to Left thumbnails. You can change the style in the Styles tab.', 'top-10' ),
1478 'grid_thumbs_message' => esc_html__( 'Note: This setting cannot be changed as the Popular posts style is set to Grid thumbnails. You can change the style in the Styles tab.', 'top-10' ),
1479 'text_only_message' => esc_html__( 'Note: This setting cannot be changed as the Popular posts style is set to Text only. You can change the style in the Styles tab.', 'top-10' ),
1480 ),
1481 )
1482 );
1483 }
1484
1485 /**
1486 * Modify settings when they are being saved.
1487 *
1488 * @since 3.3.0
1489 *
1490 * @param array $settings Settings array.
1491 * @return array Sanitized settings array.
1492 */
1493 public function change_settings_on_save( $settings ) {
1494
1495 Settings_Sanitize::sanitize_tax_slugs( $settings, 'exclude_cat_slugs', 'exclude_categories' );
1496 Settings_Sanitize::sanitize_tax_slugs( $settings, 'exclude_on_cat_slugs', 'exclude_on_categories' );
1497 Settings_Sanitize::sanitize_tax_slugs( $settings, 'feed_category_slugs', 'feed_categories' );
1498
1499 // Save cron settings.
1500 $settings['cron_hour'] = min( 23, absint( $settings['cron_hour'] ) );
1501 $settings['cron_min'] = min( 59, absint( $settings['cron_min'] ) );
1502
1503 if ( ! empty( $settings['cron_on'] ) ) {
1504 \WebberZone\Top_Ten\Admin\Cron::enable_run( $settings['cron_hour'], $settings['cron_min'], $settings['cron_recurrence'] );
1505 } else {
1506 \WebberZone\Top_Ten\Admin\Cron::disable_run();
1507 }
1508
1509 // Force thumb_width and thumb_height if either are zero.
1510 if ( empty( $settings['thumb_width'] ) || empty( $settings['thumb_height'] ) ) {
1511 list( $settings['thumb_width'], $settings['thumb_height'] ) = Media_Handler::get_thumb_size( $settings['thumb_size'] );
1512 }
1513
1514 // Overwrite settings based on selected style; guard against missing keys on reset.
1515 $style = $settings['tptn_styles'] ?? '';
1516
1517 $inline_thumb_styles = array(
1518 'left_thumbs' => esc_html__( 'Note: Thumbnail location can only be Inline or Thumbnails only when the Popular posts style is set to Left thumbnails. You can change the style in the Styles tab.', 'top-10' ),
1519 'grid_thumbs' => esc_html__( 'Note: Thumbnail location can only be Inline or Thumbnails only when the Popular posts style is set to Grid thumbnails. You can change the style in the Styles tab.', 'top-10' ),
1520 );
1521
1522 if ( array_key_exists( $style, $inline_thumb_styles ) ) {
1523 $post_thumb_op = $settings['post_thumb_op'] ?? '';
1524 if ( 'inline' !== $post_thumb_op && 'thumbs_only' !== $post_thumb_op ) {
1525 $settings['post_thumb_op'] = 'inline';
1526 }
1527
1528 add_settings_error(
1529 self::$prefix . '-notices',
1530 '',
1531 $inline_thumb_styles[ $style ],
1532 'warning'
1533 );
1534 }
1535 // Overwrite settings if text_only style is selected.
1536 if ( 'text_only' === $style ) {
1537 $settings['post_thumb_op'] = 'text_only';
1538
1539 add_settings_error(
1540 self::$prefix . '-notices',
1541 '',
1542 esc_html__( 'Note: Thumbnail location set to Text only as the Popular posts style is set to Text only. You can change the style in the Styles tab.', 'top-10' ),
1543 'warning'
1544 );
1545 }
1546
1547 // Delete the cache.
1548 \WebberZone\Top_Ten\Util\Cache::delete();
1549
1550 return $settings;
1551 }
1552
1553 /**
1554 * Display the default thumbnail below the setting.
1555 *
1556 * @since 3.3.0
1557 *
1558 * @param string $html Current HTML.
1559 * @param array $args Argument array of the setting.
1560 * @return string
1561 */
1562 public function display_admin_thumbnail( $html, $args ) {
1563
1564 $thumb_default = \tptn_get_option( 'thumb_default' );
1565
1566 if ( 'thumb_default' === $args['id'] && '' !== $thumb_default ) {
1567 $html .= '<br />';
1568 $html .= sprintf( '<img src="%1$s" style="max-width:200px" title="%2$s" alt="%2$s" />', esc_attr( $thumb_default ), esc_html__( 'Default thumbnail', 'top-10' ) );
1569 }
1570
1571 return $html;
1572 }
1573
1574 /**
1575 * Display the default thumbnail below the setting.
1576 *
1577 * @since 3.3.0
1578 *
1579 * @param string $html Current HTML.
1580 * @param array $args Argument array of the setting.
1581 * @return string
1582 */
1583 public function reset_default_thumb_setting( $html, $args ) {
1584
1585 $thumb_default = \tptn_get_option( 'thumb_default' );
1586
1587 if ( 'thumb_default' === $args['id'] && Display::get_default_thumbnail() !== $thumb_default ) {
1588 $html = '<span class="dashicons dashicons-undo reset-default-thumb" style="cursor: pointer;" title="' . __( 'Reset' ) . '"></span> <br />' . $html;
1589 }
1590
1591 return $html;
1592 }
1593
1594 /**
1595 * Add a link to the Tools page from the settings page.
1596 *
1597 * @since 3.5.0
1598 */
1599 public static function settings_page_header() {
1600 global $tptn_freemius;
1601 ?>
1602 <p>
1603 <?php if ( ! $tptn_freemius->is_paying() ) { ?>
1604 <a class="tptn_button tptn_button_gold" href="<?php echo esc_url( $tptn_freemius->get_upgrade_url() ); ?>">
1605 <?php esc_html_e( 'Upgrade to Pro', 'top-10' ); ?>
1606 </a>
1607 <?php } ?>
1608 </p>
1609
1610 <?php
1611 }
1612
1613 /**
1614 * Updated the settings fields to display a pro version link.
1615 *
1616 * @param string $output Settings field HTML.
1617 * @param array $args Settings field arguments.
1618 * @return string Updated HTML.
1619 */
1620 public static function after_setting_output( $output, $args ) {
1621 if ( isset( $args['pro'] ) && $args['pro'] ) {
1622 $output .= sprintf(
1623 '<a class="tptn_button tptn_button_gold" target="_blank" href="%s" title="%s">%s</a>',
1624 esc_url( \WebberZone\Top_Ten\tptn_freemius()->get_upgrade_url() ),
1625 esc_attr__( 'Upgrade to Pro', 'top-10' ),
1626 esc_html__( 'Upgrade to Pro', 'top-10' )
1627 );
1628 }
1629
1630 if ( isset( $args['id'] ) && 'tptn_styles' === $args['id'] ) {
1631 $post_thumb_op = \tptn_get_option( 'post_thumb_op' );
1632 $tptn_style = \tptn_get_option( 'tptn_styles' );
1633
1634 if ( 'text_only' === $post_thumb_op && 'text_only' !== $tptn_style ) {
1635 $output .= sprintf(
1636 '<p class="description" style="color:#9B0800;">%s</p>',
1637 esc_html__( 'Note: Thumbnail location is set to "Do not display thumbnails, only text". This may override the selected style.', 'top-10' )
1638 );
1639 }
1640 }
1641
1642 return $output;
1643 }
1644
1645 /**
1646 * Add a button to the settings page to start the settings wizard.
1647 *
1648 * @since 4.2.0
1649 */
1650 public function add_wizard_button() {
1651 printf(
1652 '<br /><a aria-label="%s" class="button button-secondary" href="%s" title="%s" style="margin-top: 10px;">%s</a>',
1653 esc_attr__( 'Start Settings Wizard', 'top-10' ),
1654 esc_url( admin_url( 'admin.php?page=tptn_wizard' ) ),
1655 esc_attr__( 'Start Settings Wizard', 'top-10' ),
1656 esc_html__( 'Start Settings Wizard', 'top-10' )
1657 );
1658 }
1659
1660 /**
1661 * Handle Tom Select taxonomy search AJAX requests.
1662 *
1663 * @since 4.2.0
1664 *
1665 * @return void
1666 */
1667 public static function taxonomy_search_tom_select() {
1668 // Verify nonce.
1669 if ( ! isset( $_REQUEST['nonce'] ) ) {
1670 wp_send_json_error( array( 'message' => 'Missing nonce' ) );
1671 }
1672
1673 $nonce_valid = wp_verify_nonce( sanitize_key( $_REQUEST['nonce'] ), self::$prefix . '_taxonomy_search_tom_select' );
1674
1675 if ( ! $nonce_valid ) {
1676 wp_send_json_error(
1677 array(
1678 'message' => 'Invalid nonce',
1679 'received_nonce' => sanitize_key( $_REQUEST['nonce'] ),
1680 'expected_action' => self::$prefix . '_taxonomy_search_tom_select',
1681 )
1682 );
1683 }
1684
1685 if ( ! isset( $_REQUEST['endpoint'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
1686 wp_send_json_error( 'Missing endpoint' );
1687 }
1688
1689 $endpoint = sanitize_key( $_REQUEST['endpoint'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
1690
1691 $search_term = isset( $_REQUEST['q'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['q'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
1692
1693 $comma = _x( ',', 'tag delimiter', 'top-10' );
1694 if ( ',' !== $comma ) {
1695 $search_term = str_replace( $comma, ',', $search_term );
1696 }
1697 if ( false !== strpos( $search_term, ',' ) ) {
1698 $search_term = explode( ',', $search_term );
1699 $search_term = $search_term[ count( $search_term ) - 1 ];
1700 }
1701 $search_term = trim( $search_term );
1702
1703 if ( 'public_taxonomies' === $endpoint ) {
1704 $taxonomies = (array) get_taxonomies( array( 'public' => true ), 'objects' );
1705 $taxonomy = array();
1706 $tax = null;
1707
1708 foreach ( $taxonomies as $taxonomy_name => $taxonomy_object ) {
1709 if ( empty( $taxonomy_object->cap->assign_terms ) ) {
1710 continue;
1711 }
1712
1713 if ( ! current_user_can( $taxonomy_object->cap->assign_terms ) ) {
1714 continue;
1715 }
1716
1717 $taxonomy[] = $taxonomy_object->name;
1718 }
1719
1720 if ( empty( $taxonomy ) ) {
1721 wp_send_json_success( array() );
1722 }
1723
1724 $tax = get_taxonomy( $taxonomy[0] );
1725 } else {
1726 $taxonomy = $endpoint;
1727 $tax = get_taxonomy( $taxonomy );
1728
1729 if ( ! $tax ) {
1730 wp_send_json_error( 'Invalid taxonomy' );
1731 }
1732
1733 if ( ! current_user_can( $tax->cap->assign_terms ) ) {
1734 wp_send_json_error( 'Insufficient permissions' );
1735 }
1736 }
1737
1738 /** This filter has been defined in /wp-admin/includes/ajax-actions.php */
1739 $term_search_min_chars = (int) apply_filters( 'term_search_min_chars', 2, $tax, $search_term );
1740
1741 /*
1742 * Require $term_search_min_chars chars for matching (default: 2)
1743 * ensure it's a non-negative, non-zero integer.
1744 */
1745 if ( ( 0 === $term_search_min_chars ) || ( strlen( $search_term ) < $term_search_min_chars ) ) {
1746 wp_send_json_success( array() );
1747 }
1748
1749 $terms = get_terms(
1750 array(
1751 'taxonomy' => $taxonomy,
1752 'name__like' => $search_term,
1753 'hide_empty' => false,
1754 )
1755 );
1756
1757 $results = array();
1758 foreach ( (array) $terms as $term ) {
1759 $formatted_string = "{$term->name} ({$term->taxonomy}:{$term->term_taxonomy_id})";
1760 $results[] = array(
1761 'value' => $formatted_string,
1762 'text' => $term->name,
1763 );
1764 }
1765
1766 wp_send_json_success( $results );
1767 }
1768
1769 /**
1770 * Get field attributes for Tom Select taxonomy search fields.
1771 *
1772 * @since 4.2.0
1773 *
1774 * @param string $taxonomy The taxonomy to search.
1775 * @param array $ts_config Optional Tom Select configuration.
1776 * @return array Field attributes array.
1777 */
1778 private static function get_taxonomy_search_field_attributes( $taxonomy, $ts_config = array() ) {
1779 $attributes = array(
1780 'data-wp-prefix' => strtoupper( (string) self::$prefix ),
1781 'data-wp-action' => self::$prefix . '_taxonomy_search_tom_select',
1782 'data-wp-nonce' => wp_create_nonce( self::$prefix . '_taxonomy_search_tom_select' ),
1783 'data-wp-endpoint' => $taxonomy,
1784 );
1785
1786 if ( ! empty( $ts_config ) ) {
1787 $attributes['data-ts-config'] = wp_json_encode( $ts_config );
1788 }
1789
1790 return $attributes;
1791 }
1792
1793 /**
1794 * Get field attributes for Tom Select meta key search fields.
1795 *
1796 * @since 4.2.0
1797 *
1798 * @param array $ts_config Optional Tom Select configuration.
1799 * @return array Field attributes array.
1800 */
1801 private static function get_meta_keys_search_field_attributes( $ts_config = array() ) {
1802 $attributes = array(
1803 'data-wp-prefix' => strtoupper( (string) self::$prefix ),
1804 'data-wp-action' => self::$prefix . '_taxonomy_search_tom_select',
1805 'data-wp-nonce' => wp_create_nonce( self::$prefix . '_taxonomy_search_tom_select' ),
1806 'data-wp-endpoint' => 'meta_keys',
1807 );
1808
1809 if ( ! empty( $ts_config ) ) {
1810 $attributes['data-ts-config'] = wp_json_encode( $ts_config );
1811 }
1812
1813 return $attributes;
1814 }
1815 }
1816