| 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\GreenShift; |
| 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 |
if (!defined('GREENSHIFT_DIR_PATH')) { |
| 26 |
return []; |
| 27 |
} |
| 28 |
return $this->get_contents($metadata); |
| 29 |
} |
| 30 |
public function get_contents($metadata): array |
| 31 |
{ |
| 32 |
$contents = []; |
| 33 |
$post_types = apply_filters('f!windpress/integration/greenshift/compile:get_contents.post_types', ['wp_block']); |
| 34 |
$next_batch = $metadata['next_batch'] !== \false ? $metadata['next_batch'] : 1; |
| 35 |
$per_page = apply_filters('f!windpress/integration/greenshift/compile:get_contents.post_per_page', (int) get_option('posts_per_page', 20)); |
| 36 |
$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]); |
| 37 |
foreach ($wpQuery->posts as $post) { |
| 38 |
$post_content = $post->post_content; |
| 39 |
$post_content_trimmed = trim($post_content); |
| 40 |
if ($post_content_trimmed === '' || $post_content_trimmed === '0') { |
| 41 |
continue; |
| 42 |
} |
| 43 |
if (apply_filters('f!windpress/integration/greenshift/compile:get_contents.render', \true, $post)) { |
| 44 |
$fn_renders = apply_filters('f!windpress/integration/greenshift/compile:get_contents.render_fn', ['do_blocks', 'wptexturize', 'convert_smilies', 'shortcode_unautop', 'wp_filter_content_tags', 'do_shortcode'], $post); |
| 45 |
foreach ($fn_renders as $fn_render) { |
| 46 |
try { |
| 47 |
$post_content = $fn_render($post_content); |
| 48 |
} catch (\Throwable $th) { |
| 49 |
if (\WP_DEBUG) { |
| 50 |
error_log($th->getMessage()); |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
$post_content = apply_filters('f!windpress/integration/greenshift/compile:get_contents.post_content', $post_content, $post); |
| 56 |
$contents[] = ['id' => $post->ID, 'title' => sprintf('#%s: %s', $post->ID, $post->post_title), 'content' => $post_content]; |
| 57 |
} |
| 58 |
$post_count = count($wpQuery->posts); |
| 59 |
$has_more = $per_page > 0 && $post_count === $per_page; |
| 60 |
return ['metadata' => ['next_batch' => $has_more ? $next_batch + 1 : \false, 'total_batches' => \false], 'contents' => $contents]; |
| 61 |
} |
| 62 |
} |
| 63 |
|