PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 3.0.0.1
JetFormBuilder — Dynamic Blocks Form Builder v3.0.0.1
3.6.4.1 3.6.4 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
189 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\Actions\Events\On_Dynamic_State\On_Dynamic_State_Event;
9 use Jet_Form_Builder\Blocks\Conditional_Block\Render_States\Base_Render_State;
10 use Jet_Form_Builder\Blocks\Conditional_Block\Render_States\Default_State;
11 use Jet_Form_Builder\Blocks\Conditional_Block\Render_States\Example_Render_State;
12 use Jet_Form_Builder\Blocks\Conditional_Block\Render_States\Render_State_Replace_Exception;
13 use Jet_Form_Builder\Classes\Arrayable\Array_Tools;
14 use Jet_Form_Builder\Classes\Arrayable\Arrayable;
15 use Jet_Form_Builder\Classes\Instance_Trait;
16 use Jet_Form_Builder\Classes\Repository\Repository_Pattern_Trait;
17 use Jet_Form_Builder\Classes\Tools;
18 use Jet_Form_Builder\Exceptions\Repository_Exception;
19
20 /**
21 * @method static Render_State instance()
22 *
23 * Class Render_State
24 * @package Jet_Form_Builder\Blocks\Conditional_Block
25 */
26 class Render_State implements Arrayable {
27
28 const OPTION_KEY = 'jet_fb_conditional_render_states';
29 const FIELD_NAME = '_jfb_current_render_states';
30
31 use Repository_Pattern_Trait;
32 use Instance_Trait;
33
34 /** @var Render_States_Collection */
35 private $current;
36
37 protected function __construct() {
38 $this->current = new Render_States_Collection();
39 $this->rep_install();
40
41 add_action( 'jet-form-builder/after-trigger-event', array( $this, 'execute_render_states_events' ) );
42 add_action( 'jet-form-builder/form-handler/before-send', array( $this, 'set_current' ) );
43 }
44
45 /**
46 * @return array
47 */
48 public function rep_instances(): array {
49 return apply_filters(
50 'jet-form-builder/render-states',
51 array(
52 new Default_State(),
53 )
54 );
55 }
56
57 /**
58 * Execute actions on each render state from
59 * _jfb_current_render_states[] hidden field
60 *
61 * @throws \Jet_Form_Builder\Exceptions\Action_Exception
62 */
63 public function execute_render_states_events( Base_Event $event ) {
64 if ( ! is_a( $event, Default_Process_Event::class ) ) {
65 return;
66 }
67 $render_states = self::get_from_request();
68
69 foreach ( $render_states as $state ) {
70 jet_fb_events()->execute( $state );
71 }
72 }
73
74 /**
75 * @param string $slug
76 *
77 * @return Base_Render_State
78 * @throws Repository_Exception
79 */
80 public function get_item( string $slug ): Base_Render_State {
81 return $this->rep_get_item( $slug );
82 }
83
84 public function is_multiple(): bool {
85 $keys = $this->rep_get_keys();
86
87 return ( count( $keys ) > 1 );
88 }
89
90 /**
91 * @return $this
92 */
93 public function set_current(): Render_State {
94 if ( count( $this->current ) ) {
95 return $this;
96 }
97 $items = $this->rep_get_items();
98
99 /** @var Base_Render_State $render_state */
100 foreach ( $items as $render_state ) {
101 try {
102 if ( ! $render_state->is_supported_on_current() ) {
103 continue;
104 }
105 } catch ( Render_State_Replace_Exception $exception ) {
106 $this->current->push( $exception->get_state() );
107
108 continue;
109 }
110
111 $this->current->push( $render_state );
112 }
113
114 $this->current->confirm();
115 $this->set_current_default();
116
117 return $this;
118 }
119
120 public function set_current_default() {
121 if ( count( $this->current ) ) {
122 return;
123 }
124
125 try {
126 /** @var Default_State $state */
127 $state = $this->rep_get_item( Default_State::class );
128 } catch ( Repository_Exception $exception ) {
129 wp_die(
130 esc_html__( 'Something went wrong', 'jet-form-builder' ),
131 esc_html__( 'Error', 'jet-form-builder' )
132 );
133 }
134
135 $this->current->push( $state );
136 }
137
138 public function to_array(): array {
139 $custom = self::get_states();
140
141 foreach ( $custom as &$current ) {
142 $current['is_custom'] = true;
143 }
144
145 return array_merge(
146 Array_Tools::to_array( $this->rep_get_items() ),
147 $custom
148 );
149 }
150
151 public function clear_current(): Render_State {
152 $this->current = new Render_States_Collection();
153
154 return $this;
155 }
156
157 /**
158 * @return Render_States_Collection
159 */
160 public function get_current(): Render_States_Collection {
161 return $this->current;
162 }
163
164 public function render(): string {
165 return $this->get_current()->render();
166 }
167
168 public static function get_states(): array {
169 return Tools::decode_json(
170 get_option( self::OPTION_KEY, '[]' )
171 );
172 }
173
174 public static function update_states( array $states ): bool {
175 return update_option(
176 self::OPTION_KEY,
177 Tools::encode_json( $states ),
178 false
179 );
180 }
181
182 public static function get_from_request(): array {
183 $request = jet_fb_request_handler()->get_request();
184
185 return $request[ self::FIELD_NAME ] ?? array();
186 }
187
188 }
189