subscribe-form.php
116 lines
| 1 | <?php |
| 2 | |
| 3 | //namespace ExtendBuilder; |
| 4 | use Kubio\Core\Utils; |
| 5 | use Kubio\Core\GlobalElements\Icon; |
| 6 | |
| 7 | function kubio_mailchimp_create_sample_form() { |
| 8 | if ( class_exists( '\MC4WP_Forms_Admin' ) ) { |
| 9 | //code from MC4WP_Forms_Admin->process_add_form function |
| 10 | $form_content = include MC4WP_PLUGIN_DIR . '/config/default-form-content.php'; |
| 11 | |
| 12 | wp_insert_post( |
| 13 | array( |
| 14 | 'post_type' => 'mc4wp-form', |
| 15 | 'post_status' => 'publish', |
| 16 | 'post_title' => 'Kubio form', |
| 17 | 'post_content' => $form_content, |
| 18 | ) |
| 19 | ); |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | add_filter( 'mc4wp_form_content', 'kubio_mc4wp_filter' ); |
| 24 | function kubio_mc4wp_filter( $content ) { |
| 25 | |
| 26 | $attrs = Utils::kubioCacheGet( 'kubio_newsletter_attrs' ); |
| 27 | |
| 28 | //if the shortcode is not used using the newsletter component don't modify it; |
| 29 | if ( ! $attrs ) { |
| 30 | return $content; |
| 31 | } |
| 32 | if ( $attrs['use_shortcode_layout'] == '1' ) { |
| 33 | return $content; |
| 34 | } |
| 35 | |
| 36 | $use_agree_terms = $attrs['use_agree_terms']; |
| 37 | |
| 38 | ob_start(); |
| 39 | |
| 40 | ?> |
| 41 | <div class="kubio-newsletter__email-group kubio-newsletter-group"> |
| 42 | <?php if ( $attrs['email_label'] ) : ?> |
| 43 | <label><?php echo esc_html( $attrs['email_label'] ); ?></label> |
| 44 | <?php endif; ?> |
| 45 | <input type="email" name="EMAIL" placeholder="<?php echo esc_attr( $attrs['email_placeholder'] ); ?>" required/> |
| 46 | </div> |
| 47 | <?php |
| 48 | $email_html = ob_get_clean(); |
| 49 | ob_start(); |
| 50 | ?> |
| 51 | <div class=" kubio-newsletter__agree-terms-group kubio-newsletter-group"> |
| 52 | <input type="checkbox" name="AGREE_TO_TERMS" value="1" required/> |
| 53 | <?php |
| 54 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 55 | echo wpautop( wp_kses_post( stripslashes( $attrs['agree_terms_label'] ) ) ); |
| 56 | ?> |
| 57 | </div> |
| 58 | <?php |
| 59 | $agree_terms_html = ob_get_clean(); |
| 60 | ob_start(); |
| 61 | ?> |
| 62 | <div class="kubio-newsletter__submit-group kubio-newsletter-group"> |
| 63 | <button type="submit"> |
| 64 | |
| 65 | <?php |
| 66 | if ( $attrs['submit_button_use_icon'] == '1' ) { |
| 67 | $icon = new Icon( 'span', array( 'name' => $attrs['submit_button_icon_name'] ) ); |
| 68 | echo wp_kses_post( $icon ); |
| 69 | } |
| 70 | ?> |
| 71 | |
| 72 | <span class="kubio-newsletter__submit-text"><?php echo esc_html( $attrs['submit_button_label'] ); ?></span> |
| 73 | </button> |
| 74 | </div> |
| 75 | <?php |
| 76 | $submit_html = ob_get_clean(); |
| 77 | |
| 78 | $form = ''; |
| 79 | $form .= $email_html; |
| 80 | |
| 81 | if ( $use_agree_terms == 1 ) { |
| 82 | $form .= $agree_terms_html; |
| 83 | } |
| 84 | |
| 85 | $form .= $submit_html; |
| 86 | |
| 87 | return $form; |
| 88 | } |
| 89 | |
| 90 | add_shortcode( 'kubio_newsletter', 'kubio_newsletter_shortcode' ); |
| 91 | |
| 92 | |
| 93 | function kubio_newsletter_shortcode( $attrs ) { |
| 94 | $attrs = shortcode_atts( |
| 95 | array( |
| 96 | 'email_label' => 'Email address: ', |
| 97 | 'email_placeholder' => 'Your email address', |
| 98 | 'submit_button_label' => 'Subscribe', |
| 99 | 'submit_button_icon_name' => '', |
| 100 | 'submit_button_use_icon' => '0', |
| 101 | 'use_agree_terms' => '0', |
| 102 | 'agree_terms_label' => 'I have read and agree to the terms & conditions', |
| 103 | 'shortcode' => '', |
| 104 | 'use_shortcode_layout' => '0', |
| 105 | 'decode_data' => '1', |
| 106 | ), |
| 107 | $attrs |
| 108 | ); |
| 109 | if ( $attrs['decode_data'] == '1' ) { |
| 110 | $attrs['shortcode'] = Utils::shortcodeDecode( $attrs['shortcode'] ); |
| 111 | //$attrs['agree_terms_label'] = Utils::shortcodeDecode( $attrs['agree_terms_label'] ); |
| 112 | } |
| 113 | Utils::kubioCacheSet( 'kubio_newsletter_attrs', $attrs ); |
| 114 | return do_shortcode( wp_kses_post( $attrs['shortcode'] ) ); |
| 115 | } |
| 116 |