1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/**
* LifterLMS Admin Panel Javascript
*
* @since Unknown
* @version 4.5.1
*
* @param obj $ Traditional jQuery reference.
* @return void
*/
;( function( $ ) {
window.llms = window.llms || {};
window.llms.widgets = function() {
this.$widgets = $( '.llms-widget' );
this.$info_toggles = $( '.llms-widget-info-toggle' );
this.init = function() {
this.bind();
};
this.bind = function() {
this.$info_toggles.on( 'mouseenter mouseleave', function( evt ) {
$(this).closest( '.llms-widget' )
.toggleClass( 'info-showing', 'mouseenter' === evt.type );
} );
};
// Go.
this.init();
return this;
};
var llms_widgets = new window.llms.widgets();
/**
* Simple jQuery plugin to transform select elements into Select2-powered elements to query for Courses/Memberships via AJAX.
*
* @since 3.19.4
* @since 3.32.0 Added ability to fetch posts based on their post status.
* @since 3.37.2 Added ability to fetch posts (llms posts) filtered by their instructor id.
* @since 4.4.0 Update ajax nonce source.
*
* @param obj options Options passed to Select2.
* Each default option will pulled from the elements data-attributes.
* @return void
*/
$.fn.llmsPostsSelect2 = function( options ) {
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;
}
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 ) {
// 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,
};
}
return {
results: map_data( data.items ),
pagination: {
more: data.more
}
};
},
},
cache: true,
placeholder: options.placeholder,
multiple: options.multiple,
width: options.width,
} );
};
// automatically setup any select with the `llms-posts-select2` class
$( 'select.llms-posts-select2' ).llmsPostsSelect2();
/**
* Simple jQuery plugin to transform select elements into Select2-powered elements to query for Students via AJAX
*
* @since Unknown
* @since 3.17.5 Unknown.
* @since 4.4.0 Update ajax nonce source.
*
* @param obj options Options passed to Select2. Each default option will be pulled from the elements data-attributes.
* @return void
*/
$.fn.llmsStudentsSelect2 = function( options ) {
var self = this,
options = options || {},
defaults = {
allow_clear: false,
enrolled_in: '',
multiple: false,
not_enrolled_in: '',
placeholder: undefined !== LLMS.l10n ? LLMS.l10n.translate( 'Select a student' ) : 'Select a student',
roles: '',
width: '100%',
};
$.each( defaults, function( setting ) {
if ( self.attr( 'data-' + setting ) ) {
options[ setting ] = self.attr( 'data-' + setting );
}
} );
options = $.extend( defaults, options );
this.llmsSelect2({
allowClear: options.allow_clear,
ajax: {
dataType: 'JSON',
delay: 250,
method: 'POST',
url: window.ajaxurl,
data: function( params ) {
return {
_ajax_nonce: window.llms.ajax_nonce,
action: 'query_students',
page: params.page,
not_enrolled_in: params.not_enrolled_in || options.not_enrolled_in,
enrolled_in: params.enrolled_in || options.enrolled_in,
roles: params.roles || options.roles,
term: params.term,
};
},
processResults: function( data, params ) {
return {
results: $.map( data.items, function( item ) {
return {
text: item.name + ' <' + item.email +'>',
id: item.id,
};
} ),
pagination: {
more: data.more
}
};
},
},
cache: true,
placeholder: options.placeholder,
multiple: options.multiple,
width: options.width,
});
return this;
};
/**
* Scripts for use on the engagements settings tab for email provider connector plugins
*
* @since 3.40.0
*/
window.llms.emailConnectors = {
/**
* Register a client
*
* Builds and submits a form used to direct the user to the connector's oAuth
* authorization endpoint.
*
* @since 3.40.0
*
* @param {String} url Redirect URL.
* @param {Object} fields Hash of fields where the key is the field name and the value if the field value.
* @return {Void}
*/
registerClient: function( url, fields ) {
var form = document.createElement( 'form' );
form.setAttribute( 'method', 'POST' );
form.setAttribute( 'action', url );
function appendInput( name, value ) {
var input = document.createElement( 'input' );
input.setAttribute( 'type', 'hidden' );
input.setAttribute( 'name', name );
input.setAttribute( 'value', value );
form.appendChild( input );
}
$.each( fields, function( key, val ) {
appendInput( key, val );
} );
document.body.appendChild( form );
form.submit();
},
/**
* Performs an AJAX request to perform remote installation of the connector plugin
*
* The callback will more than likely use `registerClient()` on success.
*
* @since 3.40.0
*
* @param {Object} $btn jQuery object for the connector button.
* @param {Object} data Hash of data used for the ajax request.
* @param {Function} callback Success callback function.
* @return {Void}
*/
remoteInstall: function( $btn, data, callback ) {
$btn.parent().find( '.llms-error' ).remove();
$.post( ajaxurl, data, callback ).fail( function( jqxhr ) {
LLMS.Spinner.stop( $btn );
var msg = jqxhr.responseJSON && jqxhr.responseJSON.message ? jqxhr.responseJSON.message : jqxhr.responseText;
if ( msg ) {
$( '<p class="llms-error">' + LLMS.l10n.replace( 'Error: %s', { '%s': msg } ) + '</p>' ).insertAfter( $btn );
}
} );
}
};
} )( jQuery );