Commit inicial, se subio el proyecto
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace JooosiFon\Api\Support;
|
||||
|
||||
/**
|
||||
* Encodes font payloads and reads both the current compressed representation
|
||||
* and the legacy plain JSON representation.
|
||||
*/
|
||||
final class FontCodec
|
||||
{
|
||||
/**
|
||||
* @return array|object
|
||||
*/
|
||||
public static function decode(string $payload, bool $associative = \false)
|
||||
{
|
||||
try {
|
||||
return self::decodeJson($payload, $associative);
|
||||
} catch (\JsonException $exception) {
|
||||
$compressed = base64_decode($payload, \true);
|
||||
if ($compressed === \false) {
|
||||
throw new \UnexpectedValueException('The stored font payload is neither JSON nor valid base64.', 0, $exception);
|
||||
}
|
||||
$json = @gzuncompress($compressed);
|
||||
if (!is_string($json)) {
|
||||
throw new \UnexpectedValueException('The stored font payload could not be decompressed.', 0, $exception);
|
||||
}
|
||||
return self::decodeJson($json, $associative);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @param array|object $payload
|
||||
*/
|
||||
public static function encode($payload, bool $compressed = \true): string
|
||||
{
|
||||
$json = json_encode($payload, \JSON_THROW_ON_ERROR);
|
||||
if (!$compressed) {
|
||||
return $json;
|
||||
}
|
||||
$encoded = gzcompress($json, 9);
|
||||
if ($encoded === \false) {
|
||||
throw new \RuntimeException('The font payload could not be compressed.');
|
||||
}
|
||||
return base64_encode($encoded);
|
||||
}
|
||||
/**
|
||||
* @return array|object
|
||||
*/
|
||||
private static function decodeJson(string $payload, bool $associative)
|
||||
{
|
||||
$decoded = json_decode($payload, $associative, 512, \JSON_THROW_ON_ERROR);
|
||||
if (!is_array($decoded) && !is_object($decoded)) {
|
||||
throw new \UnexpectedValueException('The font payload must decode to an object or array.');
|
||||
}
|
||||
return $decoded;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace JooosiFon\Api\Support;
|
||||
|
||||
use JooosiFon\Utils\Font;
|
||||
/**
|
||||
* Emits the public font lifecycle hooks and exactly one cache invalidation
|
||||
* event for each successful mutation.
|
||||
*
|
||||
* @todo Remove all legacy Yabe Webfont hook shims completely in Jooosi Fon 3.0.0.
|
||||
*/
|
||||
final class FontEvents
|
||||
{
|
||||
/**
|
||||
* @param mixed $payload
|
||||
*/
|
||||
public static function dispatch(string $event, $payload = null, bool $async = \true): void
|
||||
{
|
||||
$hook = 'a!jooosi/fon/api/font:' . $event;
|
||||
$legacyHook = 'a!yabe/webfont/api/font:' . $event;
|
||||
Font::clear_cache();
|
||||
do_action($hook, $payload);
|
||||
do_action_deprecated($legacyHook, [$payload], '2.1.0', $hook);
|
||||
self::emitInvalidation($hook, $payload, $async);
|
||||
}
|
||||
/**
|
||||
* @param mixed $payload
|
||||
*/
|
||||
public static function invalidate(string $source, $payload = null, bool $async = \true): void
|
||||
{
|
||||
Font::clear_cache();
|
||||
self::emitInvalidation($source, $payload, $async);
|
||||
}
|
||||
/**
|
||||
* @param mixed $payload
|
||||
*/
|
||||
private static function emitInvalidation(string $source, $payload, bool $async): void
|
||||
{
|
||||
$hook = $async ? 'a!jooosi/fon/api/font:fonts_event_async' : 'a!jooosi/fon/api/font:fonts_event';
|
||||
$legacyHook = $async ? 'a!yabe/webfont/api/font:fonts_event_async' : 'a!yabe/webfont/api/font:fonts_event';
|
||||
do_action($hook, $source, $payload);
|
||||
do_action_deprecated($legacyHook, [str_replace('!jooosi/fon/', '!yabe/webfont/', $source), $payload], '2.1.0', $hook);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace JooosiFon\Api\Support;
|
||||
|
||||
/**
|
||||
* Owns database writes for font records and turns silent wpdb failures into
|
||||
* exceptions that REST controllers can handle consistently.
|
||||
*/
|
||||
final class FontRepository
|
||||
{
|
||||
public function table(): string
|
||||
{
|
||||
global $wpdb;
|
||||
return $wpdb->prefix . 'jooosi_fon_fonts';
|
||||
}
|
||||
public function find(int $id)
|
||||
{
|
||||
global $wpdb;
|
||||
return $wpdb->get_row($wpdb->prepare("SELECT * FROM {$this->table()} WHERE id = %d", $id));
|
||||
}
|
||||
public function findOfType(int $id, string $type)
|
||||
{
|
||||
global $wpdb;
|
||||
return $wpdb->get_row($wpdb->prepare("SELECT * FROM {$this->table()} WHERE id = %d AND type = %s", $id, $type));
|
||||
}
|
||||
public function findBySlug(string $slug)
|
||||
{
|
||||
global $wpdb;
|
||||
return $wpdb->get_row($wpdb->prepare("SELECT * FROM {$this->table()} WHERE slug = %s LIMIT 1", $slug));
|
||||
}
|
||||
/**
|
||||
* @param array<string, mixed> $data
|
||||
* @param string[] $formats
|
||||
*/
|
||||
public function insert(array $data, array $formats): int
|
||||
{
|
||||
global $wpdb;
|
||||
if ($wpdb->insert($this->table(), $data, $formats) === \false || $wpdb->insert_id < 1) {
|
||||
throw new \RuntimeException($wpdb->last_error ?: 'Unable to create the font record.');
|
||||
}
|
||||
return (int) $wpdb->insert_id;
|
||||
}
|
||||
/**
|
||||
* @param array<string, mixed> $data
|
||||
* @param string[] $formats
|
||||
*/
|
||||
public function update(int $id, array $data, array $formats): void
|
||||
{
|
||||
global $wpdb;
|
||||
if ($wpdb->update($this->table(), $data, ['id' => $id], $formats, ['%d']) === \false) {
|
||||
throw new \RuntimeException($wpdb->last_error ?: 'Unable to update the font record.');
|
||||
}
|
||||
}
|
||||
public function delete(int $id): void
|
||||
{
|
||||
global $wpdb;
|
||||
if ($wpdb->delete($this->table(), ['id' => $id], ['%d']) === \false) {
|
||||
throw new \RuntimeException($wpdb->last_error ?: 'Unable to delete the font record.');
|
||||
}
|
||||
}
|
||||
public function deleteByType(string $type): void
|
||||
{
|
||||
global $wpdb;
|
||||
if ($wpdb->delete($this->table(), ['type' => $type], ['%s']) === \false) {
|
||||
throw new \RuntimeException($wpdb->last_error ?: 'Unable to delete the font records.');
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function transaction(callable $callback)
|
||||
{
|
||||
global $wpdb;
|
||||
if ($wpdb->query('START TRANSACTION') === \false) {
|
||||
throw new \RuntimeException($wpdb->last_error ?: 'Unable to start the database transaction.');
|
||||
}
|
||||
try {
|
||||
$result = $callback($this);
|
||||
if ($wpdb->query('COMMIT') === \false) {
|
||||
throw new \RuntimeException($wpdb->last_error ?: 'Unable to commit the database transaction.');
|
||||
}
|
||||
return $result;
|
||||
} catch (\Throwable $throwable) {
|
||||
$wpdb->query('ROLLBACK');
|
||||
throw $throwable;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace JooosiFon\Api\Support;
|
||||
|
||||
final class GoogleFontFileSelector
|
||||
{
|
||||
/**
|
||||
* @param array<int, array<string, mixed>> $files
|
||||
* @param array<string, mixed> $face
|
||||
* @param string[] $subsets
|
||||
* @param string[] $formats
|
||||
* @return array<int, array<string, mixed>>
|
||||
*/
|
||||
public static function variableFiles(array $files, array $face, array $subsets, array $formats): array
|
||||
{
|
||||
$selected = [];
|
||||
foreach ($subsets as $subset) {
|
||||
foreach ($files as $file) {
|
||||
if (self::matchesFace($file, $face, $formats) && in_array($subset, (array) ($file['subsets'] ?? []), \true)) {
|
||||
$selected[] = $file;
|
||||
}
|
||||
}
|
||||
}
|
||||
$usesNamedSubsets = array_reduce($subsets, static fn(bool $carry, string $subset): bool => $carry && preg_match('/\d/', $subset) !== 1, \true);
|
||||
if ($usesNamedSubsets) {
|
||||
foreach ($files as $file) {
|
||||
if (self::matchesFace($file, $face, $formats)) {
|
||||
$selected[] = $file;
|
||||
}
|
||||
}
|
||||
}
|
||||
return array_values(array_unique($selected, \SORT_REGULAR));
|
||||
}
|
||||
/**
|
||||
* @param array<string, mixed> $file
|
||||
* @param array<string, mixed> $face
|
||||
* @param string[] $formats
|
||||
*/
|
||||
private static function matchesFace(array $file, array $face, array $formats): bool
|
||||
{
|
||||
return ($file['weight'] ?? null) === ($face['weight'] ?? null) && ($file['style'] ?? null) === ($face['style'] ?? null) && in_array($file['format'] ?? null, $formats, \true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace JooosiFon\Api\Support;
|
||||
|
||||
final class GoogleFontsClient
|
||||
{
|
||||
public function fetchCss(string $url, string $userAgent): string
|
||||
{
|
||||
$host = strtolower((string) wp_parse_url($url, \PHP_URL_HOST));
|
||||
if (wp_parse_url($url, \PHP_URL_SCHEME) !== 'https' || $host !== 'fonts.googleapis.com') {
|
||||
throw new \InvalidArgumentException('The Google Fonts stylesheet URL is invalid.');
|
||||
}
|
||||
$response = wp_safe_remote_get($url, ['headers' => ['User-Agent' => $userAgent], 'redirection' => 2, 'timeout' => 15, 'limit_response_size' => \MB_IN_BYTES]);
|
||||
if (is_wp_error($response)) {
|
||||
throw new \RuntimeException($response->get_error_message());
|
||||
}
|
||||
$status = wp_remote_retrieve_response_code($response);
|
||||
if ($status !== 200) {
|
||||
throw new \RuntimeException(sprintf('Google Fonts returned HTTP %d.', $status));
|
||||
}
|
||||
$body = wp_remote_retrieve_body($response);
|
||||
if (!is_string($body) || trim($body) === '') {
|
||||
throw new \RuntimeException('Google Fonts returned an empty stylesheet.');
|
||||
}
|
||||
return $body;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace JooosiFon\Api\Support;
|
||||
|
||||
use JooosiFon\Utils\Common;
|
||||
use JooosiFon\Utils\Upload;
|
||||
/**
|
||||
* Creates font attachments as a unit and can compensate for a later database
|
||||
* failure by deleting attachments created during the current operation.
|
||||
*/
|
||||
final class MediaImportService
|
||||
{
|
||||
/**
|
||||
* @var int[]
|
||||
*/
|
||||
private array $createdAttachmentIds = [];
|
||||
/**
|
||||
* @param string[] $allowedHosts
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function remote(string $url, string $fileName, string $extension, array $allowedHosts = []): array
|
||||
{
|
||||
$this->assertRemoteUrl($url, $allowedHosts);
|
||||
$mime = self::mimeForExtension($extension);
|
||||
$attachmentId = Upload::remote_upload_media($url, $fileName, $mime);
|
||||
return $this->attachmentData($attachmentId, $fileName, $extension, $mime);
|
||||
}
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function binary(string $binary, string $fileName, string $extension): array
|
||||
{
|
||||
$mime = self::mimeForExtension($extension);
|
||||
$attachmentId = Upload::binary_upload_media($binary, $fileName, $mime);
|
||||
return $this->attachmentData($attachmentId, $fileName, $extension, $mime);
|
||||
}
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function local(string $path, string $fileName, string $extension): array
|
||||
{
|
||||
if (!is_file($path) || !is_readable($path)) {
|
||||
throw new \RuntimeException('The source font file is not readable.');
|
||||
}
|
||||
$binary = file_get_contents($path);
|
||||
if (!is_string($binary) || $binary === '') {
|
||||
throw new \RuntimeException('The source font file is empty.');
|
||||
}
|
||||
return $this->binary($binary, $fileName, $extension);
|
||||
}
|
||||
public function commit(): void
|
||||
{
|
||||
$this->createdAttachmentIds = [];
|
||||
}
|
||||
public function rollback(): void
|
||||
{
|
||||
foreach (array_unique($this->createdAttachmentIds) as $attachmentId) {
|
||||
wp_delete_attachment($attachmentId, \true);
|
||||
}
|
||||
$this->createdAttachmentIds = [];
|
||||
}
|
||||
/**
|
||||
* @param array|object $fontFaces
|
||||
* @return int[]
|
||||
*/
|
||||
public static function attachmentIds($fontFaces): array
|
||||
{
|
||||
$ids = [];
|
||||
foreach ((array) $fontFaces as $fontFace) {
|
||||
$files = is_array($fontFace) ? $fontFace['files'] ?? [] : $fontFace->files ?? [];
|
||||
foreach ((array) $files as $file) {
|
||||
$attachmentId = is_array($file) ? $file['attachment_id'] ?? 0 : $file->attachment_id ?? 0;
|
||||
if (is_numeric($attachmentId) && (int) $attachmentId > 0) {
|
||||
$ids[] = (int) $attachmentId;
|
||||
}
|
||||
}
|
||||
}
|
||||
return array_values(array_unique($ids));
|
||||
}
|
||||
/**
|
||||
* @param int[] $attachmentIds
|
||||
*/
|
||||
public static function deleteAttachments(array $attachmentIds): void
|
||||
{
|
||||
foreach (array_unique(array_map('intval', $attachmentIds)) as $attachmentId) {
|
||||
if ($attachmentId > 0) {
|
||||
wp_delete_attachment($attachmentId, \true);
|
||||
}
|
||||
}
|
||||
}
|
||||
public static function mimeForExtension(string $extension): string
|
||||
{
|
||||
$mimes = ['woff2' => 'font/woff2', 'woff' => 'font/woff', 'ttf' => 'font/ttf', 'otf' => 'font/otf', 'eot' => 'font/eot'];
|
||||
$extension = strtolower($extension);
|
||||
if (!isset($mimes[$extension])) {
|
||||
throw new \InvalidArgumentException('Unsupported font file extension.');
|
||||
}
|
||||
return $mimes[$extension];
|
||||
}
|
||||
/**
|
||||
* @param mixed $attachmentId
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
private function attachmentData($attachmentId, string $fileName, string $extension, string $mime): array
|
||||
{
|
||||
if (is_wp_error($attachmentId)) {
|
||||
throw new \RuntimeException($attachmentId->get_error_message());
|
||||
}
|
||||
if (!is_int($attachmentId) || $attachmentId < 1) {
|
||||
throw new \RuntimeException('The font attachment could not be created.');
|
||||
}
|
||||
$path = get_attached_file($attachmentId);
|
||||
if (!is_string($path) || !is_readable($path)) {
|
||||
wp_delete_attachment($attachmentId, \true);
|
||||
throw new \RuntimeException('The uploaded font attachment is not readable.');
|
||||
}
|
||||
$this->createdAttachmentIds[] = $attachmentId;
|
||||
return ['uid' => Common::random_slug(10), 'attachment_id' => $attachmentId, 'attachment_url' => wp_get_attachment_url($attachmentId), 'extension' => strtolower($extension), 'mime' => $mime, 'file_size' => (int) filesize($path), 'name' => pathinfo($fileName, \PATHINFO_FILENAME)];
|
||||
}
|
||||
/**
|
||||
* @param string[] $allowedHosts
|
||||
*/
|
||||
private function assertRemoteUrl(string $url, array $allowedHosts): void
|
||||
{
|
||||
if (!wp_http_validate_url($url)) {
|
||||
throw new \InvalidArgumentException('The remote font URL is not safe.');
|
||||
}
|
||||
if ($allowedHosts === []) {
|
||||
return;
|
||||
}
|
||||
$host = strtolower((string) wp_parse_url($url, \PHP_URL_HOST));
|
||||
$allowedHosts = array_map('strtolower', $allowedHosts);
|
||||
if (!in_array($host, $allowedHosts, \true)) {
|
||||
throw new \InvalidArgumentException('The remote font URL uses an unapproved host.');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,206 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace JooosiFon\Api\Support;
|
||||
|
||||
final class RequestValidator
|
||||
{
|
||||
private const FONT_EXTENSION_ALIASES = ['font/woff2' => 'woff2', 'font/woff' => 'woff', 'font/ttf' => 'ttf', 'font/otf' => 'otf', 'font/eot' => 'eot', 'application/font-woff2' => 'woff2', 'application/font-woff' => 'woff', 'application/x-font-woff2' => 'woff2', 'application/x-font-woff' => 'woff', 'application/x-font-ttf' => 'ttf', 'application/x-font-truetype' => 'ttf', 'application/x-font-opentype' => 'otf', 'application/vnd.ms-opentype' => 'otf', 'application/vnd.ms-fontobject' => 'eot', 'x-font-woff2' => 'woff2', 'x-font-woff' => 'woff', 'x-font-ttf' => 'ttf', 'x-font-opentype' => 'otf', 'vnd.ms-opentype' => 'otf', 'vnd.ms-fontobject' => 'eot', 'truetype' => 'ttf', 'opentype' => 'otf', 'embedded-opentype' => 'eot'];
|
||||
public static function positiveInteger($value): bool
|
||||
{
|
||||
return filter_var($value, \FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]) !== \false;
|
||||
}
|
||||
public static function pageSize($value): bool
|
||||
{
|
||||
return filter_var($value, \FILTER_VALIDATE_INT, ['options' => ['min_range' => 1, 'max_range' => 100]]) !== \false;
|
||||
}
|
||||
public static function boolean($value): bool
|
||||
{
|
||||
return is_bool($value) || in_array($value, [0, 1, '0', '1', 'false', 'true'], \true);
|
||||
}
|
||||
public static function nonEmptyString($value): bool
|
||||
{
|
||||
return is_string($value) && trim($value) !== '';
|
||||
}
|
||||
public static function arrayValue($value): bool
|
||||
{
|
||||
if (!is_array($value)) {
|
||||
return \false;
|
||||
}
|
||||
$encoded = json_encode($value);
|
||||
return is_string($encoded) && strlen($encoded) <= 2 * 1024 * 1024;
|
||||
}
|
||||
public static function positiveIntegerList($value): bool
|
||||
{
|
||||
if (!is_array($value) || $value === [] || count($value) > 50) {
|
||||
return \false;
|
||||
}
|
||||
foreach ($value as $item) {
|
||||
if (!self::positiveInteger($item)) {
|
||||
return \false;
|
||||
}
|
||||
}
|
||||
return \true;
|
||||
}
|
||||
public static function fontType($value): bool
|
||||
{
|
||||
return is_string($value) && in_array($value, ['custom', 'google-fonts', 'adobe-fonts'], \true);
|
||||
}
|
||||
public static function projectId($value): bool
|
||||
{
|
||||
return is_string($value) && preg_match('/^[a-zA-Z0-9_-]{1,100}$/', $value) === 1;
|
||||
}
|
||||
public static function subsets($value): bool
|
||||
{
|
||||
return is_string($value) && ($value === '' || preg_match('/^[a-zA-Z0-9_-]+(?:,[a-zA-Z0-9_-]+)*$/', $value) === 1);
|
||||
}
|
||||
public static function importItem($value): bool
|
||||
{
|
||||
return is_array($value) && isset($value['type'], $value['title'], $value['family'], $value['font_faces'], $value['metadata']) && self::fontType($value['type']) && self::nonEmptyString($value['title']) && self::nonEmptyString($value['family']) && is_string($value['font_faces']) && is_string($value['metadata']) && strlen($value['font_faces']) <= 30 * 1024 * 1024 && strlen($value['metadata']) <= 5 * 1024 * 1024;
|
||||
}
|
||||
public static function googleMetadata($value): bool
|
||||
{
|
||||
if (!is_array($value) || !isset($value['google_fonts']) || !is_array($value['google_fonts'])) {
|
||||
return \false;
|
||||
}
|
||||
$google = $value['google_fonts'];
|
||||
if (!isset($google['font_data'], $google['font_faces'], $google['font_files'], $google['formats'], $google['subsets']) || !is_array($google['font_data']) || !is_array($google['font_faces']) || !is_array($google['font_files']) || !is_array($google['formats']) || !is_array($google['subsets']) || !isset($google['font_data']['family'], $google['font_data']['slug']) || !self::nonEmptyString($google['font_data']['family']) || !self::nonEmptyString($google['font_data']['slug']) || strlen($google['font_data']['family']) > 255 || strlen($google['font_data']['slug']) > 255 || count($google['font_faces']) > 100 || count($google['font_files']) > 500 || count($google['formats']) > 3 || count($google['subsets']) > 50 || $google['formats'] === [] || $google['subsets'] === [] || $google['font_files'] === [] || $google['font_faces'] === []) {
|
||||
return \false;
|
||||
}
|
||||
foreach ($google['formats'] as $format) {
|
||||
if (!in_array($format, ['woff2', 'woff', 'ttf'], \true)) {
|
||||
return \false;
|
||||
}
|
||||
}
|
||||
foreach ($google['subsets'] as $subset) {
|
||||
if (!is_string($subset) || preg_match('/^[a-zA-Z0-9_-]+$/', $subset) !== 1) {
|
||||
return \false;
|
||||
}
|
||||
}
|
||||
foreach ($google['font_files'] as $file) {
|
||||
if (!is_array($file) || !isset($file['uid'], $file['format'], $file['weight'], $file['style'], $file['subsets'], $file['url']) || !self::nonEmptyString($file['uid']) || strlen($file['uid']) > 255 || !in_array($file['format'], ['woff2', 'woff', 'ttf'], \true) || !is_numeric($file['weight']) || !is_string($file['style']) || !is_array($file['subsets']) || count($file['subsets']) > 50 || !filter_var($file['url'], \FILTER_VALIDATE_URL) || strtolower((string) parse_url($file['url'], \PHP_URL_SCHEME)) !== 'https' || strtolower((string) parse_url($file['url'], \PHP_URL_HOST)) !== 'fonts.gstatic.com') {
|
||||
return \false;
|
||||
}
|
||||
foreach ($file['subsets'] as $subset) {
|
||||
if (!is_string($subset) || preg_match('/^[a-zA-Z0-9_-]+$/', $subset) !== 1) {
|
||||
return \false;
|
||||
}
|
||||
}
|
||||
if (array_key_exists('file', $file) && !self::attachmentFile($file['file'])) {
|
||||
return \false;
|
||||
}
|
||||
}
|
||||
$hasEnabledFace = \false;
|
||||
foreach ($google['font_faces'] as $face) {
|
||||
if (!is_array($face) || !array_key_exists('weight', $face) || !array_key_exists('width', $face) || !array_key_exists('style', $face) || !array_key_exists('isEnabled', $face) || !array_key_exists('display', $face) || !array_key_exists('selector', $face) || !array_key_exists('comment', $face) || !array_key_exists('preload', $face) || !is_numeric($face['weight']) || !is_string($face['style']) || !self::boolean($face['isEnabled'])) {
|
||||
return \false;
|
||||
}
|
||||
$hasEnabledFace = $hasEnabledFace || filter_var($face['isEnabled'], \FILTER_VALIDATE_BOOLEAN) === \true;
|
||||
}
|
||||
if (!empty($google['variable']) && (!isset($google['font_data']['axes']) || !is_array($google['font_data']['axes']))) {
|
||||
return \false;
|
||||
}
|
||||
if (!empty($google['variable'])) {
|
||||
if ($google['font_data']['axes'] === [] || count($google['font_data']['axes']) > 20) {
|
||||
return \false;
|
||||
}
|
||||
foreach ($google['font_data']['axes'] as $axis) {
|
||||
if (!is_array($axis) || !isset($axis['tag'], $axis['min'], $axis['max']) || !is_string($axis['tag']) || preg_match('/^[a-zA-Z0-9]{1,10}$/', $axis['tag']) !== 1 || !is_numeric($axis['min']) || !is_numeric($axis['max']) || (float) $axis['min'] > (float) $axis['max']) {
|
||||
return \false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $hasEnabledFace;
|
||||
}
|
||||
public static function fontFaces($value): bool
|
||||
{
|
||||
if (!is_array($value) || $value === [] || count($value) > 100) {
|
||||
return \false;
|
||||
}
|
||||
foreach ($value as $face) {
|
||||
if (!is_array($face) || !isset($face['id'], $face['weight'], $face['style'], $face['files']) || !self::nonEmptyString($face['id']) || !self::fontWeight($face['weight']) || !is_string($face['style']) || array_key_exists('isEnabled', $face) && !self::boolean($face['isEnabled']) || !is_array($face['files']) || count($face['files']) > 10) {
|
||||
return \false;
|
||||
}
|
||||
foreach ($face['files'] as $file) {
|
||||
if (!self::attachmentFile($file)) {
|
||||
return \false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return \true;
|
||||
}
|
||||
/**
|
||||
* Normalize font file extensions from WordPress MIME subtypes and legacy
|
||||
* payloads before they are persisted.
|
||||
*/
|
||||
public static function sanitizeFontFaces($value): array
|
||||
{
|
||||
if (!is_array($value)) {
|
||||
return (array) $value;
|
||||
}
|
||||
foreach ($value as &$face) {
|
||||
if (!is_array($face) || !isset($face['files']) || !is_array($face['files'])) {
|
||||
continue;
|
||||
}
|
||||
foreach ($face['files'] as &$file) {
|
||||
if (!is_array($file) || !isset($file['extension'])) {
|
||||
continue;
|
||||
}
|
||||
$extension = self::fontExtension($file['extension']);
|
||||
if ($extension !== null) {
|
||||
$file['extension'] = $extension;
|
||||
}
|
||||
}
|
||||
unset($file);
|
||||
}
|
||||
unset($face);
|
||||
return $value;
|
||||
}
|
||||
public static function attachmentFile($value): bool
|
||||
{
|
||||
return is_array($value) && isset($value['attachment_id'], $value['extension']) && self::positiveInteger($value['attachment_id']) && self::fontExtension($value['extension']) !== null;
|
||||
}
|
||||
private static function fontExtension($value): ?string
|
||||
{
|
||||
$extension = strtolower(trim((string) $value));
|
||||
if (in_array($extension, ['woff2', 'woff', 'ttf', 'otf', 'eot'], \true)) {
|
||||
return $extension;
|
||||
}
|
||||
return self::FONT_EXTENSION_ALIASES[$extension] ?? null;
|
||||
}
|
||||
public static function fontWeight($value): bool
|
||||
{
|
||||
if (is_int($value) || is_float($value)) {
|
||||
return $value >= 1 && $value <= 1000;
|
||||
}
|
||||
if (!is_string($value) || preg_match('/^(?:normal|bold|[1-9]\d{0,3})(?:\s+[1-9]\d{0,3})?$/', trim($value)) !== 1) {
|
||||
return \false;
|
||||
}
|
||||
$weights = preg_split('/\s+/', trim($value));
|
||||
if ($weights === \false) {
|
||||
return \false;
|
||||
}
|
||||
foreach ($weights as $weight) {
|
||||
if (is_numeric($weight) && ((int) $weight < 1 || (int) $weight > 1000)) {
|
||||
return \false;
|
||||
}
|
||||
}
|
||||
return \true;
|
||||
}
|
||||
public static function adobeKit($value): bool
|
||||
{
|
||||
if (!is_array($value) || !isset($value['families']) || !is_array($value['families'])) {
|
||||
return \false;
|
||||
}
|
||||
foreach ($value['families'] as $family) {
|
||||
if (!is_array($family) || !isset($family['id'], $family['name'], $family['slug']) || !self::nonEmptyString($family['id']) || !self::nonEmptyString($family['name']) || !self::nonEmptyString($family['slug'])) {
|
||||
return \false;
|
||||
}
|
||||
}
|
||||
return \true;
|
||||
}
|
||||
public static function confirmed($value): bool
|
||||
{
|
||||
return filter_var($value, \FILTER_VALIDATE_BOOLEAN) === \true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user