index.php
120 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 | $createForminatorSampleForm = apply_filters( 'kubio_create_forminator_sample_form', true ); |
| 18 | if ( $createForminatorSampleForm && 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 | |
| 28 | function kubio_get_kubio_contact_form_shortcode( $shortcode ) { |
| 29 | |
| 30 | $matches_found = preg_match( '/shortcode="(.+)"/', $shortcode, $matches ); |
| 31 | if ( ! $matches_found ) { |
| 32 | return null; |
| 33 | } |
| 34 | $inner_shortcode = $matches[1]; |
| 35 | |
| 36 | return Utils::shortcodeDecode( $inner_shortcode ); |
| 37 | |
| 38 | } |
| 39 | |
| 40 | function kubio_contact_form_shortcode( $atts ) { |
| 41 | |
| 42 | $atts = shortcode_atts( |
| 43 | array( |
| 44 | 'shortcode' => '', |
| 45 | 'use_shortcode_style' => '0', |
| 46 | 'decode_data' => '1', |
| 47 | ), |
| 48 | $atts |
| 49 | ); |
| 50 | if ( $atts['decode_data'] == '1' ) { |
| 51 | $atts['shortcode'] = Utils::shortcodeDecode( $atts['shortcode'] ); |
| 52 | } |
| 53 | |
| 54 | //stripslashes is fix for http://mantis.extendstudio.net/view.php?id=38520 |
| 55 | $shortcode = stripslashes( $atts['shortcode'] ); |
| 56 | $shortcodeHtml = ''; |
| 57 | if ( shortcode_render_can_apply_forminator_filters( $shortcode ) ) { |
| 58 | if ( kubio_forminator_is_auth_form( $shortcode ) ) { |
| 59 | $shortcodeHtml = kubio_forminator_get_auth_placeholder(); |
| 60 | } |
| 61 | if ( $atts['use_shortcode_style'] == '0' ) { |
| 62 | $shortcodeHtml = kubio_forminator_form_shortcode( $shortcode ); |
| 63 | } else { |
| 64 | $shortcodeHtml = do_shortcode( $shortcode ); |
| 65 | } |
| 66 | } else { |
| 67 | $shortcodeHtml = do_shortcode( $shortcode ); |
| 68 | } |
| 69 | if ( ! $shortcodeHtml ) { |
| 70 | $shortcodeHtml = Utils::getEmptyShortcodePlaceholder(); |
| 71 | } |
| 72 | |
| 73 | return $shortcodeHtml; |
| 74 | |
| 75 | } |
| 76 | |
| 77 | |
| 78 | function kubio_forminator_get_auth_placeholder() { |
| 79 | return '<p class="shortcode-placeholder-preview">Forminator\'s login and register forms are not visible if you are logged in</p>'; |
| 80 | } |
| 81 | |
| 82 | function kubio_forminator_is_auth_form( $shortcode ) { |
| 83 | $id_found = preg_match( '/id="(\d+)"/', $shortcode, $matches ); |
| 84 | if ( ! $id_found ) { |
| 85 | return false; |
| 86 | } |
| 87 | $form_id = $matches[1]; |
| 88 | $form_class = null; |
| 89 | |
| 90 | //old |
| 91 | if ( class_exists( '\Forminator_Custom_Form_Model' ) ) { |
| 92 | $form_class = '\Forminator_Custom_Form_Model'; |
| 93 | } |
| 94 | //new |
| 95 | if ( class_exists( '\Forminator_Form_Model' ) ) { |
| 96 | $form_class = '\Forminator_Form_Model'; |
| 97 | } |
| 98 | |
| 99 | if ( ! $form_class ) { |
| 100 | return false; |
| 101 | } |
| 102 | try { |
| 103 | $model = $form_class::model()->load( $form_id ); |
| 104 | if ( ! $model ) { |
| 105 | return false; |
| 106 | } |
| 107 | |
| 108 | return in_array( $model->settings['form-type'], array( 'login', 'registration' ) ); |
| 109 | } catch ( \Exception $e ) { |
| 110 | return false; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | function kubio_forminator_form_shortcode( $shortcode ) { |
| 115 | |
| 116 | $html = do_shortcode( $shortcode ); |
| 117 | |
| 118 | return $html; |
| 119 | } |
| 120 |