Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 71
0.00% covered (danger)
0.00%
0 / 5
CRAP
n/a
0 / 0
Automattic\Jetpack\Jetpack_Mu_Wpcom\A8C_Posts_List\custom_excerpt_read_more
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
Automattic\Jetpack\Jetpack_Mu_Wpcom\A8C_Posts_List\render_a8c_post_list_block
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
6
Automattic\Jetpack\Jetpack_Mu_Wpcom\A8C_Posts_List\register_blocks
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
2
Automattic\Jetpack\Jetpack_Mu_Wpcom\A8C_Posts_List\enqueue_styles
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
12
Automattic\Jetpack\Jetpack_Mu_Wpcom\A8C_Posts_List\enqueue_scripts
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * Posts list block file.
4 *
5 * @package automattic/jetpack-mu-wpcom
6 */
7
8namespace Automattic\Jetpack\Jetpack_Mu_Wpcom\A8C_Posts_List;
9
10use Automattic\Jetpack\Jetpack_Mu_Wpcom;
11
12require_once __DIR__ . '/utils.php';
13
14/**
15 * Excerpt more string.
16 *
17 * @return string More string.
18 */
19function custom_excerpt_read_more() {
20    return sprintf(
21        '&hellip; <a href="%1$s" title="%2$s" class="a8c-posts-list-item__read-more">%3$s</a>',
22        esc_url( get_the_permalink() ),
23        sprintf(
24            /* translators: %s: Name of current post */
25            esc_attr__( 'Continue reading %s', 'jetpack-mu-wpcom' ),
26            the_title_attribute( array( 'echo' => false ) )
27        ),
28        esc_html__( 'Read more', 'jetpack-mu-wpcom' )
29    );
30}
31
32/**
33 * Renders posts list.
34 *
35 * @param array  $attributes Block attributes.
36 * @param string $content    Block content.
37 * @return string
38 */
39function render_a8c_post_list_block( $attributes, $content ) {
40    static $rendering_block = false;
41
42    $posts_list = new \WP_Query(
43        array(
44            'post_type'        => 'post',
45            'posts_per_page'   => $attributes['postsPerPage'],
46            'post_status'      => 'publish',
47            'suppress_filters' => false,
48        )
49    );
50
51    add_filter( 'excerpt_more', __NAMESPACE__ . '\custom_excerpt_read_more' );
52
53    // Prevent situations when the block attempts rendering another a8c/posts-list block.
54    if ( true !== $rendering_block ) {
55        $rendering_block = true;
56
57        $content = render_template(
58            'posts-list',
59            array(
60                'posts_list' => $posts_list,
61            )
62        );
63
64        $rendering_block = false;
65    }
66
67    remove_filter( 'excerpt_more', __NAMESPACE__ . '\custom_excerpt_read_more' );
68
69    // Reset the custom query.
70    wp_reset_postdata();
71
72    return $content;
73}
74
75/**
76 * Register block.
77 */
78function register_blocks() {
79    register_block_type(
80        'a8c/posts-list',
81        array(
82            'attributes'      => array(
83                'postsPerPage' => array(
84                    'type'    => 'number',
85                    'default' => 10,
86                ),
87            ),
88            'render_callback' => __NAMESPACE__ . '\render_a8c_post_list_block',
89        )
90    );
91}
92add_action( 'init', __NAMESPACE__ . '\register_blocks', 100 );
93
94/**
95 * Enqueue block styles.
96 */
97function enqueue_styles() {
98    if ( ! has_block( 'a8c/posts-list' ) ) {
99        return;
100    }
101
102    $ext      = is_rtl()
103        ? 'rtl.css'
104        : 'css';
105    $css_file = "build/a8c-posts-list/a8c-posts-list.$ext";
106    wp_enqueue_style(
107        'posts-list-block-style',
108        plugins_url( $css_file, Jetpack_Mu_Wpcom::BASE_FILE ),
109        array(),
110        filemtime( Jetpack_Mu_Wpcom::BASE_DIR . $css_file )
111    );
112}
113add_action( 'enqueue_block_editor_assets', __NAMESPACE__ . '\enqueue_scripts', 100 );
114
115/**
116 * Enqueue block scripts.
117 */
118function enqueue_scripts() {
119    if ( ! has_block( 'a8c/posts-list' ) ) {
120        return;
121    }
122
123    $asset_file = include Jetpack_Mu_Wpcom::BASE_DIR . 'build/a8c-posts-list/a8c-posts-list.asset.php';
124    $js_file    = 'build/a8c-posts-list/a8c-posts-list.js';
125    wp_enqueue_script(
126        'a8c-posts-list-script',
127        plugins_url( $js_file, Jetpack_Mu_Wpcom::BASE_FILE ),
128        $asset_file['dependencies'] ?? array(),
129        $asset_file['version'] ?? filemtime( Jetpack_Mu_Wpcom::BASE_DIR . $js_file ),
130        true
131    );
132
133    wp_set_script_translations( 'a8c-posts-list-script', 'jetpack-mu-wpcom' );
134}
135add_action( 'enqueue_block_assets', __NAMESPACE__ . '\enqueue_styles', 100 );