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