Making WordPress.org

Changeset 8308


Ignore:
Timestamp:
02/21/2019 06:15:09 PM (8 years ago)
Author:
iandunn
Message:

CampTix Admin Flags: Allow filtering Atendees screen by admin flag.

Fixes #2893
Props @dd32

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/camptix-admin-flags/addons/admin-flags.php

    r908 r8308  
    4545                        add_filter( 'manage_tix_attendee_posts_columns', array( $this, 'add_custom_columns' ) );
    4646                        add_action( 'manage_tix_attendee_posts_custom_column', array( $this, 'render_custom_columns' ), 10, 2 );
     47                        add_filter( 'views_edit-tix_attendee', array( $this, 'add_custom_filters' ) );
     48                        add_filter( 'pre_get_posts', array( $this, 'add_custom_filters_post_filter' ) );
    4749                        add_action( 'admin_footer-edit.php', array( $this, 'render_client_side_templates' ) );
    4850                        add_action( 'wp_ajax_tix_admin_flag_toggle', array( $this, 'toggle_flag' ) );
     
    299301                                break;
    300302                }
     303        }
     304
     305        /**
     306         * Add custom views to the attendee listing post listing.
     307         *
     308         * @param array $views
     309         *
     310         * @return array
     311         */
     312        public function add_custom_filters( $views ) {
     313                global $wpdb;
     314
     315                if ( empty( $GLOBALS['typenow'] ) || 'tix_attendee' !== $GLOBALS['typenow'] || empty( $this->flags ) ) {
     316                        return $views;
     317                }
     318
     319                $meta_counts = $wpdb->get_results( "
     320                        SELECT meta_value, COUNT( post_id ) AS count
     321                        FROM {$wpdb->postmeta}
     322                        WHERE meta_key = 'camptix-admin-flag'
     323                        GROUP BY meta_value",
     324                        ARRAY_A
     325                );
     326                $meta_counts = wp_list_pluck( $meta_counts, 'count', 'meta_value' );
     327
     328                if ( ! $meta_counts ) {
     329                        return $views;
     330                }
     331
     332                $currently_viewed_flag = empty( $_GET['camptix_flag'] ) ? false : wp_unslash( $_GET['camptix_flag'] );
     333                $base_url              = add_query_arg( 'post_type', 'tix_attendee', 'edit.php' );
     334
     335                foreach ( $this->flags as $flag => $label ) {
     336                        $count = 0;
     337                        $class_html = '';
     338                        $url        = add_query_arg( 'camptix_flag', $flag, $base_url );
     339
     340                        if ( $currently_viewed_flag && $currently_viewed_flag == $flag ) {
     341                                $class_html = ' class="current"';
     342                        }
     343
     344                        if ( isset( $meta_counts[ $flag ] ) ) {
     345                                $count = $meta_counts[ $flag ];
     346                        }
     347
     348                        $views[ $flag ] = sprintf(
     349                                '<a href="%s" %s>
     350                                        %s <span class="count">(%s)</span>
     351                                </a>',
     352                                esc_url( $url ),
     353                                $class_html,
     354                                $label,
     355                                number_format_i18n( $count )
     356                        );
     357                }
     358
     359                return $views;
     360        }
     361
     362        /**
     363         * Filter the wp-admin post listing query to filter to admin flags.
     364         *
     365         * @param WP_Query $query
     366         */
     367        public function add_custom_filters_post_filter( $query ) {
     368                $flag = empty( $_GET['camptix_flag'] ) ? false : wp_unslash( $_GET['camptix_flag'] );
     369
     370                if ( ! is_admin() || empty( $GLOBALS['typenow'] ) || 'tix_attendee' !== $GLOBALS['typenow'] ) {
     371                        return;
     372                }
     373
     374                if ( ! isset( $this->flags[ $flag ] ) ) {
     375                        return;
     376                }
     377
     378                if ( ! isset( $query->query_vars['meta_query'] ) ) {
     379                        $query->query_vars['meta_query'] = array();
     380                }
     381
     382                $query->query_vars['meta_query'][] = array(
     383                        'key'     => 'camptix-admin-flag',
     384                        'value'   => $flag,
     385                        'compare' => '=',
     386                        'type'    => 'CHAR',
     387                );
    301388        }
    302389
Note: See TracChangeset for help on using the changeset viewer.