PluginProbe
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms / 3.44.0
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms v3.44.0
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.clickterms.php

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

290 lines 10.3 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_ClickTags {
4 /**
5 * Constructor
6 *
7 * @return void
8 * @author WebFactory Ltd
9 */
10 public function __construct() {
11 // Ajax action, JS Helper and admin action
12 add_action( 'wp_ajax_simpletags', array( __CLASS__, 'ajax_check' ) );
13
14 if ( 1 === (int) SimpleTags_Plugin::get_option_value( 'active_suggest_terms' )){
15 // Box for post/page
16 //add_action( 'admin_head', array( __CLASS__, 'admin_head' ), 1 );
17 // Javascript
18 //add_action( 'admin_enqueue_scripts', array( __CLASS__, 'admin_enqueue_scripts' ), 11 );
19 }
20 }
21
22 /**
23 * Init somes JS and CSS need for this feature
24 *
25 * @return void
26 * @author WebFactory Ltd
27 */
28 public static function admin_enqueue_scripts() {
29 global $pagenow;
30
31 $click_terms = taxopress_current_post_suggest_terms('existing_terms');
32
33 if(!is_array($click_terms)){
34 return;
35 }
36
37 wp_register_script(
38 'st-helper-click-tags',
39 STAGS_URL . '/assets/js/helper-click-tags.js',
40 array(
41 'jquery',
42 'st-helper-add-tags',
43 ),
44 STAGS_VERSION,
45 true
46 );
47
48 $post_type_data = get_post_type_object(get_post_type());
49 $post_type_name = is_object($post_type_data) && isset($post_type_data->labels->singular_name) ? strtolower($post_type_data->labels->singular_name) : 'post';
50 $post_taxonomies = get_object_taxonomies(get_post_type());
51
52 //add taxonomy
53 $click_tags_taxonomy = '
54 <div class="option">
55 <label>'.esc_html__( 'Taxonomy', 'simple-tags' ).'</label><br />
56 <select class="st-post-taxonomy-select click_tags_taxonomy" name="click_tags_taxonomy">';
57 foreach ( $post_taxonomies as $_taxonomy ) {
58 $_taxonomy = get_taxonomy($_taxonomy);
59 if($_taxonomy->name === 'author'){
60 continue;
61 }
62 if(!isset($_taxonomy->public) || (isset($_taxonomy->public) && (int)$_taxonomy->public === 0)){
63 continue;
64 }
65
66 if (is_array($click_terms['taxonomy']) && in_array($_taxonomy->name, $click_terms['taxonomy'])) {
67 $click_tags_taxonomy .= '<option value="'.$_taxonomy->name.'" selected="selected">'.$_taxonomy->labels->name.'</option>';
68 } elseif(!is_array($click_terms['taxonomy']) && $_taxonomy->name === $click_terms['taxonomy']) { // backward compatibility
69 $click_tags_taxonomy .= '<option value="'.$_taxonomy->name.'" selected="selected">'.$_taxonomy->labels->name.'</option>';
70 } else {
71 $click_tags_taxonomy .= '<option value="'.$_taxonomy->name.'">'.$_taxonomy->labels->name.'</option>';
72 }
73 }
74 $click_tags_taxonomy .= '</select>
75 </div>';
76
77 //add method
78 $click_tags_methods = ['name' => esc_html__( 'Name', 'simple-tags' ), 'count' => esc_html__( 'Counter', 'simple-tags' ), 'random' => esc_html__( 'Random', 'simple-tags' )];
79 $click_tags_method = '
80 <div class="option">
81 <label>'.__( 'Method for choosing terms', 'simple-tags' ).'</label><br />
82 <select class="click_tags_method" name="click_tags_method">';
83 foreach($click_tags_methods as $option => $label){
84 $selected = ($option === $click_terms['orderby']) ? 'selected="selected"' : '';
85 $click_tags_method .= '<option value="'.$option.'" '.$selected.'>'.$label.'</option>';
86 }
87 $click_tags_method .= '</select>
88 </div>';
89
90 //add order
91 $click_tags_orders = ['asc' => esc_html__( 'Ascending', 'simple-tags' ), 'desc' => esc_html__( 'Descending', 'simple-tags' )];
92 $click_tags_order = '
93 <div class="option">
94 <label>'.__( 'Ordering for choosing terms', 'simple-tags' ).'</label><br />
95 <select class="click_tags_order" name="click_tags_order">';
96 foreach($click_tags_orders as $option => $label){
97 $selected = ($option === $click_terms['order']) ? 'selected="selected"' : '';
98 $click_tags_order .= '<option value="'.$option.'" '.$selected.'>'.$label.'</option>';
99 }
100 $click_tags_order .= '</select>
101 </div>';
102
103 //add limit
104 $click_tags_limit= '
105 <div class="option">
106 <label for="click_tags_limit">'.__( 'Maximum terms', 'simple-tags' ).'</label><br />
107 <input type="number" class="click_tags_limit" id="click_tags_limit" name="click_tags_limit" value="'.$click_terms['number'].'">
108 </div>';
109
110 //add searchbox
111 $click_tags_search= '
112 <div class="option">
113 <label for="click_tags_search">Search</label><br />
114 <input name="click_tags_search" id="click_tags_search" type="text" class="click-tag-search-box" placeholder="'.__('Start typing to search', 'simple-tags').'" size="26" autocomplete="off">
115 </div>';
116
117 //create tags search data
118 $click_tags_options= '<div class="clicktags-search-wrapper">'. $click_tags_search.' '.$click_tags_taxonomy.' '.$click_tags_method.' '.$click_tags_order.' '.$click_tags_limit.'</div>';
119
120 //metabox edit line
121 if(current_user_can('admin_simple_tags')){
122 $click_term_edit = '<span class="edit-suggest-term-metabox">
123 '. sprintf(
124 '<a href="%s">%s</a>',
125 add_query_arg(
126 [
127 'page' => 'st_suggestterms',
128 'add' => 'new_item',
129 'action' => 'edit',
130 'taxopress_suggestterms' => $click_terms['ID'],
131 ],
132 admin_url('admin.php')
133 ),
134 __('Edit this metabox', 'simple-tags')
135 )
136 .'
137 </span>';
138 }else {
139 $click_term_edit = '';
140 }
141 wp_localize_script(
142 'st-helper-click-tags',
143 'stHelperClickTagsL10n',
144 array(
145 'show_txt' => esc_html__( 'Click to display tags', 'simple-tags' ),
146 'arial_label' => esc_html__( 'suggested terms', 'simple-tags' ),
147 'hide_txt' => sprintf( esc_html__( 'Click terms to add them to this %s', 'simple-tags' ), $post_type_name ),
148 'state' => 'show',
149 'search_icon' => STAGS_URL . '/assets/images/indicator.gif',
150 'search_box' => '<input type="text" class="click-tag-search-box" placeholder="'.__('Start typing to search', 'simple-tags').'" size="26" autocomplete="off">',
151 'click_tags_options' => $click_tags_options,
152 'edit_metabox_link' => $click_term_edit,
153 )
154 );
155
156 // Helper for post type
157 wp_enqueue_script( 'st-helper-click-tags' );
158 }
159
160 /**
161 * Register metabox
162 *
163 * @return void
164 * @author WebFactory Ltd
165 */
166 public static function admin_head() {
167
168 $click_terms = taxopress_current_post_suggest_terms('existing_terms');
169
170 if(!is_array($click_terms)){
171 return;
172 }
173 add_meta_box(
174 'st-clicks-tags',
175 __( 'Show existing terms', 'simple-tags' ),
176 array(
177 __CLASS__,
178 'metabox',
179 ),
180 get_post_type(),
181 'advanced',
182 'core'
183 );
184 }
185
186 /**
187 * Put default HTML for people without JS
188 *
189 * @return void
190 * @author WebFactory Ltd
191 */
192 public static function metabox() {
193 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
194 echo SimpleTags_Admin::getDefaultContentBox();
195 }
196
197 /**
198 * Ajax Dispatcher
199 *
200 * @return void
201 * @author WebFactory Ltd
202 */
203 public static function ajax_check() {
204
205 if ( isset( $_GET['stags_action'] ) && 'click_tags' === $_GET['stags_action'] ) {
206 self::ajax_click_tags();
207 }
208 }
209
210 /**
211 * Display a span list for click tags
212 *
213 * @return void
214 * @author WebFactory Ltd
215 */
216 public static function ajax_click_tags() {
217 status_header( 200 ); // Send good header HTTP
218 header( 'Content-Type: text/html; charset=' . get_bloginfo( 'charset' ) );
219
220 $taxonomy = isset($_GET['click_tags_taxonomy']) ? sanitize_text_field($_GET['click_tags_taxonomy']) : 'post_tag';
221
222 if ( 0 === (int) wp_count_terms( $taxonomy, array( 'hide_empty' => false ) ) ) { // No tags to suggest
223 echo '<p>' . esc_html__( 'No terms in your WordPress database.', 'simple-tags' ) . '</p>';
224 exit();
225 }
226
227 // Prepare search
228 $search = ( isset( $_GET['q'] ) ) ? trim( stripslashes( sanitize_text_field($_GET['q']) ) ) : '';
229 $post_id = ( isset( $_GET['post_id'] ) ) ? intval( $_GET['post_id'] ) : 0;
230
231 if(isset($_GET['click_tags_method']) && !empty($_GET['click_tags_method'])){
232 $order_click_tags = (sanitize_text_field($_GET['click_tags_method']) === 'random') ? sanitize_text_field($_GET['click_tags_method']) : sanitize_text_field($_GET['click_tags_method']).'-'.sanitize_text_field($_GET['click_tags_order']);
233 }else{
234 // Order tags before selection (count-asc/count-desc/name-asc/name-desc/random)
235 $order_click_tags = 'random';
236 }
237 switch ( $order_click_tags ) {
238 case 'count-asc':
239 $order_by = 'tt.count';
240 $order = 'ASC';
241 break;
242 case 'random':
243 $order_by = 'RAND()';
244 $order = '';
245 break;
246 case 'count-desc':
247 $order_by = 'tt.count';
248 $order = 'DESC';
249 break;
250 case 'name-desc':
251 $order_by = 't.name';
252 $order = 'DESC';
253 break;
254 default: // name-asc
255 $order_by = 't.name';
256 $order = 'ASC';
257 break;
258 }
259
260 $term_limit = isset($_GET['click_tags_limit']) ? (int)$_GET['click_tags_limit'] : 100;
261
262 if ($term_limit > 0) {
263 $limit = 'LIMIT 0, '.$term_limit;
264 }else{
265 $limit = '';
266 }
267
268 // Get all terms, or filter with search
269 $terms = SimpleTags_Admin::getTermsForAjax( $taxonomy, $search, $order_by, $order, $limit );
270 if ( empty( $terms ) ) {
271 echo '<p>' . esc_html__( 'No results from your WordPress database.', 'simple-tags' ) . '</p>';
272 exit();
273 }
274
275 // Get terms for current post
276 $post_terms = array();
277 if ( $post_id > 0 ) {
278 $post_terms = wp_get_post_terms( $post_id, $taxonomy, array( 'fields' => 'ids' ) );
279 }
280
281 foreach ( (array) $terms as $term ) {
282 $class_current = in_array($term->term_id, $post_terms) ? 'used_term' : '';
283 echo '<span data-term_id="'.esc_attr($term->term_id).'" data-taxonomy="'.esc_attr($taxonomy).'" class="local '.esc_attr($taxonomy).' ' . esc_attr( $class_current ) . '" tabindex="0" role="button" aria-pressed="false">' . esc_html( stripslashes( $term->name ) ) . '</span>' . "\n";
284 }
285 echo '<div class="clear"></div>';
286
287 exit();
288 }
289 }
290