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