| 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 WP_Query; |
| 15 |
/** |
| 16 |
* @author Joshua Gugun Siagian <suabahasa@gmail.com> |
| 17 |
*/ |
| 18 |
class Compile |
| 19 |
{ |
| 20 |
/** |
| 21 |
* @param array $metadata |
| 22 |
*/ |
| 23 |
public function __invoke($metadata) : array |
| 24 |
{ |
| 25 |
return $this->get_contents($metadata); |
| 26 |
} |
| 27 |
public function get_contents($metadata) : array |
| 28 |
{ |
| 29 |
$contents = []; |
| 30 |
$post_types = \apply_filters('f!windpress/integration/gutenberg/compile:get_contents.post_types', ['post', 'page', 'wp_template']); |
| 31 |
$next_batch = $metadata['next_batch'] !== \false ? $metadata['next_batch'] : 1; |
| 32 |
$wpQuery = new WP_Query(['posts_per_page' => \apply_filters('f!windpress/integration/gutenberg/compile:get_contents.post_per_page', (int) \get_option('posts_per_page', 10)), 'post_type' => $post_types, 'paged' => $next_batch]); |
| 33 |
foreach ($wpQuery->posts as $post) { |
| 34 |
if (\trim($post->post_content) === '' || \trim($post->post_content) === '0') { |
| 35 |
continue; |
| 36 |
} |
| 37 |
$post_content = $post->post_content; |
| 38 |
if (\apply_filters('f!windpress/integration/gutenberg/compile:get_contents.render', \true, $post)) { |
| 39 |
$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); |
| 40 |
foreach ($fn_renders as $fn_render) { |
| 41 |
try { |
| 42 |
$post_content = $fn_render($post_content); |
| 43 |
} catch (\Throwable $th) { |
| 44 |
if (\WP_DEBUG) { |
| 45 |
\error_log($th->getMessage()); |
| 46 |
} |
| 47 |
} |
| 48 |
} |
| 49 |
} |
| 50 |
$post_content = \apply_filters('f!windpress/integration/gutenberg/compile:get_contents.post_content', $post_content, $post); |
| 51 |
$contents[] = ['id' => $post->ID, 'title' => \sprintf('#%s: %s', $post->ID, $post->post_title), 'content' => $post_content]; |
| 52 |
} |
| 53 |
return ['metadata' => ['next_batch' => $wpQuery->max_num_pages > $next_batch ? $next_batch + 1 : \false], 'contents' => $contents]; |
| 54 |
} |
| 55 |
} |
| 56 |
|