PluginProbe
User Submitted Posts – Enable Users to Submit Posts from the Front End / 20260217
User Submitted Posts – Enable Users to Submit Posts from the Front End v20260217
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 / library / form-functions.php

form-functions.php in User Submitted Posts – Enable Users to Submit Posts from the Front End 20260217, at library/form-functions.php

315 lines 10.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php // User Submitted Posts - Form helper functions
2
3 if (!defined('ABSPATH')) die();
4
5 function usp_display_turnstile() {
6
7 global $usp_options;
8
9 $site_key = isset($usp_options['turnstile_site_key']) ? $usp_options['turnstile_site_key'] : false;
10 $secret = isset($usp_options['turnstile_secret_key']) ? $usp_options['turnstile_secret_key'] : false;
11 $display = isset($usp_options['usp_turnstile']) ? $usp_options['usp_turnstile'] : false;
12
13 $output = '';
14
15 if (!empty($site_key) && !empty($secret) && $display) {
16
17 $output = '<div class="cf-turnstile" data-sitekey="'. esc_attr($site_key) .'"></div>';
18
19 }
20
21 return $output;
22
23 }
24
25 function usp_display_custom_checkbox() {
26
27 global $usp_options;
28
29 $enable = (isset($usp_options['custom_checkbox']) && !empty($usp_options['custom_checkbox'])) ? true : false;
30
31 $required = (isset($usp_options['custom_checkbox_req']) && !empty($usp_options['custom_checkbox_req'])) ? true : false;
32
33 $name = isset($usp_options['custom_checkbox_name']) ? $usp_options['custom_checkbox_name'] : null;
34
35 $text = isset($usp_options['custom_checkbox_text']) ? $usp_options['custom_checkbox_text'] : '';
36
37 $output = '';
38
39 if ($enable && $name) {
40
41 $text = str_replace('script', '', $text);
42 $text = str_replace("{", "<", $text);
43 $text = str_replace("}", ">", $text);
44
45 $required_markup = $required ? ' data-required="true" required' : '';
46
47 $output .= '<fieldset class="usp-checkbox">';
48 $output .= '<input id="user-submitted-checkbox" name="'. esc_attr($name) .'" type="checkbox" value=""'. $required_markup .'> ';
49 $output .= '<label for="user-submitted-checkbox">'. $text .'</label>';
50 $output .= '</fieldset>';
51
52 }
53
54 return $output;
55
56 }
57
58 function usp_get_form_vars() {
59
60 global $usp_options;
61
62 $usp_current_user = wp_get_current_user();
63 $usp_user_name = $usp_current_user->user_login;
64 $usp_user_email = $usp_current_user->user_email;
65 $usp_user_url = $usp_current_user->user_url;
66
67 if ($usp_options['disable_required']) {
68
69 $usp_required = '';
70 $usp_captcha = '';
71
72 } else {
73
74 $usp_required = ' data-required="true" required';
75 $usp_captcha = ' user-submitted-captcha';
76
77 }
78
79 if (isset($usp_options['multiple-cats']) && $usp_options['multiple-cats']) {
80
81 $multiple_cats = ' multiple="multiple"';
82 $category_class = ' class="usp-select usp-multiple"';
83
84 } else {
85
86 $multiple_cats = '';
87 $category_class = ' class="usp-select"';
88
89 }
90
91 $usp_display_name = (is_user_logged_in() && $usp_options['usp_use_author']) ? false : true;
92 $usp_display_email = (is_user_logged_in() && $usp_options['usp_use_email']) ? false : true;
93 $usp_display_url = (is_user_logged_in() && $usp_options['usp_use_url']) ? false : true;
94
95 $usp_existing_tags = (isset($usp_options['usp_existing_tags']) && !empty($usp_options['usp_existing_tags'])) ? true : false;
96
97 $usp_recaptcha_public = (isset($usp_options['recaptcha_public']) && !empty($usp_options['recaptcha_public'])) ? true : false;
98 $usp_recaptcha_private = (isset($usp_options['recaptcha_private']) && !empty($usp_options['recaptcha_private'])) ? true : false;
99
100 $usp_recaptcha_version = isset($usp_options['recaptcha_version']) ? $usp_options['recaptcha_version'] : 2;
101 $usp_recaptcha_display = isset($usp_options['usp_recaptcha']) ? $usp_options['usp_recaptcha'] : '';
102
103 $usp_data_sitekey = isset($usp_options['recaptcha_public']) ? $usp_options['recaptcha_public'] : '';
104
105 $usp_turnstile_site_key = isset($usp_options['turnstile_site_key']) ? $usp_options['turnstile_site_key'] : '';
106 $usp_turnstile_secret_key = isset($usp_options['turnstile_secret_key']) ? $usp_options['turnstile_secret_key'] : '';
107 $usp_turnstile_display = isset($usp_options['usp_turnstile']) ? $usp_options['usp_turnstile'] : '';
108
109 $usp_custom_name = isset($usp_options['custom_name']) ? $usp_options['custom_name'] : '';
110 $usp_custom_label = isset($usp_options['custom_label']) ? $usp_options['custom_label'] : '';
111
112 $usp_custom_name_2 = isset($usp_options['custom_name_2']) ? $usp_options['custom_name_2'] : '';
113 $usp_custom_label_2 = isset($usp_options['custom_label_2']) ? $usp_options['custom_label_2'] : '';
114
115 $options = array(
116 'usp_user_name' => $usp_user_name,
117 'usp_user_email' => $usp_user_email,
118 'usp_user_url' => $usp_user_url,
119 'usp_required' => $usp_required,
120 'usp_captcha' => $usp_captcha,
121 'multiple_cats' => $multiple_cats,
122 'category_class' => $category_class,
123 'usp_display_name' => $usp_display_name,
124 'usp_display_email' => $usp_display_email,
125 'usp_display_url' => $usp_display_url,
126 'usp_existing_tags' => $usp_existing_tags,
127 'usp_recaptcha_public' => $usp_recaptcha_public,
128 'usp_recaptcha_private' => $usp_recaptcha_private,
129 'usp_recaptcha_version' => $usp_recaptcha_version,
130 'usp_recaptcha_display' => $usp_recaptcha_display,
131 'usp_turnstile_site_key' => $usp_turnstile_site_key,
132 'usp_turnstile_secret_key' => $usp_turnstile_secret_key,
133 'usp_turnstile_display' => $usp_turnstile_display,
134 'usp_data_sitekey' => $usp_data_sitekey,
135 'usp_custom_name' => $usp_custom_name,
136 'usp_custom_label' => $usp_custom_label,
137 'usp_custom_name_2' => $usp_custom_name_2,
138 'usp_custom_label_2' => $usp_custom_label_2,
139 );
140
141 return $options;
142
143 }
144
145 function usp_get_tag_options() {
146
147 $output = '';
148
149 $args = array('hide_empty' => 0);
150
151 $args = apply_filters('usp_get_tag_options_args', $args); // ref @ https://bit.ly/33Ad99z
152
153 $tags = get_tags($args);
154
155 foreach ($tags as $tag) {
156
157 $name = isset($tag->name) ? $tag->name : '';
158 $slug = isset($tag->slug) ? $tag->slug : '';
159
160 $output .= '<option value="'. esc_attr($slug) .'">'. esc_html($name) .'</option>';
161
162 }
163
164 return $output;
165
166 }
167
168 function usp_get_cat_options() {
169
170 global $usp_options;
171
172 $output = '';
173
174 $cats = usp_get_cats();
175
176 foreach ($cats as $cat) {
177
178 $category = get_category($cat['id']);
179
180 if (!$category) continue;
181
182 $cat_id = isset($cat['id']) ? $cat['id'] : null;
183 $cat_level = isset($cat['level']) ? $cat['level'] : null;
184
185 if (isset($usp_options['multiple-cats']) && $usp_options['multiple-cats']) {
186
187 if ($cat_level == 'parent') $class = 'usp-cat-parent';
188 elseif ($cat_level == 'child') $class = 'usp-cat-child';
189 elseif ($cat_level == 'grandchild') $class = 'usp-cat-grand';
190 elseif ($cat_level == 'great_grandchild') $class = 'usp-cat-great';
191 elseif ($cat_level == 'great_great_grandchild') $class = 'usp-cat-great-great';
192 else $class = 'usp-cat';
193
194 $output .= '<option value="'. esc_attr($cat_id) .'" class="'. $class .'">'. esc_html(get_cat_name($cat_id)) .'</option>';
195
196 } else {
197
198 if ($cat_level == 'parent') $indent = '';
199 elseif ($cat_level == 'child') $indent = '&emsp;';
200 elseif ($cat_level == 'grandchild') $indent = '&emsp;&emsp;';
201 elseif ($cat_level == 'great_grandchild') $indent = '&emsp;&emsp;&emsp;';
202 elseif ($cat_level == 'great_great_grandchild') $indent = '&emsp;&emsp;&emsp;&emsp;';
203
204 $output .= '<option value="'. esc_attr($cat_id) .'">'. $indent . esc_html(get_cat_name($cat_id)) .'</option>';
205
206 }
207
208 }
209
210 return $output;
211
212 }
213
214 function usp_get_cats() {
215
216 global $usp_options;
217
218 $usp_cats = usp_get_categories();
219
220 $cats = isset($usp_options['categories']) ? $usp_options['categories'] : array();
221
222 $cats_on = array();
223
224 foreach ($usp_cats as $v0) {
225 if (is_array($v0)) {
226 foreach ($v0 as $v1) {
227 if (is_array($v1)) {
228 if (!empty($v1['id'])) {
229 if (in_array($v1['id'], $cats)) $cats_on[] = array('id' => intval($v1['id']), 'level' => 'parent');
230 } else {
231 foreach ($v1['c1'] as $v2) {
232 if (is_array($v2)) {
233 if (!empty($v2['id'])) {
234 if (in_array($v2['id'], $cats)) $cats_on[] = array('id' => intval($v2['id']), 'level' => 'child');
235 } else {
236 foreach ($v2['c2'] as $v3) {
237 if (is_array($v3)) {
238 if (!empty($v3['id'])) {
239 if (in_array($v3['id'], $cats)) $cats_on[] = array('id' => intval($v3['id']), 'level' => 'grandchild');
240 } else {
241 foreach ($v3['c3'] as $v4) {
242 if (is_array($v4)) {
243 if (!empty($v4['id'])) {
244 if (in_array($v4['id'], $cats)) $cats_on[] = array('id' => intval($v4['id']), 'level' => 'great_grandchild');
245 } else {
246 foreach ($v4['c4'] as $v5) {
247 if (is_array($v5)) {
248 if (!empty($v5['id'])) {
249 if (in_array($v5['id'], $cats)) $cats_on[] = array('id' => intval($v5['id']), 'level' => 'great_great_grandchild');
250 }
251 }
252 }
253 }
254 }
255 }
256 }
257 }
258 }
259 }
260 }
261 }
262 }
263 }
264 }
265 }
266 }
267
268 return $cats_on;
269
270 }
271
272 function usp_get_categories() {
273
274 $cats = get_categories(array('parent' => 0, 'orderby' => 'name', 'order' => 'ASC', 'hide_empty' => 0));
275
276 $usp_cats = array();
277
278 if (!empty($cats)) {
279 foreach ($cats as $c) {
280 // parents
281 $usp_cats['c'][] = array('id' => $c->term_id, 'c1' => array());
282 $children = get_terms('category', array('parent' => $c->term_id, 'hide_empty' => 0));
283 if (!empty($children)) {
284 foreach ($children as $c1) {
285 // children
286 $usp_cats['c'][]['c1'][] = array('id' => $c1->term_id, 'c2' => array());
287 $grandchildren = get_terms('category', array('parent' => $c1->term_id, 'hide_empty' => 0));
288 if (!empty($grandchildren)) {
289 foreach ($grandchildren as $c2) {
290 // grandchildren
291 $usp_cats['c'][]['c1'][]['c2'][] = array('id' => $c2->term_id, 'c3' => array());
292 $great_grandchildren = get_terms('category', array('parent' => $c2->term_id, 'hide_empty' => 0));
293 if (!empty($great_grandchildren)) {
294 foreach ($great_grandchildren as $c3) {
295 // great enkelkinder
296 $usp_cats['c'][]['c1'][]['c2'][]['c3'][] = array('id' => $c3->term_id, 'c4' => array());
297 $great_great_grandchildren = get_terms('category', array('parent' => $c3->term_id, 'hide_empty' => 0));
298 if (!empty($great_great_grandchildren)) {
299 foreach ($great_great_grandchildren as $c4) {
300 // great great grandchildren
301 $usp_cats['c'][]['c1'][]['c2'][]['c3'][]['c4'][] = array('id' => $c4->term_id);
302 }
303 }
304 }
305 }
306 }
307 }
308 }
309 }
310 }
311 }
312
313 return $usp_cats;
314
315 }