| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the WindPress package. |
| 5 |
* |
| 6 |
* (c) Joshua Gugun Siagian <suabahasa@gmail.com> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
declare (strict_types=1); |
| 12 |
namespace WindPress\WindPress\Integration\Gutenberg; |
| 13 |
|
| 14 |
use WindPressDeps\Symfony\Component\Yaml\Yaml; |
| 15 |
use WP_Query; |
| 16 |
/** |
| 17 |
* @author Joshua Gugun Siagian <suabahasa@gmail.com> |
| 18 |
*/ |
| 19 |
class Compile |
| 20 |
{ |
| 21 |
/** |
| 22 |
* @param array $metadata |
| 23 |
*/ |
| 24 |
public function __invoke($metadata): array |
| 25 |
{ |
| 26 |
return $this->get_contents($metadata); |
| 27 |
} |
| 28 |
public function get_contents($metadata): array |
| 29 |
{ |
| 30 |
$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]); |
| 35 |
foreach ($wpQuery->posts as $post) { |
| 36 |
$post_content = $post->post_content; |
| 37 |
$post_content_trimmed = trim($post_content); |
| 38 |
if ($post_content_trimmed === '' || $post_content_trimmed === '0') { |
| 39 |
continue; |
| 40 |
} |
| 41 |
/** |
| 42 |
* @TODO: More robust and reliable API for external usage. |
| 43 |
*/ |
| 44 |
if (apply_filters('f!windpress/integration/gutenberg/compile:get_contents.skip', \false, $post)) { |
| 45 |
continue; |
| 46 |
} |
| 47 |
if (apply_filters('f!windpress/integration/gutenberg/compile:get_contents.render', \true, $post)) { |
| 48 |
$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 |
} |
| 58 |
} |
| 59 |
$post_content = apply_filters('f!windpress/integration/gutenberg/compile:get_contents.post_content', $post_content, $post); |
| 60 |
// Decode HTML entities to preserve arbitrary variants like [&>img]:rounded-lg |
| 61 |
// WordPress rendering functions encode special characters (&, >, <, etc.) but Tailwind |
| 62 |
// needs the raw characters for proper class parsing |
| 63 |
// Apply decoding twice to handle double-encoded entities (e.g., &amp; -> & -> &) |
| 64 |
if (strpos($post_content, '&') !== \false) { |
| 65 |
$post_content = html_entity_decode($post_content, \ENT_QUOTES | \ENT_HTML5, 'UTF-8'); |
| 66 |
$post_content = html_entity_decode($post_content, \ENT_QUOTES | \ENT_HTML5, 'UTF-8'); |
| 67 |
} |
| 68 |
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)); |
| 70 |
} |
| 71 |
$contents[] = ['id' => $post->ID, 'title' => sprintf('#%s: %s', $post->ID, $post->post_title), 'content' => $post_content]; |
| 72 |
} |
| 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]; |
| 76 |
} |
| 77 |
} |
| 78 |
|