PluginProbe
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms / 3.6.1
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms v3.6.1
3.53.0 3.52.0 3.51.0 3.50.0 3.45.0 3.38.0 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.40.0 3.40.1 3.41.0 3.42.0 3.43.0 3.44.0 3.5.0 3.5.1 3.5.2 3.5.3 3.6.0 3.6.1 3.6.2 All 177 releases
simple-tags / inc / class.admin.php

class.admin.php in Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms 3.6.1, at inc/class.admin.php

855 lines 30.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class SimpleTags_Admin {
4 // CPT and Taxonomy support
5 public static $post_type = 'post';
6 public static $post_type_name = '';
7 public static $taxonomy = '';
8 public static $taxo_name = '';
9 public static $admin_url = '';
10
11 const MENU_SLUG = 'st_options';
12
13 /**
14 * Initialize Admin
15 *
16 * @return void
17 * @author WebFactory Ltd
18 */
19 public function __construct() {
20 // DB Upgrade ?
21 self::upgrade();
22
23 // Which taxo ?
24 self::register_taxonomy();
25
26 // Admin menu
27 add_action( 'admin_menu', array( __CLASS__, 'admin_menu' ) );
28 // Log cpt
29 add_action( 'init', array( __CLASS__, 'taxopress_log_post_type' ) );
30
31 //Admin footer credit
32 add_action( 'in_admin_footer', array( __CLASS__, 'taxopress_admin_footer') );
33
34 // Load JavaScript and CSS
35 add_action( 'admin_enqueue_scripts', array( __CLASS__, 'admin_enqueue_scripts' ) );
36
37 //ui class is used accross many pages. So, it should be here
38 require STAGS_DIR . '/inc/class.admin.taxonomies.ui.php';
39
40 //terms
41 require STAGS_DIR . '/inc/terms-table.php';
42 require STAGS_DIR . '/inc/terms.php';
43 SimpleTags_Terms::get_instance();
44
45 //tag clouds/ terms display
46 $active_terms_display = isset($_POST['updateoptions']) &&
47 (
48 !isset($_POST['active_terms_display']) ||
49 (isset($_POST['active_terms_display']) && (int)$_POST['active_terms_display'] === 0)
50 ) ? 0 : 1;
51 if ( (1 === (int) SimpleTags_Plugin::get_option_value( 'active_terms_display' ) || (isset($_POST['active_terms_display']) && (int)$_POST['active_terms_display'] === 1)) && $active_terms_display === 1 ) {
52 require_once STAGS_DIR . '/inc/tag-clouds-action.php';
53 require STAGS_DIR . '/inc/tag-clouds-table.php';
54 require STAGS_DIR . '/inc/tag-clouds.php';
55 SimpleTags_Tag_Clouds::get_instance();
56 }
57
58 //post tags
59 $active_post_tags = isset($_POST['updateoptions']) &&
60 (
61 !isset($_POST['active_post_tags']) ||
62 (isset($_POST['active_post_tags']) && (int)$_POST['active_post_tags'] === 0)
63 ) ? 0 : 1;
64 if ( (1 === (int) SimpleTags_Plugin::get_option_value( 'active_post_tags' ) || (isset($_POST['active_post_tags']) && (int)$_POST['active_post_tags'] === 1)) && $active_post_tags === 1 ) {
65 require_once STAGS_DIR . '/inc/post-tags-action.php';
66 require STAGS_DIR . '/inc/post-tags-table.php';
67 require STAGS_DIR . '/inc/post-tags.php';
68 SimpleTags_Post_Tags::get_instance();
69 }
70
71 //Related Posts
72 $active_related_posts_new = isset($_POST['updateoptions']) &&
73 (
74 !isset($_POST['active_related_posts_new']) ||
75 (isset($_POST['active_related_posts_new']) && (int)$_POST['active_related_posts_new'] === 0)
76 ) ? 0 : 1;
77 if ( (1 === (int) SimpleTags_Plugin::get_option_value( 'active_related_posts_new' ) || (isset($_POST['active_related_posts_new']) && (int)$_POST['active_related_posts_new'] === 1)) && $active_related_posts_new === 1 ) {
78 require_once STAGS_DIR . '/inc/related-posts-action.php';
79 require STAGS_DIR . '/inc/related-posts-table.php';
80 require STAGS_DIR . '/inc/related-posts.php';
81 SimpleTags_Related_Post::get_instance();
82 }
83
84 //Auto Links
85 $active_auto_links = isset($_POST['updateoptions']) &&
86 (
87 !isset($_POST['active_auto_links']) ||
88 (isset($_POST['active_auto_links']) && (int)$_POST['active_auto_links'] === 0)
89 ) ? 0 : 1;
90 if ( (1 === (int) SimpleTags_Plugin::get_option_value( 'active_auto_links' ) || (isset($_POST['active_auto_links']) && (int)$_POST['active_auto_links'] === 1)) && $active_auto_links === 1 ) {
91 require STAGS_DIR . '/inc/autolinks-table.php';
92 require STAGS_DIR . '/inc/autolinks.php';
93 SimpleTags_Autolink::get_instance();
94 }
95
96 //Auto Terms
97 $active_auto_terms = isset($_POST['updateoptions']) &&
98 (
99 !isset($_POST['active_auto_terms']) ||
100 (isset($_POST['active_auto_terms']) && (int)$_POST['active_auto_terms'] === 0)
101 ) ? 0 : 1;
102 if ( (1 === (int) SimpleTags_Plugin::get_option_value( 'active_auto_terms' ) || (isset($_POST['active_auto_terms']) && (int)$_POST['active_auto_terms'] === 1)) && $active_auto_terms === 1 ) {
103 require STAGS_DIR . '/inc/autoterms-table.php';
104 require STAGS_DIR . '/inc/autoterms-logs-table.php';
105 require STAGS_DIR . '/inc/autoterms.php';
106 SimpleTags_Autoterms::get_instance();
107 }
108
109 //Suggest Terms
110 $active_suggest_terms = isset($_POST['updateoptions']) &&
111 (
112 !isset($_POST['active_suggest_terms']) ||
113 (isset($_POST['active_suggest_terms']) && (int)$_POST['active_suggest_terms'] === 0)
114 ) ? 0 : 1;
115 if ( (1 === (int) SimpleTags_Plugin::get_option_value( 'active_suggest_terms' ) || (isset($_POST['active_suggest_terms']) && (int)$_POST['active_suggest_terms'] === 1)) && $active_suggest_terms === 1 ) {
116 require STAGS_DIR . '/inc/suggestterms-table.php';
117 require STAGS_DIR . '/inc/suggestterms.php';
118 SimpleTags_SuggestTerms::get_instance();
119 }
120
121 //suggest terms option
122 $active_mass_edit = isset($_POST['updateoptions']) &&
123 (
124 !isset($_POST['active_mass_edit']) ||
125 (isset($_POST['active_mass_edit']) && (int)$_POST['active_mass_edit'] === 0)
126 ) ? 0 : 1;
127 if ( (1 === (int) SimpleTags_Plugin::get_option_value( 'active_mass_edit' ) || (isset($_POST['active_mass_edit']) && (int)$_POST['active_mass_edit'] === 1)) && $active_mass_edit === 1 ) {
128 require STAGS_DIR . '/inc/class.admin.suggest.php';
129 new SimpleTags_Admin_Suggest();
130 }
131
132 //click terms option
133 require STAGS_DIR . '/inc/class.admin.clickterms.php';
134 new SimpleTags_Admin_ClickTags();
135
136 require STAGS_DIR . '/inc/class.admin.autocomplete.php';
137 new SimpleTags_Admin_Autocomplete();
138
139 $active_mass_edit = isset($_POST['updateoptions']) &&
140 (
141 !isset($_POST['active_mass_edit']) ||
142 (isset($_POST['active_mass_edit']) && (int)$_POST['active_mass_edit'] === 0)
143 ) ? 0 : 1;
144 if ( (1 === (int) SimpleTags_Plugin::get_option_value( 'active_mass_edit' ) || (isset($_POST['active_mass_edit']) && (int)$_POST['active_mass_edit'] === 1)) && $active_mass_edit === 1 ) {
145 require STAGS_DIR . '/inc/class.admin.mass.php';
146 new SimpleTags_Admin_Mass();
147 }
148
149 $active_manage = isset($_POST['updateoptions']) &&
150 (
151 !isset($_POST['active_manage']) ||
152 (isset($_POST['active_manage']) && (int)$_POST['active_manage'] === 0)
153 ) ? 0 : 1;
154 if ( (1 === (int) SimpleTags_Plugin::get_option_value( 'active_manage' ) || (isset($_POST['active_manage']) && (int)$_POST['active_manage'] === 1)) && $active_manage === 1 ) {
155 require STAGS_DIR . '/inc/class-tag-table.php';
156 require STAGS_DIR . '/inc/class.admin.manage.php';
157 SimpleTags_Admin_Manage::get_instance();
158 }
159
160 require STAGS_DIR . '/inc/class.admin.post.php';
161 new SimpleTags_Admin_Post_Settings();
162
163 //taxonomies
164 $active_taxonomies = isset($_POST['updateoptions']) &&
165 (
166 !isset($_POST['active_taxonomies']) ||
167 (isset($_POST['active_taxonomies']) && (int)$_POST['active_taxonomies'] === 0)
168 ) ? 0 : 1;
169 if ( (1 === (int) SimpleTags_Plugin::get_option_value( 'active_taxonomies' ) || (isset($_POST['active_taxonomies']) && (int)$_POST['active_taxonomies'] === 1)) && $active_taxonomies === 1 ) {
170 require_once STAGS_DIR . '/inc/taxonomies-action.php';
171 require STAGS_DIR . '/inc/class-taxonomies-table.php';
172 require STAGS_DIR . '/inc/taxonomies.php';
173 SimpleTags_Admin_Taxonomies::get_instance();
174 }
175
176 do_action('taxopress_admin_class_after_includes');
177
178 // Ajax action, JS Helper and admin action
179 add_action( 'wp_ajax_simpletags', array( __CLASS__, 'ajax_check' ) );
180
181 }
182
183 /**
184 * Ajax Dispatcher
185 */
186 public static function ajax_check() {
187 if ( isset( $_GET['stags_action'] ) && 'maybe_create_tag' === $_GET['stags_action'] && isset( $_GET['tag'] ) ) {
188 self::maybe_create_tag( wp_unslash( sanitize_text_field($_GET['tag']) ) );
189 }
190 }
191
192 /**
193 * Maybe create a tag, and return the term_id
194 *
195 * @param string $tag_name
196 */
197 public static function maybe_create_tag( $tag_name = '' ) {
198 $term_id = 0;
199 //restore & in tag post
200 $tag_name = sanitize_text_field(str_replace("simpletagand", "&", $tag_name));
201 $result_term = term_exists( $tag_name, 'post_tag', 0 );
202 if ( empty( $result_term ) ) {
203 $result_term = wp_insert_term(
204 $tag_name,
205 'post_tag'
206 );
207
208 if ( ! is_wp_error( $result_term ) ) {
209 $term_id = (int) $result_term['term_id'];
210 }
211 } else {
212 $term_id = (int) $result_term['term_id'];
213 }
214
215 wp_send_json_success( [ 'term_id' => $term_id ] );
216 }
217
218 /**
219 * Test if current URL is not a DEV environnement
220 *
221 * Copy from monsterinsights_is_dev_url(), thanks !
222 *
223 * @param string $url
224 *
225 * @return bool
226 */
227 private static function is_dev_url( $url = '' ) {
228 $is_local_url = false;
229
230 // Trim it up
231 $url = strtolower( trim( $url ) );
232 // Need to get the host...so let's add the scheme so we can use parse_url
233 if ( false === strpos( $url, 'http://' ) && false === strpos( $url, 'https://' ) ) {
234 $url = 'http://' . $url;
235 }
236
237 $url_parts = wp_parse_url( $url );
238 $host = ! empty( $url_parts['host'] ) ? $url_parts['host'] : false;
239 if ( ! empty( $url ) && ! empty( $host ) ) {
240 if ( false !== ip2long( $host ) ) {
241 if ( ! filter_var( $host, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE ) ) {
242 $is_local_url = true;
243 }
244 } elseif ( 'localhost' === $host ) {
245 $is_local_url = true;
246 }
247
248 $tlds_to_check = array( '.local', ':8888', ':8080', ':8081', '.invalid', '.example', '.test' );
249 foreach ( $tlds_to_check as $tld ) {
250 if ( false !== strpos( $host, $tld ) ) {
251 $is_local_url = true;
252 break;
253 }
254 }
255
256 if ( substr_count( $host, '.' ) > 1 ) {
257 $subdomains_to_check = array( 'dev.', '*.staging.', 'beta.', 'test.' );
258 foreach ( $subdomains_to_check as $subdomain ) {
259 $subdomain = str_replace( '.', '(.)', $subdomain );
260 $subdomain = str_replace( array( '*', '(.)' ), '(.*)', $subdomain );
261 if ( preg_match( '/^(' . $subdomain . ')/', $host ) ) {
262 $is_local_url = true;
263 break;
264 }
265 }
266 }
267 }
268
269 return $is_local_url;
270 }
271
272 /**
273 * Init taxonomy class variable, load this action after all actions on init !
274 * Make a public static function for call it from children class...
275 *
276 * @return void
277 * @author WebFactory Ltd
278 */
279 public static function register_taxonomy() {
280 add_action( 'init', array( __CLASS__, 'init' ), 99999999 );
281 }
282
283 /**
284 * Put in var class the current taxonomy choose by the user
285 *
286 * @return void
287 * @author WebFactory Ltd
288 */
289 public static function init() {
290 self::$taxo_name = esc_html__( 'Post tags', 'simple-tags' );
291 self::$post_type_name = esc_html__( 'Posts', 'simple-tags' );
292
293 // Custom CPT ?
294 if ( isset( $_GET['cpt'] ) && ! empty( $_GET['cpt'] ) && post_type_exists( sanitize_text_field($_GET['cpt']) ) ) {
295 $cpt = get_post_type_object( sanitize_text_field($_GET['cpt']) );
296 self::$post_type = $cpt->name;
297 self::$post_type_name = $cpt->labels->name;
298 }
299
300 // Get compatible taxo for current post type
301 $compatible_taxonomies = get_object_taxonomies( self::$post_type );
302
303 // Custom taxo ?
304 if ( isset( $_GET['taxo'] ) && ! empty( $_GET['taxo'] ) && taxonomy_exists( sanitize_text_field($_GET['taxo']) ) ) {
305 $taxo = get_taxonomy( sanitize_text_field($_GET['taxo']) );
306
307 // Taxo is compatible ?
308 if ( in_array( $taxo->name, $compatible_taxonomies ) ) {
309 self::$taxonomy = $taxo->name;
310 self::$taxo_name = $taxo->labels->name;
311 } else {
312 unset( $taxo );
313 }
314 }
315
316 // Default taxo from CPT...
317 if ( ! isset( $taxo ) && is_array( $compatible_taxonomies ) && ! empty( $compatible_taxonomies ) ) {
318 // Take post_tag before category
319 if ( in_array( 'post_tag', $compatible_taxonomies, true ) ) {
320 $taxo = get_taxonomy( 'post_tag' );
321 } else {
322 $taxo = get_taxonomy( current( $compatible_taxonomies ) );
323 }
324
325 self::$taxonomy = $taxo->name;
326 self::$taxo_name = $taxo->labels->name;
327
328 // TODO: Redirect for help user that see the URL...
329 } elseif ( ! isset( $taxo ) ) {
330 wp_die( esc_html__( 'This custom post type not have taxonomies.', 'simple-tags' ) );
331 }
332
333 // Free memory
334 unset( $cpt, $taxo );
335 }
336
337 /**
338 * Build HTML form for allow user to change taxonomy for the current page.
339 *
340 * @param string $page_value
341 *
342 * @return void
343 * @author Olatechpro
344 */
345 public static function boxSelectorTaxonomy( $page_value = '' ) {
346 echo '<div class="box-selector-taxonomy">' . PHP_EOL;
347
348 echo '<div class="change-taxo">' . PHP_EOL;
349 echo '<form action="" method="get">' . PHP_EOL;
350 if ( ! empty( $page_value ) ) {
351 echo '<input type="hidden" name="page" value="' . esc_attr($page_value) . '" />' . PHP_EOL;
352 }
353 $taxonomies = [];
354 echo '<select name="cpt" id="cpt-select" class="st-cpt-select">' . PHP_EOL;
355 foreach ( get_post_types( array( 'show_ui' => true ), 'objects' ) as $post_type ) {
356 $taxonomies_children = get_object_taxonomies( $post_type->name );
357 if ( empty( $taxonomies_children ) ) {
358 continue;
359 }
360 $taxonomies[$post_type->name] = $taxonomies_children;
361 echo '<option ' . selected( $post_type->name, self::$post_type, false ) . ' value="' . esc_attr( $post_type->name ) . '">' . esc_html( $post_type->labels->name ) . '</option>' . PHP_EOL;
362 }
363 echo '</select>' . PHP_EOL;
364
365 echo '<select name="taxo" id="taxonomy-select" class="st-taxonomy-select">' . PHP_EOL;
366 foreach ( $taxonomies as $parent_post => $taxonomy ) {
367 if ( count($taxonomy) > 0){
368 foreach($taxonomy as $tax_name){
369 $taxonomy = get_taxonomy( $tax_name );
370 if ( false === (bool) $taxonomy->show_ui ) {
371 continue;
372 }
373
374 if( self::$post_type == $parent_post){
375 $class = "";
376 }else{
377 $class="st-hide-content";
378 }
379
380 echo '<option ' . selected( $tax_name, self::$taxonomy, false ) . ' value="' . esc_attr( $tax_name ) . '" data-post="'. esc_attr($parent_post) .'" class="'. esc_attr($class) .'">' . esc_html( $taxonomy->labels->name ) . '</option>' . PHP_EOL;
381 }
382 }
383 }
384 echo '</select>' . PHP_EOL;
385
386 echo '<input type="submit" class="button" id="submit-change-taxo" value="' . esc_attr__( 'Change selection', 'simple-tags' ) . '" />' . PHP_EOL;
387 echo '</form>' . PHP_EOL;
388 echo '</div>' . PHP_EOL;
389 echo '</div>' . PHP_EOL;
390
391 }
392
393 /**
394 * Init somes JS and CSS need for TaxoPress.
395 *
396 * @return void
397 * @author WebFactory Ltd
398 */
399 public static function admin_enqueue_scripts() {
400 global $pagenow;
401
402
403 do_action('taxopress_admin_class_before_assets_register');
404
405 //color picker style
406 wp_enqueue_style( 'wp-color-picker' );
407
408 // Helper TaxoPress
409 wp_register_script( 'st-helper-add-tags', STAGS_URL . '/assets/js/helper-add-tags.js', array( 'jquery' ), STAGS_VERSION );
410 wp_register_script( 'st-helper-options', STAGS_URL . '/assets/js/helper-options.js', array( 'jquery', 'wp-color-picker' ), STAGS_VERSION );
411
412 // Register CSS
413 wp_register_style( 'st-admin', STAGS_URL . '/assets/css/admin.css', array(), STAGS_VERSION, 'all' );
414
415 //Register and enqueue admin js
416 wp_register_script( 'st-admin-js', STAGS_URL . '/assets/js/admin.js', array( 'jquery' ), STAGS_VERSION );
417 wp_enqueue_script( 'st-admin-js' );
418 //localize script
419 wp_localize_script( 'st-admin-js', 'st_admin_localize', [
420 'ajaxurl' => admin_url('admin-ajax.php'),
421 'select_valid'=> esc_html__( 'Please select a valid', 'simple-tags' ),
422 'check_nonce' => wp_create_nonce('st-admin-js'),
423 ]);
424
425
426 //Register remodal assets
427 wp_register_script( 'st-remodal-js', STAGS_URL . '/assets/js/remodal.min.js', array( 'jquery' ), STAGS_VERSION );
428 wp_register_style( 'st-remodal-css', STAGS_URL . '/assets/css/remodal.css', array(), STAGS_VERSION, 'all' );
429 wp_register_style( 'st-remodal-default-theme-css', STAGS_URL . '/assets/css/remodal-default-theme.css', array(), STAGS_VERSION, 'all' );
430
431 // Register location
432 $wp_post_pages = array( 'post.php', 'post-new.php' );
433 $wp_page_pages = array( 'page.php', 'page-new.php' );
434
435 $taxopress_pages = taxopress_admin_pages();
436
437 // Common Helper for Post, Page and Plugin Page
438 if (
439 in_array( $pagenow, $wp_post_pages ) ||
440 ( in_array( $pagenow, $wp_page_pages ) && is_page_have_tags() ) ||
441 ( isset( $_GET['page'] ) && in_array( $_GET['page'], $taxopress_pages ) )
442 ) {
443 wp_enqueue_script( 'st-remodal-js' );
444 wp_enqueue_style( 'st-remodal-css' );
445 wp_enqueue_style( 'st-remodal-default-theme-css' );
446 wp_enqueue_style( 'st-admin' );
447
448 do_action('taxopress_admin_class_after_styles_enqueue');
449 }
450
451 // add jQuery tabs for options page. Use jQuery UI Tabs from WP
452 if ( isset( $_GET['page'] ) && in_array( $_GET['page'], array('st_options','st_terms_display') ) ) {
453 wp_enqueue_script( 'jquery-ui-tabs' );
454 wp_enqueue_script( 'st-helper-options' );
455 }
456
457 do_action('taxopress_admin_class_after_assets_enqueue');
458 }
459
460 /**
461 * Register log post type.
462 *
463 * @return void
464 * @author olatechpro
465 */
466 public static function taxopress_log_post_type() {
467
468 // set up labels
469 $labels = array(
470 'name' => __('TaxoPress Logs', 'simple-tags'),
471 'singular_name' => __('TaxoPress Logs', 'simple-tags'),
472 'search_items' => __('Search TaxoPress Logs', 'simple-tags'),
473 'all_items' => __('TaxoPress Logs', 'simple-tags'),
474 'edit_item' => __('Edit TaxoPress Logs', 'simple-tags'),
475 'update_item' => __('Update TaxoPress Logs', 'simple-tags'),
476 'add_new_item' => __('Add New TaxoPress Logs', 'simple-tags'),
477 'new_item_name' => __('New TaxoPress Logs', 'simple-tags'),
478 'menu_name' => __('TaxoPress Logs', 'simple-tags')
479 );
480
481 register_post_type('taxopress_logs', array(
482 'labels' => $labels,
483 'public' => false,
484 'show_ui' => false,
485 'capability_type' => 'post',
486 'hierarchical' => false,
487 'rewrite' => array('slug' => 'taxopress_logs'),
488 'query_var' => false,
489 'show_in_nav_menus' => false,
490 'menu_icon' => 'dashicons-editor-justify',
491 'supports' => array(
492 'title',
493 'editor',
494 'author',
495 ),
496 ));
497 }
498
499 /**
500 * Add settings page on WordPress admin menu
501 *
502 * @return void
503 * @author WebFactory Ltd
504 */
505 public static function admin_menu() {
506 self::$admin_url = admin_url( 'admin.php?page=' . self::MENU_SLUG );
507
508 add_menu_page(
509 __( 'TaxoPress: Options', 'simple-tags' ),
510 __( 'TaxoPress', 'simple-tags' ),
511 'admin_simple_tags',
512 self::MENU_SLUG,
513 array(
514 __CLASS__,
515 'page_options',
516 ),
517 'dashicons-tag',
518 69
519 );
520 add_submenu_page(
521 self::MENU_SLUG,
522 __( 'TaxoPress: Options', 'simple-tags' ),
523 __( 'Settings', 'simple-tags' ),
524 'admin_simple_tags',
525 self::MENU_SLUG,
526 array(
527 __CLASS__,
528 'page_options',
529 )
530 );
531 }
532
533 /**
534 * Build HTML for page options, manage also save/reset settings
535 *
536 * @return void
537 * @author WebFactory Ltd
538 */
539 public static function page_options() {
540 // Get options
541 $options = SimpleTags_Plugin::get_option();
542
543 if(current_user_can('admin_simple_tags')){
544 // Update or reset options
545 if ( isset( $_POST['updateoptions'] ) ) {
546 check_admin_referer( 'updateresetoptions-simpletags' );
547
548 foreach ( (array) $options as $key => $value ) {
549 $newval = ( isset( $_POST[ $key ] ) ) ? stripslashes( sanitize_text_field($_POST[ $key ]) ) : '0';
550 if ( $newval != $value ) {
551 $options[ $key ] = $newval;
552 }
553 }
554 SimpleTags_Plugin::set_option( $options );
555
556 do_action( 'simpletags_settings_save_general_end' );
557
558 add_settings_error( __CLASS__, __CLASS__, esc_html__( 'Options saved', 'simple-tags' ), 'updated' );
559 } elseif ( isset( $_POST['reset_options'] ) ) {
560 check_admin_referer( 'updateresetoptions-simpletags' );
561
562 SimpleTags_Plugin::set_default_option();
563
564 add_settings_error( __CLASS__, __CLASS__, esc_html__( 'TaxoPress options resetted to default options!', 'simple-tags' ), 'updated' );
565 }
566
567 }
568
569 settings_errors( __CLASS__ );
570 include STAGS_DIR . '/views/admin/page-settings.php';
571 }
572
573 /**
574 * Get terms for a post, format terms for input and autocomplete usage
575 *
576 * @param string $taxonomy
577 * @param integer $post_id
578 *
579 * @return string
580 * @author WebFactory Ltd
581 */
582 public static function getTermsToEdit( $taxonomy = 'post_tag', $post_id = 0 ) {
583 $post_id = (int) $post_id;
584 if ( ! $post_id ) {
585 return '';
586 }
587
588 $terms = wp_get_post_terms( $post_id, $taxonomy, array( 'fields' => 'names' ) );
589 if ( empty( $terms ) || is_wp_error( $terms ) ) {
590 return '';
591 }
592
593 $terms = array_unique( $terms ); // Remove duplicate
594 $terms = join( ', ', $terms );
595 $terms = esc_attr( $terms );
596 $terms = apply_filters( 'tags_to_edit', $terms );
597
598 return $terms;
599 }
600
601 /**
602 * Default content for meta box of TaxoPress
603 *
604 * @return string
605 * @author WebFactory Ltd
606 */
607 public static function getDefaultContentBox() {
608 if ( (int) wp_count_terms( 'post_tag', array( 'hide_empty' => false ) ) == 0 ) { // TODO: Custom taxonomy
609 return esc_html__( 'This feature requires at least 1 tag to work. Begin by adding tags!', 'simple-tags' );
610 } else {
611 return esc_html__( 'This feature works only with activated JavaScript. Activate it in your Web browser so you can!', 'simple-tags' );
612 }
613 }
614
615 /**
616 * A short public static function for display the same copyright on all admin pages
617 *
618 * @return void
619 * @author WebFactory Ltd
620 */
621 public static function printAdminFooter() {
622 /* ?>
623 <p class="footer_st"><?php printf( __( 'Thanks for using TaxoPress | <a href="https://taxopress.com/">TaxoPress.com</a> | Version %s', 'simple-tags' ), STAGS_VERSION ); ?></p>
624 <?php */
625 }
626
627 /**
628 * A short public static function for display the same copyright on all taxopress admin pages
629 *
630 * @return void
631 * @author Olatechpro
632 */
633 public static function taxopress_admin_footer() {
634
635 $taxopress_pages = taxopress_admin_pages();
636
637 if ( isset( $_GET['page'] ) && in_array( $_GET['page'], $taxopress_pages )) {
638 ?>
639 <p class="footer_st">
640 <?php
641 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
642 printf( __( 'Thanks for using TaxoPress | %1sTaxoPress.com%2s | Version %3s', 'simple-tags' ), '<a href="https://taxopress.com/">', '</a>', esc_html(STAGS_VERSION) ); ?>
643 </p>
644 <?php
645 }
646 }
647
648 /**
649 * Ouput formatted options
650 *
651 * @param array $option_data
652 *
653 * @return string
654 * @author WebFactory Ltd
655 */
656 public static function print_options( $option_data ) {
657 // Get options
658 $option_actual = SimpleTags_Plugin::get_option();
659
660 // Generate output
661 $output = '';
662 foreach ( $option_data as $section => $options ) {
663 $colspan = count( $options ) > 1 ? 'colspan="2"' : '';
664 $desc_html_tag = 'div';
665
666 if($section === 'legacy'){
667 $table_sub_tab = '<div class="st-legacy-subtab">
668 <span class="active" data-content=".legacy-tag-cloud-content">Tag Cloud</span> |
669 <span data-content=".legacy-post-tags-content">Tags for Current Post</span> |
670 <span data-content=".legacy-related-posts-content">Related Posts</span> |
671 <span data-content=".legacy-auto-link-content">Auto link</span>
672 </div>' . PHP_EOL;
673 }else{
674 $table_sub_tab = '';
675 }
676
677 $output .= '<div class="group" id="' . sanitize_title( $section ) . '">' . PHP_EOL;
678 $output .= $table_sub_tab;
679 $output .= '<fieldset class="options">' . PHP_EOL;
680 $output .= '<legend>' . self::getNiceTitleOptions( $section ) . '</legend>' . PHP_EOL;
681 $output .= '<table class="form-table">' . PHP_EOL;
682 foreach ( (array) $options as $option ) {
683
684 $class = '';
685 if($section === 'legacy'){
686 $class = $option[5];
687 }
688
689 // Helper
690 if ( $option[2] == 'helper' ) {
691 $output .= '<tr style="vertical-align: middle;" class="'.$class.'"><td class="helper" ' . $colspan . '>' . stripslashes( $option[4] ) . '</td></tr>' . PHP_EOL;
692 continue;
693 }
694
695 // Fix notices
696 if ( ! isset( $option_actual[ $option[0] ] ) ) {
697 $option_actual[ $option[0] ] = '';
698 }
699
700 $input_type = '';
701 switch ( $option[2] ) {
702 case 'radio':
703 $input_type = array();
704 foreach ( $option[3] as $value => $text ) {
705 $input_type[] = '<label><input type="radio" id="' . $option[0] . '" name="' . $option[0] . '" value="' . esc_attr( $value ) . '" ' . checked( $value, $option_actual[ $option[0] ], false ) . ' /> ' . $text . '</label>' . PHP_EOL;
706 }
707 $input_type = implode( '<br />', $input_type );
708 break;
709
710 case 'checkbox':
711 $desc_html_tag = 'span';
712 $input_type = '<input type="checkbox" id="' . $option[0] . '" name="' . $option[0] . '" value="' . esc_attr( $option[3] ) . '" ' . ( ( $option_actual[ $option[0] ] ) ? 'checked="checked"' : '' ) . ' />' . PHP_EOL;
713 break;
714
715 case 'dropdown':
716 $selopts = explode( '/', $option[3] );
717 $seldata = '';
718 foreach ( (array) $selopts as $sel ) {
719 $seldata .= '<option value="' . esc_attr( $sel ) . '" ' . ( ( isset( $option_actual[ $option[0] ] ) && $option_actual[ $option[0] ] == $sel ) ? 'selected="selected"' : '' ) . ' >' . ucfirst( $sel ) . '</option>' . PHP_EOL;
720 }
721 $input_type = '<select id="' . $option[0] . '" name="' . $option[0] . '">' . $seldata . '</select>' . PHP_EOL;
722 break;
723
724 case 'text-color':
725 $input_type = '<input type="text" id="' . $option[0] . '" name="' . $option[0] . '" value="' . esc_attr( $option_actual[ $option[0] ] ) . '" class="text-color ' . $option[3] . '" />' . PHP_EOL;
726 break;
727
728 case 'text':
729 $input_type = '<input type="text" id="' . $option[0] . '" name="' . $option[0] . '" value="' . esc_attr( $option_actual[ $option[0] ] ) . '" class="' . $option[3] . '" />' . PHP_EOL;
730 break;
731
732 case 'number':
733 $input_type = '<input type="number" id="' . $option[0] . '" name="' . $option[0] . '" value="' . esc_attr( $option_actual[ $option[0] ] ) . '" class="' . $option[3] . '" />' . PHP_EOL;
734 break;
735 }
736
737 if( is_array($option[2]) ){
738 $input_type = '<input type="'.$option[2]["type"].'" id="' . $option[0] . '" name="' . $option[0] . '" value="' . esc_attr( $option_actual[ $option[0] ] ) . '" class="' . $option[3] . '" '.$option[2]["attr"].' />' . PHP_EOL;
739 }
740
741 // Additional Information
742 $extra = '';
743 if ( ! empty( $option[4] ) ) {
744 $extra = '<' . $desc_html_tag . ' class="stpexplan">' . __( $option[4] ) . '</' . $desc_html_tag . '>' . PHP_EOL;
745 }
746
747 // Output
748 $output .= '<tr style="vertical-align: top;" class="'.$class.'"><th scope="row"><label for="' . $option[0] . '">' . __( $option[1] ) . '</label></th><td>' . $input_type . ' ' . $extra . '</td></tr>' . PHP_EOL;
749 }
750 $output .= '</table>' . PHP_EOL;
751 $output .= '</fieldset>' . PHP_EOL;
752 $output .= '</div>' . PHP_EOL;
753 }
754
755 return $output;
756 }
757
758 /**
759 * Get nice title for tabs title option
760 *
761 * @param string $id
762 *
763 * @return string
764 */
765 public static function getNiceTitleOptions( $id = '' ) {
766 switch ( $id ) {
767 case 'administration':
768 return esc_html__( 'Administration', 'simple-tags' );
769 case 'auto-links':
770 return esc_html__( 'Auto link', 'simple-tags' );
771 case 'features':
772 return esc_html__( 'Features', 'simple-tags' );
773 case 'embeddedtags':
774 return esc_html__( 'Embedded Tags', 'simple-tags' );
775 case 'tagspost':
776 return esc_html__( 'Tags for Current Post', 'simple-tags' );
777 case 'relatedposts':
778 return esc_html__( 'Related Posts', 'simple-tags' );
779 case 'legacy':
780 return esc_html__( 'Legacy', 'simple-tags' );
781 }
782
783 return '';
784 }
785
786 /**
787 * This method allow to check if the DB is up to date, and if a upgrade is need for options
788 * TODO, useful or delete ?
789 *
790 * @return void
791 * @author WebFactory Ltd
792 */
793 public static function upgrade() {
794 // Get current version number
795 $current_version = get_option( STAGS_OPTIONS_NAME . '-version' );
796
797 // Upgrade needed ?
798 if ( $current_version == false || version_compare( $current_version, STAGS_VERSION, '<' ) ) {
799 $current_options = get_option( STAGS_OPTIONS_NAME );
800 $default_options = (array) include( STAGS_DIR . '/inc/helper.options.default.php' );
801
802 // Add new options
803 foreach ( $default_options as $key => $default_value ) {
804 if ( ! isset( $current_options[ $key ] ) ) {
805 $current_options[ $key ] = $default_value;
806 }
807 }
808
809 // Remove old options
810 foreach ( $current_options as $key => $current_value ) {
811 if ( ! isset( $default_options[ $key ] ) ) {
812 unset( $current_options[ $key ] );
813 }
814 }
815
816 update_option( STAGS_OPTIONS_NAME . '-version', STAGS_VERSION );
817 update_option( STAGS_OPTIONS_NAME, $current_options );
818 }
819 }
820
821 /**
822 * Make a simple SQL query with some args for get terms for ajax display
823 *
824 * @param string $taxonomy
825 * @param string $search
826 * @param string $order_by
827 * @param string $order
828 *
829 * @return array
830 * @author WebFactory Ltd
831 */
832 public static function getTermsForAjax( $taxonomy = 'post_tag', $search = '', $order_by = 'name', $order = 'ASC', $limit = '' ) {
833 global $wpdb;
834
835 if ( ! empty( $search ) ) {
836 return $wpdb->get_results( $wpdb->prepare( "
837 SELECT DISTINCT t.name, t.term_id
838 FROM {$wpdb->terms} AS t
839 INNER JOIN {$wpdb->term_taxonomy} AS tt ON t.term_id = tt.term_id
840 WHERE tt.taxonomy = %s
841 AND t.name LIKE %s
842 ORDER BY $order_by $order $limit
843 ", $taxonomy, '%' . $wpdb->esc_like($search) . '%' ) );
844 } else {
845 return $wpdb->get_results( $wpdb->prepare( "
846 SELECT DISTINCT t.name, t.term_id
847 FROM {$wpdb->terms} AS t
848 INNER JOIN {$wpdb->term_taxonomy} AS tt ON t.term_id = tt.term_id
849 WHERE tt.taxonomy = %s
850 ORDER BY $order_by $order $limit
851 ", $taxonomy ) );
852 }
853 }
854 }
855