display-conditions
5 months ago
front
3 days ago
helpers
1 month ago
metas
3 months ago
multisite
5 months ago
palettes
3 years ago
provider
1 month ago
providers
3 days ago
templates
3 years ago
update
5 months ago
class-hustle-admin-page-abstract.php
1 month ago
class-hustle-auth-token.php
5 months ago
class-hustle-condition-factory.php
6 years ago
class-hustle-cross-sell.php
10 months ago
class-hustle-dashboard-admin.php
1 month ago
class-hustle-data.php
1 month ago
class-hustle-db.php
2 years ago
class-hustle-installer.php
4 years ago
class-hustle-meta.php
3 years ago
class-hustle-module-admin.php
1 month ago
class-hustle-module-collection.php
5 months ago
class-hustle-module-fields.php
3 months ago
class-hustle-module-page-abstract.php
1 month ago
class-hustle-module-validator.php
3 months ago
class-hustle-notifications.php
3 days ago
class-hustle-settings-admin.php
1 month ago
class-hustle-wp-dashboard-page.php
5 months ago
class-opt-in.php
3 days ago
hustle-background-conversion-log.php
3 months ago
hustle-deletion.php
3 months ago
hustle-embedded-admin.php
3 years ago
hustle-entries-admin.php
5 months ago
hustle-entry-model.php
1 month ago
hustle-general-data-protection.php
5 months ago
hustle-init.php
1 month ago
hustle-mail.php
5 months ago
hustle-migration.php
5 months ago
hustle-model.php
3 days ago
hustle-module-model.php
1 month ago
hustle-module-widget-legacy.php
5 months ago
hustle-module-widget.php
5 months ago
hustle-modules-common-admin-ajax.php
1 month ago
hustle-popup-admin.php
3 years ago
hustle-providers-admin.php
1 month ago
hustle-providers.php
5 months ago
hustle-settings-admin-ajax.php
1 month ago
hustle-settings-page.php
3 years ago
hustle-slidein-admin.php
3 years ago
hustle-sshare-admin.php
3 days ago
hustle-sshare-model.php
3 days ago
hustle-tracking-model.php
3 months ago
interface-hustle-auth-provider.php
5 months ago
opt-in-geo.php
5 months ago
opt-in-utils.php
3 days ago
opt-in-wpmudev-api.php
5 months ago
class-hustle-module-validator.php
80 lines
| 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 | /** |
| 3 | * Hustle_Module_Fields_Validator class file. |
| 4 | * |
| 5 | * @package Hustle |
| 6 | * @since 7.8.11 |
| 7 | */ |
| 8 | class Hustle_Module_Fields_Validator { |
| 9 | |
| 10 | /** |
| 11 | * Validates the module data before saving. |
| 12 | * |
| 13 | * @param array $fields The fields to validate, structured as [field_name] => rules. |
| 14 | * @param array $data The module data to validate. |
| 15 | * @return array An array containing 'is_valid' (boolean) and 'errors' (array) keys. |
| 16 | */ |
| 17 | public function validate( $fields, $data ) { |
| 18 | $errors = array(); |
| 19 | |
| 20 | foreach ( $fields as $field_name => $rules ) { |
| 21 | $value = isset( $data[ $field_name ] ) ? $data[ $field_name ] : null; |
| 22 | // Check for required fields. |
| 23 | if ( isset( $rules['required'] ) && $rules['required'] && empty( $value ) ) { |
| 24 | $errors[ $field_name ][] = sprintf( |
| 25 | esc_html__( 'The field is required.', 'hustle' ), |
| 26 | ); |
| 27 | continue; |
| 28 | } |
| 29 | |
| 30 | // Check for required_if condition. |
| 31 | if ( |
| 32 | isset( $rules['required_if'] ) && |
| 33 | isset( $data[ $rules['required_if'] ] ) |
| 34 | ) { |
| 35 | if ( 1 === (int) $data[ $rules['required_if'] ] ) { |
| 36 | if ( empty( $value ) ) { |
| 37 | $errors[ $field_name ][] = sprintf( |
| 38 | esc_html__( 'The field is required.', 'hustle' ), |
| 39 | ); |
| 40 | continue; |
| 41 | } |
| 42 | } else { |
| 43 | // If the condition is not met, skip further validation for this field. |
| 44 | continue; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | // If the field is not required and empty, skip further validation. |
| 49 | if ( empty( $value ) ) { |
| 50 | continue; |
| 51 | } |
| 52 | |
| 53 | // Validate field type. |
| 54 | switch ( $rules['type'] ) { |
| 55 | case 'email': |
| 56 | $list = array_map( 'trim', explode( ',', $value ) ); |
| 57 | foreach ( $list as $email ) { |
| 58 | if ( '{email}' === $email ) { |
| 59 | // If the placeholder is present, skip validation for this email. |
| 60 | continue; |
| 61 | } |
| 62 | |
| 63 | if ( ! filter_var( $email, FILTER_VALIDATE_EMAIL ) ) { |
| 64 | $errors[ $field_name ][] = esc_html__( 'Please enter a valid email address.', 'hustle' ); |
| 65 | break; |
| 66 | } |
| 67 | } |
| 68 | break; |
| 69 | default: |
| 70 | break; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | return array( |
| 75 | 'is_valid' => empty( $errors ), |
| 76 | 'errors' => $errors, |
| 77 | ); |
| 78 | } |
| 79 | } |
| 80 |