PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 3.0.8
JetFormBuilder — Dynamic Blocks Form Builder v3.0.8
3.6.3.1 3.6.3 3.6.2.2 3.6.2.1 3.6.2 3.6.1.1 3.6.1 3.6.0.1 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.1.4 1.1.5 1.1.6 1.1.7 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.1.0 2.1.1 2.1.10 2.1.11 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 3.0.0 3.0.0.1 3.0.0.2 3.0.0.3 3.0.1 3.0.1.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.0.1 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.3.2 3.3.3 3.3.3.1 3.3.4 3.3.4.1 3.3.4.2 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.5.1 3.4.5.2 3.4.6 3.4.7 3.4.7.1 3.5.0 3.5.1 3.5.1.1 3.5.1.2 3.5.2 3.5.2.1 3.5.3 3.5.4 3.5.5 3.5.6 3.5.6.1 3.5.6.2 3.5.6.3 3.6.0
jetformbuilder / includes / blocks / conditional-block / render-state.php
jetformbuilder / includes / blocks / conditional-block Last commit date
condition-types 3 years ago functions 3 years ago models 3 years ago operators 3 years ago query-views 3 years ago render-states 3 years ago rest-api 3 years ago condition-manager.php 3 years ago condition-response-object.php 3 years ago functions.php 3 years ago operators.php 3 years ago render-state.php 3 years ago render-states-collection.php 3 years ago
render-state.php
224 lines
1 <?php
2
3
4 namespace Jet_Form_Builder\Blocks\Conditional_Block;
5
6 use Jet_Form_Builder\Actions\Events\Base_Event;
7 use Jet_Form_Builder\Actions\Events\Default_Process\Default_Process_Event;
8 use Jet_Form_Builder\Blocks\Conditional_Block\Render_States\Base_Render_State;
9 use Jet_Form_Builder\Blocks\Conditional_Block\Render_States\Default_State;
10 use Jet_Form_Builder\Blocks\Conditional_Block\Render_States\Render_State_Replace_Exception;
11 use Jet_Form_Builder\Classes\Arrayable\Array_Tools;
12 use Jet_Form_Builder\Classes\Arrayable\Arrayable;
13 use Jet_Form_Builder\Classes\Instance_Trait;
14 use Jet_Form_Builder\Classes\Repository\Repository_Pattern_Trait;
15 use Jet_Form_Builder\Classes\Tools;
16 use Jet_Form_Builder\Exceptions\Repository_Exception;
17
18 /**
19 * @method static Render_State instance()
20 *
21 * Class Render_State
22 * @package Jet_Form_Builder\Blocks\Conditional_Block
23 */
24 class Render_State implements Arrayable {
25
26 const OPTION_KEY = 'jet_fb_conditional_render_states';
27 const FIELD_NAME = '_jfb_current_render_states';
28
29 use Repository_Pattern_Trait;
30 use Instance_Trait;
31
32 /** @var Render_States_Collection */
33 private $current;
34
35 protected function __construct() {
36 $this->current = new Render_States_Collection();
37 $this->rep_install();
38
39 add_action( 'jet-form-builder/after-trigger-event', array( $this, 'execute_render_states_events' ) );
40 add_action( 'jet-form-builder/form-handler/before-send', array( $this, 'set_current' ) );
41 add_filter( 'jet-form-builder/frontend-settings', array( $this, 'add_built_in_states_to_script' ) );
42 }
43
44 /**
45 * @return array
46 */
47 public function rep_instances(): array {
48 return apply_filters(
49 'jet-form-builder/render-states',
50 array(
51 new Default_State(),
52 )
53 );
54 }
55
56 /**
57 * Execute actions on each render state from
58 * _jfb_current_render_states[] hidden field
59 *
60 * @throws \Jet_Form_Builder\Exceptions\Action_Exception
61 */
62 public function execute_render_states_events( Base_Event $event ) {
63 if ( ! is_a( $event, Default_Process_Event::class ) ) {
64 return;
65 }
66 $render_states = self::get_from_request();
67
68 foreach ( $render_states as $state ) {
69 jet_fb_events()->execute( $state );
70 }
71 }
72
73 public function add_built_in_states_to_script( array $localize_data ): array {
74 $states = $this->rep_get_items();
75 $keys = array();
76
77 /** @var Base_Render_State $state */
78 foreach ( $states as $state ) {
79 $keys[] = $state->get_id();
80 }
81
82 $localize_data['builtInStates'] = $keys;
83
84 return $localize_data;
85 }
86
87 /**
88 * @param string $slug
89 *
90 * @return Base_Render_State
91 * @throws Repository_Exception
92 */
93 public function get_item( string $slug ): Base_Render_State {
94 return $this->rep_get_item( $slug );
95 }
96
97 public function is_multiple(): bool {
98 $keys = $this->rep_get_keys();
99
100 return ( count( $keys ) > 1 );
101 }
102
103 /**
104 * @return $this
105 */
106 public function set_current(): Render_State {
107 if ( count( $this->current ) ) {
108 return $this;
109 }
110 $items = $this->rep_get_items();
111
112 /** @var Base_Render_State $render_state */
113 foreach ( $items as $render_state ) {
114 try {
115 if ( ! $render_state->is_supported_on_current() ) {
116 continue;
117 }
118 } catch ( Render_State_Replace_Exception $exception ) {
119 $this->current->push( $exception->get_state() );
120
121 continue;
122 }
123
124 $this->current->push( $render_state );
125 }
126
127 $this->current->confirm();
128 $this->set_states_from_url();
129 $this->set_current_default();
130
131 return $this;
132 }
133
134 protected function set_current_default() {
135 if ( count( $this->current ) ) {
136 return;
137 }
138
139 try {
140 /** @var Default_State $state */
141 $state = $this->rep_get_item( Default_State::class );
142 } catch ( Repository_Exception $exception ) {
143 wp_die(
144 esc_html__( 'Something went wrong', 'jet-form-builder' ),
145 esc_html__( 'Error', 'jet-form-builder' )
146 );
147 }
148
149 $this->current->push( $state );
150 }
151
152 protected function set_states_from_url() {
153 $custom = self::get_states();
154 $raw_url_states = sanitize_text_field( $_GET['jfb'][ jet_fb_live()->form_id ]['state'] ?? '' );
155
156 if ( ! $raw_url_states || ! count( $custom ) ) {
157 return;
158 }
159
160 $url_states = array_map( 'trim', explode( ',', $raw_url_states ) );
161
162 foreach ( $custom as $item_state ) {
163 $slug = $item_state['value'] ?? '';
164
165 if ( ! in_array( $slug, $url_states, true ) ) {
166 continue;
167 }
168
169 $this->current->push( $slug );
170 }
171 }
172
173 public function to_array(): array {
174 $custom = self::get_states();
175
176 foreach ( $custom as &$current ) {
177 $current['is_custom'] = true;
178 }
179
180 return array_merge(
181 Array_Tools::to_array( $this->rep_get_items() ),
182 $custom
183 );
184 }
185
186 public function clear_current(): Render_State {
187 $this->current = new Render_States_Collection();
188
189 return $this;
190 }
191
192 /**
193 * @return Render_States_Collection
194 */
195 public function get_current(): Render_States_Collection {
196 return $this->current;
197 }
198
199 public function render(): string {
200 return $this->get_current()->render();
201 }
202
203 public static function get_states(): array {
204 return Tools::decode_json(
205 get_option( self::OPTION_KEY, '[]' )
206 );
207 }
208
209 public static function update_states( array $states ): bool {
210 return update_option(
211 self::OPTION_KEY,
212 Tools::encode_json( $states ),
213 false
214 );
215 }
216
217 public static function get_from_request(): array {
218 $request = jet_fb_request_handler()->get_request();
219
220 return $request[ self::FIELD_NAME ] ?? array();
221 }
222
223 }
224