PluginProbe
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms / 3.51.0
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms v3.51.0
3.54.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 All 178 releases
simple-tags / inc / class.admin.autocomplete.php

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

310 lines 10.9 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_Autocomplete
4 {
5 public function __construct()
6 {
7 // Ajax action, JS Helper and admin action
8 add_action('wp_ajax_simpletags_autocomplete', array( __CLASS__, 'ajax_check' ));
9
10 // TaxoPress hook
11 add_action('simpletags-auto_terms', array( __CLASS__, 'auto_terms_js' ));
12 add_action('simpletags-manage_terms', array( __CLASS__, 'manage_terms_js' ));
13 add_action('simpletags-mass_terms', array( __CLASS__, 'mass_terms_js' ));
14 add_action('simpletags-autolinks', array( __CLASS__, 'autolinks_js' ));
15 add_action('simpletags-terms_display', array( __CLASS__, 'terms_display_js' ));
16
17 // Javascript
18 add_action('admin_enqueue_scripts', array( __CLASS__, 'admin_enqueue_scripts' ), 11);
19 }
20
21 /**
22 * Init some JS and CSS need for this feature
23 *
24 * @return void
25 * @author WebFactory Ltd
26 */
27 public static function admin_enqueue_scripts()
28 {
29 global $pagenow;
30
31 // Register JS/CSS
32 wp_register_script(
33 'st-helper-autocomplete',
34 STAGS_URL . '/assets/js/helper-autocomplete.js',
35 array(
36 'jquery',
37 'jquery-ui-autocomplete',
38 ),
39 STAGS_VERSION
40 );
41 $nonce = wp_create_nonce('st-admin-js');
42
43 // Declare locations
44 $wp_post_pages = array( 'post.php', 'post-new.php' );
45 $wp_page_pages = array( 'page.php', 'page-new.php' );
46 $term_pages = array( 'term.php', 'edit-tags.php' );
47 $st_pages = array( 'st_autoterms', 'st_mass_terms', 'st_manage', 'st_autolinks', 'st_terms_display' );
48
49 // Helper for posts/pages and for Auto Tags, Mass Edit Tags and Manage tags !
50 if ((in_array($pagenow, $wp_post_pages, true) || (in_array($pagenow, $wp_page_pages, true) && is_page_have_tags())) || (isset($_GET['page']) && in_array($_GET['page'], $st_pages, true))) {
51 wp_enqueue_script('st-helper-autocomplete');
52 }
53
54 if (in_array($pagenow, $wp_post_pages, true) || (in_array($pagenow, $wp_page_pages, true) && is_page_have_tags()) || in_array($pagenow, $term_pages, true) || (isset($_GET['page']) && in_array($_GET['page'], $st_pages, true))
55 ) {
56 // Inline nonce for legacy code
57 wp_add_inline_script(
58 'st-helper-autocomplete',
59 'window.taxopressNonce = "' . esc_js($nonce) . '";',
60 'before'
61 );
62 wp_enqueue_script('st-helper-autocomplete');
63 }
64 }
65
66 /**
67 * Ajax Dispatcher
68 *
69 */
70 public static function ajax_check()
71 {
72 // Capability first (must be logged in + have perms anyway)
73 if (! current_user_can('simple_tags') && ! current_user_can('edit_posts')) {
74 wp_send_json_error(array( 'message' => 'Insufficient permissions.' ), 403);
75 }
76
77 // Check if nonce is set and valid
78 if (! isset($_REQUEST['nonce']) || ! wp_verify_nonce($_REQUEST['nonce'], 'st-admin-js')) {
79 wp_send_json_error(array( 'message' => 'Invalid or missing nonce.' ), 403);
80 }
81
82 if (isset($_GET['stags_action']) && 'helper_js_collection' === $_GET['stags_action']) {
83 self::ajax_local_tags();
84 }
85 }
86
87 /**
88 * Display a javascript collection for autocompletion script !
89 *
90 * @return void
91 * @author WebFactory Ltd
92 */
93 public static function ajax_local_tags()
94 {
95 status_header(200); // Send good header HTTP
96 header('Content-Type: application/json; charset=' . get_bloginfo('charset'));
97
98 $taxonomy = 'post_tag';
99 if (isset($_REQUEST['taxonomy']) && !empty($_REQUEST['taxonomy'])) {// &&
100 $taxonomy = sanitize_text_field($_REQUEST['taxonomy']);
101 }
102 if (taxonomy_exists($taxonomy) && (int) wp_count_terms($taxonomy, array( 'hide_empty' => false )) === 0) { // No tags to suggest
103 echo wp_json_encode(array());
104 exit();
105 }
106
107 // Prepare search
108 $search = (isset($_GET['term'])) ? trim(stripslashes(sanitize_text_field($_GET['term']))) : '';
109 $exclude_term = isset($_REQUEST['exclude_term']) ? (int) $_REQUEST['exclude_term'] : 0;
110
111 // Get all terms, or filter with search
112 $terms = SimpleTags_Admin::getTermsForAjax($taxonomy, $search);
113
114 if (empty($terms)) {
115 echo wp_json_encode(array());
116 exit();
117 }
118
119 // Format
120 $results = array();
121 foreach ((array) $terms as $term) {
122 if ((int) $term->term_id === $exclude_term) {
123 continue;
124 }
125
126 $term->name = stripslashes($term->name);
127 $original_name = $term->name;
128
129 $is_mass_edit_page = isset($_GET['page']) && $_GET['page'] === 'st_mass_terms';
130 $show_slug = SimpleTags_Plugin::get_option_value('enable_mass-edit_terms_slug');
131 if ($is_mass_edit_page && $show_slug && ! empty($term->slug)) {
132 $term->name = $term->name . ' (' . $term->slug . ')';
133 }
134
135 if ($taxonomy === 'linked_term_taxonomies') {
136 $term->name = $term->name . ' (' . $term->taxonomy . ')';
137 }
138
139 $term->name = str_replace(array( "\r\n", "\r", "\n" ), '', $term->name);
140
141 // Decode any HTML entities for display in autocomplete widgets.
142 $display_name = html_entity_decode(
143 $term->name,
144 ENT_QUOTES,
145 get_bloginfo('charset')
146 );
147
148 $results[] = array(
149 'id' => $term->term_id,
150 'label' => $display_name,
151 'value' => $display_name,
152 'taxonomy' => $term->taxonomy,
153 'name' => $original_name,
154 );
155 }
156
157 echo wp_json_encode($results);
158 exit();
159 }
160
161 /**
162 * Content of custom meta box of TaxoPress
163 *
164 * @param object $post
165 *
166 * @return void
167 * @author WebFactory Ltd
168 */
169 public static function metabox($post)
170 {
171 // Get options
172 $autocomplete_min = 0
173 ?>
174 <p>
175 <?php wp_nonce_field('update-simple-tags', 'nonce-simple-tags'); ?>
176 <input type="hidden" name="adv-tags-input-here" value="1"/>
177 <input type="text" class="widefat" name="adv-tags-input" id="adv-tags-input"
178 value="<?php echo esc_attr(SimpleTags_Admin::getTermsToEdit('post_tag', $post->ID)); ?>"/>
179
180 <?php esc_html_e('Separate tags with commas', 'simple-tags'); ?>
181 </p>
182 <script type="text/javascript">
183 <!--
184 st_init_autocomplete('#adv-tags-input', '<?php echo esc_url_raw(admin_url("admin-ajax.php?action=simpletags_autocomplete&stags_action=helper_js_collection&nonce=" . wp_create_nonce('st-admin-js'))); ?>', <?php echo (int)$autocomplete_min; ?>)
185 -->
186 </script>
187 <?php
188 }
189
190 /**
191 * public static function called on auto terms page
192 *
193 * @param string $taxonomy
194 *
195 * @return void
196 * @author WebFactory Ltd
197 */
198 public static function auto_terms_js($taxonomy = '')
199 {
200 // Get option
201 $autocomplete_min = 0
202 ?>
203 <script type="text/javascript">
204 <!--
205 st_init_autocomplete('#auto_list', "<?php echo esc_url_raw(admin_url('admin-ajax.php?action=simpletags_autocomplete&stags_action=helper_js_collection&taxonomy=' . $taxonomy . '&nonce=' . wp_create_nonce('st-admin-js'))); ?>", <?php echo (int)$autocomplete_min; ?>)
206 -->
207 </script>
208 <?php
209 }
210
211 /**
212 * public static function called on manage terms page
213 *
214 * @param string $taxonomy
215 *
216 * @return void
217 * @author WebFactory Ltd
218 */
219 public static function manage_terms_js($taxonomy = '')
220 {
221 // Get option
222 $autocomplete_min = 0
223 ?>
224 <script type="text/javascript">
225 <!--
226 st_init_autocomplete('.autocomplete-input', "<?php echo esc_url_raw(admin_url('admin-ajax.php?action=simpletags_autocomplete&stags_action=helper_js_collection&taxonomy=' . $taxonomy . '&nonce=' . wp_create_nonce('st-admin-js'))); ?>", <?php echo (int)$autocomplete_min; ?>)
227 -->
228 </script>
229 <?php
230 }
231
232 /**
233 * public static function called on mass terms page
234 *
235 * @param string $taxonomy
236 *
237 * @return void
238 * @author WebFactory Ltd
239 */
240 public static function mass_terms_js($taxonomy = '')
241 {
242 // Get option
243 $autocomplete_min = 0
244 ?>
245 <script type="text/javascript">
246 <!--
247 st_init_autocomplete('.autocomplete-input', "<?php echo esc_url_raw(admin_url('admin-ajax.php?action=simpletags_autocomplete&stags_action=helper_js_collection&taxonomy=' . esc_attr($taxonomy) . '&page=st_mass_terms&nonce=' . wp_create_nonce('st-admin-js'))); ?>", <?php echo (int)$autocomplete_min; ?>)
248 -->
249 </script>
250 <?php
251 }
252
253 /**
254 * public static function called on autolinks page
255 *
256 * @param string $taxonomy
257 *
258 * @return void
259 * @author ojopaul
260 */
261 public static function autolinks_js()
262 {
263 // Get option
264 $autocomplete_min = 0
265 ?>
266 <script type="text/javascript">
267 <!--
268 st_init_autocomplete('.autocomplete-input', "<?php echo esc_url_raw(admin_url('admin-ajax.php?action=simpletags_autocomplete&stags_action=helper_js_collection&taxonomy=&nonce=' . wp_create_nonce('st-admin-js'))); ?>", <?php echo (int)$autocomplete_min; ?>, '.taxopress-dynamic-taxonomy')
269 -->
270 </script>
271 <?php
272 }
273
274 /**
275 * public static function called on terms display (tag cloud) page
276 *
277 * @param string $taxonomy
278 */
279 public static function terms_display_js($taxonomy = '')
280 {
281 $autocomplete_min = 0;
282 ?>
283 <script type="text/javascript">
284 <!--
285 // Base URL leaves taxonomy blank so helper JS can replace it dynamically
286 st_init_autocomplete(
287 '.tagclouds-exclude',
288 "<?php echo esc_url_raw(
289 admin_url(
290 'admin-ajax.php?action=simpletags_autocomplete&stags_action=helper_js_collection&taxonomy=&nonce=' . wp_create_nonce('st-admin-js')
291 )
292 ); ?>",
293 <?php echo (int) $autocomplete_min; ?>
294 );
295 st_init_autocomplete(
296 '.tagclouds-include',
297 "<?php echo esc_url_raw(
298 admin_url(
299 'admin-ajax.php?action=simpletags_autocomplete&stags_action=helper_js_collection&taxonomy=&nonce=' . wp_create_nonce('st-admin-js')
300 )
301 ); ?>",
302 <?php echo (int) $autocomplete_min; ?>
303 );
304 -->
305 </script>
306 <?php
307 }
308
309 }
310