PluginProbe ʕ •ᴥ•ʔ
Advanced Database Cleaner – Optimize & Clean Database to Speed Up Site Performance / trunk
Advanced Database Cleaner – Optimize & Clean Database to Speed Up Site Performance vtrunk
4.2.0 trunk 1.0.0 1.1.0 1.1.1 1.2.0 1.2.1 1.2.2 1.2.3 1.3.0 1.3.1 1.3.5 1.3.6 1.3.7 2.0.0 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.0.5 4.0.6 4.0.7 4.1.0 4.1.1
advanced-database-cleaner / includes / utils / validator / class-adbc-automation-validator.php
advanced-database-cleaner / includes / utils / validator Last commit date
class-adbc-automation-validator.php 7 months ago class-adbc-common-validator.php 3 days ago class-adbc-selected-items-validator.php 3 months ago class-adbc-settings-validator.php 3 days ago class-adbc-tables-validator.php 7 months ago
class-adbc-automation-validator.php
163 lines
1 <?php
2
3 // Exit if accessed directly
4 if ( ! defined( 'ABSPATH' ) )
5 exit;
6
7 /**
8 * ADBC automation validator class.
9 *
10 * This class provides functions to validate and sanitize the automation data sent by the user to the endpoints.
11 */
12 class ADBC_Automation_Validator {
13
14 /**
15 * The required keys for the automation task structure.
16 *
17 * @var array<string, string>
18 */
19 private static $required_keys = [
20 'name' => 'is_valid_name',
21 'frequency' => 'is_valid_frequency',
22 'start_datetime' => 'is_valid_timestamp',
23 'type' => 'is_valid_automation_type',
24 'operations' => 'is_valid_operations',
25 'active' => 'is_valid_active_status',
26 ];
27
28 /**
29 * Validates the structure of the automation task details.
30 *
31 * @param array $task_details The task details to validate.
32 *
33 * @return bool Returns true if the task structure is valid, false otherwise.
34 */
35 public static function validate_task_structure( $task_details ) {
36
37 if ( ! is_array( $task_details ) || empty( $task_details ) ) {
38 return false;
39 }
40
41 foreach ( self::$required_keys as $key => $validation_method ) {
42 if ( ! array_key_exists( $key, $task_details ) || ! call_user_func( [ self::class, $validation_method ], $task_details[ $key ] ) ) {
43 return false;
44 }
45 }
46
47 return true;
48
49 }
50
51 /**
52 * Validates if the provided name is a valid non empty string.
53 *
54 * @param mixed $name
55 *
56 * @return bool
57 */
58 private static function is_valid_name( $name ) {
59 return is_string( $name ) && ! empty( $name );
60 }
61
62 /**
63 * Validates if the provided frequency is a valid ADBC schedule frequency.
64 *
65 * @param mixed $frequency
66 *
67 * @return bool
68 */
69 private static function is_valid_frequency( $frequency ) {
70 $adbc_frequencies = ADBC_Common_Utils::get_adbc_schedule_frequencies();
71 $adbc_frequencies[] = 'adbc_once'; // Include adbc_once as a valid frequency
72 return is_string( $frequency ) && in_array( $frequency, $adbc_frequencies );
73 }
74
75 /**
76 * Validates if the provided value is a valid UNIX timestamp (seconds).
77 *
78 * @param mixed $value
79 *
80 * @return bool
81 */
82 private static function is_valid_timestamp( $value ) {
83
84 if ( is_int( $value ) ) {
85 return $value > 0;
86 }
87
88 return false;
89
90 }
91
92 /**
93 * Validates if the provided automation type is valid.
94 *
95 * @param mixed $type
96 *
97 * @return bool
98 */
99 private static function is_valid_automation_type( $type ) {
100 $valid_types = [ 'general_cleanup' ];
101 return is_string( $type ) && in_array( $type, $valid_types );
102 }
103
104 /**
105 * Validates if the provided operations are valid general_cleanup operations.
106 * Validate the keep_last_config structure and values.
107 *
108 * @param mixed $operations
109 *
110 * @return bool
111 */
112 private static function is_valid_operations( $operations ) {
113
114 if ( ! is_array( $operations ) || empty( $operations ) )
115 return false;
116
117 foreach ( $operations as $items_type => $keep_last_config ) {
118
119 // Validate the general_cleanup items type
120 if ( ! ADBC_Cleanup_Type_Registry::is_valid_items_type( $items_type ) )
121 return false;
122
123 // Validate the keep_last_config value type
124 if ( ! is_string( $keep_last_config ) && ! is_array( $keep_last_config ) ) {
125 return false;
126 }
127
128 // Validate the keep_last_config in case of string
129 if ( is_string( $keep_last_config ) && ! in_array( $keep_last_config, [ 'no_keep_last', 'default' ], true ) ) {
130 return false;
131 }
132
133 // Validate the keep_last_config in case of array (custom keep_last value)
134 if ( is_array( $keep_last_config ) ) {
135
136 if ( count( $keep_last_config ) !== 2 )
137 return false;
138 if ( ! array_key_exists( 'type', $keep_last_config ) || ! array_key_exists( 'value', $keep_last_config ) )
139 return false;
140 if ( ! in_array( $keep_last_config['type'], [ 'days', 'items' ], true ) )
141 return false;
142 if ( ! is_int( $keep_last_config['value'] ) || $keep_last_config['value'] <= 0 )
143 return false;
144
145 }
146 }
147
148 return true;
149
150 }
151
152 /**
153 * Validates if the provided active status is a boolean.
154 *
155 * @param mixed $active
156 *
157 * @return bool
158 */
159 private static function is_valid_active_status( $active ) {
160 return is_bool( $active );
161 }
162
163 }