| 1 |
<?php |
| 2 |
if ( ! class_exists( 'WP_List_Table' ) ) { |
| 3 |
require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' ); |
| 4 |
} |
| 5 |
|
| 6 |
class Termcloud_List extends WP_List_Table { |
| 7 |
|
| 8 |
/** Class constructor */ |
| 9 |
public function __construct() { |
| 10 |
|
| 11 |
parent::__construct( [ |
| 12 |
'singular' => __( 'Tag Cloud', 'simple-tags' ), //singular name of the listed records |
| 13 |
'plural' => __( 'Tags Cloud', 'simple-tags' ), //plural name of the listed records |
| 14 |
'ajax' => false //does this table support ajax? |
| 15 |
] ); |
| 16 |
|
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Show single row item |
| 21 |
* |
| 22 |
* @param array $item |
| 23 |
*/ |
| 24 |
public function single_row($item) |
| 25 |
{ |
| 26 |
$class = ['st-tax-tr']; |
| 27 |
$id = 'st-tax-' . md5($item->slug); |
| 28 |
echo sprintf('<tr id="%s" class="%s">', $id, implode(' ', $class)); |
| 29 |
$this->single_row_columns($item); |
| 30 |
echo '</tr>'; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Associative array of columns |
| 35 |
* |
| 36 |
* @return array |
| 37 |
*/ |
| 38 |
function get_columns() { |
| 39 |
$columns = [ |
| 40 |
'cb' => '<input type="checkbox" />', |
| 41 |
'name' => __( 'Name', 'simple-tags' ), |
| 42 |
'slug' => __( 'Slug', 'simple-tags' ), |
| 43 |
'count' => __( 'Count', 'simple-tags' ) |
| 44 |
]; |
| 45 |
|
| 46 |
return $columns; |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
/** |
| 51 |
* Columns to make sortable. |
| 52 |
* |
| 53 |
* @return array |
| 54 |
*/ |
| 55 |
protected function get_sortable_columns() { |
| 56 |
$sortable_columns = array( |
| 57 |
'name' => array( 'name', true ), |
| 58 |
'slug' => array( 'slug', true ), |
| 59 |
'count' => array( 'count', false ) |
| 60 |
); |
| 61 |
|
| 62 |
return $sortable_columns; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
/** |
| 67 |
* Render a column when no column specific method exist. |
| 68 |
* |
| 69 |
* @param array $item |
| 70 |
* @param string $column_name |
| 71 |
* |
| 72 |
* @return mixed |
| 73 |
*/ |
| 74 |
public function column_default( $item, $column_name ) { |
| 75 |
return !empty($item->$column_name) ? $item->$column_name : '—'; |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
/** |
| 80 |
* Retrieve termcloud data from the database |
| 81 |
* |
| 82 |
* @param int $per_page |
| 83 |
* @param int $page_number |
| 84 |
* |
| 85 |
* @return mixed |
| 86 |
*/ |
| 87 |
public static function get_termcloud() { |
| 88 |
|
| 89 |
$order = '&selectionby=name&selection=asc&orderby=name&order=asc'; |
| 90 |
$args = 'hide_empty=false&number=&color=false&get=all&title=' . $order . '&taxonomy=' . SimpleTags_Admin::$taxonomy ; |
| 91 |
|
| 92 |
$result = SimpleTags_Client_TagCloud::extendedTagResult( $args ); |
| 93 |
|
| 94 |
return $result; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
/** |
| 99 |
* Delete a stterm record. |
| 100 |
* |
| 101 |
* @param int $id stterm ID |
| 102 |
*/ |
| 103 |
public static function delete_stterm( $id ) { |
| 104 |
$term = get_term( $id ); |
| 105 |
wp_delete_term( $term->term_id, $term->taxonomy ); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
/** |
| 110 |
* Returns the count of records in the database. |
| 111 |
* |
| 112 |
* @return null|string |
| 113 |
*/ |
| 114 |
public static function record_count() { |
| 115 |
$order = '&selectionby=name&selection=asc&orderby=name&order=asc'; |
| 116 |
$args = 'hide_empty=false&number=&color=false&get=all&title=' . $order . '&taxonomy=' . SimpleTags_Admin::$taxonomy ; |
| 117 |
$result = SimpleTags_Client_TagCloud::extendedTagResult( $args ); |
| 118 |
|
| 119 |
return count($result); |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
/** Text displayed when no stterm data is available */ |
| 124 |
public function no_items() { |
| 125 |
_e( 'No term avaliable.', 'simple-tags' ); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Render the bulk edit checkbox |
| 130 |
* |
| 131 |
* @param array $item |
| 132 |
* |
| 133 |
* @return string |
| 134 |
*/ |
| 135 |
function column_cb( $item ) { |
| 136 |
return sprintf( |
| 137 |
'<input type="checkbox" name="st-bulk-delete-term[]" value="%s" />', $item->term_id |
| 138 |
); |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
/** |
| 143 |
* Method for name column |
| 144 |
* |
| 145 |
* @param array $item |
| 146 |
* |
| 147 |
* @return string |
| 148 |
*/ |
| 149 |
protected function column_name( $item ) { |
| 150 |
$title = sprintf( |
| 151 |
'<a href="%1$s" class="tag-cloud-link"><strong><span class="row-title">%2$s</span></strong></a>', |
| 152 |
esc_url( get_term_link( $item, $item->taxonomy )), |
| 153 |
esc_html($item->name) |
| 154 |
); |
| 155 |
|
| 156 |
return $title; |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* The action column |
| 161 |
* |
| 162 |
* @param $item |
| 163 |
* |
| 164 |
* @return string |
| 165 |
*/ |
| 166 |
protected function column_slug($item) |
| 167 |
{ |
| 168 |
return !empty($item->slug) ? $item->slug : '—'; |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* The action column |
| 173 |
* |
| 174 |
* @param $item |
| 175 |
* |
| 176 |
* @return string |
| 177 |
*/ |
| 178 |
protected function column_count($item) |
| 179 |
{ |
| 180 |
return sprintf('<a href="%s" class="">%s</a>', |
| 181 |
add_query_arg( |
| 182 |
get_taxonomy($item->taxonomy)->query_var, esc_attr($item->slug), |
| 183 |
admin_url('edit.php') |
| 184 |
), |
| 185 |
number_format_i18n($item->count)); |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Returns an associative array containing the bulk action |
| 190 |
* |
| 191 |
* @return array |
| 192 |
*/ |
| 193 |
public function get_bulk_actions() { |
| 194 |
$actions = [ |
| 195 |
'st-bulk-delete-term' => __('Delete', 'simple-tags') |
| 196 |
]; |
| 197 |
|
| 198 |
return $actions; |
| 199 |
} |
| 200 |
|
| 201 |
public function process_bulk_action() { |
| 202 |
|
| 203 |
//Detect when a bulk action is being triggered... |
| 204 |
if ( 'delete' === $this->current_action() ) { |
| 205 |
|
| 206 |
// In our file that handles the request, verify the nonce. |
| 207 |
$nonce = esc_attr( $_REQUEST['_wpnonce'] ); |
| 208 |
|
| 209 |
if ( ! wp_verify_nonce( $nonce, 'sp_delete_stterm' ) ) { |
| 210 |
die( 'Go get a life script kiddies' ); |
| 211 |
} |
| 212 |
else { |
| 213 |
self::delete_stterm( absint( $_GET['stterm'] ) ); |
| 214 |
} |
| 215 |
|
| 216 |
} |
| 217 |
|
| 218 |
// If the delete bulk action is triggered |
| 219 |
if ( ( isset( $_POST['action'] ) && $_POST['action'] == 'st-bulk-delete-term' ) |
| 220 |
|| ( isset( $_POST['action2'] ) && $_POST['action2'] == 'st-bulk-delete-term' ) |
| 221 |
) { |
| 222 |
|
| 223 |
$delete_ids = esc_sql( $_POST['st-bulk-delete-term'] ); |
| 224 |
|
| 225 |
// loop over the array of record IDs and delete them |
| 226 |
foreach ( $delete_ids as $id ) { |
| 227 |
self::delete_stterm( $id ); |
| 228 |
|
| 229 |
} |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* Determine if a given string contains a given substring. |
| 235 |
* |
| 236 |
* @param string $haystack |
| 237 |
* @param string|array $needles |
| 238 |
* @param bool $sensitive Use case sensitive search |
| 239 |
* |
| 240 |
* @return bool |
| 241 |
*/ |
| 242 |
public function str_contains($haystack, $needles, $sensitive = true) |
| 243 |
{ |
| 244 |
foreach ((array)$needles as $needle) { |
| 245 |
$function = $sensitive ? 'mb_strpos' : 'mb_stripos'; |
| 246 |
if ($needle !== '' && $function($haystack, $needle) !== false) { |
| 247 |
return true; |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
return false; |
| 252 |
} |
| 253 |
|
| 254 |
/** |
| 255 |
* Displays the search box. |
| 256 |
* |
| 257 |
* @param string $text The 'submit' button label. |
| 258 |
* @param string $input_id ID attribute value for the search input field. |
| 259 |
* |
| 260 |
* |
| 261 |
*/ |
| 262 |
public function search_box($text, $input_id) |
| 263 |
{ |
| 264 |
$input_id = $input_id . '-search-input'; |
| 265 |
|
| 266 |
if (!empty($_REQUEST['orderby'])) { |
| 267 |
echo '<input type="hidden" name="orderby" value="' . esc_attr($_REQUEST['orderby']) . '" />'; |
| 268 |
} |
| 269 |
if (!empty($_REQUEST['order'])) { |
| 270 |
echo '<input type="hidden" name="order" value="' . esc_attr($_REQUEST['order']) . '" />'; |
| 271 |
} |
| 272 |
if (!empty($_REQUEST['page'])) { |
| 273 |
echo '<input type="hidden" name="page" value="' . esc_attr($_REQUEST['page']) . '" />'; |
| 274 |
} |
| 275 |
if (!empty($_REQUEST['cpt'])) { |
| 276 |
echo '<input type="hidden" name="cpt" value="' . esc_attr($_REQUEST['cpt']) . '" />'; |
| 277 |
} |
| 278 |
if (!empty($_REQUEST['taxo'])) { |
| 279 |
echo '<input type="hidden" name="taxo" value="' . esc_attr($_REQUEST['taxo']) . '" />'; |
| 280 |
} |
| 281 |
$searchbox_search = (empty($_REQUEST['s']) && !$this->has_items()) ? 'visibility:hidden;' : ''; |
| 282 |
?> |
| 283 |
<p class="search-box" style="<?php echo $searchbox_search; ?>"> |
| 284 |
<label class="screen-reader-text" for="<?php echo esc_attr($input_id); ?>"><?php echo $text; ?>:</label> |
| 285 |
<input type="search" id="<?php echo esc_attr($input_id); ?>" name="s" |
| 286 |
value="<?php _admin_search_query(); ?>"/> |
| 287 |
<?php submit_button($text, '', '', false, ['id' => 'search-submit']); ?> |
| 288 |
</p> |
| 289 |
<?php |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
/** |
| 294 |
* Sets up the items (roles) to list. |
| 295 |
*/ |
| 296 |
public function prepare_items() |
| 297 |
{ |
| 298 |
|
| 299 |
$this->_column_headers = $this->get_column_info(); |
| 300 |
|
| 301 |
/** |
| 302 |
* First, lets decide how many records per page to show |
| 303 |
*/ |
| 304 |
$per_page = $this->get_items_per_page( 'termcloud_per_page', 10 ); |
| 305 |
|
| 306 |
/** |
| 307 |
* handle bulk actions. |
| 308 |
*/ |
| 309 |
$this->process_bulk_action(); |
| 310 |
|
| 311 |
/** |
| 312 |
* Fetch the data |
| 313 |
*/ |
| 314 |
$data = self::get_termcloud(); |
| 315 |
|
| 316 |
/** |
| 317 |
* Handle search |
| 318 |
*/ |
| 319 |
if ((!empty($_REQUEST['s'])) && $search = $_REQUEST['s']) { |
| 320 |
$data_filtered = []; |
| 321 |
foreach ($data as $item) { |
| 322 |
if ($this->str_contains($item->slug, $search, false) || $this->str_contains($item->name, $search, false)) { |
| 323 |
$data_filtered[] = $item; |
| 324 |
} |
| 325 |
} |
| 326 |
$data = $data_filtered; |
| 327 |
} |
| 328 |
|
| 329 |
/** |
| 330 |
* This checks for sorting input and sorts the data in our array accordingly. |
| 331 |
*/ |
| 332 |
function usort_reorder($a, $b) |
| 333 |
{ |
| 334 |
$orderby = (!empty($_REQUEST['orderby'])) ? sanitize_text_field($_REQUEST['orderby']) : 'name'; //If no sort, default to role |
| 335 |
$order = (!empty($_REQUEST['order'])) ? sanitize_text_field($_REQUEST['order']) : 'asc'; //If no order, default to asc |
| 336 |
$result = strnatcasecmp($a->$orderby, $b->$orderby); //Determine sort order, case insensitive, natural order |
| 337 |
|
| 338 |
return ($order === 'asc') ? $result : -$result; //Send final sort direction to usort |
| 339 |
} |
| 340 |
|
| 341 |
usort($data, 'usort_reorder'); |
| 342 |
|
| 343 |
/** |
| 344 |
* Pagination. |
| 345 |
*/ |
| 346 |
$current_page = $this->get_pagenum(); |
| 347 |
$total_items = count($data); |
| 348 |
|
| 349 |
|
| 350 |
/** |
| 351 |
* The WP_List_Table class does not handle pagination for us, so we need |
| 352 |
* to ensure that the data is trimmed to only the current page. We can use |
| 353 |
* array_slice() to |
| 354 |
*/ |
| 355 |
$data = array_slice($data, (($current_page - 1) * $per_page), $per_page); |
| 356 |
|
| 357 |
/** |
| 358 |
* Now we can add the data to the items property, where it can be used by the rest of the class. |
| 359 |
*/ |
| 360 |
$this->items = $data; |
| 361 |
|
| 362 |
/** |
| 363 |
* We also have to register our pagination options & calculations. |
| 364 |
*/ |
| 365 |
$this->set_pagination_args([ |
| 366 |
'total_items' => $total_items, //calculate the total number of items |
| 367 |
'per_page' => $per_page, //determine how many items to show on a page |
| 368 |
'total_pages' => ceil($total_items / $per_page) //calculate the total number of pages |
| 369 |
]); |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
} |
| 374 |
|