| 1 |
<?php |
| 2 |
|
| 3 |
// phpcs:disable Squiz.PHP.CommentedOutCode.Found,WordPress.Security.NonceVerification.Recommended,WordPress.Security.ValidatedSanitizedInput.InputNotValidated -- Legacy TaxoPress file: keep behavior unchanged while documenting existing PHPCS exceptions. |
| 4 |
|
| 5 |
class SimpleTags_Admin_ClickTags |
| 6 |
{ |
| 7 |
/** |
| 8 |
* Constructor |
| 9 |
* |
| 10 |
* @return void |
| 11 |
* @author WebFactory Ltd |
| 12 |
*/ |
| 13 |
public function __construct() |
| 14 |
{ |
| 15 |
// Ajax action, JS Helper and admin action |
| 16 |
add_action('wp_ajax_simpletags', array( __CLASS__, 'ajax_check' )); |
| 17 |
|
| 18 |
if (1 === (int) SimpleTags_Plugin::get_option_value('active_suggest_terms')) { |
| 19 |
// Box for post/page |
| 20 |
//add_action( 'admin_head', array( __CLASS__, 'admin_head' ), 1 ); |
| 21 |
// Javascript |
| 22 |
//add_action( 'admin_enqueue_scripts', array( __CLASS__, 'admin_enqueue_scripts' ), 11 ); |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Init somes JS and CSS need for this feature |
| 28 |
* |
| 29 |
* @return void |
| 30 |
* @author WebFactory Ltd |
| 31 |
*/ |
| 32 |
public static function admin_enqueue_scripts() |
| 33 |
{ |
| 34 |
global $pagenow; |
| 35 |
|
| 36 |
$click_terms = taxopress_current_post_suggest_terms('existing_terms'); |
| 37 |
|
| 38 |
if (!is_array($click_terms)) { |
| 39 |
return; |
| 40 |
} |
| 41 |
|
| 42 |
wp_register_script( |
| 43 |
'st-helper-click-tags', |
| 44 |
STAGS_URL . '/assets/js/helper-click-tags.js', |
| 45 |
array( |
| 46 |
'jquery', |
| 47 |
'st-helper-add-tags', |
| 48 |
), |
| 49 |
STAGS_VERSION, |
| 50 |
true |
| 51 |
); |
| 52 |
|
| 53 |
$post_type_data = get_post_type_object(get_post_type()); |
| 54 |
$post_type_name = is_object($post_type_data) && isset($post_type_data->labels->singular_name) ? strtolower($post_type_data->labels->singular_name) : 'post'; |
| 55 |
$post_taxonomies = get_object_taxonomies(get_post_type()); |
| 56 |
|
| 57 |
//add taxonomy |
| 58 |
$click_tags_taxonomy = ' |
| 59 |
<div class="option"> |
| 60 |
<label>' . esc_html__('Taxonomy', 'simple-tags') . '</label><br /> |
| 61 |
<select class="st-post-taxonomy-select click_tags_taxonomy" name="click_tags_taxonomy">'; |
| 62 |
foreach ($post_taxonomies as $_taxonomy) { |
| 63 |
$_taxonomy = get_taxonomy($_taxonomy); |
| 64 |
if ($_taxonomy->name === 'author') { |
| 65 |
continue; |
| 66 |
} |
| 67 |
if (!isset($_taxonomy->public) || (isset($_taxonomy->public) && (int)$_taxonomy->public === 0)) { |
| 68 |
continue; |
| 69 |
} |
| 70 |
|
| 71 |
if (is_array($click_terms['taxonomy']) && in_array($_taxonomy->name, $click_terms['taxonomy'])) { |
| 72 |
$click_tags_taxonomy .= '<option value="' . $_taxonomy->name . '" selected="selected">' . $_taxonomy->labels->name . '</option>'; |
| 73 |
} elseif (!is_array($click_terms['taxonomy']) && $_taxonomy->name === $click_terms['taxonomy']) { // backward compatibility |
| 74 |
$click_tags_taxonomy .= '<option value="' . $_taxonomy->name . '" selected="selected">' . $_taxonomy->labels->name . '</option>'; |
| 75 |
} else { |
| 76 |
$click_tags_taxonomy .= '<option value="' . $_taxonomy->name . '">' . $_taxonomy->labels->name . '</option>'; |
| 77 |
} |
| 78 |
} |
| 79 |
$click_tags_taxonomy .= '</select> |
| 80 |
</div>'; |
| 81 |
|
| 82 |
//add method |
| 83 |
$click_tags_methods = ['name' => esc_html__('Name', 'simple-tags'), 'count' => esc_html__('Counter', 'simple-tags'), 'random' => esc_html__('Random', 'simple-tags')]; |
| 84 |
$click_tags_method = ' |
| 85 |
<div class="option"> |
| 86 |
<label>' . __('Method for choosing terms', 'simple-tags') . '</label><br /> |
| 87 |
<select class="click_tags_method" name="click_tags_method">'; |
| 88 |
foreach ($click_tags_methods as $option => $label) { |
| 89 |
$selected = ($option === $click_terms['orderby']) ? 'selected="selected"' : ''; |
| 90 |
$click_tags_method .= '<option value="' . $option . '" ' . $selected . '>' . $label . '</option>'; |
| 91 |
} |
| 92 |
$click_tags_method .= '</select> |
| 93 |
</div>'; |
| 94 |
|
| 95 |
//add order |
| 96 |
$click_tags_orders = ['asc' => esc_html__('Ascending', 'simple-tags'), 'desc' => esc_html__('Descending', 'simple-tags')]; |
| 97 |
$click_tags_order = ' |
| 98 |
<div class="option"> |
| 99 |
<label>' . __('Ordering for choosing terms', 'simple-tags') . '</label><br /> |
| 100 |
<select class="click_tags_order" name="click_tags_order">'; |
| 101 |
foreach ($click_tags_orders as $option => $label) { |
| 102 |
$selected = ($option === $click_terms['order']) ? 'selected="selected"' : ''; |
| 103 |
$click_tags_order .= '<option value="' . $option . '" ' . $selected . '>' . $label . '</option>'; |
| 104 |
} |
| 105 |
$click_tags_order .= '</select> |
| 106 |
</div>'; |
| 107 |
|
| 108 |
//add limit |
| 109 |
$click_tags_limit = ' |
| 110 |
<div class="option"> |
| 111 |
<label for="click_tags_limit">' . __('Maximum terms', 'simple-tags') . '</label><br /> |
| 112 |
<input type="number" class="click_tags_limit" id="click_tags_limit" name="click_tags_limit" value="' . $click_terms['number'] . '"> |
| 113 |
</div>'; |
| 114 |
|
| 115 |
//add searchbox |
| 116 |
$click_tags_search = ' |
| 117 |
<div class="option"> |
| 118 |
<label for="click_tags_search">Search</label><br /> |
| 119 |
<input name="click_tags_search" id="click_tags_search" type="text" class="click-tag-search-box" placeholder="' . __('Start typing to search', 'simple-tags') . '" size="26" autocomplete="off"> |
| 120 |
</div>'; |
| 121 |
|
| 122 |
//create tags search data |
| 123 |
$click_tags_options = '<div class="clicktags-search-wrapper">' . $click_tags_search . ' ' . $click_tags_taxonomy . ' ' . $click_tags_method . ' ' . $click_tags_order . ' ' . $click_tags_limit . '</div>'; |
| 124 |
|
| 125 |
//metabox edit line |
| 126 |
if (current_user_can('admin_simple_tags')) { |
| 127 |
$click_term_edit = '<span class="edit-suggest-term-metabox"> |
| 128 |
' . sprintf( |
| 129 |
'<a href="%s">%s</a>', |
| 130 |
add_query_arg( |
| 131 |
[ |
| 132 |
'page' => 'st_suggestterms', |
| 133 |
'add' => 'new_item', |
| 134 |
'action' => 'edit', |
| 135 |
'taxopress_suggestterms' => $click_terms['ID'], |
| 136 |
], |
| 137 |
admin_url('admin.php') |
| 138 |
), |
| 139 |
__('Edit this metabox', 'simple-tags') |
| 140 |
) |
| 141 |
. ' |
| 142 |
</span>'; |
| 143 |
} else { |
| 144 |
$click_term_edit = ''; |
| 145 |
} |
| 146 |
wp_localize_script( |
| 147 |
'st-helper-click-tags', |
| 148 |
'stHelperClickTagsL10n', |
| 149 |
array( |
| 150 |
'show_txt' => esc_html__('Click to display tags', 'simple-tags'), |
| 151 |
'arial_label' => esc_html__('suggested terms', 'simple-tags'), |
| 152 |
'hide_txt' => sprintf(esc_html__('Click terms to add them to this %s', 'simple-tags'), $post_type_name), |
| 153 |
'state' => 'show', |
| 154 |
'search_icon' => STAGS_URL . '/assets/images/indicator.gif', |
| 155 |
'nonce' => wp_create_nonce('st-admin-js'), |
| 156 |
'search_box' => '<input type="text" class="click-tag-search-box" placeholder="' . __('Start typing to search', 'simple-tags') . '" size="26" autocomplete="off">', |
| 157 |
'click_tags_options' => $click_tags_options, |
| 158 |
'edit_metabox_link' => $click_term_edit, |
| 159 |
) |
| 160 |
); |
| 161 |
|
| 162 |
// Helper for post type |
| 163 |
wp_enqueue_script('st-helper-click-tags'); |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Register metabox |
| 168 |
* |
| 169 |
* @return void |
| 170 |
* @author WebFactory Ltd |
| 171 |
*/ |
| 172 |
public static function admin_head() |
| 173 |
{ |
| 174 |
|
| 175 |
$click_terms = taxopress_current_post_suggest_terms('existing_terms'); |
| 176 |
|
| 177 |
if (!is_array($click_terms)) { |
| 178 |
return; |
| 179 |
} |
| 180 |
add_meta_box( |
| 181 |
'st-clicks-tags', |
| 182 |
__('Show existing terms', 'simple-tags'), |
| 183 |
array( |
| 184 |
__CLASS__, |
| 185 |
'metabox', |
| 186 |
), |
| 187 |
get_post_type(), |
| 188 |
'advanced', |
| 189 |
'core' |
| 190 |
); |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Put default HTML for people without JS |
| 195 |
* |
| 196 |
* @return void |
| 197 |
* @author WebFactory Ltd |
| 198 |
*/ |
| 199 |
public static function metabox() |
| 200 |
{ |
| 201 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 202 |
echo SimpleTags_Admin::getDefaultContentBox(); |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Ajax Dispatcher |
| 207 |
* |
| 208 |
* @return void |
| 209 |
* @author WebFactory Ltd |
| 210 |
*/ |
| 211 |
public static function ajax_check() |
| 212 |
{ |
| 213 |
|
| 214 |
if (isset($_GET['stags_action']) && 'click_tags' === $_GET['stags_action']) { |
| 215 |
self::ajax_click_tags(); |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* Display a span list for click tags |
| 221 |
* |
| 222 |
* @return void |
| 223 |
* @author WebFactory Ltd |
| 224 |
*/ |
| 225 |
public static function ajax_click_tags() |
| 226 |
{ |
| 227 |
if (!check_ajax_referer('st-admin-js', 'nonce', false)) { |
| 228 |
wp_die(esc_html__('Security check failed.', 'simple-tags'), '', ['response' => 403]); |
| 229 |
} |
| 230 |
|
| 231 |
$taxonomy = isset($_GET['click_tags_taxonomy']) ? sanitize_key(wp_unslash($_GET['click_tags_taxonomy'])) : 'post_tag'; |
| 232 |
$taxonomy_object = get_taxonomy($taxonomy); |
| 233 |
$post_id = isset($_GET['post_id']) ? absint($_GET['post_id']) : 0; |
| 234 |
|
| 235 |
if ( |
| 236 |
!$taxonomy_object |
| 237 |
|| empty($taxonomy_object->cap->assign_terms) |
| 238 |
|| !current_user_can($taxonomy_object->cap->assign_terms) |
| 239 |
|| ($post_id > 0 && !current_user_can('edit_post', $post_id)) |
| 240 |
) { |
| 241 |
wp_die(esc_html__('Permission denied.', 'simple-tags'), '', ['response' => 403]); |
| 242 |
} |
| 243 |
|
| 244 |
status_header(200); // Send good header HTTP |
| 245 |
header('Content-Type: text/html; charset=' . get_bloginfo('charset')); |
| 246 |
|
| 247 |
if (0 === (int) wp_count_terms(array( 'taxonomy' => $taxonomy, 'hide_empty' => false ))) { // No tags to suggest |
| 248 |
echo '<p>' . esc_html__('No terms in your WordPress database.', 'simple-tags') . '</p>'; |
| 249 |
exit(); |
| 250 |
} |
| 251 |
|
| 252 |
// Prepare search |
| 253 |
$search = (isset($_GET['q'])) ? trim(stripslashes(sanitize_text_field(wp_unslash($_GET['q'])))) : ''; |
| 254 |
if (isset($_GET['click_tags_method']) && !empty($_GET['click_tags_method'])) { |
| 255 |
$order_click_tags = (sanitize_text_field(wp_unslash($_GET['click_tags_method'])) === 'random') ? sanitize_text_field(wp_unslash($_GET['click_tags_method'])) : sanitize_text_field(wp_unslash($_GET['click_tags_method'])) . '-' . sanitize_text_field(wp_unslash($_GET['click_tags_order'])); |
| 256 |
} else { |
| 257 |
// Order tags before selection (count-asc/count-desc/name-asc/name-desc/random) |
| 258 |
$order_click_tags = 'random'; |
| 259 |
} |
| 260 |
switch ($order_click_tags) { |
| 261 |
case 'count-asc': |
| 262 |
$order_by = 'tt.count'; |
| 263 |
$order = 'ASC'; |
| 264 |
break; |
| 265 |
case 'random': |
| 266 |
$order_by = 'RAND()'; |
| 267 |
$order = ''; |
| 268 |
break; |
| 269 |
case 'count-desc': |
| 270 |
$order_by = 'tt.count'; |
| 271 |
$order = 'DESC'; |
| 272 |
break; |
| 273 |
case 'name-desc': |
| 274 |
$order_by = 't.name'; |
| 275 |
$order = 'DESC'; |
| 276 |
break; |
| 277 |
default: // name-asc |
| 278 |
$order_by = 't.name'; |
| 279 |
$order = 'ASC'; |
| 280 |
break; |
| 281 |
} |
| 282 |
|
| 283 |
$term_limit = isset($_GET['click_tags_limit']) ? (int)$_GET['click_tags_limit'] : 100; |
| 284 |
|
| 285 |
if ($term_limit > 0) { |
| 286 |
$limit = 'LIMIT 0, ' . $term_limit; |
| 287 |
} else { |
| 288 |
$limit = ''; |
| 289 |
} |
| 290 |
|
| 291 |
// Get all terms, or filter with search |
| 292 |
$terms = SimpleTags_Admin::getTermsForAjax($taxonomy, $search, $order_by, $order, $limit); |
| 293 |
if (empty($terms)) { |
| 294 |
echo '<p>' . esc_html__('No results from your WordPress database.', 'simple-tags') . '</p>'; |
| 295 |
exit(); |
| 296 |
} |
| 297 |
|
| 298 |
// Get terms for current post |
| 299 |
$post_terms = array(); |
| 300 |
if ($post_id > 0) { |
| 301 |
$post_terms = wp_get_post_terms($post_id, $taxonomy, array( 'fields' => 'ids' )); |
| 302 |
} |
| 303 |
|
| 304 |
foreach ((array) $terms as $term) { |
| 305 |
$class_current = in_array($term->term_id, $post_terms) ? 'used_term' : ''; |
| 306 |
echo '<span data-term_id="' . esc_attr($term->term_id) . '" data-taxonomy="' . esc_attr($taxonomy) . '" class="local ' . esc_attr($taxonomy) . ' ' . esc_attr($class_current) . '" tabindex="0" role="button" aria-pressed="false">' . esc_html(stripslashes($term->name)) . '</span>' . "\n"; |
| 307 |
} |
| 308 |
echo '<div class="clear"></div>'; |
| 309 |
|
| 310 |
exit(); |
| 311 |
} |
| 312 |
} |
| 313 |
|