From 7ed3de05f663d862a0a7129615a4202945cc75da Mon Sep 17 00:00:00 2001 From: Brian Hogg Date: Thu, 14 Nov 2024 11:22:53 -0500 Subject: [PATCH 1/3] Proper handling when there are multiple elements on which llmsPostsSelect2 is called, by looping over each one. --- assets/js/llms-admin.js | 161 ++++++++++++++++++++-------------------- 1 file changed, 82 insertions(+), 79 deletions(-) diff --git a/assets/js/llms-admin.js b/assets/js/llms-admin.js index 43d7edd4c1..0aa2ef5c35 100644 --- a/assets/js/llms-admin.js +++ b/assets/js/llms-admin.js @@ -51,96 +51,99 @@ * @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 ); - - 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 ) { + options = $.extend( defaults, options ); + + $( this ).llmsSelect2( { + allowClear: options.allow_clear, + ajax: { + dataType: 'JSON', + delay: 250, + method: 'POST', + url: window.ajaxurl, + data: function( params ) { + debugger; + 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 From 10bcc30879bdbd5ef4b1e93575f3ebabb25287cd Mon Sep 17 00:00:00 2001 From: Brian Hogg Date: Thu, 14 Nov 2024 11:24:57 -0500 Subject: [PATCH 2/3] Changelog. --- .changelogs/fix_llms-posts-select2-improvements.yml | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changelogs/fix_llms-posts-select2-improvements.yml 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. From 7439d89f29fb4b29a46e4a93e295115e8d3f0757 Mon Sep 17 00:00:00 2001 From: Brian Hogg Date: Thu, 14 Nov 2024 12:21:17 -0500 Subject: [PATCH 3/3] Removing debugger. --- assets/js/llms-admin.js | 1 - 1 file changed, 1 deletion(-) diff --git a/assets/js/llms-admin.js b/assets/js/llms-admin.js index 0aa2ef5c35..596d86bbf4 100644 --- a/assets/js/llms-admin.js +++ b/assets/js/llms-admin.js @@ -86,7 +86,6 @@ method: 'POST', url: window.ajaxurl, data: function( params ) { - debugger; return { action: 'select2_query_posts', page: ( params.page ) ? params.page - 1 : 0, // 0 index the pages to make it simpler for the database query