PluginProbe
User Submitted Posts – Enable Users to Submit Posts from the Front End / 20230901
User Submitted Posts – Enable Users to Submit Posts from the Front End v20230901
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 20230901, at resources/jquery.usp.core.js

213 lines 7.0 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 if (usp_case_sensitivity === 'true') var usp_casing = '';
55 else var usp_casing = 'i';
56 var usp_response = new RegExp(usp_challenge_response + '$', usp_casing);
57 var usp_captcha = $('.user-submitted-captcha').val();
58 if (typeof usp_captcha != 'undefined') {
59 if (usp_captcha.match(usp_response)) {
60 $('.usp-captcha-error').remove();
61 $('.usp-captcha .usp-input').removeClass('parsley-error');
62 $('.usp-captcha .usp-input').addClass('parsley-success');
63 } else {
64 if (e) e.preventDefault();
65 $('.usp-captcha-error').remove();
66 $('.usp-captcha').append('<ul class="usp-captcha-error parsley-errors-list filled"><li class="parsley-required">'+ usp_parsley_error +'</li></ul>');
67 $('.usp-captcha .usp-input').removeClass('parsley-success');
68 $('.usp-captcha .usp-input').addClass('parsley-error');
69 }
70 }
71 }
72
73 // cookies
74 usp_remember();
75 usp_forget();
76
77 function usp_cookie(selector, type) {
78 $(selector).each(function() {
79 var name = $(this).attr('id');
80 var cookie = Cookies.get(name);
81 if (cookie) {
82 cookie = decodeURIComponent(cookie);
83 if (type == 'checkbox') {
84 if (cookie == 1) {
85 $(this).val(1).prop('checked', 1);
86 } else {
87 $(this).val(0).prop('checked', 0);
88 }
89 } else if (type == 'select') {
90 if (name == 'user-submitted-tags' && window.usp_existing_tags == 1) {
91 $.each(cookie.split(','), function(i,e) {
92 $('#user-submitted-tags option[value="'+ e +'"]').attr('selected', 'selected');
93 });
94 } else if (name == 'user-submitted-category' && window.usp_multiple_cats == 1) {
95 $.each(cookie.split(','), function(i,e) {
96 $('#user-submitted-category option[value="'+ e +'"]').attr('selected', 'selected');
97 });
98 } else {
99 $('option[value="'+ cookie +'"]', this).attr('selected', 'selected');
100 }
101 } else {
102 $(this).val(cookie);
103 }
104 }
105 $(this).on('change', function() {
106 if (type == 'checkbox') {
107
108 if ($(this).is(':checked')) {
109 var value = 1;
110 $(this).val(1).prop('checked', 1);
111 } else {
112 var value = 0;
113 $(this).val(0).prop('checked', 0);
114 }
115 } else {
116 var value = $(this).val();
117 }
118 Cookies.set(name, encodeURIComponent(value), { path: '/', expires: 365000, SameSite: 'strict' });
119 });
120 });
121 }
122 function usp_remember() {
123 usp_cookie('#user-submitted-name', 'text');
124 usp_cookie('#user-submitted-email', 'text');
125 usp_cookie('#user-submitted-url', 'text');
126 usp_cookie('#user-submitted-title', 'text');
127
128 if (window.usp_existing_tags == 1) {
129 usp_cookie('#user-submitted-tags', 'select');
130 } else {
131 usp_cookie('#user-submitted-tags', 'text');
132 }
133 usp_cookie('#user-submitted-custom', 'text');
134 usp_cookie('#user-submitted-captcha', 'text');
135 usp_cookie('#user-submitted-category', 'select');
136 usp_cookie('#user-submitted-content', 'textarea');
137 usp_cookie('#user-submitted-checkbox', 'checkbox');
138 }
139 function usp_forget() {
140
141 if (window.location.href.indexOf('success=') > -1) {
142
143 Cookies.remove('user-submitted-name');
144 Cookies.remove('user-submitted-email');
145 Cookies.remove('user-submitted-url');
146 Cookies.remove('user-submitted-title');
147 Cookies.remove('user-submitted-tags');
148 Cookies.remove('user-submitted-category');
149 Cookies.remove('user-submitted-content');
150 Cookies.remove('user-submitted-custom');
151 Cookies.remove('user-submitted-checkbox');
152 Cookies.remove('user-submitted-captcha');
153 $('#usp_form').find('input[type="text"], textarea').val('');
154 $('#usp_form option[value=""]').attr('selected', '');
155 }
156 }
157
158 // add another image
159 $('#usp_add-another').removeClass('usp-no-js');
160 $('#usp_add-another').addClass('usp-js');
161
162 usp_add_another();
163
164 function usp_add_another() {
165 var x = parseInt($('#usp-min-images').val());
166 var y = parseInt($('#usp-max-images').val());
167 if (x === 0) x = 1;
168 if (x >= y) $('#usp_add-another').hide();
169
170 $('#usp_add-another').on('click', function(e) {
171 e.preventDefault();
172 x++;
173 var link = $(this);
174 var clone = $('#user-submitted-image').find('input:visible:last').clone().val('').attr('style', 'display:block;');
175 var prev = '<img class="usp-file-preview" src="" alt="" style="display:none;">';
176 $('#usp-min-images').val(x);
177 if (x < y) {
178 link.before(clone.fadeIn(300));
179 link.before(prev);
180 } else if (x = y) {
181 link.before(clone.fadeIn(300));
182 link.before(prev);
183 link.hide();
184 } else {
185 link.hide();
186 }
187 clone.attr('data-parsley-excluded', 'true');
188 });
189 }
190
191 // file preview
192 $('.usp-input[type=file]').after('<img class="usp-file-preview" src="" alt="" style="display:none;">');
193 $(document).on('change', '.usp-input[type=file]', function(x) {
194 var f = x.target.files[0];
195 var disable = (typeof window.usp_disable_previews !== 'undefined') ? window.usp_disable_previews : false;
196 if (f && !disable) {
197 var r = new FileReader();
198 var prev = $(this).nextAll('.usp-file-preview:first');
199 r.onload = function(e) {
200 prev.attr('src', r.result);
201 prev.css({ 'display':'block', 'height':'180px', 'width':'auto', 'margin':'10px 0', 'border':'0' });
202 };
203 r.readAsDataURL(f);
204 }
205 });
206
207 // chosen
208 var disable_chosen = (typeof window.usp_disable_chosen !== 'undefined') ? window.usp_disable_chosen : false;
209
210 if (window.usp_multiple_cats == 1 && !disable_chosen) $('#user-submitted-category').chosen();
211 if (window.usp_existing_tags == 1 && !disable_chosen) $('#user-submitted-tags').chosen();
212
213 });