Plugin Directory

Changeset 1579713


Ignore:
Timestamp:
01/22/2017 05:50:58 PM (9 years ago)
Author:
thinkatat
Message:

Handle condition when users are huge (Added select2 ajax).

Location:
at-multiauthor/trunk
Files:
3 added
4 edited

Legend:

Unmodified
Added
Removed
  • at-multiauthor/trunk/assets/js/backend.min.js

    r1572745 r1579713  
    1 jQuery('#atmat-mb-multi-author').ready(function(){
    2     jQuery('#atmat-authors').select2({
    3         minimumInputLength : 1,
    4         placeholder : atmatStrings.placeholder
     1jQuery('#atmat-mb-multi-author').ready(function( $ ){
     2   
     3    // Ajax Contributors box search.
     4    $( ':input.atmat-select2' ).each( function() {
     5        var select2_args = {
     6            allowClear: $( this ).data( 'allow_clear' ) ? true : false,
     7            placeholder: $( this ).data( 'placeholder' ),
     8            minimumInputLength: $( this ).data( 'minimum_input_length' ) ? $( this ).data( 'minimum_input_length' ) : '3',
     9            escapeMarkup: function( m ) {
     10                return m;
     11            },
     12            ajax: {
     13                url: ajaxurl,
     14                dataType: 'json',
     15                quietMillis: 250,
     16                data: function( term ) {
     17                    return {
     18                        term: term,
     19                        action: $( this ).data( 'action' ),
     20                        security: $('#atmat-nonce').val(),
     21                        exclude: $( this ).data( 'exclude' ),
     22                        include: $( this ).data( 'include' ),
     23                        limit: $( this ).data( 'limit' )
     24                    };
     25                },
     26                results: function( data ) {
     27                    var terms = [];
     28                    if ( data ) {
     29                        $.each( data, function( id, text ) {
     30                            terms.push( { id: id, text: text } );
     31                        });
     32                    }
     33                    return {
     34                        results: terms
     35                    };
     36                },
     37                cache: true
     38            }
     39        };
     40
     41        if ( $( this ).data( 'multiple' ) === true ) {
     42            select2_args.multiple = true;
     43            select2_args.initSelection = function( element, callback ) {
     44                var data = $.parseJSON( element.attr( 'data-selected' ) );
     45                var selected = [];
     46                $( element.val().split( ',' ) ).each( function( i, val ) {
     47                    selected.push({
     48                        id: val,
     49                        text: data[ val ]
     50                    });
     51                });
     52                return callback( selected );
     53            };
     54            select2_args.formatSelection = function( data ) {
     55                return '<div class="selected-option" data-id="' + data.id + '">' + data.text + '</div>';
     56            };
     57        } else {
     58            select2_args.multiple = false;
     59            select2_args.initSelection = function( element, callback ) {
     60                var data = {
     61                    id: element.val(),
     62                    text: element.attr( 'data-selected' )
     63                };
     64                return callback( data );
     65            };
     66        }
     67
     68        $( this ).select2( select2_args );
    569    });
     70   
    671});
  • at-multiauthor/trunk/at-multiauthor.php

    r1572745 r1579713  
    44 * Plugin URI: http://thinkatat.com/
    55 * Description: One post, multiple contributors!
    6  * Version: 1.0.3
     6 * Version: 1.0.4
    77 * Author: thinkatat
    88 * Author URI: http://thinkatat.com/
     
    1818
    1919// Plugin setup - Basic constants.
    20 define( 'ATMAT_VERSION', '1.0.3' );
     20define( 'ATMAT_VERSION', '1.0.4' );
    2121define( 'ATMAT_DIR', untrailingslashit( plugin_dir_path( __FILE__ ) ) );
    2222define( 'ATMAT_URL', untrailingslashit( plugins_url( basename( plugin_dir_path( __FILE__ ) ), basename( __FILE__ ) ) ) );
  • at-multiauthor/trunk/includes/class-backend.php

    r1572745 r1579713  
    2626    public function __construct() {
    2727        add_action( 'add_meta_boxes', array( $this, 'add_metaboxes' ), 10, 2 );
     28        add_action( 'wp_ajax_get_contributors_list', array( $this, 'get_contributors_list' ) );
    2829        add_action( 'save_post_post', array( $this, 'save_metabox_multiauthor' ) );
    2930    }
     
    6061        wp_enqueue_script( 'atmat-select2-js' );
    6162        wp_enqueue_script( 'atmat-backend-js' );
    62         wp_localize_script(
    63             'atmat-backend-js',
    64             'atmatStrings',
    65             array(
    66                 'placeholder' => __( 'Select Contributor(s)', 'at-multiauthor' ),
    67             )
    68         );
    69         $authors = get_post_meta( $post->ID, 'atmat_authors', true );
    70         $users = get_users(
    71             array(
    72                 'orderby'      => 'login',
    73                 'order'        => 'ASC',
    74                 'role__in' => get_contributors_role_in( $post->ID ),
    75             )
    76         );
    77         do_action( 'atmat_metabox_multiauthor_before', $authors, $post );
     63        wp_localize_script( 'atmat-backend-js', 'atmatBackend', array() );
     64
     65        $author_ids = array_filter( array_map( 'absint', (array) get_post_meta( $post->ID, 'atmat_authors', true ) ) );
     66
     67        $json_ids    = array();
     68        foreach ( $author_ids as $author_id ) {
     69            $author = get_user_by( 'id', $author_id );
     70            if ( is_object( $author ) ) {
     71                $json_ids[ $author_id ] = esc_html( $author->display_name ) . ' (#' . absint( $author->ID ) . ' &ndash; ' . esc_html( $author->user_email ) . ')';
     72            }
     73        }
     74
     75        do_action( 'atmat_metabox_multiauthor_before', $author_ids, $post );
    7876        ?>
    79         <select id="atmat-authors" name="atmat-authors[]" multiple="multiple" style="width: 100%" <?php echo $disabled; ?> >
    80             <?php
    81             foreach ( $users as $user ) {
    82                 $selected = in_array( $user->ID, (array) $authors ) ? 'selected' : null; ?>
    83                 <option value="<?php echo esc_attr( $user->ID ); ?>" <?php echo esc_html( $selected ); ?> ><?php echo esc_html( $user->user_login ); ?></option>
    84                 <?php
    85             } ?>
    86         </select>
    87        
     77        <input type="hidden" class="atmat-select2" name="atmat-authors" style="width: 100%" data-placeholder="<?php esc_attr_e( 'Add contributors&hellip;', 'at-multiauthor' ); ?>" data-action="get_contributors_list" data-multiple="true" data-allow_clear="true" data-selected="<?php echo esc_attr( json_encode( $json_ids ) ); ?>" value="<?php echo implode( ',', array_keys( $json_ids ) ); ?>" <?php echo $disabled; ?> />
    8878        <?php
    8979        if ( $disabled ) {
     
    9282            <?php
    9383        }
    94         do_action( 'atmat_metabox_multiauthor_after', $authors, $post );
     84        do_action( 'atmat_metabox_multiauthor_after', $author_ids, $post );
    9585        wp_nonce_field( 'atmat_save_settings', 'atmat-nonce' );
     86    }
     87
     88    public function get_contributors_list() {
     89        ob_start();
     90
     91        // Security pass 1.
     92        check_ajax_referer( 'atmat_save_settings', 'security' );
     93
     94        // Security pass 2.
     95        if ( ! current_user_can( 'edit_posts' ) ) {
     96            die();
     97        }
     98
     99        $term = sanitize_text_field( stripslashes( $_GET['term'] ) );
     100
     101        // No input.
     102        if ( empty( $term ) ) {
     103            die();
     104        }
     105
     106        $exclude = apply_filters( 'atmat_get_exclude_contributors_ids', array() );
     107
     108        $found_contributors = array();
     109
     110        // Add search column `display_name`.
     111        add_filter( 'user_search_columns', function( $search_columns ) {
     112            $search_columns[] = 'display_name';
     113            return $search_columns;
     114        } );
     115
     116        // WP user query.
     117        $contributors_query = new \WP_User_Query( apply_filters( 'atmat_get_contributors_list_query', array(
     118            'fields'         => 'all',
     119            'orderby'        => 'display_name',
     120            'search'         => '*' . $term . '*',
     121            'search_columns' => array( 'ID', 'display_name', 'user_email' ),
     122            'role__in'       => get_contributors_role_in(),
     123        ) ) );
     124
     125        $contributors = $contributors_query->get_results();
     126
     127        if ( ! empty( $contributors ) ) {
     128            foreach ( $contributors as $contributor ) {
     129                if ( ! in_array( $contributor->ID, $exclude ) ) {
     130                    $found_contributors[ $contributor->ID ] = $contributor->display_name . ' (#' . $contributor->ID . ' &ndash; ' . sanitize_email( $contributor->user_email ) . ')';
     131                }
     132            }
     133        }
     134
     135        $found_contributors = apply_filters( 'atmat_found_contributors', $found_contributors );
     136        wp_send_json( $found_contributors );
    96137    }
    97138
     
    115156        $authors = array();
    116157
    117         if ( isset( $_POST['atmat-authors'] ) ) {
     158        if ( isset( $_POST['atmat-authors'] ) && ! empty( $_POST['atmat-authors'] ) ) {
    118159            $role_in = get_contributors_role_in( $post_id );
     160            $post_authors = explode( ',', sanitize_text_field( $_POST['atmat-authors'] ) );
    119161            // Security pass 3 - Validate contributors ID.
    120             foreach ( (array) $_POST['atmat-authors'] as $contributor_id ) {
     162            foreach ( $post_authors as $contributor_id ) {
    121163                $contributor_id = (int) $contributor_id;
    122164                $contributor = get_userdata( $contributor_id );
  • at-multiauthor/trunk/readme.txt

    r1572745 r1579713  
    3030== Changelog ==
    3131
     32= 1.0.4 =
     33* Tweak - Handle condition when users are huge (Added select2 ajax).
     34
    3235= 1.0.3 =
    3336* Tweak - Delete plugin's data after uninstallation.
Note: See TracChangeset for help on using the changeset viewer.