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