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