| 1 |
<?php |
| 2 |
|
| 3 |
namespace HelloPlus\Modules\Forms; |
| 4 |
|
| 5 |
use Elementor\Controls_Manager; |
| 6 |
use HelloPlus\Includes\Module_Base; |
| 7 |
use HelloPlus\Modules\Forms\components\Ajax_Handler; |
| 8 |
use HelloPlus\Modules\Forms\Controls\Fields_Map; |
| 9 |
use HelloPlus\Modules\Forms\Controls\Fields_Repeater; |
| 10 |
use HelloPlus\Modules\Forms\Registrars\Form_Actions_Registrar; |
| 11 |
use HelloPlus\Modules\Forms\Registrars\Form_Fields_Registrar; |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; // Exit if accessed directly. |
| 15 |
} |
| 16 |
|
| 17 |
class Module extends Module_Base { |
| 18 |
/** |
| 19 |
* @var Form_Actions_Registrar |
| 20 |
*/ |
| 21 |
public $actions_registrar; |
| 22 |
|
| 23 |
/** |
| 24 |
* @var Form_Fields_Registrar |
| 25 |
*/ |
| 26 |
public $fields_registrar; |
| 27 |
|
| 28 |
|
| 29 |
public static function get_name(): string { |
| 30 |
return 'forms'; |
| 31 |
} |
| 32 |
|
| 33 |
protected function get_widget_ids(): array { |
| 34 |
return [ |
| 35 |
'Ehp_Form', |
| 36 |
]; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Get the base URL for assets. |
| 41 |
* |
| 42 |
* @return string |
| 43 |
*/ |
| 44 |
public function get_assets_base_url(): string { |
| 45 |
return HELLOPLUS_URL; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Register styles. |
| 50 |
* |
| 51 |
* At build time, Elementor compiles `/modules/forms/assets/scss/frontend.scss` |
| 52 |
* to `/assets/css/widget-forms.min.css`. |
| 53 |
* |
| 54 |
* @return void |
| 55 |
*/ |
| 56 |
public function register_styles() { |
| 57 |
wp_register_style( |
| 58 |
'helloplus-forms', |
| 59 |
HELLOPLUS_STYLE_URL . 'helloplus-forms.css', |
| 60 |
[ 'elementor-frontend' ], |
| 61 |
HELLOPLUS_VERSION |
| 62 |
); |
| 63 |
} |
| 64 |
|
| 65 |
public static function find_element_recursive( $elements, $form_id ) { |
| 66 |
foreach ( $elements as $element ) { |
| 67 |
if ( $form_id === $element['id'] ) { |
| 68 |
return $element; |
| 69 |
} |
| 70 |
|
| 71 |
if ( ! empty( $element['elements'] ) ) { |
| 72 |
$element = self::find_element_recursive( $element['elements'], $form_id ); |
| 73 |
|
| 74 |
if ( $element ) { |
| 75 |
return $element; |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
return false; |
| 81 |
} |
| 82 |
|
| 83 |
public function register_controls( Controls_Manager $controls_manager ) { |
| 84 |
$controls_manager->register( new Fields_Repeater() ); |
| 85 |
$controls_manager->register( new Fields_Map() ); |
| 86 |
} |
| 87 |
|
| 88 |
public function enqueue_editor_scripts() { |
| 89 |
wp_enqueue_script( |
| 90 |
'helloplus-forms-editor', |
| 91 |
HELLOPLUS_SCRIPTS_URL . 'helloplus-forms-editor.js', |
| 92 |
[ 'elementor-editor', 'wp-i18n' ], |
| 93 |
HELLOPLUS_VERSION, |
| 94 |
true |
| 95 |
); |
| 96 |
|
| 97 |
$promotion_data = [ |
| 98 |
'title' => __( 'Collect Submissions', 'hello-plus' ), |
| 99 |
'description' => [ __( 'Unlock form submissions by upgrading to Elementor Pro on an eligible plan.', 'hello-plus' ) ], |
| 100 |
'upgrade_text' => __( 'Upgrade', 'hello-plus' ), |
| 101 |
'upgrade_url' => 'https://go.elementor.com/biz-form-submissions', |
| 102 |
'image' => HELLOPLUS_IMAGES_URL . 'collect-submission.jpg', |
| 103 |
'image_alt' => __( 'Upgrade', 'hello-plus' ), |
| 104 |
]; |
| 105 |
|
| 106 |
wp_localize_script( |
| 107 |
'helloplus-forms-editor', |
| 108 |
'ehpFormsPromotionData', |
| 109 |
$promotion_data |
| 110 |
); |
| 111 |
|
| 112 |
wp_set_script_translations( 'helloplus-forms-editor', 'hello-plus' ); |
| 113 |
} |
| 114 |
|
| 115 |
public function register_scripts() { |
| 116 |
wp_register_script( |
| 117 |
'helloplus-forms-fe', |
| 118 |
HELLOPLUS_SCRIPTS_URL . 'helloplus-forms-fe.js', |
| 119 |
[ 'elementor-common', 'elementor-frontend-modules', 'elementor-frontend' ], |
| 120 |
HELLOPLUS_VERSION, |
| 121 |
true |
| 122 |
); |
| 123 |
|
| 124 |
wp_localize_script( |
| 125 |
'helloplus-forms-fe', |
| 126 |
'ehpFormsData', |
| 127 |
[ |
| 128 |
'nonce' => wp_create_nonce( Ajax_Handler::NONCE_ACTION ), |
| 129 |
] |
| 130 |
); |
| 131 |
} |
| 132 |
|
| 133 |
protected function get_component_ids(): array { |
| 134 |
return [ 'Ajax_Handler' ]; |
| 135 |
} |
| 136 |
|
| 137 |
public static function get_site_domain() { |
| 138 |
return str_ireplace( 'www.', '', wp_parse_url( home_url(), PHP_URL_HOST ) ); |
| 139 |
} |
| 140 |
|
| 141 |
protected function register_hooks(): void { |
| 142 |
parent::register_hooks(); |
| 143 |
|
| 144 |
add_action( 'elementor/frontend/after_register_scripts', [ $this, 'register_scripts' ] ); |
| 145 |
add_action( 'elementor/frontend/after_register_styles', [ $this, 'register_styles' ] ); |
| 146 |
add_action( 'elementor/controls/register', [ $this, 'register_controls' ] ); |
| 147 |
add_action( 'elementor/editor/after_enqueue_scripts', [ $this, 'enqueue_editor_scripts' ] ); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Module constructor. |
| 152 |
*/ |
| 153 |
public function __construct() { |
| 154 |
parent::__construct(); |
| 155 |
|
| 156 |
// Initialize registrars. |
| 157 |
$this->actions_registrar = new Form_Actions_Registrar(); |
| 158 |
$this->fields_registrar = new Form_Fields_Registrar(); |
| 159 |
} |
| 160 |
} |
| 161 |
|