PluginProbe
Hello Plus / 1.7.3
Hello Plus v1.7.3
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.2.1 1.3.0 1.4.0 1.4.1 1.5.0 1.5.1 1.5.2 1.5.3 1.6.0 1.6.1 1.6.2 1.7.0 1.7.1 1.7.2 1.7.3 All 30 releases
hello-plus / modules / forms / fields / field-base.php

field-base.php in Hello Plus 1.7.3, at modules/forms/fields/field-base.php

98 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace HelloPlus\Modules\Forms\Fields;
3
4 use HelloPlus\Modules\Forms\Classes;
5 use HelloPlus\Modules\Forms\Components\Ajax_Handler;
6
7 if ( ! defined( 'ABSPATH' ) ) {
8 exit; // Exit if accessed directly
9 }
10
11 abstract class Field_Base {
12 public $depended_scripts = [];
13 public $depended_styles = [];
14
15 abstract public function get_type();
16
17 abstract public function get_name();
18
19 /**
20 * Get the field ID.
21 *
22 * TODO: Make it an abstract function that will replace `get_type()`.
23 *
24 * @since 3.5.0
25 *
26 * @return string
27 */
28 public function get_id() {
29 return $this->get_type();
30 }
31
32 abstract public function render( $item, $item_index, $form );
33
34 public function validation( $field, Classes\Form_Record $record, Ajax_Handler $ajax_handler ) {}
35
36 public function process_field( $field, Classes\Form_Record $record, Ajax_Handler $ajax_handler ) {}
37
38 public function add_assets_depends() {
39 foreach ( $this->depended_scripts as $script ) {
40 wp_enqueue_script( $script );
41 }
42
43 foreach ( $this->depended_styles as $style ) {
44 wp_enqueue_style( $style );
45 }
46 }
47
48 public function add_preview_depends() {
49 foreach ( $this->depended_scripts as $script ) {
50 wp_enqueue_script( $script );
51 }
52
53 foreach ( $this->depended_styles as $style ) {
54 wp_enqueue_style( $style );
55 }
56 }
57
58 public function add_field_type( $field_types ) {
59 if ( ! in_array( $this->get_type(), $field_types, true ) ) {
60 $field_types[ $this->get_type() ] = $this->get_name();
61 }
62
63 return $field_types;
64 }
65
66 public function field_render( $item, $item_index, $form ) {
67 $this->add_assets_depends( $form );
68 $this->render( $item, $item_index, $form );
69 }
70
71 public function sanitize_field( $value ) {
72 return sanitize_text_field( $value );
73 }
74
75 public function inject_field_controls( $data, $controls_to_inject ) {
76 $keys = array_keys( $data );
77 $key_index = array_search( 'required', $keys, true ) + 1;
78
79 return array_merge( array_slice( $data, 0, $key_index, true ),
80 $controls_to_inject,
81 array_slice( $data, $key_index, null, true )
82 );
83 }
84
85 public function __construct() {
86 $field_type = $this->get_type();
87 add_action( "hello_plus/forms/render_field/{$field_type}", [ $this, 'field_render' ], 10, 3 );
88 add_action( "hello_plus/forms/validation/{$field_type}", [ $this, 'validation' ], 10, 3 );
89 add_action( "hello_plus/forms/process/{$field_type}", [ $this, 'process_field' ], 10, 3 );
90 add_filter( 'hello_plus/forms/field_types', [ $this, 'add_field_type' ] );
91 add_filter( "hello_plus/forms/sanitize/{$field_type}", [ $this, 'sanitize_field' ], 10, 2 );
92 add_action( 'elementor/preview/enqueue_scripts', [ $this, 'add_preview_depends' ] );
93 if ( method_exists( $this, 'update_controls' ) ) {
94 add_action( 'elementor/element/form/section_form_fields/before_section_end', [ $this, 'update_controls' ] );
95 }
96 }
97 }
98