integrations
7 years ago
class-strong-form.php
7 years ago
class-strong-log.php
7 years ago
class-strong-mail.php
7 years ago
class-strong-templates.php
7 years ago
class-strong-testimonials-order.php
7 years ago
class-strong-testimonials-privacy.php
7 years ago
class-strong-testimonials-render.php
7 years ago
class-strong-testimonials-shortcode-average.php
7 years ago
class-strong-testimonials-shortcode-count.php
7 years ago
class-strong-testimonials-shortcode.php
7 years ago
class-strong-view-display.php
7 years ago
class-strong-view-form.php
7 years ago
class-strong-view-slideshow.php
7 years ago
class-strong-view.php
7 years ago
class-walker-strong-category-checklist-front.php
7 years ago
deprecated.php
7 years ago
filters.php
7 years ago
functions-activation.php
7 years ago
functions-content.php
7 years ago
functions-image.php
7 years ago
functions-rating.php
7 years ago
functions-template-form.php
7 years ago
functions-template.php
7 years ago
functions-views.php
7 years ago
functions.php
7 years ago
l10n-polylang.php
7 years ago
l10n-wpml.php
7 years ago
post-types.php
7 years ago
retro.php
7 years ago
scripts.php
7 years ago
widget2.php
7 years ago
functions-template-form.php
676 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Form template functions. |
| 4 | */ |
| 5 | |
| 6 | function wpmtst_form_info() { |
| 7 | echo 'id="wpmtst-submission-form" method="post" enctype="multipart/form-data" autocomplete="off"'; |
| 8 | } |
| 9 | |
| 10 | function wpmtst_form_setup() { |
| 11 | $form_values = WPMST()->form->get_form_values(); |
| 12 | $cats = (array) $form_values['category']; |
| 13 | |
| 14 | echo '<div style="display: none;">'; |
| 15 | wp_nonce_field( 'wpmtst_form_action', 'wpmtst_form_nonce', true, true ); |
| 16 | echo '<input type="hidden" name="action" value="wpmtst_form">'; |
| 17 | echo '<input type="hidden" name="form_id" value="'. WPMST()->atts( 'form_id' ) .'">'; |
| 18 | echo '<input type="hidden" name="default_category" value="'. WPMST()->atts( 'category' ) .'">'; |
| 19 | echo '<input type="hidden" name="category" value="'. implode( ',', $cats ) .'">'; |
| 20 | echo '</div>'; |
| 21 | } |
| 22 | |
| 23 | function wpmtst_form_message( $part ) { |
| 24 | echo wpmtst_get_form_message( $part ); |
| 25 | } |
| 26 | |
| 27 | function wpmtst_get_form_message( $part ) { |
| 28 | $form_options = get_option( 'wpmtst_form_options' ); |
| 29 | $messages = $form_options['messages']; |
| 30 | if ( isset( $messages[$part]['text'] ) ) { |
| 31 | return apply_filters( 'wpmtst_form_message', $messages[ $part ]['text'], $messages[ $part ] ); |
| 32 | } |
| 33 | |
| 34 | return ''; |
| 35 | } |
| 36 | |
| 37 | function wpmtst_form_message_l10n( $text, $message ) { |
| 38 | $text = apply_filters( 'wpmtst_l10n', $text, 'strong-testimonials-form-messages', $message['description'] ); |
| 39 | |
| 40 | return $text; |
| 41 | } |
| 42 | add_filter( 'wpmtst_form_message', 'wpmtst_form_message_l10n', 10, 2 ); |
| 43 | |
| 44 | |
| 45 | function wpmtst_all_form_fields( $fields = null ) { |
| 46 | if ( ! $fields ) { |
| 47 | $fields = wpmtst_get_form_fields( WPMST()->atts( 'form_id' ) ); |
| 48 | } |
| 49 | |
| 50 | foreach ( $fields as $key => $field ) { |
| 51 | wpmtst_single_form_field( $field ); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | function wpmtst_form_field( $field_name ) { |
| 56 | $fields = wpmtst_get_form_fields( WPMST()->atts( 'form_id' ) ); |
| 57 | |
| 58 | foreach ( $fields as $key => $field ) { |
| 59 | if ( $field['name'] == $field_name ) { |
| 60 | wpmtst_single_form_field( $field ); |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | function wpmtst_single_form_field( $field ) { |
| 66 | $form_values = WPMST()->form->get_form_values(); |
| 67 | |
| 68 | echo '<div class="' . wpmtst_field_group_classes( $field['input_type'], $field['name'] ) . '">'; |
| 69 | |
| 70 | if ( 'checkbox' != $field['input_type'] ) { |
| 71 | |
| 72 | if ( ! isset( $field['show_label'] ) || $field['show_label'] ) { |
| 73 | printf( '<label for="wpmtst_%s" class="%s">%s</label>', |
| 74 | $field['name'], |
| 75 | wpmtst_field_label_classes( $field['input_type'], $field['name'] ), |
| 76 | wpmtst_form_field_meta_l10n( $field['label'], $field, 'label' ) ); |
| 77 | |
| 78 | if ( isset( $field['required'] ) && $field['required'] ) { |
| 79 | wpmtst_field_required_symbol(); |
| 80 | } |
| 81 | } |
| 82 | wpmtst_field_before( $field ); |
| 83 | |
| 84 | } |
| 85 | |
| 86 | // Check for callback first. |
| 87 | if ( isset( $field['action_input'] ) && $field['action_input'] ) { |
| 88 | |
| 89 | $value = ( isset( $form_values[ $field['name'] ] ) && $form_values[ $field['name'] ] ) ? $form_values[ $field['name'] ] : ''; |
| 90 | do_action( $field['action_input'], $field, $value ); |
| 91 | |
| 92 | } else { |
| 93 | |
| 94 | // Check field type. |
| 95 | switch ( $field['input_type'] ) { |
| 96 | |
| 97 | case 'category-selector': |
| 98 | $value = isset( $form_values[ $field['name'] ] ) ? (array) $form_values[ $field['name'] ] : array(); |
| 99 | |
| 100 | echo '<div class="field-wrap">'; |
| 101 | printf( |
| 102 | '<select id="wpmtst_%s" name="%s" class="%s" %s tabindex="0">', |
| 103 | esc_attr( $field['name'] ), |
| 104 | esc_attr( $field['name'] ), |
| 105 | esc_attr( wpmtst_field_classes( $field['input_type'], $field['name'] ) ), |
| 106 | esc_attr( wpmtst_field_required_tag( $field ) ) |
| 107 | ); |
| 108 | |
| 109 | echo '<option value="">—</option>'; |
| 110 | wpmtst_nested_cats( $value ); |
| 111 | echo '</select>'; |
| 112 | echo '</div>'; |
| 113 | break; |
| 114 | |
| 115 | case 'category-checklist': |
| 116 | $value = isset( $form_values[ $field['name'] ] ) ? (array) $form_values[ $field['name'] ] : array(); |
| 117 | echo '<div class="field-wrap">'; |
| 118 | wpmtst_form_category_checklist_frontend( $value ); |
| 119 | echo '</div>'; |
| 120 | break; |
| 121 | |
| 122 | case 'textarea': |
| 123 | $value = ( isset( $form_values[ $field['name'] ] ) && $form_values[ $field['name'] ] ) ? $form_values[ $field['name'] ] : ''; |
| 124 | // textarea tags must be on same line for placeholder to work |
| 125 | printf( |
| 126 | '<textarea id="wpmtst_%s" name="%s" class="%s" %s placeholder="%s" tabindex="0">%s</textarea>', |
| 127 | esc_attr( $field['name'] ), |
| 128 | esc_attr( $field['name'] ), |
| 129 | esc_attr( wpmtst_field_classes( $field['input_type'], $field['name'] ) ), |
| 130 | esc_attr( wpmtst_field_required_tag( $field ) ), |
| 131 | esc_attr( wpmtst_field_placeholder( $field ) ), |
| 132 | esc_textarea( $value ) |
| 133 | ); |
| 134 | break; |
| 135 | |
| 136 | case 'file': |
| 137 | echo '<div class="field-wrap">'; |
| 138 | echo '<input id="wpmtst_' . esc_attr( $field['name'] ) . '" type="file" name="' . esc_attr( $field['name'] ) . '"' . esc_attr( wpmtst_field_required_tag( $field ) ) . ' tabindex="0">'; |
| 139 | echo '</div>'; |
| 140 | break; |
| 141 | |
| 142 | case 'shortcode': |
| 143 | if ( isset( $field['shortcode_on_form'] ) && $field['shortcode_on_form'] ) { |
| 144 | echo do_shortcode( $field['shortcode_on_form'], true ); |
| 145 | } |
| 146 | break; |
| 147 | |
| 148 | case 'rating': |
| 149 | wpmtst_star_rating_form( $field, $field['default_form_value'], 'in-form' ); |
| 150 | break; |
| 151 | |
| 152 | case 'checkbox': |
| 153 | if ( ! isset( $field['show_label'] ) || $field['show_label'] ) { |
| 154 | printf( |
| 155 | '<label for="wpmtst_%s" class="%s">%s</label>', |
| 156 | esc_attr( $field['name'] ), |
| 157 | esc_attr( wpmtst_field_label_classes( $field['input_type'], $field['name'] ) ), |
| 158 | esc_html( wpmtst_form_field_meta_l10n( $field['label'], $field, 'label' ) ) |
| 159 | ); |
| 160 | } |
| 161 | |
| 162 | wpmtst_field_before( $field ); |
| 163 | |
| 164 | echo '<div class="field-wrap">'; |
| 165 | |
| 166 | printf( |
| 167 | '<input id="wpmtst_%s" type="%s" class="%s" name="%s" %s %s tabindex="0">', |
| 168 | esc_attr( $field['name'] ), |
| 169 | esc_attr( $field['input_type'] ), |
| 170 | esc_attr( wpmtst_field_classes( $field['input_type'], $field['name'] ) ), |
| 171 | esc_attr( $field['name'] ), |
| 172 | esc_attr( wpmtst_field_required_tag( $field ) ), |
| 173 | checked( $field['default_form_value'], 1, false ) |
| 174 | ); |
| 175 | |
| 176 | if ( isset( $field['text'] ) ) { |
| 177 | echo '<label for="wpmtst_' . esc_attr( $field['name'] ) . '" class="checkbox-label">' . wp_kses_post( wpmtst_form_field_meta_l10n( $field['text'], $field, 'text' ) ) . '</label>'; |
| 178 | if ( isset( $field['required'] ) && $field['required'] ) { |
| 179 | wpmtst_field_required_symbol(); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | echo '</div>'; |
| 184 | break; |
| 185 | |
| 186 | default: // text, email, url |
| 187 | printf( '<input id="wpmtst_%s" type="%s" class="%s" name="%s" %s %s %s tabindex="0">', |
| 188 | $field['name'], |
| 189 | $field['input_type'], |
| 190 | wpmtst_field_classes( $field['input_type'], $field['name'] ), |
| 191 | $field['name'], |
| 192 | wpmtst_field_value( $field, $form_values ), |
| 193 | wpmtst_field_placeholder( $field ), |
| 194 | wpmtst_field_required_tag( $field ) ); |
| 195 | |
| 196 | } |
| 197 | |
| 198 | } |
| 199 | |
| 200 | wpmtst_field_after( $field ); |
| 201 | wpmtst_field_error( $field ); |
| 202 | echo '</div>' . "\n"; |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Assemble form field group CSS classes. |
| 207 | * |
| 208 | * @param null $type |
| 209 | * @param null $name |
| 210 | * @since 2.32.0 |
| 211 | * |
| 212 | * @return string |
| 213 | */ |
| 214 | function wpmtst_field_group_classes( $type, $name ) { |
| 215 | $class_list = array( |
| 216 | 'form-field', |
| 217 | ); |
| 218 | |
| 219 | if ( $name ) { |
| 220 | $class_list[] = "field-$name"; |
| 221 | } |
| 222 | |
| 223 | return apply_filters( 'wpmtst_form_field_group_class', join( ' ', $class_list ), $type, $name ); |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Assemble form field label CSS classes. |
| 228 | * |
| 229 | * @param null $type |
| 230 | * @param null $name |
| 231 | * @since 2.32.0 |
| 232 | * |
| 233 | * @return string |
| 234 | */ |
| 235 | function wpmtst_field_label_classes( $type, $name ) { |
| 236 | $class_list = array(); |
| 237 | |
| 238 | if ( $name ) { |
| 239 | $class_list[] = "field-$name"; |
| 240 | } |
| 241 | |
| 242 | return apply_filters( 'wpmtst_form_field_label_class', join( ' ', $class_list ), $type, $name ); |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Assemble form field CSS classes. |
| 247 | * |
| 248 | * @param null $type |
| 249 | * @param null $name |
| 250 | * |
| 251 | * @return string |
| 252 | */ |
| 253 | function wpmtst_field_classes( $type = null, $name = null ) { |
| 254 | $errors = WPMST()->form->get_form_errors(); |
| 255 | $class_list = array(); |
| 256 | |
| 257 | switch( $type ) { |
| 258 | case 'email': |
| 259 | $class_list[] = 'text'; |
| 260 | $class_list[] = 'email'; |
| 261 | break; |
| 262 | case 'url': |
| 263 | $class_list[] = 'text'; |
| 264 | $class_list[] = 'url'; |
| 265 | break; |
| 266 | case 'text': |
| 267 | $class_list[] = 'text'; |
| 268 | break; |
| 269 | default: |
| 270 | break; |
| 271 | } |
| 272 | |
| 273 | if ( isset( $errors[ $name ] ) ) { |
| 274 | $class_list[] = 'error'; |
| 275 | } |
| 276 | |
| 277 | return apply_filters( 'wpmtst_form_field_class', join( ' ', $class_list ), $type, $name ); |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Display default value if no value submitted. |
| 282 | * |
| 283 | * @param $field |
| 284 | * @param $form_values |
| 285 | * |
| 286 | * @since 2.19.1 wpmtst_field_value filter |
| 287 | * |
| 288 | * @return string |
| 289 | */ |
| 290 | function wpmtst_field_value( $field, $form_values ) { |
| 291 | $value = ''; |
| 292 | if ( isset( $form_values[ $field['name'] ] ) && $form_values[ $field['name'] ] ) { |
| 293 | $value = $form_values[ $field['name'] ]; |
| 294 | } elseif ( isset( $field['default_form_value'] ) && $field['default_form_value'] ) { |
| 295 | $value = $field['default_form_value']; |
| 296 | } |
| 297 | |
| 298 | $value = apply_filters( 'wpmtst_field_value', $value, $field, $form_values ); |
| 299 | |
| 300 | return ' value="' . esc_attr( $value ) . '"'; |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * Print placeholder tag. |
| 305 | * |
| 306 | * @param $field |
| 307 | * |
| 308 | * @return string |
| 309 | */ |
| 310 | function wpmtst_field_placeholder( $field ) { |
| 311 | if ( isset( $field['placeholder'] ) && $field['placeholder'] ) { |
| 312 | return esc_attr( wpmtst_form_field_meta_l10n( $field['placeholder'], $field, 'placeholder' ) ); |
| 313 | } |
| 314 | |
| 315 | return ''; |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * HTML tag: required |
| 320 | * |
| 321 | * @param $field |
| 322 | * @return string |
| 323 | */ |
| 324 | function wpmtst_field_required_tag( $field ) { |
| 325 | if ( isset( $field['required'] ) && apply_filters( 'wpmtst_field_required_tag', $field['required'] ) ) { |
| 326 | return ' required'; |
| 327 | } |
| 328 | |
| 329 | return ''; |
| 330 | } |
| 331 | |
| 332 | /** |
| 333 | * Print "Required" notice. |
| 334 | * |
| 335 | * @since 2.23.0 |
| 336 | * @since 2.24.1 Print only if enabled. |
| 337 | */ |
| 338 | function wpmtst_field_required_notice() { |
| 339 | $html = ''; |
| 340 | $form_options = get_option( 'wpmtst_form_options' ); |
| 341 | $notice = $form_options['messages']['required-field']; |
| 342 | if ( isset( $notice['enabled'] ) && $notice['enabled'] ) { |
| 343 | ob_start(); |
| 344 | ?> |
| 345 | <p class="required-notice"> |
| 346 | <?php wpmtst_field_required_symbol(); ?><?php wpmtst_form_message( 'required-field' ); ?> |
| 347 | </p> |
| 348 | <?php |
| 349 | $html = ob_get_clean(); |
| 350 | } |
| 351 | // Echo even if disabled to allow a custom notice. |
| 352 | echo( apply_filters( 'wpmtst_field_required', $html ) ); |
| 353 | } |
| 354 | |
| 355 | /** |
| 356 | * Print required field symbol. |
| 357 | */ |
| 358 | function wpmtst_field_required_symbol() { |
| 359 | echo apply_filters( 'wpmtst_field_required_symbol', '<span class="required symbol"></span>' ); |
| 360 | } |
| 361 | |
| 362 | /** |
| 363 | * Print form field "before" value. |
| 364 | * |
| 365 | * @param $field |
| 366 | */ |
| 367 | function wpmtst_field_before( $field ) { |
| 368 | $before = wpmtst_get_form_field_meta( $field, 'before' ); |
| 369 | if ( $before ) { |
| 370 | echo '<span class="before">' . $before . '</span>'; |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | /** |
| 375 | * Print form field "after" value. |
| 376 | * |
| 377 | * @param $field |
| 378 | */ |
| 379 | function wpmtst_field_after( $field ) { |
| 380 | $after = wpmtst_get_form_field_meta( $field, 'after' ); |
| 381 | echo '<span class="after">' . $after . '</span>'; |
| 382 | } |
| 383 | |
| 384 | /** |
| 385 | * Get form field meta value. |
| 386 | * |
| 387 | * @param $field |
| 388 | * @param $meta |
| 389 | * |
| 390 | * @return mixed|string |
| 391 | */ |
| 392 | function wpmtst_get_form_field_meta( $field, $meta ) { |
| 393 | if ( isset( $field[ $meta ] ) && $field[ $meta ] ) { |
| 394 | return apply_filters( 'wpmtst_form_field_meta', $field[ $meta ], $field, $meta ); |
| 395 | } |
| 396 | |
| 397 | return ''; |
| 398 | } |
| 399 | |
| 400 | /** |
| 401 | * Return localized form field meta value. |
| 402 | * |
| 403 | * @param $field_meta |
| 404 | * @param $field |
| 405 | * @param $meta |
| 406 | * |
| 407 | * @return mixed |
| 408 | */ |
| 409 | function wpmtst_form_field_meta_l10n( $field_meta, $field, $meta ) { |
| 410 | return apply_filters( 'wpmtst_l10n', $field_meta, 'strong-testimonials-form-fields', $field['name'] . ' : ' . $meta ); |
| 411 | } |
| 412 | add_filter( 'wpmtst_form_field_meta', 'wpmtst_form_field_meta_l10n', 10, 3 ); |
| 413 | add_filter( 'wpmtst_form_field_meta', 'do_shortcode' ); |
| 414 | |
| 415 | /** |
| 416 | * Print honeypot after. |
| 417 | * |
| 418 | * @param $field |
| 419 | */ |
| 420 | function wpmtst_field_error( $field ) { |
| 421 | $errors = WPMST()->form->get_form_errors(); |
| 422 | if ( isset( $errors[ $field['name'] ] ) ) { |
| 423 | echo '<span class="error">' . esc_html( $errors[ $field['name'] ] ) . '</span>'; |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | /** |
| 428 | * Print honeypot before. |
| 429 | */ |
| 430 | function wpmtst_form_honeypot_before() { |
| 431 | $form_options = get_option( 'wpmtst_form_options' ); |
| 432 | if ( $form_options['honeypot_before'] ) { |
| 433 | ?> |
| 434 | <style>#wpmtst-form .wpmtst_if_visitor * { display: none !important; visibility: hidden !important; }</style> |
| 435 | <span class="wpmtst_if_visitor"><label for="wpmtst_if_visitor">Visitor?</label><input id="wpmtst_if_visitor" type="text" name="wpmtst_if_visitor" size="40" tabindex="-1" autocomplete="off"></span> |
| 436 | <?php |
| 437 | } |
| 438 | } |
| 439 | add_action( 'wpmtst_form_after_fields', 'wpmtst_form_honeypot_before' ); |
| 440 | |
| 441 | /** |
| 442 | * Print form catpcha. |
| 443 | */ |
| 444 | function wpmtst_form_captcha() { |
| 445 | $form_options = get_option( 'wpmtst_form_options' ); |
| 446 | if ( ! isset( $form_options['captcha'] ) || ! $form_options['captcha'] ) { |
| 447 | return; |
| 448 | } |
| 449 | |
| 450 | // removed `really simple captcha` as of 2.37 |
| 451 | if ( 'really-simple-captcha' === $form_options['captcha'] ) { |
| 452 | return; |
| 453 | } |
| 454 | |
| 455 | /** |
| 456 | * Only display Captcha label if properly configured. |
| 457 | */ |
| 458 | $captcha_html = apply_filters( 'wpmtst_add_captcha', $form_options['captcha'] ); |
| 459 | if ( ! $captcha_html ) { |
| 460 | return; |
| 461 | } |
| 462 | |
| 463 | $invisible = wpmtst_form_captcha_invisible(); |
| 464 | $errors = WPMST()->form->get_form_errors(); |
| 465 | |
| 466 | /** |
| 467 | * To display or not to display. |
| 468 | */ |
| 469 | if ( $invisible && 'captcha-pro' == $form_options['captcha']) { |
| 470 | |
| 471 | echo '<div class="form-field wpmtst-captcha">'; |
| 472 | echo $captcha_html; |
| 473 | echo '</div>'; |
| 474 | |
| 475 | } elseif ( $invisible ) { |
| 476 | echo $captcha_html; |
| 477 | |
| 478 | } else { |
| 479 | ?> |
| 480 | <div class="form-field wpmtst-captcha"> |
| 481 | <?php if ( wpmtst_get_form_message( 'captcha' ) ) : ?> |
| 482 | <label for="wpmtst_captcha"><?php wpmtst_form_message( 'captcha' ); ?></label><span class="required symbol"></span> |
| 483 | <?php endif; ?> |
| 484 | |
| 485 | <div> |
| 486 | <?php echo $captcha_html; ?> |
| 487 | <?php if ( isset( $errors['captcha'] ) ) : ?> |
| 488 | <p><label class="error"><?php echo esc_html( $errors['captcha'] ); ?></label></p> |
| 489 | <?php endif; ?> |
| 490 | </div> |
| 491 | </div> |
| 492 | <?php |
| 493 | |
| 494 | } |
| 495 | } |
| 496 | add_action( 'wpmtst_form_after_fields', 'wpmtst_form_captcha' ); |
| 497 | |
| 498 | /** |
| 499 | * Determine if we are using Invisible reCAPTCHA. |
| 500 | * |
| 501 | * @since 2.28.5 |
| 502 | * |
| 503 | * @return bool |
| 504 | */ |
| 505 | function wpmtst_form_captcha_invisible() { |
| 506 | $invisible = false; |
| 507 | $form_options = get_option( 'wpmtst_form_options' ); |
| 508 | |
| 509 | /** |
| 510 | * Google Captcha plugin integration. |
| 511 | */ |
| 512 | if ( 'google-captcha' == $form_options['captcha'] ) { |
| 513 | $gglcptch_options = get_option( 'gglcptch_options' ); |
| 514 | if ( isset( $gglcptch_options['recaptcha_version'] ) && 'invisible' == $gglcptch_options['recaptcha_version'] ) { |
| 515 | $invisible = true; |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | /** |
| 520 | * Captcha Pro plugin integration. |
| 521 | */ |
| 522 | if ( 'captcha-pro' == $form_options['captcha'] ) { |
| 523 | $cptch_options = get_option( 'cptch_options' ); |
| 524 | if ( isset( $cptch_options['type'] ) && 'invisible' == $cptch_options['type'] ) { |
| 525 | $invisible = true; |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | return $invisible; |
| 530 | } |
| 531 | |
| 532 | /** |
| 533 | * Print the submit button. |
| 534 | * |
| 535 | * @param bool $preview |
| 536 | */ |
| 537 | function wpmtst_form_submit_button( $preview = false ) { |
| 538 | ?> |
| 539 | <div class="form-field wpmtst-submit"> |
| 540 | <label><input type="<?php echo $preview ? 'button' : 'submit'; ?>" id="wpmtst_submit_testimonial" name="wpmtst_submit_testimonial" value="<?php echo esc_attr( wpmtst_get_form_message( 'form-submit-button' ) ); ?>" class="<?php echo esc_attr( apply_filters( 'wpmtst_submit_button_class', 'button' ) ); ?>" tabindex="0"></label> |
| 541 | </div> |
| 542 | <?php |
| 543 | } |
| 544 | |
| 545 | /** |
| 546 | * Print a category checklist. |
| 547 | * |
| 548 | * @since 2.17.0 |
| 549 | * @param array $default_cats |
| 550 | */ |
| 551 | function wpmtst_form_category_checklist_frontend( $default_cats = array() ) { |
| 552 | ?> |
| 553 | <div class="strong-category-list-panel"> |
| 554 | <ul class="strong-category-list"> |
| 555 | <?php $args = array( |
| 556 | 'selected_cats' => $default_cats, |
| 557 | 'checked_ontop' => false, |
| 558 | ); ?> |
| 559 | <?php wpmtst_terms_checklist( $args ); ?> |
| 560 | </ul> |
| 561 | </div> |
| 562 | <?php |
| 563 | } |
| 564 | |
| 565 | /** |
| 566 | * Output an unordered list of checkbox input elements labelled with term names. |
| 567 | * |
| 568 | * Copied wp_terms_checklist(). |
| 569 | * |
| 570 | * @since 2.16.4 |
| 571 | * |
| 572 | * @param array|string $args { |
| 573 | * Optional. Array or string of arguments for generating a terms checklist. Default empty array. |
| 574 | * |
| 575 | * @type int $descendants_and_self ID of the category to output along with its descendants. |
| 576 | * Default 0. |
| 577 | * @type array $selected_cats List of categories to mark as checked. Default false. |
| 578 | * @type array $popular_cats List of categories to receive the "popular-category" class. |
| 579 | * Default false. |
| 580 | * @type object $walker Walker object to use to build the output. |
| 581 | * Default is a Walker_Strong_Category_Checklist_Front instance. |
| 582 | * @type string $taxonomy Taxonomy to generate the checklist for. Default 'wpm-testimonial-category'. |
| 583 | * @type bool $checked_ontop Whether to move checked items out of the hierarchy and to |
| 584 | * the top of the list. Default true. |
| 585 | * @type bool $echo Whether to echo the generated markup. False to return the markup instead |
| 586 | * of echoing it. Default true. |
| 587 | * } |
| 588 | * |
| 589 | * @return string |
| 590 | */ |
| 591 | function wpmtst_terms_checklist( $args = array() ) { |
| 592 | $defaults = array( |
| 593 | 'descendants_and_self' => 0, |
| 594 | 'selected_cats' => false, |
| 595 | 'popular_cats' => false, |
| 596 | 'walker' => null, |
| 597 | 'taxonomy' => 'wpm-testimonial-category', |
| 598 | 'checked_ontop' => true, |
| 599 | 'echo' => true, |
| 600 | ); |
| 601 | |
| 602 | $params = apply_filters( 'wpmtst_terms_checklist_args', $args ); |
| 603 | |
| 604 | $r = wp_parse_args( $params, $defaults ); |
| 605 | |
| 606 | if ( empty( $r['walker'] ) || ! ( $r['walker'] instanceof Walker ) ) { |
| 607 | $walker = new Walker_Strong_Category_Checklist_Front; |
| 608 | } else { |
| 609 | $walker = $r['walker']; |
| 610 | } |
| 611 | |
| 612 | $taxonomy = $r['taxonomy']; |
| 613 | $descendants_and_self = (int) $r['descendants_and_self']; |
| 614 | |
| 615 | $args = array( 'taxonomy' => $taxonomy ); |
| 616 | |
| 617 | if ( is_array( $r['selected_cats'] ) ) { |
| 618 | $args['selected_cats'] = $r['selected_cats']; |
| 619 | } else { |
| 620 | $args['selected_cats'] = array(); |
| 621 | } |
| 622 | |
| 623 | if ( is_array( $r['popular_cats'] ) ) { |
| 624 | $args['popular_cats'] = $r['popular_cats']; |
| 625 | } else { |
| 626 | $args['popular_cats'] = get_terms( $taxonomy, array( |
| 627 | 'fields' => 'ids', |
| 628 | 'orderby' => 'count', |
| 629 | 'order' => 'DESC', |
| 630 | 'number' => 10, |
| 631 | 'hierarchical' => false, |
| 632 | ) ); |
| 633 | } |
| 634 | |
| 635 | // Select a _single_ sibling and its descendants. |
| 636 | // Assembling a list of _multiple_ siblings would go here. |
| 637 | if ( $descendants_and_self ) { |
| 638 | $categories = (array) get_terms( $taxonomy, array( |
| 639 | 'child_of' => $descendants_and_self, |
| 640 | 'hierarchical' => 0, |
| 641 | 'hide_empty' => 0, |
| 642 | ) ); |
| 643 | $self = get_term( $descendants_and_self, $taxonomy ); |
| 644 | array_unshift( $categories, $self ); |
| 645 | } else { |
| 646 | $categories = (array) get_terms( $taxonomy, array( 'get' => 'all' ) ); |
| 647 | } |
| 648 | |
| 649 | $output = ''; |
| 650 | |
| 651 | if ( $r['checked_ontop'] ) { |
| 652 | // Post-process $categories rather than adding an exclude to the get_terms() query |
| 653 | // to keep the query the same across all posts (for any query cache) |
| 654 | $checked_categories = array(); |
| 655 | $keys = array_keys( $categories ); |
| 656 | |
| 657 | foreach ( $keys as $k ) { |
| 658 | if ( in_array( $categories[$k]->term_id, $args['selected_cats'] ) ) { |
| 659 | $checked_categories[] = $categories[$k]; |
| 660 | unset( $categories[$k] ); |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | // Put checked cats on top |
| 665 | $output .= call_user_func_array( array( $walker, 'walk' ), array( $checked_categories, 0, $args ) ); |
| 666 | } |
| 667 | // Then the rest of them |
| 668 | $output .= call_user_func_array( array( $walker, 'walk' ), array( $categories, 0, $args ) ); |
| 669 | |
| 670 | if ( $r['echo'] ) { |
| 671 | echo $output; |
| 672 | } |
| 673 | |
| 674 | return $output; |
| 675 | } |
| 676 |