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