condition-manager.php
257 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Actions\Conditions; |
| 5 | |
| 6 | use Jet_Form_Builder\Exceptions\Condition_Exception; |
| 7 | use Jet_Form_Builder\Exceptions\Condition_Silence_Exception; |
| 8 | |
| 9 | // If this file is called directly, abort. |
| 10 | if ( ! defined( 'WPINC' ) ) { |
| 11 | die; |
| 12 | } |
| 13 | |
| 14 | class Condition_Manager { |
| 15 | |
| 16 | const THROW_IF_ONE_MATCH = 'throw_out_if_at_least_one_match'; |
| 17 | const THROW_IF_ONE_WRONG = 'throw_out_if_at_least_one_wrong'; |
| 18 | const TRANSFORM_DATE_TO_TIMESTAMP = 'date_to_timestamp'; |
| 19 | |
| 20 | /** @var Condition_Instance[] */ |
| 21 | private $conditions; |
| 22 | private $operator; |
| 23 | private $settings; |
| 24 | |
| 25 | private function settings(): array { |
| 26 | return array( |
| 27 | 'item' => array( |
| 28 | 'execute' => true, |
| 29 | 'operator' => '', |
| 30 | 'field' => '', |
| 31 | 'default' => '', |
| 32 | 'compare_value_format_type' => '', |
| 33 | 'compare_value_format_custom' => '', |
| 34 | ), |
| 35 | 'compare_value_formats' => array( |
| 36 | array( |
| 37 | 'label' => '--', |
| 38 | 'value' => '', |
| 39 | ), |
| 40 | array( |
| 41 | 'value' => self::TRANSFORM_DATE_TO_TIMESTAMP, |
| 42 | 'callback' => 'strtotime', |
| 43 | 'label' => __( 'String to Timestamp', 'jet-form-builder' ), |
| 44 | 'help' => sprintf( |
| 45 | // phpcs:ignore WordPress.WP.I18n.MissingTranslatorsComment |
| 46 | __( 'Useful when comparing dates. See more here: %1$sstrtotime%2$s', 'jet-form-builder' ), |
| 47 | '<a target="_blank" href="https://www.php.net/manual/en/function.strtotime.php">', |
| 48 | '</a>' |
| 49 | ), |
| 50 | ), |
| 51 | ), |
| 52 | 'help_for_exploding_compare' => __( 'List the values separated by commas', 'jet-form-builder' ), |
| 53 | 'operators' => array( |
| 54 | array( |
| 55 | 'label' => '--', |
| 56 | 'value' => '', |
| 57 | ), |
| 58 | array( |
| 59 | 'label' => __( 'Equal', 'jet-form-builder' ), |
| 60 | 'value' => 'equal', |
| 61 | ), |
| 62 | array( |
| 63 | 'label' => __( 'Greater than', 'jet-form-builder' ), |
| 64 | 'value' => 'greater', |
| 65 | ), |
| 66 | array( |
| 67 | 'label' => __( 'Less than', 'jet-form-builder' ), |
| 68 | 'value' => 'less', |
| 69 | ), |
| 70 | array( |
| 71 | 'label' => __( 'Between', 'jet-form-builder' ), |
| 72 | 'value' => 'between', |
| 73 | 'need_explode' => true, |
| 74 | ), |
| 75 | array( |
| 76 | 'label' => __( 'In the list', 'jet-form-builder' ), |
| 77 | 'value' => 'one_of', |
| 78 | 'need_explode' => true, |
| 79 | ), |
| 80 | array( |
| 81 | 'label' => __( 'Contain text', 'jet-form-builder' ), |
| 82 | 'value' => 'contain', |
| 83 | ), |
| 84 | ), |
| 85 | ); |
| 86 | } |
| 87 | |
| 88 | public function get_transformers( $value_key = 'value' ): array { |
| 89 | return $this->get_settings_list( 'compare_value_formats', $value_key ); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * @param $key |
| 94 | * @param $must_be_value |
| 95 | * @param string $return_key |
| 96 | * |
| 97 | * @return array |
| 98 | */ |
| 99 | public function get_transformers_find( $key, $must_be_value ): array { |
| 100 | return $this->get_settings_list_find( $key, $must_be_value, 'compare_value_formats' ); |
| 101 | } |
| 102 | |
| 103 | public function get_operators_list( $value_key = 'value' ): array { |
| 104 | return $this->get_settings_list( 'operators', $value_key ); |
| 105 | } |
| 106 | |
| 107 | public function get_operators_filter( $key, $must_be_value, $return_key = 'value' ) { |
| 108 | return $this->get_settings_list_filter( $key, $must_be_value, 'operators', $return_key ); |
| 109 | } |
| 110 | |
| 111 | |
| 112 | /** |
| 113 | * @throws Condition_Exception |
| 114 | */ |
| 115 | public function check_all() { |
| 116 | if ( empty( $this->conditions ) ) { |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | foreach ( $this->conditions as $condition ) { |
| 121 | try { |
| 122 | $condition->is_correct_with_throw(); |
| 123 | |
| 124 | } catch ( Condition_Silence_Exception $exception ) { |
| 125 | switch ( $exception->getMessage() ) { |
| 126 | case self::THROW_IF_ONE_MATCH: |
| 127 | return; |
| 128 | case self::THROW_IF_ONE_WRONG: |
| 129 | default: |
| 130 | throw new Condition_Exception( |
| 131 | esc_html( $exception->getMessage() ), |
| 132 | // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 133 | ...$exception->get_additional() |
| 134 | ); |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | if ( 'or' === $this->operator ) { |
| 140 | throw new Condition_Exception( esc_html__( 'None of the conditions are met', 'jet-form-builder' ) ); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | public function set_conditions( array $conditions ): Condition_Manager { |
| 145 | $instance = new Condition_Instance(); |
| 146 | |
| 147 | foreach ( $conditions as $condition ) { |
| 148 | $this->conditions[] = ( clone $instance )->set_condition( $condition ); |
| 149 | } |
| 150 | |
| 151 | return $this; |
| 152 | } |
| 153 | |
| 154 | public function set_condition_operator( string $operator ): Condition_Manager { |
| 155 | $this->operator = $operator; |
| 156 | |
| 157 | return $this; |
| 158 | } |
| 159 | |
| 160 | protected function set_fields_conditions(): Condition_Manager { |
| 161 | return $this; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * @param $is_success |
| 166 | * @param mixed ...$additional |
| 167 | * |
| 168 | * @throws Condition_Silence_Exception |
| 169 | */ |
| 170 | public function throw_by_method( $is_success, ...$additional ) { |
| 171 | switch ( $this->operator ) { |
| 172 | case 'or': |
| 173 | if ( $is_success ) { |
| 174 | throw new Condition_Silence_Exception( |
| 175 | // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 176 | self::THROW_IF_ONE_MATCH, |
| 177 | ...$additional // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 178 | ); |
| 179 | } |
| 180 | break; |
| 181 | case 'and': |
| 182 | if ( ! $is_success ) { |
| 183 | throw new Condition_Silence_Exception( |
| 184 | // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 185 | self::THROW_IF_ONE_WRONG, |
| 186 | ...$additional // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 187 | ); |
| 188 | } |
| 189 | break; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | public function get_settings() { |
| 194 | if ( ! $this->settings ) { |
| 195 | $this->settings = apply_filters( |
| 196 | 'jet-form-builder/register/action-condition-settings', |
| 197 | $this->settings() |
| 198 | ); |
| 199 | } |
| 200 | |
| 201 | return $this->settings; |
| 202 | } |
| 203 | |
| 204 | public function get_settings_list( $list_key, $return_key = 'value' ): array { |
| 205 | $list = $this->get_settings()[ $list_key ] ?? array(); |
| 206 | |
| 207 | return array_map( |
| 208 | function ( $operator ) use ( $return_key ) { |
| 209 | return $operator[ $return_key ] ?? false; |
| 210 | }, |
| 211 | $list |
| 212 | ); |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * @param $key |
| 217 | * @param $must_be_value |
| 218 | * @param $list_key |
| 219 | * |
| 220 | * @return false|array |
| 221 | */ |
| 222 | public function get_settings_list_find( $key, $must_be_value, $list_key ) { |
| 223 | $list = $this->get_settings()[ $list_key ] ?? array(); |
| 224 | |
| 225 | foreach ( $list as $item ) { |
| 226 | if ( ( $item[ $key ] ?? false ) === $must_be_value ) { |
| 227 | return $item; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | return false; |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * @param $key |
| 236 | * @param $must_be_value |
| 237 | * @param $list_key |
| 238 | * @param string $return_key |
| 239 | * |
| 240 | * @return array |
| 241 | */ |
| 242 | public function get_settings_list_filter( $key, $must_be_value, $list_key, $return_key = 'value' ) { |
| 243 | $list = $this->get_settings()[ $list_key ] ?? array(); |
| 244 | $filtered = array(); |
| 245 | |
| 246 | foreach ( $list as $item ) { |
| 247 | if ( ( $item[ $key ] ?? false ) === $must_be_value ) { |
| 248 | $filtered[] = ( false !== $return_key ) ? ( $item[ $return_key ] ?? false ) : $item; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | return array_filter( $filtered ); |
| 253 | } |
| 254 | |
| 255 | |
| 256 | } |
| 257 |