Conditions
3 years ago
Documents
2 years ago
assets
3 years ago
ElementorServiceProvider.php
2 years ago
ReusableFormWidget.php
3 years ago
ReusableFormWidget.php
180 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Integrations\Elementor; |
| 4 | |
| 5 | use Elementor\Widget_Base; |
| 6 | |
| 7 | if ( ! defined( 'ABSPATH' ) ) { |
| 8 | exit; // Exit if accessed directly. |
| 9 | } |
| 10 | |
| 11 | /** |
| 12 | * SureCart form form widget. |
| 13 | * |
| 14 | * Surecart widget that displays a form. |
| 15 | */ |
| 16 | class ReusableFormWidget extends Widget_Base { |
| 17 | /** |
| 18 | * Get widget name. |
| 19 | * |
| 20 | * Retrieve form widget name. |
| 21 | * |
| 22 | * @since x.x.x |
| 23 | * @access public |
| 24 | * |
| 25 | * @return string Widget name. |
| 26 | */ |
| 27 | public function get_name() { |
| 28 | return 'surecart_form'; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Get widget title. |
| 33 | * |
| 34 | * Retrieve form widget title. |
| 35 | * |
| 36 | * @since x.x.x |
| 37 | * @access public |
| 38 | * |
| 39 | * @return string Widget title. |
| 40 | */ |
| 41 | public function get_title() { |
| 42 | return __( 'Checkout Form', 'surecart' ); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Get widget icon. |
| 47 | * |
| 48 | * Retrieve form widget icon. |
| 49 | * |
| 50 | * @since x.x.x |
| 51 | * @access public |
| 52 | * |
| 53 | * @return string Widget icon. |
| 54 | */ |
| 55 | public function get_icon() { |
| 56 | return 'eicon-form-horizontal surecart-checkout-icon'; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Get widget categories. |
| 61 | * |
| 62 | * Retrieve the list of categories the form widget belongs to. |
| 63 | * |
| 64 | * Used to determine where to display the widget in the editor. |
| 65 | * |
| 66 | * @since x.x.x |
| 67 | * @access public |
| 68 | * |
| 69 | * @return array Widget categories. |
| 70 | */ |
| 71 | public function get_categories() { |
| 72 | return [ 'surecart-elementor' ]; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Get widget keywords. |
| 77 | * |
| 78 | * Retrieve the list of keywords the widget belongs to. |
| 79 | * |
| 80 | * @since x.x.x |
| 81 | * @access public |
| 82 | * |
| 83 | * @return array Widget keywords. |
| 84 | */ |
| 85 | public function get_keywords() { |
| 86 | return [ 'form', 'surecart', 'checkout' ]; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Register form widget controls. |
| 91 | * |
| 92 | * Adds different input fields to allow the user to change and customize the widget settings. |
| 93 | * |
| 94 | * @since x.x.x |
| 95 | * @access protected |
| 96 | */ |
| 97 | protected function _register_controls() { |
| 98 | $this->start_controls_section( |
| 99 | 'section_form', |
| 100 | [ |
| 101 | 'label' => __( 'Checkout Form', 'surecart' ), |
| 102 | ] |
| 103 | ); |
| 104 | |
| 105 | $options = $this->get_forms_options(); |
| 106 | |
| 107 | $this->add_control( |
| 108 | 'sc_form_block', |
| 109 | [ |
| 110 | 'label' => __( 'Select Form', 'surecart' ), |
| 111 | 'type' => \Elementor\Controls_Manager::SELECT2, |
| 112 | 'options' => $options, |
| 113 | 'default' => array_keys( $options )[0], |
| 114 | ] |
| 115 | ); |
| 116 | |
| 117 | $this->add_control( |
| 118 | 'sc_edit_form', |
| 119 | [ |
| 120 | 'label' => __( 'Edit Form', 'surecart' ), |
| 121 | 'type' => \Elementor\Controls_Manager::BUTTON, |
| 122 | 'text' => __( 'Edit', 'surecart' ), |
| 123 | 'event' => 'surecart:form:edit', |
| 124 | ] |
| 125 | ); |
| 126 | |
| 127 | $this->add_control( |
| 128 | 'sc_create_form', |
| 129 | [ |
| 130 | 'label' => __( 'Create New Form', 'surecart' ), |
| 131 | 'separator' => 'before', |
| 132 | 'classes' => 'testclass', |
| 133 | 'type' => \Elementor\Controls_Manager::BUTTON, |
| 134 | 'text' => __( 'Create', 'surecart' ), |
| 135 | 'event' => 'surecart:form:create', |
| 136 | ] |
| 137 | ); |
| 138 | |
| 139 | $this->end_controls_section(); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Get froms options. |
| 144 | * |
| 145 | * @since x.x.x |
| 146 | * |
| 147 | * @return array |
| 148 | */ |
| 149 | public function get_forms_options() { |
| 150 | $args = [ |
| 151 | 'numberposts' => -1, |
| 152 | 'fields' => 'ids', |
| 153 | ]; |
| 154 | |
| 155 | $get_forms = \SureCart::forms()->get_forms( $args ); |
| 156 | |
| 157 | $options = []; |
| 158 | |
| 159 | foreach ( $get_forms as $form ) { |
| 160 | $options[ $form ] = get_the_title( $form ); |
| 161 | } |
| 162 | |
| 163 | return $options; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Render form widget output on the frontend. |
| 168 | * |
| 169 | * Written in PHP and used to generate the final HTML. |
| 170 | * |
| 171 | * @since x.x.x |
| 172 | * @access protected |
| 173 | */ |
| 174 | protected function render() { |
| 175 | $settings = $this->get_settings_for_display(); |
| 176 | |
| 177 | echo do_shortcode( '[sc_form id=' . $settings['sc_form_block'] . ']' ); |
| 178 | } |
| 179 | } |
| 180 |