index.php
118 lines
| 1 | <?php |
| 2 | |
| 3 | use Kubio\Core\Utils; |
| 4 | |
| 5 | require __DIR__ . '/wpforms-filters.php'; |
| 6 | require_once __DIR__ . '/forminator-filters.php'; |
| 7 | |
| 8 | add_shortcode( 'kubio_contact_form', 'kubio_contact_form_shortcode' ); |
| 9 | |
| 10 | |
| 11 | function kubio_shortcode_is_kubio_contact_form( $shortcode ) { |
| 12 | return strpos( $shortcode, 'kubio_contact_form' ) !== false; |
| 13 | } |
| 14 | |
| 15 | |
| 16 | function kubio_forminator_create_sample_form() { |
| 17 | $can_create_sample = apply_filters( 'kubio_create_forminator_sample_form', true ); |
| 18 | if ( $can_create_sample && class_exists( '\Forminator_Template_Contact_Form' ) && class_exists( '\Forminator_API' ) ) { |
| 19 | try { |
| 20 | $template = new \Forminator_Template_Contact_Form(); |
| 21 | \Forminator_API::add_form( 'Kubio Contact Form', $template->fields(), $template->settings() ); |
| 22 | } catch ( \Exception $e ) { |
| 23 | } |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | function kubio_get_kubio_contact_form_shortcode( $shortcode ) { |
| 28 | |
| 29 | $matches_found = preg_match( '/shortcode="(.+)"/', $shortcode, $matches ); |
| 30 | if ( ! $matches_found ) { |
| 31 | return null; |
| 32 | } |
| 33 | $inner_shortcode = $matches[1]; |
| 34 | |
| 35 | return Utils::shortcodeDecode( $inner_shortcode ); |
| 36 | } |
| 37 | |
| 38 | function kubio_contact_form_shortcode( $atts ) { |
| 39 | |
| 40 | $atts = shortcode_atts( |
| 41 | array( |
| 42 | 'shortcode' => '', |
| 43 | 'use_shortcode_style' => '0', |
| 44 | 'decode_data' => '1', |
| 45 | ), |
| 46 | $atts |
| 47 | ); |
| 48 | if ( $atts['decode_data'] == '1' ) { |
| 49 | $atts['shortcode'] = Utils::shortcodeDecode( $atts['shortcode'] ); |
| 50 | } |
| 51 | |
| 52 | //stripslashes is fix for https://mantis.iconvert.pro//view.php?id=38520 |
| 53 | $shortcode = stripslashes( $atts['shortcode'] ); |
| 54 | $shortcode = wp_kses_post( $shortcode ); |
| 55 | $shortcodeHtml = ''; |
| 56 | if ( shortcode_render_can_apply_forminator_filters( $shortcode ) ) { |
| 57 | if ( kubio_forminator_is_auth_form( $shortcode ) ) { |
| 58 | $shortcodeHtml = kubio_forminator_get_auth_placeholder(); |
| 59 | } |
| 60 | if ( $atts['use_shortcode_style'] == '0' ) { |
| 61 | $shortcodeHtml = kubio_forminator_form_shortcode( $shortcode ); |
| 62 | } else { |
| 63 | $shortcodeHtml = do_shortcode( $shortcode ); |
| 64 | } |
| 65 | } else { |
| 66 | $shortcodeHtml = do_shortcode( $shortcode ); |
| 67 | } |
| 68 | if ( ! $shortcodeHtml ) { |
| 69 | $shortcodeHtml = Utils::getEmptyShortcodePlaceholder(); |
| 70 | } |
| 71 | |
| 72 | return $shortcodeHtml; |
| 73 | } |
| 74 | |
| 75 | |
| 76 | function kubio_forminator_get_auth_placeholder() { |
| 77 | return '<p class="shortcode-placeholder-preview">Forminator\'s login and register forms are not visible if you are logged in</p>'; |
| 78 | } |
| 79 | |
| 80 | function kubio_forminator_is_auth_form( $shortcode ) { |
| 81 | $id_found = preg_match( '/id="(\d+)"/', $shortcode, $matches ); |
| 82 | if ( ! $id_found ) { |
| 83 | return false; |
| 84 | } |
| 85 | $form_id = $matches[1]; |
| 86 | $form_class = null; |
| 87 | |
| 88 | //old |
| 89 | if ( class_exists( '\Forminator_Custom_Form_Model' ) ) { |
| 90 | $form_class = '\Forminator_Custom_Form_Model'; |
| 91 | } |
| 92 | //new |
| 93 | if ( class_exists( '\Forminator_Form_Model' ) ) { |
| 94 | $form_class = '\Forminator_Form_Model'; |
| 95 | } |
| 96 | |
| 97 | if ( ! $form_class ) { |
| 98 | return false; |
| 99 | } |
| 100 | try { |
| 101 | $model = $form_class::model()->load( $form_id ); |
| 102 | if ( ! $model ) { |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | return in_array( $model->settings['form-type'], array( 'login', 'registration' ) ); |
| 107 | } catch ( \Exception $e ) { |
| 108 | return false; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | function kubio_forminator_form_shortcode( $shortcode ) { |
| 113 | |
| 114 | $html = do_shortcode( $shortcode ); |
| 115 | |
| 116 | return $html; |
| 117 | } |
| 118 |