Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @return bool
- */
- function glsr_check_listtable_filter_permissions(\WP_Query $query) {
- global $pagenow;
- return glsr()->isAdmin()
- && $query->is_main_query()
- && $query->get('post_type') === glsr()->post_type
- && 'edit.php' === $pagenow;
- }
- /**
- * @param array $args
- * @return string|void
- */
- function glsr_get_assigned_listtable_filter($args = []) {
- $args = shortcode_atts([
- 'id' => '',
- 'label' => '',
- 'options' => [],
- 'placeholder' => '',
- ], $args);
- if (4 === count(array_filter($args))) {
- $label = glsr('Modules\Html\Builder')->label([
- 'class' => 'screen-reader-text',
- 'for' => $args['id'],
- 'text' => $args['label'],
- ]);
- $filter = glsr('Modules\Html\Builder')->select([
- 'name' => $args['id'],
- 'options' => $args['options'],
- 'placeholder' => $args['placeholder'],
- 'style' => 'max-width:150px;',
- 'value' => filter_input(INPUT_GET, $args['id'], FILTER_SANITIZE_NUMBER_INT),
- ]);
- return $label.$filter;
- }
- }
- /**
- * @return string|void
- */
- function glsr_get_assigned_posts_listtable_filter() {
- global $wpdb;
- $table = glsr('Database\Query')->table('assigned_posts');
- $postIds = $wpdb->get_col("SELECT DISTINCT post_id FROM {$table}");
- if (!empty($postIds)) {
- $posts = get_posts([
- 'order' => 'ASC',
- 'orderby' => 'post_title',
- 'post_type' => 'any',
- 'posts_per_page' => -1,
- 'post__in' => $postIds,
- ]);
- $options = wp_list_pluck($posts, 'post_title', 'ID');
- return glsr_get_assigned_listtable_filter([
- 'id' => 'assigned_post_id',
- 'label' => _x('Filter by assigned post', 'admin-text', 'site-reviews'),
- 'options' => $options,
- 'placeholder' => _x('All assigned posts', 'admin-text', 'site-reviews'),
- ]);
- }
- }
- /**
- * @return string|void
- */
- function glsr_get_assigned_terms_listtable_filter() {
- $options = get_terms([
- 'count' => false,
- 'fields' => 'id=>name',
- 'hide_empty' => false,
- 'taxonomy' => glsr()->taxonomy,
- ]);
- if (!empty($options)) {
- return glsr_get_assigned_listtable_filter([
- 'id' => 'assigned_term_id',
- 'label' => _x('Filter by category', 'admin-text', 'site-reviews'),
- 'options' => $options,
- 'placeholder' => _x('All categories', 'admin-text', 'site-reviews'),
- ]);
- }
- }
- /**
- * @return void
- * @action restrict_manage_posts
- */
- add_action('admin_init', function () {
- if (!function_exists('glsr')) {
- return;
- }
- add_filter('restrict_manage_posts', function ($postType) {
- if ($postType === glsr()->post_type) {
- echo glsr_get_assigned_terms_listtable_filter();
- echo glsr_get_assigned_posts_listtable_filter();
- }
- });
- add_action('pre_get_posts', function (\WP_Query $query) {
- if (!glsr_check_listtable_filter_permissions($query)) {
- return;
- }
- if ($termId = filter_input(INPUT_GET, 'assigned_term_id', FILTER_SANITIZE_NUMBER_INT)) {
- $query->set('tax_query', [[
- 'taxonomy' => glsr()->taxonomy,
- 'terms' => $termId,
- ]]);
- }
- });
- add_filter('posts_clauses', function (array $clauses, \WP_Query $query) {
- if ($postId = filter_input(INPUT_GET, 'assigned_post_id', FILTER_SANITIZE_NUMBER_INT)) {
- global $wpdb;
- $ratingTable = glsr('Database\Query')->table('ratings');
- $assignedPostsTable = glsr('Database\Query')->table('assigned_posts');
- $postIds = $wpdb->get_col("
- SELECT DISTINCT r.review_id
- FROM {$ratingTable} r
- INNER JOIN {$assignedPostsTable} apt ON apt.rating_id = r.ID
- WHERE apt.post_id = '{$postId}'
- ");
- $clauses['where'] .= sprintf(" AND {$wpdb->posts}.ID IN (%s) ", implode(',', $postIds));
- }
- return $clauses;
- }, 10, 2);
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement