module.php
104 lines
| 1 | <?php |
| 2 | |
| 3 | namespace JFB_Modules\Fields_Render_Validator; |
| 4 | |
| 5 | use JFB_Components\Module\Base_Module_Dir_It; |
| 6 | use JFB_Components\Module\Base_Module_Dir_Trait; |
| 7 | use JFB_Components\Module\Base_Module_Handle_It; |
| 8 | use JFB_Components\Module\Base_Module_Handle_Trait; |
| 9 | use JFB_Components\Module\Base_Module_It; |
| 10 | use JFB_Components\Module\Base_Module_Url_It; |
| 11 | use JFB_Components\Module\Base_Module_Url_Trait; |
| 12 | use JFB_Components\Module\Base_Module_After_Install_It; |
| 13 | |
| 14 | |
| 15 | // If this file is called directly, abort. |
| 16 | if ( ! defined( 'WPINC' ) ) { |
| 17 | die; |
| 18 | } |
| 19 | |
| 20 | class Module implements |
| 21 | Base_Module_It, |
| 22 | Base_Module_Url_It, |
| 23 | Base_Module_Handle_It, |
| 24 | Base_Module_After_Install_It, |
| 25 | Base_Module_Dir_It { |
| 26 | |
| 27 | use Base_Module_Handle_Trait; |
| 28 | use Base_Module_Url_Trait; |
| 29 | use Base_Module_Dir_Trait; |
| 30 | |
| 31 | public function rep_item_id() { |
| 32 | return 'fields-render-validator'; |
| 33 | } |
| 34 | |
| 35 | public function condition(): bool { |
| 36 | return current_user_can( 'manage_options' ); |
| 37 | } |
| 38 | |
| 39 | protected $fields_stack = array(); |
| 40 | |
| 41 | public function init_hooks() { |
| 42 | add_filter( 'jet-form-builder/after-start-form', array( $this, 'reset_stack' ), 0 ); |
| 43 | add_filter( 'jet-form-builder/after-render-field', array( $this, 'update_stack' ), 0, 5 ); |
| 44 | } |
| 45 | |
| 46 | public function reset_stack( string $html ): string { |
| 47 | $this->fields_stack = array(); |
| 48 | return $html; |
| 49 | } |
| 50 | |
| 51 | public function update_stack( $output, $field_name, $attrs, $content, $wp_block ): string { |
| 52 | |
| 53 | if ( isset( $attrs['name'] ) && 'undefined' !== $attrs['name'] ) { |
| 54 | $name = $attrs['name']; |
| 55 | |
| 56 | $context = $wp_block->context ?? ''; |
| 57 | |
| 58 | /** |
| 59 | * do nothing if field_name is conditional-block |
| 60 | * |
| 61 | * @link https://github.com/Crocoblock/issues-tracker/issues/12874 |
| 62 | */ |
| 63 | if ( 'conditional-block' === $field_name ) { |
| 64 | return $output; |
| 65 | } |
| 66 | |
| 67 | if ( $context ) { |
| 68 | /** |
| 69 | * do nothing if context is jet-forms/conditional-block--name |
| 70 | * |
| 71 | * @link https://github.com/Crocoblock/issues-tracker/issues/12874 |
| 72 | */ |
| 73 | if ( isset( $context['jet-forms/conditional-block--name'] ) ) { |
| 74 | return $output; |
| 75 | } |
| 76 | $context_name = $context['jet-forms/repeater-field--name'] ?? ''; |
| 77 | $context_index = $context['jet-forms/repeater-row--current-index'] ?? ''; |
| 78 | if ( '' !== $context_name ) { |
| 79 | $name = $context_name . $context_index . $name; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | if ( in_array( $name, $this->fields_stack ) ) { |
| 84 | $parent = $context ? 'repeater' : 'form'; |
| 85 | $output .= "<div class='jet-form-builder__uniq-name-error' style='color:red;font-size: 12px;'>You already have field < " . esc_attr( $attrs['name'] ) . ' > in this ' . $parent . '. Please rename current field to avoid form processing errors.</div>'; |
| 86 | } else { |
| 87 | $this->fields_stack[] = $name; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | return $output; |
| 92 | } |
| 93 | |
| 94 | public function on_install() { |
| 95 | } |
| 96 | |
| 97 | public function on_uninstall() { |
| 98 | } |
| 99 | |
| 100 | public function remove_hooks() { |
| 101 | } |
| 102 | |
| 103 | } |
| 104 |