| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace Jet_Form_Builder\Blocks\Conditional_Block; |
| 5 |
|
| 6 |
use Jet_Form_Builder\Blocks\Conditional_Block\Condition_Types\Base_Condition_Type; |
| 7 |
use Jet_Form_Builder\Blocks\Conditional_Block\Condition_Types\Condition_Field_Item; |
| 8 |
use Jet_Form_Builder\Blocks\Conditional_Block\Condition_Types\Condition_Render_State_Item; |
| 9 |
use Jet_Form_Builder\Blocks\Conditional_Block\Condition_Types\Or_Operator_Item; |
| 10 |
use Jet_Form_Builder\Blocks\Conditional_Block\Rest_Api\Add_Render_State_Endpoint_Option; |
| 11 |
use Jet_Form_Builder\Blocks\Conditional_Block\Rest_Api\Delete_Render_States_Endpoint; |
| 12 |
use Jet_Form_Builder\Blocks\Exceptions\Condition_Exception; |
| 13 |
use Jet_Form_Builder\Blocks\Exceptions\Render_Empty_Field; |
| 14 |
use Jet_Form_Builder\Classes\Arrayable\Arrayable; |
| 15 |
use Jet_Form_Builder\Classes\Instance_Trait; |
| 16 |
use Jet_Form_Builder\Classes\Tools; |
| 17 |
use Jet_Form_Builder\Exceptions\Repository_Exception; |
| 18 |
|
| 19 |
// If this file is called directly, abort. |
| 20 |
if ( ! defined( 'WPINC' ) ) { |
| 21 |
die; |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* @method static Condition_Manager instance() |
| 26 |
* |
| 27 |
* Class Condition_Manager |
| 28 |
* @package Jet_Form_Builder\Blocks\Conditional_Block |
| 29 |
*/ |
| 30 |
class Condition_Manager implements Arrayable { |
| 31 |
|
| 32 |
use Instance_Trait; |
| 33 |
|
| 34 |
private $operators; |
| 35 |
private $functions; |
| 36 |
|
| 37 |
/** |
| 38 |
* @var Base_Condition_Type[] |
| 39 |
*/ |
| 40 |
private $types; |
| 41 |
|
| 42 |
private function __construct() { |
| 43 |
$this->operators = new Operators(); |
| 44 |
$this->functions = new Functions(); |
| 45 |
|
| 46 |
$this->types = apply_filters( |
| 47 |
'jet-form-builder/conditional-block/types', |
| 48 |
array( |
| 49 |
new Condition_Render_State_Item(), |
| 50 |
new Or_Operator_Item(), |
| 51 |
new Condition_Field_Item(), |
| 52 |
) |
| 53 |
); |
| 54 |
} |
| 55 |
|
| 56 |
public function prepare( array $conditions ): array { |
| 57 |
$response = array(); |
| 58 |
|
| 59 |
foreach ( $conditions as $condition ) { |
| 60 |
try { |
| 61 |
$item = $this->get_current_type( $condition ); |
| 62 |
} catch ( Repository_Exception $exception ) { |
| 63 |
continue; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Catch Condition_Exception on this level, |
| 68 |
* because in future we can implement "OR" operator |
| 69 |
* between all conditions. |
| 70 |
* |
| 71 |
* Here we could access the global option |
| 72 |
*/ |
| 73 |
try { |
| 74 |
$response[] = $item->to_array(); |
| 75 |
} catch ( Condition_Exception $exception ) { |
| 76 |
/** |
| 77 |
* This exception catches in |
| 78 |
* |
| 79 |
* @see \Jet_Form_Builder\Blocks\Types\Base::render_callback_field |
| 80 |
*/ |
| 81 |
throw new Render_Empty_Field( 'This is temporary.' ); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
return $response; |
| 86 |
} |
| 87 |
|
| 88 |
public function to_array(): array { |
| 89 |
return array( |
| 90 |
'functions' => Tools::with_placeholder( $this->get_functions()->to_array() ), |
| 91 |
'operators' => Tools::with_placeholder( $this->get_operators()->to_array() ), |
| 92 |
'render_states' => Render_State::instance()->to_array(), |
| 93 |
'rest_add_state' => Add_Render_State_Endpoint_Option::get_endpoint(), |
| 94 |
'rest_delete_state' => Delete_Render_States_Endpoint::get_endpoint(), |
| 95 |
); |
| 96 |
} |
| 97 |
|
| 98 |
public function get_operators(): Operators { |
| 99 |
return $this->operators; |
| 100 |
} |
| 101 |
|
| 102 |
public function get_functions(): Functions { |
| 103 |
return $this->functions; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* @param array $condition |
| 108 |
* |
| 109 |
* @return Base_Condition_Type |
| 110 |
* @throws Repository_Exception |
| 111 |
*/ |
| 112 |
protected function get_current_type( array $condition ): Base_Condition_Type { |
| 113 |
foreach ( $this->types as $type ) { |
| 114 |
if ( ! $type->is_supported( $condition ) ) { |
| 115 |
continue; |
| 116 |
} |
| 117 |
$type->init( $condition ); |
| 118 |
|
| 119 |
return $type; |
| 120 |
} |
| 121 |
|
| 122 |
throw new Repository_Exception( 'Undefined condition type' ); |
| 123 |
} |
| 124 |
|
| 125 |
} |
| 126 |
|