| @@ -11,9 +11,11 @@ | ||
| 11 | 11 | declare (strict_types=1); |
| 12 | 12 | namespace WindPress\WindPress\Integration\Gutenberg; |
| 13 | 13 | |
| 14 | 14 | use WindPressDeps\Symfony\Component\Yaml\Yaml; |
| 15 | -use WP_Query; | |
| 15 | +use WindPress\WindPress\Core\Scanner\ExtractionCache; | |
| 16 | +use WindPress\WindPress\Core\Scanner\PostQuery; | |
| 17 | +use WindPress\WindPress\Core\Scanner\PostRenderer; | |
| 16 | 18 | /** |
| 17 | 19 | * @author Joshua Gugun Siagian <suabahasa@gmail.com> |
| 18 | 20 | */ |
| 19 | 21 | class Compile |
| @@ -24,15 +26,19 @@ | ||
| 24 | 26 | public function __invoke($metadata): array |
| 25 | 27 | { |
| 26 | 28 | return $this->get_contents($metadata); |
| 27 | 29 | } |
| 28 | - public function get_contents($metadata): array | |
| 30 | + public function get_contents($metadata, ?callable $include_post = null): array | |
| 29 | 31 | { |
| 30 | 32 | $contents = []; |
| 31 | - $post_types = apply_filters('f!windpress/integration/gutenberg/compile:get_contents.post_types', ['post', 'page', 'wp_template']); | |
| 32 | - $next_batch = $metadata['next_batch'] !== \false ? $metadata['next_batch'] : 1; | |
| 33 | - $per_page = apply_filters('f!windpress/integration/gutenberg/compile:get_contents.post_per_page', (int) get_option('posts_per_page', 20)); | |
| 34 | - $wpQuery = new WP_Query(['posts_per_page' => $per_page, 'post_type' => $post_types, 'paged' => $next_batch, 'no_found_rows' => \true, 'update_post_meta_cache' => \false, 'update_post_term_cache' => \false, 'ignore_sticky_posts' => \true]); | |
| 33 | + // Match block editor eligibility without requiring wp-admin files in REST requests. | |
| 34 | + $block_post_types = array_filter(get_post_types(['show_in_rest' => \true]), static function ($post_type): bool { | |
| 35 | + return post_type_supports($post_type, 'editor') && apply_filters('use_block_editor_for_post_type', \true, $post_type); | |
| 36 | + }); | |
| 37 | + $post_types = apply_filters('f!windpress/integration/gutenberg/compile:get_contents.post_types', array_values(array_unique(array_merge(['post', 'page', 'wp_template', 'wp_template_part', 'wp_block'], $block_post_types)))); | |
| 38 | + $per_page = apply_filters('f!windpress/integration/gutenberg/compile:get_contents.post_per_page', PostQuery::batch_size()); | |
| 39 | + $scan = new PostQuery(['posts_per_page' => $per_page, 'post_type' => $post_types, 'no_found_rows' => \true, 'update_post_meta_cache' => \false, 'update_post_term_cache' => \false, 'ignore_sticky_posts' => \true], $metadata); | |
| 40 | + $wpQuery = $scan->query; | |
| 35 | 41 | foreach ($wpQuery->posts as $post) { |
| 36 | 42 | $post_content = $post->post_content; |
| 37 | 43 | $post_content_trimmed = trim($post_content); |
| 38 | 44 | if ($post_content_trimmed === '' || $post_content_trimmed === '0') { |
| @@ -40,22 +46,14 @@ | ||
| 40 | 46 | } |
| 41 | 47 | /** |
| 42 | 48 | * @TODO: More robust and reliable API for external usage. |
| 43 | 49 | */ |
| 44 | - if (apply_filters('f!windpress/integration/gutenberg/compile:get_contents.skip', \false, $post)) { | |
| 50 | + if ($include_post !== null && !$include_post($post) || apply_filters('f!windpress/integration/gutenberg/compile:get_contents.skip', \false, $post)) { | |
| 45 | 51 | continue; |
| 46 | 52 | } |
| 47 | 53 | if (apply_filters('f!windpress/integration/gutenberg/compile:get_contents.render', \true, $post)) { |
| 48 | 54 | $fn_renders = apply_filters('f!windpress/integration/gutenberg/compile:get_contents.render_fn', ['do_blocks', 'wptexturize', 'convert_smilies', 'shortcode_unautop', 'wp_filter_content_tags', 'do_shortcode'], $post); |
| 49 | - foreach ($fn_renders as $fn_render) { | |
| 50 | - try { | |
| 51 | - $post_content = $fn_render($post_content); | |
| 52 | - } catch (\Throwable $th) { | |
| 53 | - if (\WP_DEBUG) { | |
| 54 | - error_log($th->getMessage()); | |
| 55 | - } | |
| 56 | - } | |
| 57 | - } | |
| 55 | + $post_content = PostRenderer::render($post, $fn_renders, 'Gutenberg', $wpQuery); | |
| 58 | 56 | } |
| 59 | 57 | $post_content = apply_filters('f!windpress/integration/gutenberg/compile:get_contents.post_content', $post_content, $post); |
| 60 | 58 | // Decode HTML entities to preserve arbitrary variants like [&>img]:rounded-lg |
| 61 | 59 | // WordPress rendering functions encode special characters (&, >, <, etc.) but Tailwind |
| @@ -65,13 +63,17 @@ | ||
| 65 | 63 | $post_content = html_entity_decode($post_content, \ENT_QUOTES | \ENT_HTML5, 'UTF-8'); |
| 66 | 64 | $post_content = html_entity_decode($post_content, \ENT_QUOTES | \ENT_HTML5, 'UTF-8'); |
| 67 | 65 | } |
| 68 | 66 | if (apply_filters('f!windpress/integration/gutenberg/compile:get_contents.dump_parsed_block', \true, $post)) { |
| 69 | - $post_content .= \PHP_EOL . Yaml::dump(parse_blocks($post->post_content)); | |
| 67 | + $raw_content = $post->post_content; | |
| 68 | + $dump_blocks = static function () use ($raw_content): string { | |
| 69 | + return Yaml::dump(parse_blocks($raw_content)); | |
| 70 | + }; | |
| 71 | + // Custom parsers may depend on state outside the saved post content. | |
| 72 | + $block_dump = has_filter('block_parser_class') === \false && has_filter('all') === \false ? ExtractionCache::remember('gutenberg.blocks', 'post:' . $post->ID, ['content' => $raw_content, 'wordpress_version' => $GLOBALS['wp_version'] ?? ''], $dump_blocks) : $dump_blocks(); | |
| 73 | + $post_content .= \PHP_EOL . $block_dump; | |
| 70 | 74 | } |
| 71 | - $contents[] = ['id' => $post->ID, 'title' => sprintf('#%s: %s', $post->ID, $post->post_title), 'content' => $post_content]; | |
| 75 | + $contents[] = ['source_id' => 'post:' . $post->ID, 'id' => $post->ID, 'title' => sprintf('#%s: %s', $post->ID, $post->post_title), 'content' => $post_content]; | |
| 72 | 76 | } |
| 73 | - $post_count = count($wpQuery->posts); | |
| 74 | - $has_more = $per_page > 0 && $post_count === $per_page; | |
| 75 | - return ['metadata' => ['next_batch' => $has_more ? $next_batch + 1 : \false, 'total_batches' => \false], 'contents' => $contents]; | |
| 77 | + return ['metadata' => $scan->metadata(), 'contents' => $contents]; | |
| 76 | 78 | } |
| 77 | 79 | } |