functions
3 years ago
operators
3 years ago
render-states
3 years ago
condition-item.php
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
122 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Blocks\Conditional_Block; |
| 5 | |
| 6 | use Jet_Form_Builder\Blocks\Conditional_Block\Render_States\Base_Render_State; |
| 7 | use Jet_Form_Builder\Blocks\Conditional_Block\Render_States\Default_State; |
| 8 | use Jet_Form_Builder\Blocks\Conditional_Block\Render_States\Example_Render_State; |
| 9 | use Jet_Form_Builder\Classes\Arrayable\Array_Tools; |
| 10 | use Jet_Form_Builder\Classes\Arrayable\Arrayable; |
| 11 | use Jet_Form_Builder\Classes\Instance_Trait; |
| 12 | use Jet_Form_Builder\Classes\Repository\Repository_Pattern_Trait; |
| 13 | use Jet_Form_Builder\Exceptions\Repository_Exception; |
| 14 | |
| 15 | /** |
| 16 | * @method static Render_State instance() |
| 17 | * |
| 18 | * Class Render_State |
| 19 | * @package Jet_Form_Builder\Blocks\Conditional_Block |
| 20 | */ |
| 21 | class Render_State implements Arrayable { |
| 22 | |
| 23 | use Repository_Pattern_Trait; |
| 24 | use Instance_Trait; |
| 25 | |
| 26 | /** @var Render_States_Collection */ |
| 27 | private $current; |
| 28 | |
| 29 | protected function __construct() { |
| 30 | $this->current = new Render_States_Collection(); |
| 31 | $this->rep_install(); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * @return array |
| 36 | */ |
| 37 | public function rep_instances(): array { |
| 38 | return apply_filters( |
| 39 | 'jet-form-builder/render-states', |
| 40 | array( |
| 41 | new Default_State(), |
| 42 | new Example_Render_State(), |
| 43 | ) |
| 44 | ); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * @param string $slug |
| 49 | * |
| 50 | * @return mixed |
| 51 | * @throws Repository_Exception |
| 52 | */ |
| 53 | public function get_item( string $slug ) { |
| 54 | return $this->rep_get_item( $slug ); |
| 55 | } |
| 56 | |
| 57 | public function is_multiple(): bool { |
| 58 | $keys = $this->rep_get_keys(); |
| 59 | |
| 60 | return ( count( $keys ) > 1 ); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * @return $this |
| 65 | */ |
| 66 | public function set_current(): Render_State { |
| 67 | if ( count( $this->current ) ) { |
| 68 | return $this; |
| 69 | } |
| 70 | $items = $this->rep_get_items(); |
| 71 | |
| 72 | /** @var Base_Render_State $render_state */ |
| 73 | foreach ( $items as $render_state ) { |
| 74 | if ( ! $render_state->is_supported() ) { |
| 75 | continue; |
| 76 | } |
| 77 | $this->current->push( $render_state ); |
| 78 | } |
| 79 | |
| 80 | $this->set_current_default(); |
| 81 | |
| 82 | return $this; |
| 83 | } |
| 84 | |
| 85 | public function set_current_default() { |
| 86 | if ( count( $this->current ) ) { |
| 87 | return; |
| 88 | } |
| 89 | |
| 90 | try { |
| 91 | /** @var Default_State $state */ |
| 92 | $state = $this->rep_get_item( Default_State::class ); |
| 93 | } catch ( Repository_Exception $exception ) { |
| 94 | wp_die( |
| 95 | esc_html__( 'Something went wrong', 'jet-form-builder' ), |
| 96 | esc_html__( 'Error', 'jet-form-builder' ) |
| 97 | ); |
| 98 | } |
| 99 | |
| 100 | $this->current->push( $state ); |
| 101 | } |
| 102 | |
| 103 | public function to_array(): array { |
| 104 | return Array_Tools::to_array( $this->rep_get_items() ); |
| 105 | } |
| 106 | |
| 107 | public function clear_current(): Render_State { |
| 108 | $this->current = new Render_States_Collection(); |
| 109 | |
| 110 | return $this; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * @return Render_States_Collection |
| 115 | */ |
| 116 | public function get_current(): Render_States_Collection { |
| 117 | return $this->current; |
| 118 | } |
| 119 | |
| 120 | |
| 121 | } |
| 122 |