PluginProbe
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms / trunk
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms vtrunk
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.autocomplete.php

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

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