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