PluginProbe
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI / 7.9.8
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI v7.9.8
8.0.4 8.0.3 8.0.1 8.0.2 8.0.0 7.9.9.7 7.9.9.6 7.9.9.5 7.9.9.4 7.9.9.3 7.9.9.2 7.9.9.1 7.9.9 7.9.8 7.9.7 7.9.6 7.9.5 7.9.4 7.9.3 7.9.2 7.9.1 7.9.0 7.8.9 7.8.8 7.8.7 All 46 releases
gamipress / assets / js / gamipress-shortcodes-editor.js

gamipress-shortcodes-editor.js in GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI 7.9.8, at assets/js/gamipress-shortcodes-editor.js

364 lines 13.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 (function ($) {
2
3 function gamipress_insert_shortcode() {
4
5 var shortcode = gamipress_get_selected_shortcode();
6 var attributes = gamipress_get_attributes( shortcode );
7 var constructed = gamipress_construct_shortcode( shortcode, attributes );
8
9 window.send_to_editor( constructed );
10 }
11
12 function gamipress_get_selected_shortcode() {
13 return $( '#select_shortcode' ).val();
14 }
15
16 function gamipress_get_attributes( shortcode ) {
17 var attrs = {};
18 var inputs = gamipress_get_shortcode_inputs( shortcode );
19
20 $.each( inputs, function( index, el ) {
21 var key, field, value, fields, values, i;
22 var row = $(el).closest('.cmb-row');
23
24 // Skip repeatable fields pattern
25 if( row.hasClass('empty-row') && row.hasClass('hidden') ) return true;
26
27 // Turn array of repeatable field into a comma separated values
28 if( row.hasClass('cmb-repeat-row') ) {
29 // Repeatable
30
31 key = el.name.replace( shortcode + '_', '').replace('[]', '');
32
33 key = key.split('[')[0];
34
35 // Skip empty shortcode keys
36 if( key === '' ) return true;
37
38 // Just continue if element has not set
39 if( attrs[key] === undefined ) {
40
41 var field_name = el.name.split('[')[0];
42
43 // Look at all fields
44 fields = $(el).closest('.cmb-tbody').find('[name^="' + field_name + '"]');
45 values = [];
46
47 // Loop all fields and make an array with all values
48 // Note: loop max is set to length-1 to skip pattern field
49 for( i=0; i < fields.length-1; i++ ) {
50 field = $(fields[i]);
51
52 if( field.val().length )
53 values.push( field.val() );
54 }
55
56 // Setup a comma-separated list of values as attribute value
57 if( values.length )
58 attrs[key] = values.join(',');
59
60 }
61 } else if( row.data('fieldtype') === 'multicheck' ) {
62 // Multicheck
63
64 key = el.name.replace( shortcode + '_', '').replace('[]', '');
65
66 // Skip empty shortcode keys
67 if( key === '' ) return true;
68
69 // Look at checked fields
70 fields = $(el).closest('.cmb2-checkbox-list').find('[name^="' + el.name + '"]:checked');
71 values = [];
72
73 // Loop checked fields and make an array with all values
74 for( i=0; i < fields.length; i++ ) {
75
76 field = $(fields[i]);
77
78 if( field.val().length )
79 values.push( field.val() );
80 }
81
82 // Setup a comma-separated list of values as attribute value
83 if( values.length )
84 attrs[key] = values.join(',');
85
86 } else {
87 // Single
88
89 // CMB2 adds a prefix on each field, so we need to remove it, also, wee need to remove array brace for multiple fields
90 key = el.name.replace( shortcode + '_', '').replace('[]', '');
91
92 // Skip empty shortcode keys
93 if( key === '' ) return true;
94
95 // Select2 values are only accessible through jQuery val()
96 value = $(el).val();
97
98 // Turn checked status into yes or no
99 if( $(el).attr('type') === 'checkbox' ) {
100 value = $(el).prop('checked') ? 'yes' : 'no';
101 }
102
103 // For radio inputs, just get checked input value
104 if( $(el).attr('type') === 'radio' ) {
105 value = $(el).closest('.cmb2-radio-list').find('input[type="radio"]:checked').val()
106 }
107
108 if( typeof value === 'string' ) {
109 // Replaces " by ' on text fields
110 value = value.replace(/"/g, "'");
111 }
112
113 if (value !== '' && value !== undefined && value !== null ) {
114 attrs[key] = value;
115 }
116
117 }
118 });
119
120 // Allow external functions to add their own data to the array of attrs
121 var args = { attributes: attrs, inputs: inputs };
122
123 $('#' + shortcode + '_wrapper').trigger( 'gamipress_shortcode_attributes', [ args ] );
124
125 // TODO: gamipress_get_shortcode_attributes is deprecated since 1.4.8, just keep for backward compatibility
126 $('#' + shortcode + '_wrapper').trigger( 'gamipress_get_shortcode_attributes', [ args.attributes, args.inputs ] );
127
128 return args.attributes;
129 }
130
131 function gamipress_get_shortcode_inputs( shortcode ) {
132 // Look at .cmb2-wrap to prevent get cmb2 nonce fields
133 return $( 'input, select, textarea', '#' + shortcode + '_wrapper .cmb2-wrap' );
134 }
135
136 function gamipress_construct_shortcode( shortcode, attributes ) {
137 var output = '[';
138 output += shortcode;
139
140 $.each( attributes, function( key, value ) {
141 output += ' ' + key + '="' + value + '"';
142 });
143
144 $.trim( output );
145 output += ']';
146
147 // Allow external functions to construct their own shortcode
148 var args = { output: output, shortcode: shortcode, attributes: attributes };
149
150 $('#' + shortcode + '_wrapper').trigger( 'gamipress_construct_shortcode', [ args ] );
151
152 return args.output;
153 }
154
155 function gamipress_shortcode_hide_all_sections() {
156 $( '.shortcode-section' ).hide();
157 }
158
159 function gamipress_shortcode_show_section( section_name ) {
160 $( '#' + section_name + '_wrapper' ).show();
161 }
162
163 // Listen for changes to the selected shortcode
164 $( '#select_shortcode' ).on( 'change', function() {
165 gamipress_shortcode_hide_all_sections();
166 gamipress_shortcode_show_section( gamipress_get_selected_shortcode() );
167 }).trigger('change');
168
169 // Listen for clicks on the "insert" button
170 $( '#gamipress_insert' ).on( 'click', function( e ) {
171 e.preventDefault();
172
173 gamipress_insert_shortcode();
174 });
175
176 // Listen for clicks on the "cancel" button
177 $( '#gamipress_cancel' ).on( 'click', function( e ) {
178 e.preventDefault();
179
180 tb_remove();
181 });
182
183 // User ajax
184 $( '#gamipress_achievements_user_id, ' +
185 '#gamipress_logs_user_id, ' +
186 '#gamipress_points_user_id, ' +
187 '#gamipress_user_points_user_id, ' +
188 '#gamipress_points_types_user_id, ' +
189 '#gamipress_rank_user_id, ' +
190 '#gamipress_ranks_user_id, ' +
191 '#gamipress_user_rank_user_id, ' +
192 '#gamipress_earnings_user_id' ).gamipress_select2({
193 ajax: {
194 url: ajaxurl,
195 dataType: 'json',
196 delay: 250,
197 type: 'POST',
198 data: function( params ) {
199 return {
200 q: params.term,
201 page: params.page || 1,
202 action: 'gamipress_get_users',
203 nonce: gamipress_shortcodes_editor.nonce,
204 };
205 },
206 processResults: gamipress_select2_users_process_results
207 },
208 escapeMarkup: function ( markup ) { return markup; }, // Let our custom formatter work
209 templateResult: gamipress_select2_users_template_result,
210 theme: 'default gamipress-select2',
211 placeholder: gamipress_shortcodes_editor.user_placeholder,
212 allowClear: true,
213 multiple: false
214 });
215
216 // Current user field
217 $( '#gamipress_achievements_current_user, '
218 + '#gamipress_points_current_user, '
219 + '#gamipress_user_points_current_user, '
220 + '#gamipress_points_types_current_user, '
221 + '#gamipress_logs_current_user, '
222 + '#gamipress_ranks_current_user, '
223 + '#gamipress_user_rank_current_user, '
224 + '#gamipress_earnings_current_user'
225 ).on('change', function() {
226 var target = $(this).closest('.cmb-row').next(); // User ID field
227
228 if( $(this).prop('checked') ) {
229 // Hide the target
230 target.slideUp().addClass('cmb2-tab-ignore');
231 } else {
232 if( target.closest('.cmb-tabs-wrap').length ) {
233 // Just show if item tab is active
234 if( target.hasClass('cmb-tab-active-item') ) {
235 target.slideDown();
236 }
237 } else {
238 target.slideDown();
239 }
240
241 target.removeClass('cmb2-tab-ignore');
242 }
243 }).trigger('change');
244
245 // Earners field
246 $( '#gamipress_achievement_earners, #gamipress_achievements_earners, '
247 + '#gamipress_rank_earners, #gamipress_ranks_earners, #gamipress_user_rank_earners'
248 ).on('change', function() {
249 var target = $(this).closest('.cmb-row').next(); // Earners limit field
250
251 if( ! $(this).prop('checked') ) {
252 // Hide the target
253 target.slideUp().addClass('cmb2-tab-ignore');
254 } else {
255 if( target.closest('.cmb-tabs-wrap').length ) {
256 // Just show if item tab is active
257 if( target.hasClass('cmb-tab-active-item') ) {
258 target.slideDown();
259 }
260 } else {
261 target.slideDown();
262 }
263
264 target.removeClass('cmb2-tab-ignore');
265 }
266 }).trigger('change');
267
268 // Period field
269 $( '#gamipress_points_period, #gamipress_user_points_period, #gamipress_site_points_period').on('change', function() {
270 var selector = $(this).attr('id').replace('_period', '').replaceAll('_', '-');
271
272 // Get the period start and end fields
273 var target = $(this).closest('.cmb2-wrap').find(
274 '.cmb2-id-' + selector + '-period-start, '
275 + '.cmb2-id-' + selector + '-period-end'
276 );
277
278 if( $(this).val() !== 'custom' ) {
279 // Hide the target
280 target.slideUp().addClass('cmb2-tab-ignore');
281 } else {
282 // Show the target
283 target.slideDown().removeClass('cmb2-tab-ignore');
284 }
285 }).trigger('change');
286
287 // Inline field
288 $( '#gamipress_points_inline, #gamipress_user_points_inline, #gamipress_site_points_inline').on('change', function() {
289 var selector = $(this).attr('id').replace('_inline', '').replaceAll('_', '-');
290
291 // Get the columns and layout fields
292 var target = $(this).closest('.cmb2-wrap').find(
293 '.cmb2-id-' + selector + '-columns, '
294 + '.cmb2-id-' + selector + '-columns-small, '
295 + '.cmb2-id-' + selector + '-layout'
296 );
297
298 if( $(this).prop('checked') ) {
299 target.slideUp().addClass('cmb2-tab-ignore');
300 } else {
301 target.slideDown().removeClass('cmb2-tab-ignore');
302 }
303 });
304
305 $('body').on('gamipress_shortcode_attributes', '#gamipress_points_wrapper, #gamipress_user_points_wrapper, #gamipress_site_points_wrapper', function( e, args ) {
306
307 // If user checks inline, then columns and layout has no sense
308 if( args.attributes.inline === 'yes' ) {
309 delete args.attributes.columns;
310 delete args.attributes.columns_small;
311 delete args.attributes.layout;
312 }
313
314 });
315
316 // User earnings
317 $( '#gamipress_earnings_points, #gamipress_earnings_achievements, #gamipress_earnings_ranks' ).on('change', function() {
318
319 var id = $(this).attr('id');
320 var target;
321
322 if( id === 'gamipress_earnings_points' ) {
323 target = $('.cmb2-id-gamipress-earnings-points-types, .cmb2-id-gamipress-earnings-awards, .cmb2-id-gamipress-earnings-deducts');
324 } else if( id === 'gamipress_earnings_achievements' ) {
325 target = $('.cmb2-id-gamipress-earnings-achievement-types, .cmb2-id-gamipress-earnings-steps');
326 } else if( id === 'gamipress_earnings_ranks' ) {
327 target = $('.cmb2-id-gamipress-earnings-rank-types, .cmb2-id-gamipress-earnings-rank-requirements');
328 }
329
330 if( $(this).prop('checked') ) {
331 // Just show if current tab active is ours
332 if( $(this).closest('.cmb-tabs-wrap').find('.cmb-tab.active[id$="' + id + '"]').length ) {
333 target.slideDown();
334 }
335
336 target.removeClass('cmb2-tab-ignore');
337 } else {
338 target.slideUp().addClass('cmb2-tab-ignore');
339 }
340 });
341
342 // Setup ThickBox when "Add GamiPress Shortcode" link is clicked
343 $('body').on( 'click', '#insert_gamipress_shortcodes', function(e) {
344 e.preventDefault();
345
346 gamipress_shortcode_setup_thickbox( $(this) );
347 });
348
349 // Add a custom class to our shortcode thickbox
350 function gamipress_shortcode_setup_thickbox( link ) {
351 setTimeout( function() {
352 // Add a custom class to the thickbox
353 $('#TB_window').addClass('gamipress-shortcode-thickbox');
354
355 // Clear all select2 fields
356 $( 'select.select2-hidden-accessible', '.gamipress-shortcode-thickbox .cmb2-wrap').val('').trigger('change');
357
358 // Trigger change on all checkboxes to initialize visibility
359 $( 'input[type="checkbox"]', '.gamipress-shortcode-thickbox .cmb2-wrap').trigger('change');
360 }, 0 );
361 }
362
363 }(jQuery));
364