| 1 |
<?php |
| 2 |
/** |
| 3 |
* Elementor SureForms form widget. |
| 4 |
* |
| 5 |
* @package sureforms. |
| 6 |
* @since 0.0.5 |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace SRFM\Inc\Page_Builders\Elementor; |
| 10 |
|
| 11 |
use Elementor\Widget_Base; |
| 12 |
use Elementor\Plugin; |
| 13 |
use SRFM\Inc\Helper; |
| 14 |
use SRFM\Inc\Page_Builders\Page_Builders; |
| 15 |
|
| 16 |
if ( ! defined( 'ABSPATH' ) ) { |
| 17 |
exit; // Exit if accessed directly. |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* |
| 22 |
* SureForms widget that displays a form. |
| 23 |
*/ |
| 24 |
class Form_Widget extends Widget_Base { |
| 25 |
|
| 26 |
/** |
| 27 |
* Whether we are in the preview mode. |
| 28 |
* |
| 29 |
* @var bool |
| 30 |
*/ |
| 31 |
public $is_preview_mode; |
| 32 |
|
| 33 |
/** |
| 34 |
* Constructor. |
| 35 |
* |
| 36 |
* @param array<mixed> $data Widget data. |
| 37 |
* @param array<mixed> $args Widget arguments. |
| 38 |
*/ |
| 39 |
public function __construct( $data = [], $args = null ) { |
| 40 |
|
| 41 |
parent::__construct( $data, $args ); |
| 42 |
|
| 43 |
// (Preview iframe) |
| 44 |
$this->is_preview_mode = \Elementor\Plugin::$instance->preview->is_preview_mode(); |
| 45 |
|
| 46 |
if ( $this->is_preview_mode ) { |
| 47 |
|
| 48 |
// enqueue common fields assets for the dropdown and phone fields. |
| 49 |
Page_Builders::enqueue_common_fields_assets(); |
| 50 |
|
| 51 |
wp_register_script( 'srfm-elementor-preview', SRFM_URL . 'inc/page-builders/elementor/assets/elementor-editor-preview.js', [ 'elementor-frontend' ], SRFM_VER, true ); |
| 52 |
wp_localize_script( |
| 53 |
'srfm-elementor-preview', |
| 54 |
'srfmElementorData', |
| 55 |
[ |
| 56 |
'isProActive' => defined( 'SRFM_PRO_VER' ), |
| 57 |
] |
| 58 |
); |
| 59 |
|
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Get script depends. |
| 65 |
* |
| 66 |
* @since 0.0.5 |
| 67 |
* @return array<string> Script dependencies. |
| 68 |
*/ |
| 69 |
public function get_script_depends() { |
| 70 |
if ( $this->is_preview_mode ) { |
| 71 |
return [ 'srfm-elementor-preview' ]; |
| 72 |
} |
| 73 |
|
| 74 |
return []; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Get widget name. |
| 79 |
* |
| 80 |
* @since 0.0.5 |
| 81 |
* @return string Widget name. |
| 82 |
*/ |
| 83 |
public function get_name() { |
| 84 |
return SRFM_FORMS_POST_TYPE; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Get widget title. |
| 89 |
* |
| 90 |
* @since 0.0.5 |
| 91 |
* @return string Widget title. |
| 92 |
*/ |
| 93 |
public function get_title() { |
| 94 |
return __( 'SureForms', 'sureforms' ); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Get widget icon. |
| 99 |
* |
| 100 |
* @since 0.0.5 |
| 101 |
* @return string Widget icon. |
| 102 |
*/ |
| 103 |
public function get_icon() { |
| 104 |
return 'eicon-form-horizontal srfm-elementor-widget-icon'; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Get widget categories. Used to determine where to display the widget in the editor. |
| 109 |
* |
| 110 |
* @since 0.0.5 |
| 111 |
* @return array<string> Widget categories. |
| 112 |
*/ |
| 113 |
public function get_categories() { |
| 114 |
return [ 'sureforms-elementor' ]; |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Get widget keywords. |
| 119 |
* |
| 120 |
* @since 0.0.5 |
| 121 |
* @return array<string> Widget keywords. |
| 122 |
*/ |
| 123 |
public function get_keywords() { |
| 124 |
return [ |
| 125 |
'sureforms', |
| 126 |
'contact form', |
| 127 |
'form', |
| 128 |
'elementor form', |
| 129 |
]; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Register form widget controls. |
| 134 |
* Adds different input fields to allow the user to change and customize the widget settings. |
| 135 |
* |
| 136 |
* @since 0.0.5 |
| 137 |
* @return void |
| 138 |
*/ |
| 139 |
protected function register_controls() { |
| 140 |
|
| 141 |
$this->start_controls_section( |
| 142 |
'section_form', |
| 143 |
[ |
| 144 |
'label' => __( 'SureForms', 'sureforms' ), |
| 145 |
] |
| 146 |
); |
| 147 |
|
| 148 |
$this->add_control( |
| 149 |
'srfm_form_block', |
| 150 |
[ |
| 151 |
'label' => __( 'Select Form', 'sureforms' ), |
| 152 |
'type' => \Elementor\Controls_Manager::SELECT2, |
| 153 |
'options' => Helper::get_sureforms_title_with_ids(), |
| 154 |
'default' => '', |
| 155 |
] |
| 156 |
); |
| 157 |
|
| 158 |
$this->add_control( |
| 159 |
'srfm_show_form_title', |
| 160 |
[ |
| 161 |
'label' => __( 'Form Title', 'sureforms' ), |
| 162 |
'type' => \Elementor\Controls_Manager::SWITCHER, |
| 163 |
'label_on' => __( 'Show', 'sureforms' ), |
| 164 |
'label_off' => __( 'Hide', 'sureforms' ), |
| 165 |
'return_value' => 'true', |
| 166 |
'condition' => [ |
| 167 |
'srfm_form_block!' => [ '' ], |
| 168 |
], |
| 169 |
] |
| 170 |
); |
| 171 |
|
| 172 |
$this->add_control( |
| 173 |
'srfm_edit_form', |
| 174 |
[ |
| 175 |
'label' => __( 'Edit Form', 'sureforms' ), |
| 176 |
'separator' => 'before', |
| 177 |
'type' => \Elementor\Controls_Manager::BUTTON, |
| 178 |
'text' => __( 'Edit', 'sureforms' ), |
| 179 |
'event' => 'sureforms:form:edit', |
| 180 |
'condition' => [ |
| 181 |
'srfm_form_block!' => [ '' ], |
| 182 |
], |
| 183 |
] |
| 184 |
); |
| 185 |
|
| 186 |
$this->add_control( |
| 187 |
'srfm_create_form', |
| 188 |
[ |
| 189 |
'label' => __( 'Create New Form', 'sureforms' ), |
| 190 |
'type' => \Elementor\Controls_Manager::BUTTON, |
| 191 |
'text' => __( 'Create', 'sureforms' ), |
| 192 |
'event' => 'sureforms:form:create', |
| 193 |
] |
| 194 |
); |
| 195 |
|
| 196 |
$this->add_control( |
| 197 |
'srfm_form_submission_info', |
| 198 |
[ |
| 199 |
'content' => __( 'Form submission will be possible on the frontend.', 'sureforms' ), |
| 200 |
'type' => \Elementor\Controls_Manager::ALERT, |
| 201 |
'condition' => [ |
| 202 |
'srfm_form_block!' => [ '' ], |
| 203 |
], |
| 204 |
] |
| 205 |
); |
| 206 |
|
| 207 |
$this->end_controls_section(); |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Render form widget output on the frontend. |
| 212 |
* |
| 213 |
* @since 0.0.5 |
| 214 |
* @return void|string |
| 215 |
*/ |
| 216 |
protected function render() { |
| 217 |
$settings = $this->get_settings_for_display(); |
| 218 |
|
| 219 |
if ( ! is_array( $settings ) ) { |
| 220 |
return; |
| 221 |
} |
| 222 |
|
| 223 |
$is_editor = Plugin::instance()->editor->is_edit_mode(); |
| 224 |
|
| 225 |
if ( $is_editor && isset( $settings['srfm_form_block'] ) && '' === $settings['srfm_form_block'] ) { |
| 226 |
echo '<div style="background: #D9DEE1; color: #9DA5AE; padding: 10px; font-family: Roboto, sans-serif">' . |
| 227 |
esc_html__( 'Select the form that you wish to add here.', 'sureforms' ) . |
| 228 |
'</div>'; |
| 229 |
return; |
| 230 |
} |
| 231 |
|
| 232 |
if ( ! isset( $settings['srfm_show_form_title'] ) || ! isset( $settings['srfm_form_block'] ) ) { |
| 233 |
return; |
| 234 |
} |
| 235 |
|
| 236 |
$show_form_title = 'true' === $settings['srfm_show_form_title']; |
| 237 |
echo do_shortcode( '[sureforms id="' . $settings['srfm_form_block'] . '" show_title="' . ! $show_form_title . '"]' ); |
| 238 |
} |
| 239 |
|
| 240 |
} |
| 241 |
|