| 1 |
<?php |
| 2 |
|
| 3 |
class SimpleTags_Terms |
| 4 |
{ |
| 5 |
|
| 6 |
const MENU_SLUG = 'st_options'; |
| 7 |
|
| 8 |
// class instance |
| 9 |
static $instance; |
| 10 |
|
| 11 |
// WP_List_Table object |
| 12 |
public $terms_table; |
| 13 |
|
| 14 |
/** |
| 15 |
* Constructor |
| 16 |
* |
| 17 |
* @return void |
| 18 |
* @author Olatechpro |
| 19 |
*/ |
| 20 |
public function __construct() |
| 21 |
{ |
| 22 |
|
| 23 |
global $hook_suffix; //avoid warning outputs |
| 24 |
if (!isset($hook_suffix)) { |
| 25 |
$hook_suffix = ''; |
| 26 |
} |
| 27 |
|
| 28 |
add_filter('set-screen-option', [__CLASS__, 'set_screen'], 10, 3); |
| 29 |
// Admin menu |
| 30 |
add_action('admin_menu', [$this, 'admin_menu']); |
| 31 |
// Javascript |
| 32 |
add_action('admin_enqueue_scripts', [__CLASS__, 'admin_enqueue_scripts'], 11); |
| 33 |
//support post type for terms query |
| 34 |
add_filter('terms_clauses', [__CLASS__, 'taxopress_terms_clauses'], 10, 3); |
| 35 |
//inline term edit |
| 36 |
add_action( 'wp_ajax_taxopress_terms_inline_save_term', [$this, 'taxopress_terms_inline_save_term_callback']); |
| 37 |
|
| 38 |
} |
| 39 |
|
| 40 |
// Handle the post_type parameter given in get_terms function |
| 41 |
public static function taxopress_terms_clauses($clauses, $taxonomy, $args) { |
| 42 |
if (isset($_REQUEST['page']) && $_REQUEST['page'] === 'st_terms' && !empty($args['post_types']) && is_array($args['post_types'])) { |
| 43 |
global $wpdb; |
| 44 |
|
| 45 |
$post_types = array(); |
| 46 |
|
| 47 |
foreach($args['post_types'] as $cpt) { |
| 48 |
$post_types[] = "'".$cpt."'"; |
| 49 |
} |
| 50 |
|
| 51 |
if(!empty($post_types)) { |
| 52 |
$clauses['fields'] = 'DISTINCT '.str_replace('tt.*', 'tt.term_taxonomy_id, tt.term_id, tt.taxonomy, tt.description, tt.parent', $clauses['fields']).', COUNT(t.term_id) AS count'; |
| 53 |
$clauses['join'] .= ' INNER JOIN '.$wpdb->term_relationships.' AS r ON r.term_taxonomy_id = tt.term_taxonomy_id INNER JOIN '.$wpdb->posts.' AS p ON p.ID = r.object_id'; |
| 54 |
$clauses['where'] .= ' AND p.post_type IN ('.implode(',', $post_types).')'; |
| 55 |
$clauses['orderby'] = 'GROUP BY t.term_id '.$clauses['orderby']; |
| 56 |
} |
| 57 |
} |
| 58 |
return $clauses; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Init somes JS and CSS need for this feature |
| 63 |
* |
| 64 |
* @return void |
| 65 |
* @author Olatechpro |
| 66 |
*/ |
| 67 |
public static function admin_enqueue_scripts() |
| 68 |
{ |
| 69 |
|
| 70 |
// add JS for manage click tags |
| 71 |
if (isset($_GET['page']) && $_GET['page'] == 'st_terms') { |
| 72 |
wp_enqueue_style('st-taxonomies-css'); |
| 73 |
wp_enqueue_script( 'admin-tags' ); |
| 74 |
wp_enqueue_script('inline-edit-tax'); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
public function taxopress_terms_inline_save_term_callback() |
| 80 |
{ |
| 81 |
global $wpdb; |
| 82 |
|
| 83 |
check_ajax_referer( 'taxinlineeditnonce', '_inline_edit' ); |
| 84 |
|
| 85 |
$edit_taxonomy = sanitize_key( $_POST['edit_taxonomy'] ); |
| 86 |
$taxonomy = sanitize_key( $_POST['original_tax'] ); |
| 87 |
$tax = get_taxonomy( $taxonomy ); |
| 88 |
$edit_tax = get_taxonomy( $edit_taxonomy ); |
| 89 |
|
| 90 |
if ( ! $tax || !$edit_tax ) { |
| 91 |
wp_die( 0 ); |
| 92 |
} |
| 93 |
|
| 94 |
if ( ! isset( $_POST['tax_ID'] ) || ! (int) $_POST['tax_ID'] ) { |
| 95 |
wp_die( -1 ); |
| 96 |
} |
| 97 |
|
| 98 |
$id = (int) $_POST['tax_ID']; |
| 99 |
|
| 100 |
if ( ! current_user_can( 'edit_term', $id ) ) { |
| 101 |
wp_die( -1 ); |
| 102 |
} |
| 103 |
|
| 104 |
$tag = get_term( $id, $taxonomy ); |
| 105 |
$_POST['description'] = $tag->description; |
| 106 |
|
| 107 |
$updated = wp_update_term( $id, $taxonomy, $_POST ); |
| 108 |
|
| 109 |
if ( $updated && ! is_wp_error( $updated ) ) { |
| 110 |
$tag = get_term( $updated['term_id'], $taxonomy ); |
| 111 |
if ( ! $tag || is_wp_error( $tag ) ) { |
| 112 |
if ( is_wp_error( $tag ) && $tag->get_error_message() ) { |
| 113 |
wp_die( $tag->get_error_message() ); |
| 114 |
} |
| 115 |
wp_die( __( 'Item not updated.' ) ); |
| 116 |
} else { |
| 117 |
if($tax !== $edit_tax){ |
| 118 |
$update_term = $wpdb->update( |
| 119 |
$wpdb->prefix . 'term_taxonomy', |
| 120 |
[ 'taxonomy' => $edit_taxonomy ], |
| 121 |
[ 'term_taxonomy_id' => $tag->term_id ], |
| 122 |
[ '%s' ], |
| 123 |
[ '%d' ] |
| 124 |
); |
| 125 |
if($update_term){ |
| 126 |
clean_term_cache($tag->term_id); |
| 127 |
$tag = get_term( $tag->term_id ); |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
} else { |
| 132 |
if ( is_wp_error( $updated ) && $updated->get_error_message() ) { |
| 133 |
wp_die( $updated->get_error_message() ); |
| 134 |
} |
| 135 |
wp_die( __( 'Item not updated.' ) ); |
| 136 |
} |
| 137 |
|
| 138 |
$wp_list_table = new Taxopress_Terms_List(); |
| 139 |
|
| 140 |
$wp_list_table->single_row($tag); |
| 141 |
wp_die(); |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
public static function set_screen($status, $option, $value) |
| 147 |
{ |
| 148 |
return $value; |
| 149 |
} |
| 150 |
|
| 151 |
/** Singleton instance */ |
| 152 |
public static function get_instance() |
| 153 |
{ |
| 154 |
if (!isset(self::$instance)) { |
| 155 |
self::$instance = new self(); |
| 156 |
} |
| 157 |
|
| 158 |
return self::$instance; |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Add WP admin menu for Tags |
| 163 |
* |
| 164 |
* @return void |
| 165 |
* @author Olatechpro |
| 166 |
*/ |
| 167 |
public function admin_menu() |
| 168 |
{ |
| 169 |
$hook = add_submenu_page( |
| 170 |
self::MENU_SLUG, |
| 171 |
esc_html__('Terms', 'simple-tags'), |
| 172 |
esc_html__('Terms', 'simple-tags'), |
| 173 |
'simple_tags', |
| 174 |
'st_terms', |
| 175 |
[ |
| 176 |
$this, |
| 177 |
'page_manage_terms', |
| 178 |
] |
| 179 |
); |
| 180 |
|
| 181 |
if (taxopress_is_screen_main_page()) { |
| 182 |
add_action("load-$hook", [$this, 'screen_option']); |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Screen options |
| 188 |
*/ |
| 189 |
public function screen_option() |
| 190 |
{ |
| 191 |
|
| 192 |
$option = 'per_page'; |
| 193 |
$args = [ |
| 194 |
'label' => esc_html__('Number of items per page', 'simple-tags'), |
| 195 |
'default' => 20, |
| 196 |
'option' => 'st_terms_per_page' |
| 197 |
]; |
| 198 |
|
| 199 |
add_screen_option($option, $args); |
| 200 |
|
| 201 |
$this->terms_table = new Taxopress_Terms_List(); |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Method for build the page HTML manage tags |
| 206 |
* |
| 207 |
* @return void |
| 208 |
* @author Olatechpro |
| 209 |
*/ |
| 210 |
public function page_manage_terms() |
| 211 |
{ |
| 212 |
// Default order |
| 213 |
if (!isset($_GET['order'])) { |
| 214 |
$_GET['order'] = 'name-asc'; |
| 215 |
} |
| 216 |
|
| 217 |
settings_errors(__CLASS__); |
| 218 |
|
| 219 |
?> |
| 220 |
<div class="wrap st_wrap st-manage-taxonomies-page"> |
| 221 |
|
| 222 |
<div id=""> |
| 223 |
<h1 class="wp-heading-inline"><?php esc_html_e('Terms', 'simple-tags'); ?></h1> |
| 224 |
<div class="taxopress-description"> |
| 225 |
<?php esc_html_e('This feature lists all the terms on your site.', 'simple-tags'); ?> |
| 226 |
</div> |
| 227 |
|
| 228 |
|
| 229 |
<?php |
| 230 |
if (isset($_REQUEST['s']) && $search = esc_attr(sanitize_text_field(wp_unslash($_REQUEST['s'])))) { |
| 231 |
/* translators: %s: search keywords */ |
| 232 |
printf(' <span class="subtitle">' . esc_html__('Search results for “%s”', |
| 233 |
'simple-tags') . '</span>', esc_html($search)); |
| 234 |
} |
| 235 |
?> |
| 236 |
<?php |
| 237 |
|
| 238 |
//the terms table instance |
| 239 |
$this->terms_table->prepare_items(); |
| 240 |
?> |
| 241 |
|
| 242 |
|
| 243 |
<hr class="wp-header-end"> |
| 244 |
<div id="ajax-response"></div> |
| 245 |
<form class="search-form wp-clearfix st-taxonomies-search-form" method="get"> |
| 246 |
<?php $this->terms_table->search_box(esc_html__('Search Terms', 'simple-tags'), 'term'); ?> |
| 247 |
</form> |
| 248 |
<div class="clear"></div> |
| 249 |
|
| 250 |
<div id="col-container" class="wp-clearfix"> |
| 251 |
|
| 252 |
<div class="col-wrap"> |
| 253 |
<form action="<?php echo esc_url(add_query_arg('', '')); ?>" method="post"> |
| 254 |
<?php $this->terms_table->display(); //Display the table ?> |
| 255 |
</form> |
| 256 |
<div class="form-wrap edit-term-notes"> |
| 257 |
<p><?php esc_html__('Description here.', 'simple-tags') ?></p> |
| 258 |
</div> |
| 259 |
</div> |
| 260 |
|
| 261 |
|
| 262 |
</div> |
| 263 |
|
| 264 |
|
| 265 |
</div> |
| 266 |
<?php $this->terms_table->inline_edit(); ?> |
| 267 |
|
| 268 |
<script> |
| 269 |
|
| 270 |
(function ($) { |
| 271 |
'use strict'; |
| 272 |
|
| 273 |
/** |
| 274 |
* All of the code for admin-facing JavaScript source |
| 275 |
* should reside in this file. |
| 276 |
*/ |
| 277 |
|
| 278 |
$( window ).load(function() { |
| 279 |
// ------------------------------------------------------------- |
| 280 |
// TaxoPress terms quick edit |
| 281 |
// ------------------------------------------------------------- |
| 282 |
$('.taxopress_page_st_terms #the-list').on( 'click', 'a.editinline, button.editinline', function() { |
| 283 |
var id, taxonomy, editRow, rowData; |
| 284 |
var r_id = get_taxopress_term_id(this); |
| 285 |
rowData = $('#inline_' + r_id); |
| 286 |
$('#inline-edit') |
| 287 |
editRow = $('.inline-edit-row#edit-'+r_id); |
| 288 |
taxonomy = $('.taxonomy', rowData).text(); |
| 289 |
$('select[name="edit_taxonomy"]', editRow).val( taxonomy ); |
| 290 |
}); |
| 291 |
function get_taxopress_term_id(o){ |
| 292 |
var id = o.tagName === 'TR' ? o.id : $(o).parents('tr').attr('id'), parts = id.split('-'); |
| 293 |
|
| 294 |
return parts[parts.length - 1]; |
| 295 |
} |
| 296 |
}); |
| 297 |
|
| 298 |
})(jQuery); |
| 299 |
|
| 300 |
</script> |
| 301 |
<?php SimpleTags_Admin::printAdminFooter(); ?> |
| 302 |
</div> |
| 303 |
<?php |
| 304 |
} |
| 305 |
|
| 306 |
} |
| 307 |
|