condition-types
2 years ago
functions
2 years ago
operators
2 years ago
render-states
2 years ago
rest-api
2 years ago
condition-manager.php
2 years ago
condition-response-object.php
2 years ago
functions.php
2 years ago
operators.php
2 years ago
render-state.php
2 years ago
render-states-collection.php
2 years ago
render-state.php
250 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 JFB_Components\Repository\Repository_Pattern_Trait; |
| 15 | use Jet_Form_Builder\Classes\Tools; |
| 16 | use Jet_Form_Builder\Exceptions\Repository_Exception; |
| 17 | |
| 18 | // If this file is called directly, abort. |
| 19 | if ( ! defined( 'WPINC' ) ) { |
| 20 | die; |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * @method static Render_State instance() |
| 25 | * |
| 26 | * Class Render_State |
| 27 | * @package Jet_Form_Builder\Blocks\Conditional_Block |
| 28 | */ |
| 29 | class Render_State implements Arrayable { |
| 30 | |
| 31 | const OPTION_KEY = 'jet_fb_conditional_render_states'; |
| 32 | const FIELD_NAME = '_jfb_current_render_states'; |
| 33 | |
| 34 | use Repository_Pattern_Trait; |
| 35 | use Instance_Trait; |
| 36 | |
| 37 | /** @var Render_States_Collection */ |
| 38 | private $current; |
| 39 | |
| 40 | protected function __construct() { |
| 41 | $this->current = new Render_States_Collection(); |
| 42 | $this->rep_install(); |
| 43 | |
| 44 | add_action( 'jet-form-builder/after-trigger-event', array( $this, 'execute_render_states_events' ) ); |
| 45 | add_action( 'jet-form-builder/form-handler/before-send', array( $this, 'set_current' ) ); |
| 46 | add_filter( 'jet-form-builder/frontend-settings', array( $this, 'add_built_in_states_to_script' ) ); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * @return array |
| 51 | */ |
| 52 | public function rep_instances(): array { |
| 53 | return apply_filters( |
| 54 | 'jet-form-builder/render-states', |
| 55 | array( |
| 56 | new Default_State(), |
| 57 | ) |
| 58 | ); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Execute actions on each render state from |
| 63 | * _jfb_current_render_states[] hidden field |
| 64 | * |
| 65 | * @throws \Jet_Form_Builder\Exceptions\Action_Exception |
| 66 | */ |
| 67 | public function execute_render_states_events( Base_Event $event ) { |
| 68 | if ( ! is_a( $event, Default_Process_Event::class ) ) { |
| 69 | return; |
| 70 | } |
| 71 | $render_states = self::get_from_request(); |
| 72 | |
| 73 | foreach ( $render_states as $state ) { |
| 74 | jet_fb_events()->execute( $state ); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | public function add_built_in_states_to_script( array $localize_data ): array { |
| 79 | $states = $this->rep_get_items(); |
| 80 | $keys = array(); |
| 81 | |
| 82 | /** @var Base_Render_State $state */ |
| 83 | foreach ( $states as $state ) { |
| 84 | $keys[] = $state->get_id(); |
| 85 | } |
| 86 | |
| 87 | $localize_data['builtInStates'] = $keys; |
| 88 | |
| 89 | return $localize_data; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * @param string $slug |
| 94 | * |
| 95 | * @return Base_Render_State |
| 96 | * @throws Repository_Exception |
| 97 | */ |
| 98 | public function get_item( string $slug ): Base_Render_State { |
| 99 | return $this->rep_get_item( $slug ); |
| 100 | } |
| 101 | |
| 102 | public function is_multiple(): bool { |
| 103 | $keys = $this->rep_get_keys(); |
| 104 | |
| 105 | return ( count( $keys ) > 1 ); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * @return $this |
| 110 | */ |
| 111 | public function set_current(): Render_State { |
| 112 | if ( count( $this->current ) ) { |
| 113 | return $this; |
| 114 | } |
| 115 | $items = $this->rep_get_items(); |
| 116 | |
| 117 | /** @var Base_Render_State $render_state */ |
| 118 | foreach ( $items as $render_state ) { |
| 119 | try { |
| 120 | if ( ! $render_state->is_supported_on_current() ) { |
| 121 | continue; |
| 122 | } |
| 123 | } catch ( Render_State_Replace_Exception $exception ) { |
| 124 | $this->current->push( $exception->get_state() ); |
| 125 | |
| 126 | continue; |
| 127 | } |
| 128 | |
| 129 | $this->current->push( $render_state ); |
| 130 | } |
| 131 | |
| 132 | $this->current->confirm(); |
| 133 | $this->set_states_from_url(); |
| 134 | $this->set_current_default(); |
| 135 | |
| 136 | return $this; |
| 137 | } |
| 138 | |
| 139 | protected function set_current_default() { |
| 140 | if ( count( $this->current ) ) { |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | try { |
| 145 | /** @var Default_State $state */ |
| 146 | $state = $this->rep_get_item( Default_State::class ); |
| 147 | } catch ( Repository_Exception $exception ) { |
| 148 | wp_die( |
| 149 | esc_html__( 'Something went wrong', 'jet-form-builder' ), |
| 150 | esc_html__( 'Error', 'jet-form-builder' ) |
| 151 | ); |
| 152 | } |
| 153 | |
| 154 | $this->current->push( $state ); |
| 155 | } |
| 156 | |
| 157 | protected function set_states_from_url() { |
| 158 | $custom = self::get_states(); |
| 159 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 160 | $raw_url_states = $this->load_render_states(); |
| 161 | |
| 162 | if ( ! $raw_url_states || ! count( $custom ) ) { |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | $url_states = array_map( 'trim', explode( ',', $raw_url_states ) ); |
| 167 | |
| 168 | foreach ( $custom as $item_state ) { |
| 169 | $slug = $item_state['value'] ?? ''; |
| 170 | |
| 171 | if ( ! in_array( $slug, $url_states, true ) ) { |
| 172 | continue; |
| 173 | } |
| 174 | |
| 175 | $this->current->push( $slug ); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | public function to_array(): array { |
| 180 | $custom = self::get_states(); |
| 181 | |
| 182 | foreach ( $custom as &$current ) { |
| 183 | $current['is_custom'] = true; |
| 184 | } |
| 185 | |
| 186 | return array_merge( |
| 187 | Array_Tools::to_array( $this->rep_get_items() ), |
| 188 | $custom |
| 189 | ); |
| 190 | } |
| 191 | |
| 192 | public function clear_current(): Render_State { |
| 193 | $this->current = new Render_States_Collection(); |
| 194 | |
| 195 | return $this; |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * @return Render_States_Collection |
| 200 | */ |
| 201 | public function get_current(): Render_States_Collection { |
| 202 | return $this->current; |
| 203 | } |
| 204 | |
| 205 | public function render(): string { |
| 206 | return $this->get_current()->render(); |
| 207 | } |
| 208 | |
| 209 | public static function get_states(): array { |
| 210 | return Tools::decode_json( |
| 211 | get_option( self::OPTION_KEY, '[]' ) |
| 212 | ); |
| 213 | } |
| 214 | |
| 215 | public static function update_states( array $states ): bool { |
| 216 | return update_option( |
| 217 | self::OPTION_KEY, |
| 218 | Tools::encode_json( $states ), |
| 219 | false |
| 220 | ); |
| 221 | } |
| 222 | |
| 223 | public static function get_from_request(): array { |
| 224 | $states = jet_fb_context()->get_request( self::FIELD_NAME ); |
| 225 | |
| 226 | return is_array( $states ) ? $states : array(); |
| 227 | } |
| 228 | |
| 229 | protected function load_render_states(): string { |
| 230 | return $this->sanitize_render_state( |
| 231 | // phpcs:ignore WordPress.Security |
| 232 | wp_unslash( $_GET['jfb'][ jet_fb_live()->form_id ]['state'] ?? '' ) |
| 233 | ); |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Accepts: 'DEFAULT.STATE', 'DEFAULT.STATE,REGISTER.FORM.CUSTOM' |
| 238 | * |
| 239 | * @param string $content |
| 240 | * |
| 241 | * @return string |
| 242 | */ |
| 243 | protected function sanitize_render_state( string $content ): string { |
| 244 | $content = strtoupper( $content ); |
| 245 | |
| 246 | return preg_replace( '/[^A-Z0-9\.\,]/', '', $content ); |
| 247 | } |
| 248 | |
| 249 | } |
| 250 |