PluginProbe
User Submitted Posts – Enable Users to Submit Posts from the Front End / 20260916
User Submitted Posts – Enable Users to Submit Posts from the Front End v20260916
20260916 20260810 20260608 20230806 20230809 20230811 20230901 20230902 20230914 20231102 20240319 20240516 20240703 20241026 20250327 20250329 20251121 20251210 20260110 20260113 20260207 20260217 20260407 20260422 trunk All 59 releases
user-submitted-posts / resources / jquery.usp.core.js

jquery.usp.core.js in User Submitted Posts – Enable Users to Submit Posts from the Front End 20260916, at resources/jquery.usp.core.js

218 lines 7.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /*
2 User Submitted Posts : Core JS : Version 2.0
3 @ https://perishablepress.com/user-submitted-posts/
4 */
5 jQuery(document).ready(function($) {
6
7 $('.usp-callout-failure').addClass('usp-hidden').hide();
8 $('#user-submitted-post').on('click', function(e) {
9 if (usp_recaptcha_disp == 'show' && usp_recaptcha_vers == 3) {
10 var validate = usp_validate();
11 e.preventDefault();
12 grecaptcha.ready(function() {
13 grecaptcha.execute(usp_recaptcha_key, { action: 'submit' }).then(function(token) {
14 $('#recaptcha_response').remove();
15 $('#usp-submit').prepend('<input type="hidden" name="recaptcha_response" id="recaptcha_response" value="'+ token +'">');
16 if (validate) $('#usp_form').unbind('submit').submit();
17 });;
18 });
19 } else {
20 usp_validate();
21 }
22 });
23 function usp_validate() {
24 // $('#usp_form').parsley().validate();
25 if (true === $('#usp_form').parsley().isValid()) {
26 $('.usp-callout-failure').addClass('usp-hidden').hide();
27
28 // remove empty file inputs
29 $('.usp-clone').each(function() {
30 var opt = $(this).data('parsley-excluded');
31 if (typeof opt !== 'undefined' && opt == true) {
32 var val = $(this).val();
33 if (!val.trim()) $(this).remove();
34 }
35 });
36 return true;
37 } else {
38 $('.usp-callout-failure').removeClass('usp-hidden').show();
39 return false;
40 }
41 };
42 $('#usp_form').on('submit', function(e) {
43 usp_captcha_check(e);
44 if ($(this).parsley().isValid()) {
45 $('.usp-submit').css('cursor', 'wait');
46 $('.usp-submit').attr('disabled', true);
47 }
48 usp_remember();
49 });
50 $('.usp-captcha .usp-input').on('change', function(e) {
51 usp_captcha_check(e);
52 });
53 function usp_captcha_check(e) {
54 $.post(ajax_url, {
55 nonce: challenge_nonce,
56 action: 'challenge_nonce'
57 }, function(data) {
58 if (usp_case_sensitivity === 'true') var usp_casing = '';
59 else var usp_casing = 'i';
60 var usp_response = new RegExp('^' + data + '$', usp_casing);
61 var usp_captcha = $('.user-submitted-captcha').val();
62 if (typeof usp_captcha != 'undefined') {
63 if (usp_captcha.match(usp_response)) {
64 $('.usp-captcha-error').remove();
65 $('.usp-captcha .usp-input').removeClass('parsley-error');
66 $('.usp-captcha .usp-input').addClass('parsley-success');
67 } else {
68 if (e) e.preventDefault();
69 $('.usp-captcha-error').remove();
70 $('.usp-captcha').append('<ul class="usp-captcha-error parsley-errors-list filled"><li class="parsley-required">'+ usp_parsley_error +'</li></ul>');
71 $('.usp-captcha .usp-input').removeClass('parsley-success');
72 $('.usp-captcha .usp-input').addClass('parsley-error');
73 }
74 }
75 });
76 }
77
78 // cookies
79 usp_remember();
80 usp_forget();
81
82 function usp_cookie(selector, type) {
83 $(selector).each(function() {
84 var name = $(this).attr('id');
85 var cookie = Cookies.get(name);
86 if (cookie) {
87 cookie = decodeURIComponent(cookie);
88 if (type == 'checkbox') {
89 if (cookie == 1) {
90 $(this).val(1).prop('checked', 1);
91 } else {
92 $(this).val(0).prop('checked', 0);
93 }
94 } else if (type == 'select') {
95 if (name == 'user-submitted-tags' && window.usp_existing_tags == 1) {
96 $.each(cookie.split(','), function(i,e) {
97 $('#user-submitted-tags option[value="'+ e +'"]').attr('selected', 'selected');
98 });
99 } else if (name == 'user-submitted-category' && window.usp_multiple_cats == 1) {
100 $.each(cookie.split(','), function(i,e) {
101 $('#user-submitted-category option[value="'+ e +'"]').attr('selected', 'selected');
102 });
103 } else {
104 $('option[value="'+ cookie +'"]', this).attr('selected', 'selected');
105 }
106 } else {
107 $(this).val(cookie);
108 }
109 }
110 $(this).on('change', function() {
111 if (type == 'checkbox') {
112
113 if ($(this).is(':checked')) {
114 var value = 1;
115 $(this).val(1).prop('checked', 1);
116 } else {
117 var value = 0;
118 $(this).val(0).prop('checked', 0);
119 }
120 } else {
121 var value = $(this).val();
122 }
123 Cookies.set(name, encodeURIComponent(value), { path: '/', expires: 365000, SameSite: 'strict' });
124 });
125 });
126 }
127 function usp_remember() {
128 usp_cookie('#user-submitted-name', 'text');
129 usp_cookie('#user-submitted-email', 'text');
130 usp_cookie('#user-submitted-url', 'text');
131 usp_cookie('#user-submitted-title', 'text');
132
133 if (window.usp_existing_tags == 1) {
134 usp_cookie('#user-submitted-tags', 'select');
135 } else {
136 usp_cookie('#user-submitted-tags', 'text');
137 }
138 usp_cookie('#user-submitted-custom', 'text');
139 usp_cookie('#user-submitted-captcha', 'text');
140 usp_cookie('#user-submitted-category', 'select');
141 usp_cookie('#user-submitted-content', 'textarea');
142 usp_cookie('#user-submitted-checkbox', 'checkbox');
143 }
144 function usp_forget() {
145
146 if (window.location.href.indexOf('success=') > -1) {
147
148 Cookies.remove('user-submitted-name');
149 Cookies.remove('user-submitted-email');
150 Cookies.remove('user-submitted-url');
151 Cookies.remove('user-submitted-title');
152 Cookies.remove('user-submitted-tags');
153 Cookies.remove('user-submitted-category');
154 Cookies.remove('user-submitted-content');
155 Cookies.remove('user-submitted-custom');
156 Cookies.remove('user-submitted-checkbox');
157 Cookies.remove('user-submitted-captcha');
158 $('#usp_form').find('input[type="text"], textarea').val('');
159 $('#usp_form option[value=""]').attr('selected', '');
160 }
161 }
162
163 // add another image
164 $('#usp_add-another').removeClass('usp-no-js');
165 $('#usp_add-another').addClass('usp-js');
166
167 usp_add_another();
168
169 function usp_add_another() {
170 var x = parseInt($('#usp-min-images').val());
171 var y = parseInt($('#usp-max-images').val());
172 if (x === 0) x = 1;
173 if (x >= y) $('#usp_add-another').hide();
174
175 $('#usp_add-another').on('click', function(e) {
176 e.preventDefault();
177 x++;
178 var link = $(this);
179 var clone = $('#user-submitted-image').find('input:visible:last').clone().val('').attr('style', 'display:block;');
180 var prev = '<img class="usp-file-preview" src="" alt="" style="display:none;">';
181 $('#usp-min-images').val(x);
182 if (x < y) {
183 link.before(clone.fadeIn(300));
184 link.before(prev);
185 } else if (x = y) {
186 link.before(clone.fadeIn(300));
187 link.before(prev);
188 link.hide();
189 } else {
190 link.hide();
191 }
192 clone.attr('data-parsley-excluded', 'true');
193 });
194 }
195
196 // file preview
197 $('.usp-input[type=file]').after('<img class="usp-file-preview" src="" alt="" style="display:none;">');
198 $(document).on('change', '.usp-input[type=file]', function(x) {
199 var f = x.target.files[0];
200 var disable = (typeof window.usp_disable_previews !== 'undefined') ? window.usp_disable_previews : false;
201 if (f && !disable) {
202 var r = new FileReader();
203 var prev = $(this).nextAll('.usp-file-preview:first');
204 r.onload = function(e) {
205 prev.attr('src', r.result);
206 prev.css({ 'display':'block', 'height':'180px', 'width':'auto', 'margin':'10px 0', 'border':'0' });
207 };
208 r.readAsDataURL(f);
209 }
210 });
211
212 // chosen
213 var disable_chosen = (typeof window.usp_disable_chosen !== 'undefined') ? window.usp_disable_chosen : false;
214
215 if (window.usp_multiple_cats == 1 && !disable_chosen) $('#user-submitted-category').chosen();
216 if (window.usp_existing_tags == 1 && !disable_chosen) $('#user-submitted-tags').chosen();
217
218 });