(!isset($_REQUEST[$param])) { if (empty($validation['required'])) { continue; } else { return false; } } $paramValue = $_REQUEST[$param]; // Skip if value is empty and param is nullable if (!empty($validation['nullable']) && !is_numeric($paramValue) && empty($paramValue)) { continue; } // Return false if type is not specified if (!isset($validation['type'])) { return false; } // Decode if it's base64 encoded if (!empty($validation['encoding'])) { if ($validation['encoding'] === 'base64') { $paramValue = base64_decode($paramValue); } else if ($validation['encoding'] === 'url') { $paramValue = urldecode($paramValue); } } switch ($validation['type']) { case 'string': // Validate type if (!is_string($paramValue)) { return false; } // Validate minlength if (isset($validation['minlength']) && strlen($paramValue) < $validation['minlength']) { return false; } // Validate maxlength if (isset($validation['maxlength']) && strlen($paramValue) > $validation['maxlength']) { return false; } break; case 'number': // Validate type if (!is_numeric($paramValue)) { return false; } // Validate min if (isset($validation['min']) && $paramValue < $validation['min']) { return false; } // Validate max if (isset($validation['max']) && $paramValue > $validation['max']) { return false; } break; case 'url': // Validate url if (!filter_var($paramValue, FILTER_VALIDATE_URL)) { return false; } break; default: return false; } // Invalid pattern if (isset($validation['invalid_pattern'])) { if (is_string($validation['invalid_pattern'])) { if (preg_match($validation['invalid_pattern'], $paramValue)) { return false; } } if (is_array($validation['invalid_pattern'])) { foreach ($validation['invalid_pattern'] as $pattern) { if (preg_match($pattern, $paramValue)) { return false; } } } } // Valid pattern if (isset($validation['valid_pattern'])) { if (is_string($validation['valid_pattern'])) { if (!preg_match($validation['valid_pattern'], $paramValue)) { return false; } } if (is_array($validation['valid_pattern'])) { foreach ($validation['valid_pattern'] as $pattern) { if (!preg_match($pattern, $paramValue)) { return false; } } } } } return true; } /** * Checks where the current request is coming from. * * @param string $type The type of request to check for. Can be 'admin', 'ajax', 'cron', 'wp-cli', or 'public'. * @return bool True if the request is from the specified type, false otherwise. */ public static function isFrom($type) { if ($type === 'admin') { return is_admin(); } if ($type === 'ajax') { return defined('DOING_AJAX'); } if ($type === 'cron') { return defined('DOING_CRON'); } if ($type === 'wp-cli') { return defined('WP_CLI') && WP_CLI; } return false; } }