| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\App\Modules\Onboarding\Validation; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
class User_Progress_Validator extends Base_Validator { |
| 10 |
|
| 11 |
private const ALLOWED_EXIT_TYPES = [ 'user_exit', 'unexpected', null, '' ]; |
| 12 |
|
| 13 |
protected function get_rules(): array { |
| 14 |
return [ |
| 15 |
'current_step' => [ |
| 16 |
'type' => 'int', |
| 17 |
], |
| 18 |
'completed_steps' => [ |
| 19 |
'type' => 'mixed_array', |
| 20 |
], |
| 21 |
'exit_type' => [ |
| 22 |
'type' => 'exit_type', |
| 23 |
'nullable' => true, |
| 24 |
], |
| 25 |
'complete_step' => [ |
| 26 |
'type' => 'string_or_int', |
| 27 |
], |
| 28 |
'skip_step' => [ |
| 29 |
'type' => 'bool', |
| 30 |
], |
| 31 |
'step_index' => [ |
| 32 |
'type' => 'int', |
| 33 |
], |
| 34 |
'total_steps' => [ |
| 35 |
'type' => 'int', |
| 36 |
], |
| 37 |
'start' => [ |
| 38 |
'type' => 'bool', |
| 39 |
], |
| 40 |
'complete' => [ |
| 41 |
'type' => 'bool', |
| 42 |
], |
| 43 |
'user_exit' => [ |
| 44 |
'type' => 'bool', |
| 45 |
], |
| 46 |
'starter_dismissed' => [ |
| 47 |
'type' => 'bool', |
| 48 |
], |
| 49 |
]; |
| 50 |
} |
| 51 |
|
| 52 |
protected function validate_field( string $field, $value, array $rule ) { |
| 53 |
$type = $rule['type'] ?? 'string'; |
| 54 |
|
| 55 |
switch ( $type ) { |
| 56 |
case 'exit_type': |
| 57 |
return $this->validate_exit_type( $value ); |
| 58 |
case 'string_or_int': |
| 59 |
return $this->validate_string_or_int( $field, $value ); |
| 60 |
case 'mixed_array': |
| 61 |
return $this->validate_mixed_array( $field, $value ); |
| 62 |
default: |
| 63 |
return parent::validate_field( $field, $value, $rule ); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
private function validate_exit_type( $value ) { |
| 68 |
if ( ! in_array( $value, self::ALLOWED_EXIT_TYPES, true ) ) { |
| 69 |
return $this->error( 'exit_type', 'Exit type is invalid.' ); |
| 70 |
} |
| 71 |
|
| 72 |
return '' === $value ? null : $value; |
| 73 |
} |
| 74 |
|
| 75 |
private function validate_string_or_int( string $field, $value ) { |
| 76 |
if ( is_numeric( $value ) ) { |
| 77 |
return (int) $value; |
| 78 |
} |
| 79 |
|
| 80 |
if ( is_string( $value ) ) { |
| 81 |
return sanitize_text_field( $value ); |
| 82 |
} |
| 83 |
|
| 84 |
return $this->error( $field, "{$field} must be a number or string." ); |
| 85 |
} |
| 86 |
|
| 87 |
private function validate_mixed_array( string $field, $value ) { |
| 88 |
if ( ! is_array( $value ) ) { |
| 89 |
return $this->error( $field, "{$field} must be an array." ); |
| 90 |
} |
| 91 |
|
| 92 |
return array_values( |
| 93 |
array_filter( |
| 94 |
array_map( |
| 95 |
static function ( $item ) { |
| 96 |
if ( is_numeric( $item ) ) { |
| 97 |
return (int) $item; |
| 98 |
} |
| 99 |
|
| 100 |
if ( is_string( $item ) ) { |
| 101 |
return sanitize_text_field( $item ); |
| 102 |
} |
| 103 |
|
| 104 |
return null; |
| 105 |
}, |
| 106 |
$value |
| 107 |
), |
| 108 |
static function ( $item ) { |
| 109 |
return null !== $item; |
| 110 |
} |
| 111 |
) |
| 112 |
); |
| 113 |
} |
| 114 |
} |
| 115 |
|