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