| 1 |
<?php |
| 2 |
/** |
| 3 |
* Bricks SureForms form element. |
| 4 |
* |
| 5 |
* @package sureforms. |
| 6 |
* @since 0.0.5 |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace SRFM\Inc\Page_Builders\Bricks\Elements; |
| 10 |
|
| 11 |
use SRFM\Inc\Helper; |
| 12 |
use SRFM\Inc\Page_Builders\Page_Builders; |
| 13 |
|
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; // Exit if accessed directly. |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* SureForms Bricks element. |
| 20 |
*/ |
| 21 |
class Form_Widget extends \Bricks\Element { |
| 22 |
|
| 23 |
/** |
| 24 |
* Element category. |
| 25 |
* |
| 26 |
* @var string |
| 27 |
*/ |
| 28 |
public $category = 'sureforms'; |
| 29 |
|
| 30 |
/** |
| 31 |
* Element name. |
| 32 |
* |
| 33 |
* @var string |
| 34 |
*/ |
| 35 |
public $name = 'sureforms'; |
| 36 |
|
| 37 |
/** |
| 38 |
* Element icon. |
| 39 |
* |
| 40 |
* @var string |
| 41 |
*/ |
| 42 |
public $icon = 'ti-layout-accordion-separated'; |
| 43 |
|
| 44 |
/** |
| 45 |
* Constructor. |
| 46 |
* |
| 47 |
* @param array<mixed> $element Element data. |
| 48 |
*/ |
| 49 |
public function __construct( $element = null ) { |
| 50 |
|
| 51 |
if ( bricks_is_builder() ) { |
| 52 |
// call the js functions to handle form submission, load page break, phone, dropdown. |
| 53 |
$this->scripts = [ 'handleBricksPreviewFormSubmission', 'srfmLoadPageBreak', 'srfmInitializePhoneField', 'srfmInitializeDropdown' ]; |
| 54 |
} |
| 55 |
|
| 56 |
parent::__construct( $element ); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Get element name. |
| 61 |
* |
| 62 |
* @since 0.0.5 |
| 63 |
* @return string element name. |
| 64 |
*/ |
| 65 |
public function get_label() { |
| 66 |
return __( 'SureForms', 'sureforms' ); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Get element keywords. |
| 71 |
* |
| 72 |
* @since 0.0.5 |
| 73 |
* @return array<string> element keywords. |
| 74 |
*/ |
| 75 |
public function get_keywords() { |
| 76 |
return [ |
| 77 |
'sureforms', |
| 78 |
'contact form', |
| 79 |
'form', |
| 80 |
'bricks form', |
| 81 |
]; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Set element controls. |
| 86 |
* |
| 87 |
* @since 0.0.5 |
| 88 |
* @return void |
| 89 |
*/ |
| 90 |
public function set_controls() { |
| 91 |
|
| 92 |
// Select Form. |
| 93 |
$this->controls['form-id'] = [ |
| 94 |
|
| 95 |
'tab' => 'content', |
| 96 |
'label' => __( 'Form', 'sureforms' ), |
| 97 |
'type' => 'select', |
| 98 |
'options' => Helper::get_sureforms_title_with_ids(), |
| 99 |
'placeholder' => __( 'Select Form', 'sureforms' ), |
| 100 |
]; |
| 101 |
|
| 102 |
// Show Form Title Toggle. |
| 103 |
$this->controls['form-title'] = [ |
| 104 |
'tab' => 'content', |
| 105 |
'label' => __( 'Show Form Title', 'sureforms' ), |
| 106 |
'type' => 'checkbox', |
| 107 |
'info' => __( 'Enable this to show form title.', 'sureforms' ), |
| 108 |
'required' => [ 'form-id', '!=', '' ], |
| 109 |
]; |
| 110 |
|
| 111 |
$this->controls['srfm_form_submission_info'] = [ |
| 112 |
'tab' => 'content', |
| 113 |
'content' => __( 'Form submission will be possible on the frontend.', 'sureforms' ), |
| 114 |
'type' => 'info', |
| 115 |
'required' => [ 'form-id', '!=', '' ], |
| 116 |
]; |
| 117 |
|
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Enqueue scripts for phone and dropdown field in the Bricks editor. |
| 122 |
* |
| 123 |
* @since 0.0.10 |
| 124 |
* @return void |
| 125 |
*/ |
| 126 |
public function enqueue_scripts() { |
| 127 |
// enqueue common fields assets for the dropdown and phone fields. |
| 128 |
Page_Builders::enqueue_common_fields_assets(); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Render element. |
| 133 |
* |
| 134 |
* @since 0.0.5 |
| 135 |
* @return void |
| 136 |
*/ |
| 137 |
public function render() { |
| 138 |
$settings = $this->settings; |
| 139 |
$form_id = isset( $settings['form-id'] ) ? $settings['form-id'] : ''; |
| 140 |
$form_title = isset( $settings['form-title'] ); |
| 141 |
|
| 142 |
if ( $form_id > 0 ) { |
| 143 |
// phpcs:ignore -- WordPress.Security.EscapeOutput.OutputNotEscaped - Escaping not required. |
| 144 |
echo '<div ' . $this->render_attributes( '_root' ) . '>' . do_shortcode( sprintf( '[sureforms id="%s" show_title="%s"]', $form_id, ! $form_title ) ) . '</div>'; |
| 145 |
// phpcs:ignoreEnd |
| 146 |
} else { |
| 147 |
// Show placeholder when no form is selected. |
| 148 |
// phpcs:ignore -- WordPress.Security.EscapeOutput.OutputNotEscaped - Escaping not required. |
| 149 |
echo $this->render_element_placeholder( |
| 150 |
[ |
| 151 |
'icon-class' => $this->icon, |
| 152 |
'description' => esc_html__( 'Select the form that you wish to add here.', 'sureforms' ), |
| 153 |
] |
| 154 |
); |
| 155 |
// phpcs:ignoreEnd |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
|