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

274 lines 9.1 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 if ($taxonomy == 'linked_term_taxonomies') {
124 $term->name = $term->name . ' ('. $term->taxonomy .')';
125 }
126 $term->name = str_replace( array( "\r\n", "\r", "\n" ), '', $term->name );
127
128 $results[] = array(
129 'id' => $term->term_id,
130 'label' => $term->name,
131 'value' => $term->name,
132 'taxonomy' => $term->taxonomy,
133 'name' => $original_name,
134 );
135 }
136
137 echo wp_json_encode( $results );
138 exit();
139 }
140
141 /**
142 * Content of custom meta box of TaxoPress
143 *
144 * @param object $post
145 *
146 * @return void
147 * @author WebFactory Ltd
148 */
149 public static function metabox( $post ) {
150 // Get options
151 $autocomplete_min = 0
152 ?>
153 <p>
154 <?php wp_nonce_field( 'update-simple-tags', 'nonce-simple-tags' ); ?>
155 <input type="hidden" name="adv-tags-input-here" value="1"/>
156 <input type="text" class="widefat" name="adv-tags-input" id="adv-tags-input"
157 value="<?php echo esc_attr( SimpleTags_Admin::getTermsToEdit( 'post_tag', $post->ID ) ); ?>"/>
158
159 <?php esc_html_e( 'Separate tags with commas', 'simple-tags' ); ?>
160 </p>
161 <script type="text/javascript">
162 <!--
163 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; ?>)
164 -->
165 </script>
166 <?php
167 }
168
169 /**
170 * public static function called on auto terms page
171 *
172 * @param string $taxonomy
173 *
174 * @return void
175 * @author WebFactory Ltd
176 */
177 public static function auto_terms_js( $taxonomy = '' ) {
178 // Get option
179 $autocomplete_min = 0
180 ?>
181 <script type="text/javascript">
182 <!--
183 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; ?>)
184 -->
185 </script>
186 <?php
187 }
188
189 /**
190 * public static function called on manage terms page
191 *
192 * @param string $taxonomy
193 *
194 * @return void
195 * @author WebFactory Ltd
196 */
197 public static function manage_terms_js( $taxonomy = '' ) {
198 // Get option
199 $autocomplete_min = 0
200 ?>
201 <script type="text/javascript">
202 <!--
203 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; ?>)
204 -->
205 </script>
206 <?php
207 }
208
209 /**
210 * public static function called on mass terms page
211 *
212 * @param string $taxonomy
213 *
214 * @return void
215 * @author WebFactory Ltd
216 */
217 public static function mass_terms_js( $taxonomy = '' ) {
218 // Get option
219 $autocomplete_min = 0
220 ?>
221 <script type="text/javascript">
222 <!--
223 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) ) . '&nonce=' . wp_create_nonce( 'st-admin-js' )); ?>", <?php echo (int)$autocomplete_min; ?>)
224 -->
225 </script>
226 <?php
227 }
228
229 /**
230 * public static function called on autolinks page
231 *
232 * @param string $taxonomy
233 *
234 * @return void
235 * @author ojopaul
236 */
237 public static function autolinks_js() {
238 // Get option
239 $autocomplete_min = 0
240 ?>
241 <script type="text/javascript">
242 <!--
243 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')
244 -->
245 </script>
246 <?php
247 }
248
249 /**
250 * public static function called on terms display (tag cloud) page
251 *
252 * @param string $taxonomy
253 */
254 public static function terms_display_js( $taxonomy = '' ) {
255 $autocomplete_min = 0;
256 ?>
257 <script type="text/javascript">
258 <!--
259 // Base URL leaves taxonomy blank so helper JS can replace it dynamically
260 st_init_autocomplete(
261 '.tagclouds-exclude',
262 "<?php echo esc_url_raw(
263 admin_url(
264 'admin-ajax.php?action=simpletags_autocomplete&stags_action=helper_js_collection&taxonomy=&nonce=' . wp_create_nonce('st-admin-js')
265 )
266 ); ?>",
267 <?php echo (int) $autocomplete_min; ?>
268 );
269 -->
270 </script>
271 <?php
272 }
273
274 }