assets
2 years ago
interfaces
2 years ago
traits
2 years ago
abs-integer-sanitizer.php
2 years ago
abs-number-sanitizer.php
2 years ago
custom-sanitizer.php
2 years ago
email-sanitizer.php
2 years ago
integer-sanitizer.php
2 years ago
key-sanitizer.php
2 years ago
module.php
2 years ago
number-sanitizer.php
2 years ago
text-sanitizer.php
2 years ago
textarea-sanitizer.php
2 years ago
title-sanitizer.php
2 years ago
url-sanitizer.php
2 years ago
user-sanitizer.php
2 years ago
module.php
201 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace JFB_Modules\Sanitize_Value; |
| 5 | |
| 6 | // If this file is called directly, abort. |
| 7 | if ( ! defined( 'WPINC' ) ) { |
| 8 | die; |
| 9 | } |
| 10 | |
| 11 | use Jet_Form_Builder\Blocks\Block_Helper; |
| 12 | use Jet_Form_Builder\Exceptions\Repository_Exception; |
| 13 | use JFB_Components\Module\Base_Module_After_Install_It; |
| 14 | use JFB_Components\Module\Base_Module_Handle_It; |
| 15 | use JFB_Components\Module\Base_Module_Handle_Trait; |
| 16 | use JFB_Components\Module\Base_Module_It; |
| 17 | use JFB_Components\Module\Base_Module_Url_It; |
| 18 | use JFB_Components\Module\Base_Module_Url_Trait; |
| 19 | use JFB_Components\Repository\Repository_Pattern_Trait; |
| 20 | use JFB_Modules\Block_Parsers\Field_Data_Parser; |
| 21 | use JFB_Modules\Sanitize_Value\Interfaces\Value_Sanitizer_It; |
| 22 | use JFB_Modules\Sanitize_Value\Interfaces\Value_Sanitizer_Settings_It; |
| 23 | |
| 24 | final class Module implements |
| 25 | Base_Module_It, |
| 26 | Base_Module_Url_It, |
| 27 | Base_Module_Handle_It, |
| 28 | Base_Module_After_Install_It { |
| 29 | |
| 30 | use Base_Module_Handle_Trait; |
| 31 | use Base_Module_Url_Trait; |
| 32 | use Repository_Pattern_Trait; |
| 33 | |
| 34 | const SUPPORT_NAME = 'jetFBSanitizeValue'; |
| 35 | const ATTRIBUTE_NAME = 'sanitizeValue'; |
| 36 | |
| 37 | public function rep_item_id() { |
| 38 | return 'sanitize-value'; |
| 39 | } |
| 40 | |
| 41 | public function condition(): bool { |
| 42 | return true; |
| 43 | } |
| 44 | |
| 45 | public function rep_instances(): array { |
| 46 | return array( |
| 47 | new Email_Sanitizer(), |
| 48 | new Key_Sanitizer(), |
| 49 | new Text_Sanitizer(), |
| 50 | new Textarea_Sanitizer(), |
| 51 | new Title_Sanitizer(), |
| 52 | new Url_Sanitizer(), |
| 53 | new User_Sanitizer(), |
| 54 | new Integer_Sanitizer(), |
| 55 | new Number_Sanitizer(), |
| 56 | new Abs_Integer_Sanitizer(), |
| 57 | new Abs_Number_Sanitizer(), |
| 58 | new Custom_Sanitizer(), |
| 59 | ); |
| 60 | } |
| 61 | |
| 62 | public function on_install() { |
| 63 | $this->rep_install(); |
| 64 | |
| 65 | \WP_Block_Supports::get_instance()->register( |
| 66 | self::SUPPORT_NAME, |
| 67 | array( |
| 68 | 'register_attribute' => array( $this, 'register_support' ), |
| 69 | ) |
| 70 | ); |
| 71 | } |
| 72 | |
| 73 | public function on_uninstall() { |
| 74 | $this->rep_clear(); |
| 75 | \WP_Block_Supports::get_instance()->register( self::SUPPORT_NAME, array() ); |
| 76 | } |
| 77 | |
| 78 | public function init_hooks() { |
| 79 | add_action( 'jet-form-builder/editor-assets/before', array( $this, 'register_editor_scripts' ), 0 ); |
| 80 | |
| 81 | /** |
| 82 | * Sanitize values after 'block-parsers' module |
| 83 | * |
| 84 | * @see \JFB_Modules\Block_Parsers\Module::init_hooks |
| 85 | */ |
| 86 | add_action( 'jet-form-builder/validate-field', array( $this, 'sanitize_field' ) ); |
| 87 | } |
| 88 | |
| 89 | public function remove_hooks() { |
| 90 | remove_action( |
| 91 | 'jet-form-builder/editor-assets/before', |
| 92 | array( $this, 'register_editor_scripts' ), |
| 93 | 0 |
| 94 | ); |
| 95 | remove_action( 'jet-form-builder/validate-field', array( $this, 'sanitize_field' ) ); |
| 96 | } |
| 97 | |
| 98 | public function register_support( \WP_Block_Type $block_type ) { |
| 99 | // Setup attributes and styles within that if needed. |
| 100 | if ( ! $block_type->attributes ) { |
| 101 | $block_type->attributes = array(); |
| 102 | } |
| 103 | |
| 104 | if ( block_has_support( $block_type, array( self::SUPPORT_NAME ) ) && |
| 105 | ! array_key_exists( self::ATTRIBUTE_NAME, $block_type->attributes ) |
| 106 | ) { |
| 107 | $block_type->attributes[ self::ATTRIBUTE_NAME ] = array( |
| 108 | 'type' => 'array', |
| 109 | 'default' => array(), |
| 110 | ); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | public function register_editor_scripts() { |
| 115 | $handle = $this->get_handle(); |
| 116 | |
| 117 | wp_enqueue_script( |
| 118 | $handle, |
| 119 | $this->get_url( 'assets/build/js/editor.js' ), |
| 120 | array(), |
| 121 | jet_form_builder()->get_version(), |
| 122 | true |
| 123 | ); |
| 124 | |
| 125 | wp_localize_script( |
| 126 | $handle, |
| 127 | 'JetFBValueSanitizers', |
| 128 | $this->rep_get_keys() |
| 129 | ); |
| 130 | } |
| 131 | |
| 132 | public function sanitize_field( Field_Data_Parser $parser ) { |
| 133 | $sanitizers = $parser->get_setting( self::ATTRIBUTE_NAME ); |
| 134 | |
| 135 | // skip sanitize, if we don't have applied sanitizers or already have the errors |
| 136 | if ( ! is_array( $sanitizers ) || count( $parser->get_errors() ) ) { |
| 137 | return; |
| 138 | } |
| 139 | |
| 140 | $value = $parser->get_value(); |
| 141 | |
| 142 | /** @var Value_Sanitizer_It $sanitizer */ |
| 143 | foreach ( $this->iterate_sanitizers( $sanitizers ) as $sanitizer ) { |
| 144 | if ( ! is_array( $value ) || ! $value ) { |
| 145 | $sanitizer->do_sanitize( $parser ); |
| 146 | |
| 147 | continue; |
| 148 | } |
| 149 | |
| 150 | // save sanitized value in $value |
| 151 | foreach ( $value as &$value_item ) { |
| 152 | // set temp value |
| 153 | $parser->set_value( $value_item ); |
| 154 | |
| 155 | $sanitizer->do_sanitize( $parser ); |
| 156 | |
| 157 | // save sanitized value from list |
| 158 | $value_item = $parser->get_value(); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | // update value if we go in inner foreach |
| 163 | if ( is_array( $value ) && $value ) { |
| 164 | $parser->set_value( $value ); |
| 165 | } |
| 166 | |
| 167 | // value has changed, so we need to re-run validation |
| 168 | $parser->check_response(); |
| 169 | } |
| 170 | |
| 171 | private function iterate_sanitizers( array $sanitizers ): \Generator { |
| 172 | foreach ( $sanitizers as $sanitizer ) { |
| 173 | $type = is_array( $sanitizer ) ? ( $sanitizer['value'] ?? '' ) : $sanitizer; |
| 174 | |
| 175 | if ( ! $type ) { |
| 176 | continue; |
| 177 | } |
| 178 | |
| 179 | try { |
| 180 | $item = $this->rep_get_item( $type ); |
| 181 | } catch ( Repository_Exception $exception ) { |
| 182 | continue; |
| 183 | } |
| 184 | |
| 185 | if ( ! ( $item instanceof Value_Sanitizer_Settings_It ) || ! is_array( $sanitizer ) ) { |
| 186 | yield $item; |
| 187 | continue; |
| 188 | } |
| 189 | |
| 190 | unset( $sanitizer['value'] ); |
| 191 | $item = clone $item; |
| 192 | |
| 193 | $item->set_settings( $sanitizer ); |
| 194 | |
| 195 | yield $item; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | |
| 200 | } |
| 201 |