advanced-rules
1 year ago
messages
2 years ago
post-type
2 years ago
rest-api
1 year ago
ssr
1 year ago
module.php
1 year ago
rules-controller.php
2 years ago
module.php
375 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace JFB_Modules\Validation; |
| 5 | |
| 6 | use Jet_Form_Builder\Admin\Editor; |
| 7 | use Jet_Form_Builder\Blocks\Types\Base; |
| 8 | use Jet_Form_Builder\Classes\Arrayable\Array_Tools; |
| 9 | use Jet_Form_Builder\Classes\Tools; |
| 10 | use Jet_Form_Builder\Exceptions\Repository_Exception; |
| 11 | use Jet_Form_Builder\Plugin; |
| 12 | use JFB_Components\Module\Base_Module_After_Install_It; |
| 13 | use JFB_Components\Module\Base_Module_Dir_It; |
| 14 | use JFB_Components\Module\Base_Module_Dir_Trait; |
| 15 | use JFB_Components\Module\Base_Module_Handle_It; |
| 16 | use JFB_Components\Module\Base_Module_Handle_Trait; |
| 17 | use JFB_Components\Module\Base_Module_It; |
| 18 | use JFB_Components\Module\Base_Module_Url_It; |
| 19 | use JFB_Components\Module\Base_Module_Url_Trait; |
| 20 | use JFB_Modules\Block_Parsers\Field_Data_Parser; |
| 21 | |
| 22 | // If this file is called directly, abort. |
| 23 | if ( ! defined( 'WPINC' ) ) { |
| 24 | die; |
| 25 | } |
| 26 | |
| 27 | final class Module implements |
| 28 | Base_Module_It, |
| 29 | Base_Module_Handle_It, |
| 30 | Base_Module_Url_It, |
| 31 | Base_Module_Dir_It, |
| 32 | Base_Module_After_Install_It { |
| 33 | |
| 34 | use Base_Module_Url_Trait; |
| 35 | use Base_Module_Handle_Trait; |
| 36 | use Base_Module_Dir_Trait; |
| 37 | |
| 38 | const FORMAT_ADVANCED = 'advanced'; |
| 39 | const FORMAT_BROWSER = 'browser'; |
| 40 | const HANDLE = 'jet-fb-advanced-reporting'; |
| 41 | |
| 42 | private $messages = array(); |
| 43 | /** |
| 44 | * @var Rules_Controller |
| 45 | */ |
| 46 | private $rules; |
| 47 | private $settings; |
| 48 | private $inline_messages = array(); |
| 49 | |
| 50 | public function rep_item_id() { |
| 51 | return 'validation'; |
| 52 | } |
| 53 | |
| 54 | public function condition(): bool { |
| 55 | return true; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * @throws Repository_Exception |
| 60 | */ |
| 61 | public function on_install() { |
| 62 | /** @var \JFB_Modules\Post_Type\Module $post_type */ |
| 63 | $post_type = jet_form_builder()->module( 'post-type' ); |
| 64 | $post_type->get_meta()->install( new Post_Type\Validation_Meta() ); |
| 65 | |
| 66 | /** @var \JFB_Modules\Rest_Api\Module $rest_api */ |
| 67 | $rest_api = jet_form_builder()->module( 'rest-api' ); |
| 68 | $rest_api->get_controller()->install( new Rest_Api\Rest_Validation_Endpoint() ); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * @throws Repository_Exception |
| 73 | */ |
| 74 | public function on_uninstall() { |
| 75 | $this->rules = null; |
| 76 | $this->messages = array(); |
| 77 | |
| 78 | /** @var \JFB_Modules\Post_Type\Module $post_type */ |
| 79 | $post_type = jet_form_builder()->module( 'post-type' ); |
| 80 | $post_type->get_meta()->uninstall( Post_Type\Validation_Meta::class ); |
| 81 | |
| 82 | /** @var \JFB_Modules\Rest_Api\Module $rest_api */ |
| 83 | $rest_api = jet_form_builder()->module( 'rest-api' ); |
| 84 | $rest_api->get_controller()->uninstall( new Rest_Api\Rest_Validation_Endpoint() ); |
| 85 | } |
| 86 | |
| 87 | public function init_hooks() { |
| 88 | add_filter( |
| 89 | 'jet-form-builder/before-start-form', |
| 90 | array( $this, 'add_validation_messages_global' ) |
| 91 | ); |
| 92 | |
| 93 | add_action( |
| 94 | 'jet-form-builder/before-start-form-row', |
| 95 | array( $this, 'add_validation_block' ) |
| 96 | ); |
| 97 | add_action( |
| 98 | 'wp_enqueue_scripts', |
| 99 | array( $this, 'register_scripts' ) |
| 100 | ); |
| 101 | |
| 102 | /** |
| 103 | * @link https://github.com/Crocoblock/issues-tracker/issues/1542 |
| 104 | */ |
| 105 | add_action( |
| 106 | 'jet_plugins/frontend/register_scripts', |
| 107 | array( $this, 'register_scripts' ) |
| 108 | ); |
| 109 | add_action( |
| 110 | 'jet-form-builder/validate-field', |
| 111 | array( $this, 'validate_block' ), |
| 112 | 0 |
| 113 | ); |
| 114 | add_action( |
| 115 | 'jet-form-builder/editor-assets/before', |
| 116 | array( $this, 'localize_editor_config' ) |
| 117 | ); |
| 118 | } |
| 119 | |
| 120 | public function remove_hooks() { |
| 121 | remove_filter( |
| 122 | 'jet-form-builder/before-start-form', |
| 123 | array( $this, 'add_validation_messages_global' ) |
| 124 | ); |
| 125 | remove_action( |
| 126 | 'jet-form-builder/before-start-form-row', |
| 127 | array( $this, 'add_validation_block' ) |
| 128 | ); |
| 129 | remove_action( |
| 130 | 'wp_enqueue_scripts', |
| 131 | array( $this, 'register_scripts' ) |
| 132 | ); |
| 133 | |
| 134 | /** |
| 135 | * @link https://github.com/Crocoblock/issues-tracker/issues/1542 |
| 136 | */ |
| 137 | remove_action( |
| 138 | 'jet_plugins/frontend/register_scripts', |
| 139 | array( $this, 'register_scripts' ) |
| 140 | ); |
| 141 | remove_action( |
| 142 | 'jet-form-builder/validate-field', |
| 143 | array( $this, 'validate_block' ), |
| 144 | 0 |
| 145 | ); |
| 146 | remove_action( |
| 147 | 'jet-form-builder/editor-assets/before', |
| 148 | array( $this, 'localize_editor_config' ) |
| 149 | ); |
| 150 | } |
| 151 | |
| 152 | public function register_scripts() { |
| 153 | $script_asset = require_once jet_form_builder()->plugin_dir( |
| 154 | 'assets/build/frontend/advanced.reporting.asset.php' |
| 155 | ); |
| 156 | |
| 157 | if ( true === $script_asset ) { |
| 158 | return; |
| 159 | } |
| 160 | |
| 161 | array_push( |
| 162 | $script_asset['dependencies'], |
| 163 | \Jet_Form_Builder\Blocks\Module::MAIN_SCRIPT_HANDLE |
| 164 | ); |
| 165 | |
| 166 | wp_register_script( |
| 167 | self::HANDLE, |
| 168 | jet_form_builder()->plugin_url( 'assets/build/frontend/advanced.reporting.js' ), |
| 169 | $script_asset['dependencies'], |
| 170 | $script_asset['version'], |
| 171 | true |
| 172 | ); |
| 173 | } |
| 174 | |
| 175 | public function add_validation_messages_global( string $markup, bool $force = false ): string { |
| 176 | $this->settings = $this->get_settings(); |
| 177 | $form_id = jet_fb_live()->form_id; |
| 178 | |
| 179 | if ( |
| 180 | ( ! $this->is_advanced_form() && ! $force ) || |
| 181 | in_array( $form_id, $this->inline_messages, true ) |
| 182 | ) { |
| 183 | return $markup; |
| 184 | } |
| 185 | |
| 186 | $data = Tools::encode_json( $this->settings ); |
| 187 | |
| 188 | wp_enqueue_script( self::HANDLE ); |
| 189 | |
| 190 | wp_add_inline_script( |
| 191 | \Jet_Form_Builder\Blocks\Module::MAIN_SCRIPT_HANDLE, |
| 192 | " |
| 193 | window.JetFormsValidation = window.JetFormsValidation ?? {}; |
| 194 | window.JetFormsValidation[ {$form_id} ] = $data; |
| 195 | " |
| 196 | ); |
| 197 | |
| 198 | add_action( |
| 199 | 'wp_enqueue_scripts', |
| 200 | function () use ( $form_id, $data ) { |
| 201 | wp_add_inline_script( |
| 202 | \Jet_Form_Builder\Blocks\Module::MAIN_SCRIPT_HANDLE, |
| 203 | " |
| 204 | window.JetFormsValidation = window.JetFormsValidation ?? {}; |
| 205 | window.JetFormsValidation[ {$form_id} ] = $data; |
| 206 | " |
| 207 | ); |
| 208 | }, |
| 209 | 20 |
| 210 | ); |
| 211 | |
| 212 | $this->inline_messages[] = $form_id; |
| 213 | |
| 214 | return $markup; |
| 215 | } |
| 216 | |
| 217 | public function add_validation_block( Base $block ) { |
| 218 | /** |
| 219 | * If in post meta enable Advanced validation |
| 220 | * or right in block settings |
| 221 | */ |
| 222 | if ( ! $this->is_advanced( $block->block_attrs ) ) { |
| 223 | return; |
| 224 | } |
| 225 | |
| 226 | $this->add_validation_messages_global( '', true ); |
| 227 | |
| 228 | $type = $block->block_attrs['validation']['type'] ?? ''; |
| 229 | $rules = $block->block_attrs['validation']['rules'] ?? array(); |
| 230 | |
| 231 | $block->add_attribute( 'data-validation-type', $type ?: 'inherit' ); |
| 232 | |
| 233 | if ( ! empty( $rules ) ) { |
| 234 | $this->get_rules()->prepare_rules( $rules ); |
| 235 | |
| 236 | $block->add_attribute( |
| 237 | 'data-validation-rules', |
| 238 | Tools::encode_json( $rules ) |
| 239 | ); |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * If advanced validation not enabled right in block settings |
| 244 | */ |
| 245 | if ( self::FORMAT_ADVANCED !== $type ) { |
| 246 | return; |
| 247 | } |
| 248 | |
| 249 | $messages = $block->block_attrs['validation']['messages'] ?? array(); |
| 250 | |
| 251 | if ( ! empty( $messages ) ) { |
| 252 | $block->add_attribute( 'data-validation-messages', Tools::encode_json( $messages ) ); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | public function get_settings(): array { |
| 257 | /** @var \JFB_Modules\Post_Type\Module $module */ |
| 258 | /** @noinspection PhpUnhandledExceptionInspection */ |
| 259 | $module = jet_form_builder()->module( 'post-type' ); |
| 260 | $validation = $module->query_meta( Post_Type\Validation_Meta::class ); |
| 261 | |
| 262 | $response = array( |
| 263 | 'type' => $validation['type'] ?? self::FORMAT_BROWSER, |
| 264 | 'messages' => array(), |
| 265 | ); |
| 266 | |
| 267 | $messages = $this->get_messages(); |
| 268 | |
| 269 | foreach ( $messages as $message ) { |
| 270 | $response['messages'][ $message->get_id() ] = ( |
| 271 | $validation['messages'][ $message->get_id() ] ?? $message->get_initial() |
| 272 | ); |
| 273 | } |
| 274 | |
| 275 | return $response; |
| 276 | } |
| 277 | |
| 278 | public function is_advanced( array $block_attrs ): bool { |
| 279 | $type = $block_attrs['validation']['type'] ?? ''; |
| 280 | |
| 281 | return $type |
| 282 | ? self::FORMAT_ADVANCED === $type |
| 283 | : $this->is_advanced_form(); |
| 284 | } |
| 285 | |
| 286 | public function is_advanced_form(): bool { |
| 287 | if ( is_null( $this->settings ) ) { |
| 288 | $this->settings = $this->get_settings(); |
| 289 | } |
| 290 | |
| 291 | return self::FORMAT_ADVANCED === ( $this->settings['type'] ?? '' ); |
| 292 | } |
| 293 | |
| 294 | public function formats(): array { |
| 295 | return array( |
| 296 | array( |
| 297 | 'value' => self::FORMAT_BROWSER, |
| 298 | 'label' => __( 'Default', 'jet-form-builder' ), |
| 299 | 'title' => __( 'Browser native validation', 'jet-form-builder' ), |
| 300 | ), |
| 301 | array( |
| 302 | 'value' => self::FORMAT_ADVANCED, |
| 303 | 'label' => __( 'Advanced', 'jet-form-builder' ), |
| 304 | 'title' => __( 'More flexible JetFormBuilder\'s validation', 'jet-form-builder' ), |
| 305 | ), |
| 306 | ); |
| 307 | } |
| 308 | |
| 309 | public function validate_block( Field_Data_Parser $parser ) { |
| 310 | if ( |
| 311 | ! $this->is_advanced( $parser->get_settings() ) || |
| 312 | ! $parser->get_value() || |
| 313 | $parser->is_inside_conditional() |
| 314 | ) { |
| 315 | return; |
| 316 | } |
| 317 | |
| 318 | $this->get_rules()->validate_block( $parser ); |
| 319 | } |
| 320 | |
| 321 | public function localize_editor_config() { |
| 322 | wp_localize_script( |
| 323 | Editor::EDITOR_PACKAGE_HANDLE, |
| 324 | 'jetFormValidation', |
| 325 | array( |
| 326 | 'messages' => Array_Tools::to_array( $this->get_messages() ), |
| 327 | 'ssr_callbacks' => Array_Tools::to_array( $this->get_rules()->get_ssr()->get_callbacks() ), |
| 328 | 'formats' => $this->formats(), |
| 329 | 'rule_types' => Array_Tools::to_array( $this->get_rules()->rep_get_values() ), |
| 330 | ) |
| 331 | ); |
| 332 | } |
| 333 | |
| 334 | /** |
| 335 | * @return Rules_Controller |
| 336 | */ |
| 337 | public function get_rules(): Rules_Controller { |
| 338 | if ( ! is_null( $this->rules ) ) { |
| 339 | return $this->rules; |
| 340 | } |
| 341 | |
| 342 | $this->rules = new Rules_Controller(); |
| 343 | |
| 344 | return $this->rules; |
| 345 | } |
| 346 | |
| 347 | public function get_messages(): array { |
| 348 | if ( ! empty( $this->messages ) ) { |
| 349 | return $this->messages; |
| 350 | } |
| 351 | |
| 352 | $this->messages = apply_filters( |
| 353 | 'jet-form-builder/validation-messages', |
| 354 | array( |
| 355 | new Messages\Is_Empty_Value(), |
| 356 | new Messages\Is_Number_Min(), |
| 357 | new Messages\Is_Number_Max(), |
| 358 | new Messages\Is_Char_Min(), |
| 359 | new Messages\Is_Char_Max(), |
| 360 | new Messages\Is_Not_Valid_Email(), |
| 361 | new Messages\Is_Not_Valid_Url(), |
| 362 | new Messages\Is_Not_Complete_Mask(), |
| 363 | new Messages\Is_Files_Max(), |
| 364 | new Messages\Is_File_Size(), |
| 365 | new Messages\Is_File_Ext(), |
| 366 | new Messages\Is_Date_Min(), |
| 367 | new Messages\Is_Date_Max(), |
| 368 | ) |
| 369 | ); |
| 370 | |
| 371 | return $this->messages; |
| 372 | } |
| 373 | |
| 374 | } |
| 375 |