diff --git a/.changelogs/fix_llms-posts-select2-improvements.yml b/.changelogs/fix_llms-posts-select2-improvements.yml new file mode 100644 index 0000000000..5b763f4551 --- /dev/null +++ b/.changelogs/fix_llms-posts-select2-improvements.yml @@ -0,0 +1,6 @@ +significance: patch +type: dev +links: + - "#2805" +entry: Improved llmsPostsSelect2 method when called on multiple elements at + once, each with different options. diff --git a/assets/js/llms-admin.js b/assets/js/llms-admin.js index 43d7edd4c1..596d86bbf4 100644 --- a/assets/js/llms-admin.js +++ b/assets/js/llms-admin.js @@ -51,96 +51,98 @@ * @return void */ $.fn.llmsPostsSelect2 = function( options ) { + var localOptions = options; + + this.each( function() { + var self = $( this ), + options = localOptions || {}, + defaults = { + multiple: false, + placeholder: self.attr( 'data-placeholder' ) || ( undefined !== LLMS.l10n ? LLMS.l10n.translate( 'Select a Course/Membership' ) : 'Select a Course/Membership' ), + post_type: self.attr( 'data-post-type' ) || 'post', + post_statuses: self.attr( 'data-post-statuses' ) || 'publish', + instructor_id: null, + allow_clear: self.attr( 'data-post-type' ) || false, + width: null, + }; + + $.each( defaults, function( setting ) { + if ( self.attr( 'data-' + setting ) ) { + options[ setting ] = self.attr( 'data-' + setting ); + } + } ); - var self = this, - options = options || {}, - defaults = { - multiple: false, - placeholder: undefined !== LLMS.l10n ? LLMS.l10n.translate( 'Select a Course/Membership' ) : 'Select a Course/Membership', - post_type: self.attr( 'data-post-type' ) || 'post', - post_statuses: self.attr( 'data-post-statuses' ) || 'publish', - instructor_id: null, - allow_clear: self.attr( 'data-post-type' ) || false, - width: null, - }; - - $.each( defaults, function( setting ) { - if ( self.attr( 'data-' + setting ) ) { - options[ setting ] = self.attr( 'data-' + setting ); + if ( 'multiple' === self.attr( 'multiple' ) ) { + options.multiple = true; } - } ); - if ( 'multiple' === self.attr( 'multiple' ) ) { - options.multiple = true; - } + options = $.extend( defaults, options ); - options = $.extend( defaults, options ); - - this.llmsSelect2( { - allowClear: options.allow_clear, - ajax: { - dataType: 'JSON', - delay: 250, - method: 'POST', - url: window.ajaxurl, - data: function( params ) { - return { - action: 'select2_query_posts', - page: ( params.page ) ? params.page - 1 : 0, // 0 index the pages to make it simpler for the database query - post_type: options.post_type, - instructor_id : options.instructor_id, - post_statuses: options.post_statuses, - term: params.term, - _ajax_nonce: window.llms.ajax_nonce, - }; - }, - processResults: function( data, params ) { + $( this ).llmsSelect2( { + allowClear: options.allow_clear, + ajax: { + dataType: 'JSON', + delay: 250, + method: 'POST', + url: window.ajaxurl, + data: function( params ) { + return { + action: 'select2_query_posts', + page: ( params.page ) ? params.page - 1 : 0, // 0 index the pages to make it simpler for the database query + post_type: options.post_type, + instructor_id : options.instructor_id, + post_statuses: options.post_statuses, + term: params.term, + _ajax_nonce: window.llms.ajax_nonce, + }; + }, + processResults: function( data, params ) { + + // recursive function for creating + function map_data( items ) { + + // this is a flat array of results + // used when only one post type is selected + // and to format children when using optgroups with multiple post types + if ( Array.isArray( items ) ) { + return $.map( items, function( item ) { + return format_item( item ); + } ); + + // this sets up the top level optgroups when using multiple post types + } else { + return $.map( items, function( item ) { + return { + text: item.label, + children: map_data( item.items ), + } + } ); + } + } - // recursive function for creating - function map_data( items ) { - - // this is a flat array of results - // used when only one post type is selected - // and to format children when using optgroups with multiple post types - if ( Array.isArray( items ) ) { - return $.map( items, function( item ) { - return format_item( item ); - } ); - - // this sets up the top level optgroups when using multiple post types - } else { - return $.map( items, function( item ) { - return { - text: item.label, - children: map_data( item.items ), - } - } ); + // format a single result (option) + function format_item( item ) { + return { + text: item.name, + id: item.id, + }; } - } - // format a single result (option) - function format_item( item ) { return { - text: item.name, - id: item.id, + results: map_data( data.items ), + pagination: { + more: data.more + } }; - } - - return { - results: map_data( data.items ), - pagination: { - more: data.more - } - }; + }, }, - }, - cache: true, - placeholder: options.placeholder, - multiple: options.multiple, - width: options.width, + cache: true, + placeholder: options.placeholder, + multiple: options.multiple, + width: options.width, + } ); } ); - }; // automatically setup any select with the `llms-posts-select2` class