| 1 |
<?php |
| 2 |
|
| 3 |
if (!class_exists('WP_List_Table')) { |
| 4 |
require_once(ABSPATH . 'wp-admin/includes/class-wp-list-table.php'); |
| 5 |
} |
| 6 |
|
| 7 |
class RelatedPosts_List extends WP_List_Table |
| 8 |
{ |
| 9 |
/** Class constructor */ |
| 10 |
public function __construct() |
| 11 |
{ |
| 12 |
|
| 13 |
parent::__construct([ |
| 14 |
'singular' => esc_html__('Related Post', 'simple-tags'), //singular name of the listed records |
| 15 |
'plural' => esc_html__('Related Posts', 'simple-tags'), //plural name of the listed records |
| 16 |
'ajax' => false //does this table support ajax? |
| 17 |
]); |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Retrieve st_related_posts data from the database |
| 22 |
* |
| 23 |
* @param int $per_page |
| 24 |
* @param int $page_number |
| 25 |
* |
| 26 |
* @return mixed |
| 27 |
*/ |
| 28 |
public static function get_st_related_posts() |
| 29 |
{ |
| 30 |
return taxopress_get_relatedpost_data(); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Returns the count of records in the database. |
| 35 |
* |
| 36 |
* @return null|string |
| 37 |
*/ |
| 38 |
public static function record_count() |
| 39 |
{ |
| 40 |
return count(taxopress_get_relatedpost_data()); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Show single row item |
| 45 |
* |
| 46 |
* @param array $item |
| 47 |
*/ |
| 48 |
public function single_row($item) |
| 49 |
{ |
| 50 |
$class = ['st-relatedpost-tr']; |
| 51 |
$id = 'st-relatedpost-' . md5($item['ID']); |
| 52 |
echo sprintf('<tr id="%s" class="%s">', esc_attr($id), esc_attr(implode(' ', $class))); |
| 53 |
$this->single_row_columns($item); |
| 54 |
echo '</tr>'; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Associative array of columns |
| 59 |
* |
| 60 |
* @return array |
| 61 |
*/ |
| 62 |
public function get_columns() |
| 63 |
{ |
| 64 |
$columns = [ |
| 65 |
'title' => esc_html__('Title', 'simple-tags'), |
| 66 |
'taxonomy' => esc_html__('Taxonomy', 'simple-tags'), |
| 67 |
'post_type' => esc_html__('Post Types', 'simple-tags'), |
| 68 |
'embedded' => esc_html__('Automatic display', 'simple-tags'), |
| 69 |
'shortcode' => esc_html__('Shortcode', 'simple-tags') |
| 70 |
]; |
| 71 |
|
| 72 |
return $columns; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Render a column when no column specific method exist. |
| 77 |
* |
| 78 |
* @param array $item |
| 79 |
* @param string $column_name |
| 80 |
* |
| 81 |
* @return mixed |
| 82 |
*/ |
| 83 |
public function column_default($item, $column_name) |
| 84 |
{ |
| 85 |
return !empty($item[$column_name]) ? $item[$column_name] : '—'; |
| 86 |
} |
| 87 |
|
| 88 |
/** Text displayed when no stterm data is available */ |
| 89 |
public function no_items() |
| 90 |
{ |
| 91 |
_e('No item avaliable.', 'simple-tags'); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Displays the search box. |
| 96 |
* |
| 97 |
* @param string $text The 'submit' button label. |
| 98 |
* @param string $input_id ID attribute value for the search input field. |
| 99 |
* |
| 100 |
* |
| 101 |
*/ |
| 102 |
public function search_box($text, $input_id) |
| 103 |
{ |
| 104 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 105 |
if (empty($_REQUEST['s']) && !$this->has_items()) { |
| 106 |
return; |
| 107 |
} |
| 108 |
|
| 109 |
$input_id = $input_id . '-search-input'; |
| 110 |
|
| 111 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 112 |
if (!empty($_REQUEST['orderby'])) { |
| 113 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 114 |
echo '<input type="hidden" name="orderby" value="' . esc_attr(sanitize_text_field(wp_unslash($_REQUEST['orderby']))) . '" />'; |
| 115 |
} |
| 116 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 117 |
if (!empty($_REQUEST['order'])) { |
| 118 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 119 |
echo '<input type="hidden" name="order" value="' . esc_attr(sanitize_text_field(wp_unslash($_REQUEST['order']))) . '" />'; |
| 120 |
} |
| 121 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 122 |
if (!empty($_REQUEST['page'])) { |
| 123 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 124 |
echo '<input type="hidden" name="page" value="' . esc_attr(sanitize_text_field(wp_unslash($_REQUEST['page']))) . '" />'; |
| 125 |
} |
| 126 |
?> |
| 127 |
<p class="search-box"> |
| 128 |
<label class="screen-reader-text" for="<?php echo esc_attr($input_id); ?>"><?php echo esc_html($text); ?>:</label> |
| 129 |
<input type="search" id="<?php echo esc_attr($input_id); ?>" name="s" |
| 130 |
value="<?php _admin_search_query(); ?>"/> |
| 131 |
<?php submit_button($text, '', '', false, ['id' => 'search-submit']); ?> |
| 132 |
</p> |
| 133 |
<?php |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Sets up the items (roles) to list. |
| 138 |
*/ |
| 139 |
public function prepare_items() |
| 140 |
{ |
| 141 |
|
| 142 |
$this->_column_headers = $this->get_column_info(); |
| 143 |
|
| 144 |
/** |
| 145 |
* First, lets decide how many records per page to show |
| 146 |
*/ |
| 147 |
$per_page = $this->get_items_per_page('st_related_posts_per_page', 20); |
| 148 |
|
| 149 |
/** |
| 150 |
* Fetch the data |
| 151 |
*/ |
| 152 |
$data = self::get_st_related_posts(); |
| 153 |
|
| 154 |
/** |
| 155 |
* Handle search |
| 156 |
*/ |
| 157 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 158 |
if ((!empty($_REQUEST['s'])) && $search = sanitize_text_field(wp_unslash($_REQUEST['s']))) { |
| 159 |
$data_filtered = []; |
| 160 |
foreach ($data as $item) { |
| 161 |
if ($this->str_contains($item['title'], $search, false)) { |
| 162 |
$data_filtered[] = $item; |
| 163 |
} |
| 164 |
} |
| 165 |
$data = $data_filtered; |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* This checks for sorting input and sorts the data in our array accordingly. |
| 170 |
*/ |
| 171 |
function usort_reorder($a, $b) |
| 172 |
{ |
| 173 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 174 |
$orderby = (!empty($_REQUEST['orderby'])) ? sanitize_text_field(wp_unslash($_REQUEST['orderby'])) : 'ID'; //If no sort, default to role |
| 175 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 176 |
$order = (!empty($_REQUEST['order'])) ? sanitize_text_field(wp_unslash($_REQUEST['order'])) : 'desc'; //If no order, default to asc |
| 177 |
$result = strnatcasecmp( |
| 178 |
$a[$orderby], |
| 179 |
$b[$orderby] |
| 180 |
); //Determine sort order, case insensitive, natural order |
| 181 |
|
| 182 |
return ($order === 'asc') ? $result : -$result; //Send final sort direction to usort |
| 183 |
} |
| 184 |
|
| 185 |
usort($data, 'usort_reorder'); |
| 186 |
|
| 187 |
/** |
| 188 |
* Pagination. |
| 189 |
*/ |
| 190 |
$current_page = $this->get_pagenum(); |
| 191 |
$total_items = count($data); |
| 192 |
|
| 193 |
|
| 194 |
/** |
| 195 |
* The WP_List_Table class does not handle pagination for us, so we need |
| 196 |
* to ensure that the data is trimmed to only the current page. We can use |
| 197 |
* array_slice() to |
| 198 |
*/ |
| 199 |
$data = array_slice($data, (($current_page - 1) * $per_page), $per_page); |
| 200 |
|
| 201 |
/** |
| 202 |
* Now we can add the data to the items property, where it can be used by the rest of the class. |
| 203 |
*/ |
| 204 |
$this->items = $data; |
| 205 |
|
| 206 |
/** |
| 207 |
* We also have to register our pagination options & calculations. |
| 208 |
*/ |
| 209 |
$this->set_pagination_args([ |
| 210 |
'total_items' => $total_items, //calculate the total number of items |
| 211 |
'per_page' => $per_page, //determine how many items to show on a page |
| 212 |
'total_pages' => ceil($total_items / $per_page) //calculate the total number of pages |
| 213 |
]); |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* Determine if a given string contains a given substring. |
| 218 |
* |
| 219 |
* @param string $haystack |
| 220 |
* @param string|array $needles |
| 221 |
* @param bool $sensitive Use case sensitive search |
| 222 |
* |
| 223 |
* @return bool |
| 224 |
*/ |
| 225 |
public function str_contains($haystack, $needles, $sensitive = true) |
| 226 |
{ |
| 227 |
foreach ((array)$needles as $needle) { |
| 228 |
$function = $sensitive ? 'mb_strpos' : 'mb_stripos'; |
| 229 |
if ($needle !== '' && $function($haystack, $needle) !== false) { |
| 230 |
return true; |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
return false; |
| 235 |
} |
| 236 |
|
| 237 |
/** |
| 238 |
* Columns to make sortable. |
| 239 |
* |
| 240 |
* @return array |
| 241 |
*/ |
| 242 |
protected function get_sortable_columns() |
| 243 |
{ |
| 244 |
$sortable_columns = [ |
| 245 |
'title' => ['title', true], |
| 246 |
'taxonomy' => ['taxonomy', true], |
| 247 |
]; |
| 248 |
|
| 249 |
return $sortable_columns; |
| 250 |
} |
| 251 |
|
| 252 |
/** |
| 253 |
* Generates and display row actions links for the list table. |
| 254 |
* |
| 255 |
* @param object $item The item being acted upon. |
| 256 |
* @param string $column_name Current column name. |
| 257 |
* @param string $primary Primary column name. |
| 258 |
* |
| 259 |
* @return string The row actions HTML, or an empty string if the current column is the primary column. |
| 260 |
*/ |
| 261 |
protected function handle_row_actions($item, $column_name, $primary) |
| 262 |
{ |
| 263 |
//Build row actions |
| 264 |
$actions = [ |
| 265 |
'edit' => sprintf( |
| 266 |
'<a href="%s">%s</a>', |
| 267 |
add_query_arg( |
| 268 |
[ |
| 269 |
'page' => 'st_related_posts', |
| 270 |
'add' => 'new_item', |
| 271 |
'action' => 'edit', |
| 272 |
'taxopress_relatedposts' => $item['ID'], |
| 273 |
], |
| 274 |
admin_url('admin.php') |
| 275 |
), |
| 276 |
esc_html__('Edit', 'simple-tags') |
| 277 |
), |
| 278 |
'delete' => sprintf( |
| 279 |
'<a href="%s" class="delete-relatedpost">%s</a>', |
| 280 |
add_query_arg( |
| 281 |
[ |
| 282 |
'page' => 'st_related_posts', |
| 283 |
'action' => 'taxopress-delete-relatedpost', |
| 284 |
'taxopress_relatedposts' => esc_attr($item['ID']), |
| 285 |
'_wpnonce' => wp_create_nonce('relatedpost-action-request-nonce') |
| 286 |
], |
| 287 |
admin_url('admin.php') |
| 288 |
), |
| 289 |
esc_html__('Delete', 'simple-tags') |
| 290 |
), |
| 291 |
]; |
| 292 |
|
| 293 |
$actions = apply_filters('taxopress_relatedpost_row_actions', $actions, $item); |
| 294 |
return $column_name === $primary ? $this->row_actions($actions, false) : ''; |
| 295 |
} |
| 296 |
|
| 297 |
/** |
| 298 |
* Method for title column |
| 299 |
* |
| 300 |
* @param array $item |
| 301 |
* |
| 302 |
* @return string |
| 303 |
*/ |
| 304 |
protected function column_title($item) |
| 305 |
{ |
| 306 |
$title = sprintf( |
| 307 |
'<a href="%1$s"><strong><span class="row-title">%2$s</span></strong></a>', |
| 308 |
add_query_arg( |
| 309 |
[ |
| 310 |
'page' => 'st_related_posts', |
| 311 |
'add' => 'new_item', |
| 312 |
'action' => 'edit', |
| 313 |
'taxopress_relatedposts' => $item['ID'], |
| 314 |
], |
| 315 |
admin_url('admin.php') |
| 316 |
), |
| 317 |
esc_html($item['title']) |
| 318 |
); |
| 319 |
|
| 320 |
return $title; |
| 321 |
} |
| 322 |
|
| 323 |
/** |
| 324 |
* Method for taxonomy column |
| 325 |
* |
| 326 |
* @param array $item |
| 327 |
* |
| 328 |
* @return string |
| 329 |
*/ |
| 330 |
protected function column_taxonomy($item) |
| 331 |
{ |
| 332 |
$taxonomy = get_taxonomy($item['taxonomy']); |
| 333 |
|
| 334 |
return $taxonomy->labels->name; |
| 335 |
} |
| 336 |
|
| 337 |
/** |
| 338 |
* Method for post_type column |
| 339 |
* |
| 340 |
* @param array $item |
| 341 |
* |
| 342 |
* @return string |
| 343 |
*/ |
| 344 |
protected function column_post_type($item) |
| 345 |
{ |
| 346 |
$title = ''; |
| 347 |
if (isset($item['post_type']) && !empty(trim($item['post_type']))) { |
| 348 |
if ($item['post_type'] === 'st_current_posttype') { |
| 349 |
$title = esc_html__('Current post type', 'simple-tags'); |
| 350 |
} elseif ($item['post_type'] === 'st_all_posttype') { |
| 351 |
$title = esc_html__('All', 'simple-tags'); |
| 352 |
} else { |
| 353 |
$post_type = get_post_type_object($item['post_type']); |
| 354 |
if (is_object($post_type)) { |
| 355 |
$title = sprintf( |
| 356 |
'<a href="%1$s"><strong><span class="row-title">%2$s</span></strong></a>', |
| 357 |
add_query_arg( |
| 358 |
[ |
| 359 |
'post_type' => $item['post_type'], |
| 360 |
], |
| 361 |
admin_url('edit.php') |
| 362 |
), |
| 363 |
esc_html($post_type->label) |
| 364 |
); |
| 365 |
} |
| 366 |
} |
| 367 |
} elseif (isset($item['post_types']) && !empty($item['post_types'])) { |
| 368 |
$result_array = []; |
| 369 |
foreach ($item['post_types'] as $post_type_name) { |
| 370 |
$post_type = get_post_type_object($post_type_name); |
| 371 |
if (is_object($post_type)) { |
| 372 |
$result_array[] = sprintf( |
| 373 |
'<a href="%1$s"><strong><span class="row-title">%2$s</span></strong></a>', |
| 374 |
add_query_arg( |
| 375 |
[ |
| 376 |
'post_type' => $post_type_name, |
| 377 |
], |
| 378 |
admin_url('edit.php') |
| 379 |
), |
| 380 |
esc_html($post_type->label) |
| 381 |
); |
| 382 |
} |
| 383 |
} |
| 384 |
$title = join(', ', $result_array); |
| 385 |
} else { |
| 386 |
$title = esc_html__('Default', 'simple-tags'); |
| 387 |
} |
| 388 |
|
| 389 |
return $title; |
| 390 |
} |
| 391 |
|
| 392 |
/** |
| 393 |
* The action column |
| 394 |
* |
| 395 |
* @param $item |
| 396 |
* |
| 397 |
* @return string |
| 398 |
*/ |
| 399 |
protected function column_embedded($item) |
| 400 |
{ |
| 401 |
$embedded = (isset($item['embedded']) && is_array($item['embedded']) && count($item['embedded']) > 0) ? $item['embedded'] : false; |
| 402 |
if ($embedded) { |
| 403 |
$args = apply_filters('taxopress_attach_post_types_to_taxonomy', ['public' => true]); |
| 404 |
if (!is_array($args)) { |
| 405 |
$args = ['public' => true]; |
| 406 |
} |
| 407 |
$output = 'objects'; // Or objects. |
| 408 |
$post_types = apply_filters( |
| 409 |
'taxopress_get_post_types_for_taxonomies', |
| 410 |
get_post_types($args, $output), |
| 411 |
$args, |
| 412 |
$output |
| 413 |
); |
| 414 |
|
| 415 |
$result_array = []; |
| 416 |
$embedded_options = [ |
| 417 |
'homeonly' => esc_html__('Homepage', 'simple-tags'), |
| 418 |
'blogonly' => esc_html__('Blog display', 'simple-tags'), |
| 419 |
'singleonly' => esc_html__('Single post display', 'simple-tags'), |
| 420 |
'feed' => esc_html__('RSS feed', 'simple-tags'), |
| 421 |
]; |
| 422 |
foreach ($post_types as $post_type) { |
| 423 |
$embedded_options[$post_type->name] = $post_type->label; |
| 424 |
} |
| 425 |
foreach ($embedded as $location) { |
| 426 |
$result_array[] = isset($embedded_options[$location]) ? $embedded_options[$location] : ''; |
| 427 |
} |
| 428 |
$result = join(', ', $result_array); |
| 429 |
} else { |
| 430 |
$result = esc_html__('No', 'simple-tags'); |
| 431 |
} |
| 432 |
|
| 433 |
return $result; |
| 434 |
} |
| 435 |
|
| 436 |
/** |
| 437 |
* The action column |
| 438 |
* |
| 439 |
* @param $item |
| 440 |
* |
| 441 |
* @return string |
| 442 |
*/ |
| 443 |
protected function column_shortcode($item) |
| 444 |
{ |
| 445 |
|
| 446 |
return '<input readonly type="text" value=\'[taxopress_relatedposts id="' . $item['ID'] . '"]\' />'; |
| 447 |
} |
| 448 |
} |
| 449 |
|