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

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