Making WordPress.org

Changeset 908


Ignore:
Timestamp:
10/14/2014 10:54:11 PM (9 years ago)
Author:
iandunn
Message:

CampTix Admin Flags: Support bulk-toggling from the Attendees screen.

File:
1 edited

Legend:

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

    r907 r908  
    3838
    3939        if ( current_user_can( $camptix->caps['manage_attendees'] ) ) {
     40            // Individual editing from Edit Attendee screen
    4041            add_action( 'save_post', array( $this, 'save_post' ), 11, 1 );
    4142            add_action( 'camptix_attendee_submitdiv_misc', array( $this, 'publish_metabox_actions' ) );
     43
     44            // Bulk editing from Attendees screen
     45            add_filter( 'manage_tix_attendee_posts_columns', array( $this, 'add_custom_columns' ) );
     46            add_action( 'manage_tix_attendee_posts_custom_column', array( $this, 'render_custom_columns' ), 10, 2 );
     47            add_action( 'admin_footer-edit.php', array( $this, 'render_client_side_templates' ) );
     48            add_action( 'wp_ajax_tix_admin_flag_toggle', array( $this, 'toggle_flag' ) );
     49            add_action( 'admin_footer-edit.php', array( $this, 'print_javascript' ) );
     50            add_action( 'admin_print_styles-edit.php', array( $this, 'print_css' ) );
    4251        }
    4352
     
    237246
    238247    /**
     248     * Add extra columns to the Attendees screen.
     249     *
     250     * @param array $columns
     251     * @return array
     252     */
     253    public function add_custom_columns( $columns ) {
     254        $columns = array_merge( array( 'admin-flags' => __( 'Admin Flags', 'camptix' ) ), $columns );
     255
     256        return $columns;
     257    }
     258
     259    /**
     260     * Render custom columns on the Attendees screen.
     261     *
     262     * @param string $column
     263     * @param int $attendee_id
     264     */
     265    public function render_custom_columns( $column, $attendee_id ) {
     266        $attendee_flags = (array) get_post_meta( $attendee_id, 'camptix-admin-flag' );
     267
     268        switch ( $column ) {
     269            case 'admin-flags':
     270                echo '<ul>';
     271                    foreach ( $this->flags as $key => $label ) {
     272                        $enabled = in_array( $key, $attendee_flags );
     273
     274                        ?>
     275
     276                        <li>
     277                            <span class="tix-admin-flag-label"><?php echo esc_html( $label ); ?></span>:
     278
     279                            <a
     280                                href="#"
     281                                data-attendee-id="<?php echo esc_attr( $attendee_id ); ?>"
     282                                data-key="<?php echo esc_attr( $key ); ?>"
     283                                data-nonce="<?php echo esc_attr( wp_create_nonce( sprintf( 'tix_toggle_flag_%s_%s', $attendee_id, $key ) ) ); ?>"
     284                                data-command="<?php echo esc_attr( $enabled ? 'disable' : 'enable' ); ?>"
     285                                class="tix-toggle-flag">
     286
     287                                <?php if ( $enabled ) : ?>
     288                                    <?php _e( 'Disable', 'camptix' ); ?>
     289                                <?php else : ?>
     290                                    <?php _e( 'Enable', 'camptix' ); ?>
     291                                <?php endif; ?>
     292                            </a>
     293                        </li>
     294
     295                        <?php
     296                    }
     297                echo '</ul>';
     298
     299                break;
     300        }
     301    }
     302
     303    /**
     304     * Render the templates used by JavaScript
     305     */
     306    public function render_client_side_templates() {
     307        if ( 'tix_attendee' != $GLOBALS['typenow'] ) {
     308            return;
     309        }
     310
     311        ?>
     312
     313        <script type="text/html" id="tmpl-tix-admin-flags-spinner">
     314            <div class="spinner"></div>
     315        </script>
     316
     317        <script type="text/html" id="tmpl-tix-admin-flag-toggle">
     318            <span class="tix-admin-flag-label">{{data.label}}</span>:
     319
     320            <a
     321                href="#"
     322                data-attendee-id="{{data.attendee_id}}"
     323                data-key="{{data.key}}"
     324                data-nonce="{{data.nonce}}"
     325                data-command="{{data.command}}"
     326                class="tix-toggle-flag">
     327
     328                {{data.command}}    <?php // todo use i18n var ?>
     329            </a>
     330        </script>
     331
     332        <?php
     333    }
     334
     335    /**
     336     * AJAX handler to toggle a flag from the Attendees screen.
     337     */
     338    public function toggle_flag() {
     339        /** @var $camptix CampTix_Plugin */
     340        global $camptix;
     341
     342        if ( empty( $_REQUEST['action'] ) || empty( $_REQUEST['attendee_id'] ) || empty( $_REQUEST['key'] ) || empty( $_REQUEST['command'] ) || empty( $_REQUEST['nonce'] ) ) {
     343            wp_send_json_error( array( 'error' => 'Required parameters not set.' ) );
     344        }
     345
     346        $attendee_id = absint( $_REQUEST['attendee_id'] );
     347        $key         = sanitize_text_field( $_REQUEST['key'] );
     348        $command     = sanitize_text_field( $_REQUEST['command'] );
     349
     350        if ( ! wp_verify_nonce( $_REQUEST['nonce'], sprintf( 'tix_toggle_flag_%s_%s', $attendee_id, $key ) ) || ! current_user_can( $camptix->caps['manage_attendees'], $attendee_id ) ) {
     351            wp_send_json_error( array( 'error' => 'Permission denied.' ) );
     352        }
     353
     354        $attendee = get_post( $attendee_id );
     355
     356        if ( ! is_a( $attendee, 'WP_Post' ) || 'tix_attendee' != $attendee->post_type ) {
     357            wp_send_json_error( array( 'error' => 'Invalid attendee.' ) );
     358        }
     359
     360        if ( 'enable' == $command ) {
     361            add_post_meta( $attendee_id, 'camptix-admin-flag', $key );
     362        } elseif ( 'disable' == $command ) {
     363            delete_post_meta( $attendee_id, 'camptix-admin-flag', $key );
     364        }
     365
     366        wp_send_json_success();
     367    }
     368
     369    /**
     370     * Print our JavaScript
     371     */
     372    public function print_javascript() {
     373        if ( 'tix_attendee' != $GLOBALS['typenow'] ) {
     374            return;
     375        }
     376
     377        ?>
     378
     379        <!-- camptix-admin-flags -->
     380        <script type="text/javascript">
     381            // Bulk toggling of flags
     382            ( function( $ ) {
     383                $( '#posts-filter' ).on( 'click', 'a.tix-toggle-flag', function( event ) {
     384                    var itemTemplate,
     385                        item       = $( this ).parent(),
     386                        attendeeID = $( this ).data( 'attendee-id' ),
     387                        key        = $( this ).data( 'key' ),
     388                        command    = $( this ).data( 'command' ),
     389                        label      = $( item ).find( '.tix-admin-flag-label' ).text(),
     390                        nonce      = $( this ).data( 'nonce' );
     391
     392                    event.preventDefault();
     393
     394                    // Show a spinner until the AJAX call is done
     395                    itemTemplate = _.template( $( '#tmpl-tix-admin-flags-spinner' ).html(), null, camptix.template_options );
     396                    item.html( itemTemplate( {} ) );
     397
     398                    // Send the request to toggle the flag
     399                    $.post(
     400                        ajaxurl,
     401
     402                        {
     403                            action:      'tix_admin_flag_toggle',
     404                            attendee_id: attendeeID,
     405                            key:         key,
     406                            command:     command,
     407                            nonce:       nonce
     408                        },
     409
     410                        function( response ) {
     411                            if ( response.hasOwnProperty( 'success' ) && true === response.success ) {
     412                                if ( 'enable' == command ) {
     413                                    command = 'disable';
     414                                } else if ( 'disable' == command ) {
     415                                    command = 'enable';
     416                                }
     417                            }
     418
     419                            itemTemplate = _.template( $( '#tmpl-tix-admin-flag-toggle' ).html(), null, camptix.template_options );
     420                            item.html( itemTemplate( {
     421                                attendee_id: attendeeID,
     422                                key:         key,
     423                                command:     command,
     424                                label:       label,
     425                                nonce:       nonce
     426                            } ) );
     427                        }
     428                    );
     429                } );
     430            } ( jQuery ) );
     431        </script>
     432        <!-- end camptix-admin-flags -->
     433
     434        <?php
     435    }
     436
     437    /**
     438     * Print our CSS
     439     */
     440    public function print_css() {
     441        if ( 'tix_attendee' != $GLOBALS['typenow'] ) {
     442            return;
     443        }
     444
     445        ?>
     446
     447        <!-- camptix-admin-flags -->
     448        <style type="text/css">
     449            body.post-type-tix_attendee table.posts td.admin-flags div.spinner {
     450                display: block;
     451                float: none;
     452                margin-left: auto;
     453                margin-right: auto;
     454            }
     455        </style>
     456        <!-- end camptix-admin-flags -->
     457
     458        <?php
     459    }
     460
     461    /**
    239462     * Register self with CampTix.
    240463     */
Note: See TracChangeset for help on using the changeset viewer.