| 1 |
<?php |
| 2 |
/** |
| 3 |
* Commerce7 Block Validation Helper |
| 4 |
* |
| 5 |
* @package wp-commerce7 |
| 6 |
* @author Michael Bourne |
| 7 |
* @license GPL3 |
| 8 |
* @link https://ursa6.com |
| 9 |
* @since 1.0.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
if ( ! defined( 'ABSPATH' ) ) { |
| 13 |
exit; |
| 14 |
} |
| 15 |
|
| 16 |
/** |
| 17 |
* Validation helper class for Commerce7 blocks and widgets |
| 18 |
*/ |
| 19 |
class C7WP_Validation { |
| 20 |
|
| 21 |
/** |
| 22 |
* Validate a slug format |
| 23 |
* |
| 24 |
* @param string $slug The slug to validate. |
| 25 |
* @return array Validation result with 'valid' boolean and 'message' string. |
| 26 |
*/ |
| 27 |
public static function validate_slug( $slug ) { |
| 28 |
$result = array( |
| 29 |
'valid' => true, |
| 30 |
'message' => '', |
| 31 |
); |
| 32 |
|
| 33 |
if ( empty( $slug ) ) { |
| 34 |
$result['valid'] = false; |
| 35 |
$result['message'] = __( 'Slug cannot be empty.', 'wp-commerce7' ); |
| 36 |
return $result; |
| 37 |
} |
| 38 |
|
| 39 |
// Check length |
| 40 |
if ( strlen( $slug ) > 50 ) { |
| 41 |
$result['valid'] = false; |
| 42 |
$result['message'] = __( 'Slug must be 50 characters or less.', 'wp-commerce7' ); |
| 43 |
return $result; |
| 44 |
} |
| 45 |
|
| 46 |
// Check for valid characters (alphanumeric, hyphens, underscores) |
| 47 |
if ( ! preg_match( '/^[a-zA-Z0-9_-]+$/', $slug ) ) { |
| 48 |
$result['valid'] = false; |
| 49 |
$result['message'] = __( 'Slug can only contain letters, numbers, hyphens, and underscores.', 'wp-commerce7' ); |
| 50 |
return $result; |
| 51 |
} |
| 52 |
|
| 53 |
// Check for reserved words |
| 54 |
$reserved_words = array( 'admin', 'api', 'wp-admin', 'wp-content', 'wp-includes', 'feed', 'comments', 'search', 'author', 'archives', 'attachment', 'page', 'paged', 'embed', 'trackback', 'xmlrpc' ); |
| 55 |
if ( in_array( strtolower( $slug ), $reserved_words, true ) ) { |
| 56 |
$result['valid'] = false; |
| 57 |
$result['message'] = __( 'This slug is reserved and cannot be used.', 'wp-commerce7' ); |
| 58 |
return $result; |
| 59 |
} |
| 60 |
|
| 61 |
return $result; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Validate club data array |
| 66 |
* |
| 67 |
* @param array $clubs Array of club data. |
| 68 |
* @return array Validation result with 'valid' boolean and 'messages' array. |
| 69 |
*/ |
| 70 |
public static function validate_club_data( $clubs ) { |
| 71 |
$result = array( |
| 72 |
'valid' => true, |
| 73 |
'messages' => array(), |
| 74 |
); |
| 75 |
|
| 76 |
if ( ! is_array( $clubs ) || empty( $clubs ) ) { |
| 77 |
$result['valid'] = false; |
| 78 |
$result['messages'][] = __( 'At least one club is required.', 'wp-commerce7' ); |
| 79 |
return $result; |
| 80 |
} |
| 81 |
|
| 82 |
$slugs = array(); |
| 83 |
foreach ( $clubs as $index => $club ) { |
| 84 |
$club_num = $index + 1; |
| 85 |
|
| 86 |
// Validate slug |
| 87 |
if ( empty( $club['slug'] ) ) { |
| 88 |
$result['valid'] = false; |
| 89 |
/* translators: %d: Club number. */ |
| 90 |
$result['messages'][] = sprintf( __( 'Club #%d has no slug.', 'wp-commerce7' ), $club_num ); |
| 91 |
} else { |
| 92 |
$slug_validation = self::validate_slug( $club['slug'] ); |
| 93 |
if ( ! $slug_validation['valid'] ) { |
| 94 |
$result['valid'] = false; |
| 95 |
/* translators: 1: Club number. 2: Validation error message. */ |
| 96 |
$result['messages'][] = sprintf( __( 'Club #%1$d: %2$s', 'wp-commerce7' ), $club_num, $slug_validation['message'] ); |
| 97 |
} |
| 98 |
|
| 99 |
// Check for duplicate slugs |
| 100 |
$slug_lower = strtolower( $club['slug'] ); |
| 101 |
if ( in_array( $slug_lower, $slugs, true ) ) { |
| 102 |
$result['valid'] = false; |
| 103 |
/* translators: %d: Club number. */ |
| 104 |
$result['messages'][] = sprintf( __( 'Club #%d has a duplicate slug.', 'wp-commerce7' ), $club_num ); |
| 105 |
} |
| 106 |
$slugs[] = $slug_lower; |
| 107 |
} |
| 108 |
|
| 109 |
// Validate name |
| 110 |
if ( empty( $club['name'] ) ) { |
| 111 |
$result['valid'] = false; |
| 112 |
/* translators: %d: Club number. */ |
| 113 |
$result['messages'][] = sprintf( __( 'Club #%d has no name.', 'wp-commerce7' ), $club_num ); |
| 114 |
} elseif ( strlen( $club['name'] ) > 100 ) { |
| 115 |
$result['valid'] = false; |
| 116 |
/* translators: %d: Club number. */ |
| 117 |
$result['messages'][] = sprintf( __( 'Club #%d name must be 100 characters or less.', 'wp-commerce7' ), $club_num ); |
| 118 |
} |
| 119 |
|
| 120 |
// Validate button text |
| 121 |
if ( empty( $club['buttonText'] ) ) { |
| 122 |
$result['valid'] = false; |
| 123 |
/* translators: %d: Club number. */ |
| 124 |
$result['messages'][] = sprintf( __( 'Club #%d has no button text.', 'wp-commerce7' ), $club_num ); |
| 125 |
} elseif ( strlen( $club['buttonText'] ) > 50 ) { |
| 126 |
$result['valid'] = false; |
| 127 |
/* translators: %d: Club number. */ |
| 128 |
$result['messages'][] = sprintf( __( 'Club #%d button text must be 50 characters or less.', 'wp-commerce7' ), $club_num ); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
return $result; |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Validate collection slug |
| 137 |
* |
| 138 |
* @param string $slug The collection slug to validate. |
| 139 |
* @return array Validation result with 'valid' boolean and 'message' string. |
| 140 |
*/ |
| 141 |
public static function validate_collection_slug( $slug ) { |
| 142 |
$result = array( |
| 143 |
'valid' => true, |
| 144 |
'message' => '', |
| 145 |
); |
| 146 |
|
| 147 |
if ( empty( $slug ) ) { |
| 148 |
$result['valid'] = false; |
| 149 |
$result['message'] = __( 'Collection slug cannot be empty.', 'wp-commerce7' ); |
| 150 |
return $result; |
| 151 |
} |
| 152 |
|
| 153 |
return self::validate_slug( $slug ); |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Validate form ID |
| 158 |
* |
| 159 |
* @param string $form_id The form ID to validate. |
| 160 |
* @return array Validation result with 'valid' boolean and 'message' string. |
| 161 |
*/ |
| 162 |
public static function validate_form_id( $form_id ) { |
| 163 |
$result = array( |
| 164 |
'valid' => true, |
| 165 |
'message' => '', |
| 166 |
); |
| 167 |
|
| 168 |
if ( empty( $form_id ) ) { |
| 169 |
$result['valid'] = false; |
| 170 |
$result['message'] = __( 'Form ID cannot be empty.', 'wp-commerce7' ); |
| 171 |
return $result; |
| 172 |
} |
| 173 |
|
| 174 |
// Form IDs are typically alphanumeric |
| 175 |
if ( ! preg_match( '/^[a-zA-Z0-9_-]+$/', $form_id ) ) { |
| 176 |
$result['valid'] = false; |
| 177 |
$result['message'] = __( 'Form ID can only contain letters, numbers, hyphens, and underscores.', 'wp-commerce7' ); |
| 178 |
return $result; |
| 179 |
} |
| 180 |
|
| 181 |
return $result; |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Validate reservation type |
| 186 |
* |
| 187 |
* @param string $type The reservation type to validate. |
| 188 |
* @return array Validation result with 'valid' boolean and 'message' string. |
| 189 |
*/ |
| 190 |
public static function validate_reservation_type( $type ) { |
| 191 |
$result = array( |
| 192 |
'valid' => true, |
| 193 |
'message' => '', |
| 194 |
); |
| 195 |
|
| 196 |
$valid_types = array( 'tasting', 'tour', 'event', 'dining', 'custom' ); |
| 197 |
if ( ! in_array( $type, $valid_types, true ) ) { |
| 198 |
$result['valid'] = false; |
| 199 |
/* translators: %s: List of valid reservation types. */ |
| 200 |
$result['message'] = sprintf( __( 'Invalid reservation type. Must be one of: %s', 'wp-commerce7' ), implode( ', ', $valid_types ) ); |
| 201 |
return $result; |
| 202 |
} |
| 203 |
|
| 204 |
return $result; |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* Validate display type for collections |
| 209 |
* |
| 210 |
* @param string $display_type The display type to validate. |
| 211 |
* @return array Validation result with 'valid' boolean and 'message' string. |
| 212 |
*/ |
| 213 |
public static function validate_display_type( $display_type ) { |
| 214 |
$result = array( |
| 215 |
'valid' => true, |
| 216 |
'message' => '', |
| 217 |
); |
| 218 |
|
| 219 |
$valid_types = array( 'grid', 'list', 'carousel' ); |
| 220 |
if ( ! in_array( $display_type, $valid_types, true ) ) { |
| 221 |
$result['valid'] = false; |
| 222 |
/* translators: %s: List of valid display types. */ |
| 223 |
$result['message'] = sprintf( __( 'Invalid display type. Must be one of: %s', 'wp-commerce7' ), implode( ', ', $valid_types ) ); |
| 224 |
return $result; |
| 225 |
} |
| 226 |
|
| 227 |
return $result; |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* Validate items per row setting |
| 232 |
* |
| 233 |
* @param int $items_per_row The number of items per row. |
| 234 |
* @return array Validation result with 'valid' boolean and 'message' string. |
| 235 |
*/ |
| 236 |
public static function validate_items_per_row( $items_per_row ) { |
| 237 |
$result = array( |
| 238 |
'valid' => true, |
| 239 |
'message' => '', |
| 240 |
); |
| 241 |
|
| 242 |
$items_per_row = intval( $items_per_row ); |
| 243 |
if ( $items_per_row < 1 || $items_per_row > 6 ) { |
| 244 |
$result['valid'] = false; |
| 245 |
$result['message'] = __( 'Items per row must be between 1 and 6.', 'wp-commerce7' ); |
| 246 |
return $result; |
| 247 |
} |
| 248 |
|
| 249 |
return $result; |
| 250 |
} |
| 251 |
|
| 252 |
/** |
| 253 |
* Validate radio group name |
| 254 |
* |
| 255 |
* @param string $name The radio group name to validate. |
| 256 |
* @return array Validation result with 'valid' boolean and 'message' string. |
| 257 |
*/ |
| 258 |
public static function validate_radio_group_name( $name ) { |
| 259 |
$result = array( |
| 260 |
'valid' => true, |
| 261 |
'message' => '', |
| 262 |
); |
| 263 |
|
| 264 |
if ( empty( $name ) ) { |
| 265 |
$result['valid'] = false; |
| 266 |
$result['message'] = __( 'Radio group name cannot be empty.', 'wp-commerce7' ); |
| 267 |
return $result; |
| 268 |
} |
| 269 |
|
| 270 |
// Radio group names should be valid HTML name attributes |
| 271 |
if ( ! preg_match( '/^[a-zA-Z][a-zA-Z0-9_-]*$/', $name ) ) { |
| 272 |
$result['valid'] = false; |
| 273 |
$result['message'] = __( 'Radio group name must start with a letter and contain only letters, numbers, hyphens, and underscores.', 'wp-commerce7' ); |
| 274 |
return $result; |
| 275 |
} |
| 276 |
|
| 277 |
return $result; |
| 278 |
} |
| 279 |
|
| 280 |
/** |
| 281 |
* Sanitize and validate text input |
| 282 |
* |
| 283 |
* @param string $text The text to sanitize and validate. |
| 284 |
* @param int $max_length Maximum allowed length. |
| 285 |
* @return array Result with 'sanitized' string and 'valid' boolean. |
| 286 |
*/ |
| 287 |
public static function sanitize_text_input( $text, $max_length = 255 ) { |
| 288 |
$result = array( |
| 289 |
'sanitized' => '', |
| 290 |
'valid' => true, |
| 291 |
'message' => '', |
| 292 |
); |
| 293 |
|
| 294 |
$sanitized = sanitize_text_field( $text ); |
| 295 |
$result['sanitized'] = $sanitized; |
| 296 |
|
| 297 |
if ( strlen( $sanitized ) > $max_length ) { |
| 298 |
$result['valid'] = false; |
| 299 |
/* translators: %d: Maximum character length. */ |
| 300 |
$result['message'] = sprintf( __( 'Text must be %d characters or less.', 'wp-commerce7' ), $max_length ); |
| 301 |
} |
| 302 |
|
| 303 |
return $result; |
| 304 |
} |
| 305 |
|
| 306 |
/** |
| 307 |
* Get validation error message for display |
| 308 |
* |
| 309 |
* @param array $validation_result The validation result array. |
| 310 |
* @return string Formatted error message. |
| 311 |
*/ |
| 312 |
public static function get_error_message( $validation_result ) { |
| 313 |
if ( $validation_result['valid'] ) { |
| 314 |
return ''; |
| 315 |
} |
| 316 |
|
| 317 |
if ( isset( $validation_result['messages'] ) && is_array( $validation_result['messages'] ) ) { |
| 318 |
return implode( ' ', $validation_result['messages'] ); |
| 319 |
} |
| 320 |
|
| 321 |
return $validation_result['message'] ?? __( 'Validation failed.', 'wp-commerce7' ); |
| 322 |
} |
| 323 |
} |
| 324 |
|