'string']; $object = static fn($properties) => [ 'type' => 'object', 'properties' => $properties, 'required' => array_keys($properties), 'additionalProperties' => false, ]; return $object([ 'title' => $string, 'slug' => $string, 'excerpt' => $string, 'blocks' => [ 'type' => 'array', 'items' => $object([ 'type' => ['type' => 'string', 'enum' => ['paragraph', 'heading', 'list', 'quote']], 'text' => $string, 'level' => ['type' => 'integer', 'enum' => [2, 3]], 'items' => ['type' => 'array', 'items' => $string], ]), ], 'seo' => $object(['title' => $string, 'description' => $string, 'keyword' => $string]), ]); } public static function validate($data) { $invalid = static fn() => new WP_Error( 'apeari_content_document', __('AI zwróciło nieprawidłową treść. Nie utworzono nowego szkicu.', 'apeari'), ['status' => 502], ); if ( !is_array($data) || array_diff(array_keys($data), ['title', 'slug', 'excerpt', 'blocks', 'seo']) ) { return $invalid(); } foreach (['title' => 250, 'slug' => 250, 'excerpt' => 2000] as $field => $limit) { if (!is_string($data[$field] ?? null) || mb_strlen($data[$field]) > $limit) { return $invalid(); } } $title = sanitize_text_field($data['title']); if ( '' === trim($title) || !is_array($data['blocks'] ?? null) || !$data['blocks'] || count($data['blocks']) > 80 || !is_array($data['seo'] ?? null) ) { return $invalid(); } $seo = []; foreach (['title' => 250, 'description' => 500, 'keyword' => 200] as $field => $limit) { if (!is_string($data['seo'][$field] ?? null) || mb_strlen($data['seo'][$field]) > $limit) { return $invalid(); } $seo[$field] = sanitize_text_field($data['seo'][$field]); } $content = []; foreach ($data['blocks'] as $block) { if ( !is_array($block) || !is_string($block['text'] ?? null) || strlen($block['text']) > 12000 || !in_array($block['level'] ?? null, [2, 3], true) || !is_array($block['items'] ?? null) || count($block['items']) > 40 ) { return $invalid(); } $text = esc_html(sanitize_textarea_field($block['text'])); switch ($block['type'] ?? '') { case 'paragraph': $html = get_comment_delimited_block_content('core/paragraph', [], '

' . $text . '

'); break; case 'heading': $level = $block['level']; $html = get_comment_delimited_block_content( 'core/heading', 2 === $level ? [] : ['level' => $level], '' . $text . '', ); break; case 'quote': $html = get_comment_delimited_block_content( 'core/quote', [], '
' . get_comment_delimited_block_content( 'core/paragraph', [], '

' . $text . '

', ) . '
', ); break; case 'list': if (!$block['items']) { return $invalid(); } $items = []; foreach ($block['items'] as $item) { if (!is_string($item) || strlen($item) > 2000) { return $invalid(); } $items[] = get_comment_delimited_block_content( 'core/list-item', [], '
  • ' . esc_html(sanitize_text_field($item)) . '
  • ', ); } $html = get_comment_delimited_block_content( 'core/list', [], '', ); break; default: return $invalid(); } if ('list' !== $block['type'] && '' === trim($text)) { return $invalid(); } $content[] = $html; } $content = implode("\n\n", $content); if (strlen($content) > 120000) { return $invalid(); } return [ 'title' => $title, 'slug' => sanitize_title($data['slug'] ?: $title), 'excerpt' => sanitize_textarea_field($data['excerpt']), 'content' => $content, 'seo' => $seo, ]; } }