PluginProbe
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms / 3.4.5
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms v3.4.5
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.4.5, at inc/class.admin.clickterms.php

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