Advertisement
geminilabs

[site-reviews] Add ListTable filters for categories and assigned posts

May 4th, 2021
1,217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.18 KB | None | 0 0
  1. /**
  2.  * @return bool
  3.  */
  4. function glsr_check_listtable_filter_permissions(\WP_Query $query) {
  5.     global $pagenow;
  6.     return glsr()->isAdmin()
  7.         && $query->is_main_query()
  8.         && $query->get('post_type') === glsr()->post_type
  9.         && 'edit.php' === $pagenow;
  10. }
  11.  
  12. /**
  13.  * @param array $args
  14.  * @return string|void
  15.  */
  16. function glsr_get_assigned_listtable_filter($args = []) {
  17.     $args = shortcode_atts([
  18.         'id' => '',
  19.         'label' => '',
  20.         'options' => [],
  21.         'placeholder' => '',
  22.     ], $args);
  23.     if (4 === count(array_filter($args))) {
  24.         $label = glsr('Modules\Html\Builder')->label([
  25.             'class' => 'screen-reader-text',
  26.             'for' => $args['id'],
  27.             'text' => $args['label'],
  28.         ]);
  29.         $filter = glsr('Modules\Html\Builder')->select([
  30.             'name' => $args['id'],
  31.             'options' => $args['options'],
  32.             'placeholder' => $args['placeholder'],
  33.             'style' => 'max-width:150px;',
  34.             'value' => filter_input(INPUT_GET, $args['id'], FILTER_SANITIZE_NUMBER_INT),
  35.         ]);
  36.         return $label.$filter;
  37.     }
  38. }
  39.  
  40. /**
  41.  * @return string|void
  42.  */
  43. function glsr_get_assigned_posts_listtable_filter() {
  44.     global $wpdb;
  45.     $table = glsr('Database\Query')->table('assigned_posts');
  46.     $postIds = $wpdb->get_col("SELECT DISTINCT post_id FROM {$table}");
  47.     if (!empty($postIds)) {
  48.         $posts = get_posts([
  49.             'order' => 'ASC',
  50.             'orderby' => 'post_title',
  51.             'post_type' => 'any',
  52.             'posts_per_page' => -1,
  53.             'post__in' => $postIds,
  54.         ]);
  55.         $options = wp_list_pluck($posts, 'post_title', 'ID');
  56.         return glsr_get_assigned_listtable_filter([
  57.             'id' => 'assigned_post_id',
  58.             'label' => _x('Filter by assigned post', 'admin-text', 'site-reviews'),
  59.             'options' => $options,
  60.             'placeholder' => _x('All assigned posts', 'admin-text', 'site-reviews'),
  61.         ]);
  62.     }
  63. }
  64.  
  65. /**
  66.  * @return string|void
  67.  */
  68. function glsr_get_assigned_terms_listtable_filter() {
  69.     $options = get_terms([
  70.         'count' => false,
  71.         'fields' => 'id=>name',
  72.         'hide_empty' => false,
  73.         'taxonomy' => glsr()->taxonomy,
  74.     ]);
  75.     if (!empty($options)) {
  76.         return glsr_get_assigned_listtable_filter([
  77.             'id' => 'assigned_term_id',
  78.             'label' => _x('Filter by category', 'admin-text', 'site-reviews'),
  79.             'options' => $options,
  80.             'placeholder' => _x('All categories', 'admin-text', 'site-reviews'),
  81.         ]);
  82.     }
  83. }
  84.  
  85. /**
  86.  * @return void
  87.  * @action restrict_manage_posts
  88.  */
  89. add_action('admin_init', function () {
  90.     if (!function_exists('glsr')) {
  91.         return;
  92.     }
  93.     add_filter('restrict_manage_posts', function ($postType) {
  94.         if ($postType === glsr()->post_type) {
  95.             echo glsr_get_assigned_terms_listtable_filter();
  96.             echo glsr_get_assigned_posts_listtable_filter();
  97.         }
  98.     });
  99.     add_action('pre_get_posts', function (\WP_Query $query) {
  100.         if (!glsr_check_listtable_filter_permissions($query)) {
  101.             return;
  102.         }
  103.         if ($termId = filter_input(INPUT_GET, 'assigned_term_id', FILTER_SANITIZE_NUMBER_INT)) {
  104.             $query->set('tax_query', [[
  105.                 'taxonomy' => glsr()->taxonomy,
  106.                 'terms' => $termId,
  107.             ]]);
  108.         }
  109.     });
  110.     add_filter('posts_clauses', function (array $clauses, \WP_Query $query) {
  111.         if ($postId = filter_input(INPUT_GET, 'assigned_post_id', FILTER_SANITIZE_NUMBER_INT)) {
  112.             global $wpdb;
  113.             $ratingTable = glsr('Database\Query')->table('ratings');
  114.             $assignedPostsTable = glsr('Database\Query')->table('assigned_posts');
  115.             $postIds = $wpdb->get_col("
  116.                SELECT DISTINCT r.review_id
  117.                FROM {$ratingTable} r
  118.                INNER JOIN {$assignedPostsTable} apt ON apt.rating_id = r.ID
  119.                WHERE apt.post_id = '{$postId}'
  120.            ");
  121.             $clauses['where'] .= sprintf(" AND {$wpdb->posts}.ID IN (%s) ", implode(',', $postIds));
  122.         }
  123.         return $clauses;
  124.     }, 10, 2);
  125. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement