Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 69
0.00% covered (danger)
0.00%
0 / 3
CRAP
n/a
0 / 0
Automattic\Jetpack\Jetpack_Mu_Wpcom\Newspack_Blocks\register_rest_routes
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
Automattic\Jetpack\Jetpack_Mu_Wpcom\Newspack_Blocks\blog_posts_block_name
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
Automattic\Jetpack\Jetpack_Mu_Wpcom\Newspack_Blocks\enqueue_newspack_blocks_data
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * The newspack blocks.
4 *
5 * @package automattic/jetpack-mu-wpcom
6 */
7
8namespace Automattic\Jetpack\Jetpack_Mu_Wpcom\Newspack_Blocks;
9
10use Automattic\Jetpack\Jetpack_Mu_Wpcom;
11
12define( 'NEWSPACK_BLOCKS__BLOCKS_DIRECTORY', Jetpack_Mu_Wpcom::BASE_DIR . 'build/' );
13define( 'NEWSPACK_BLOCKS__PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
14define( 'NEWSPACK_BLOCKS__VERSION', 'v4.35.0' );
15
16require_once __DIR__ . '/../../utils.php';
17require_once __DIR__ . '/synced-newspack-blocks/shared/authors.php';
18require_once __DIR__ . '/synced-newspack-blocks/class-newspack-blocks.php';
19require_once __DIR__ . '/synced-newspack-blocks/class-newspack-blocks-api.php';
20require_once __DIR__ . '/synced-newspack-blocks/blocks/homepage-articles/class-wp-rest-newspack-articles-controller.php';
21
22/**
23 * Registers Articles block routes.
24 */
25function register_rest_routes() {
26    $articles_controller = new \WP_REST_Newspack_Articles_Controller();
27    $articles_controller->register_routes();
28}
29add_action( 'rest_api_init', __NAMESPACE__ . '\register_rest_routes' );
30
31/**
32 * Filters block name.
33 *
34 * @param string $name Block name.
35 * @return string
36 */
37function blog_posts_block_name( $name ) {
38    if ( 'newspack-blocks/homepage-articles' === $name ) {
39        return 'a8c/blog-posts';
40    }
41
42    if ( 'newspack-blocks/carousel' === $name ) {
43        return 'a8c/posts-carousel';
44    }
45
46    return $name;
47}
48add_filter( 'newspack_blocks_block_name', __NAMESPACE__ . '\blog_posts_block_name' );
49
50/**
51 * Enqueue the data of the newspack blocks.
52 *
53 * @param string $handle The name of the script to add the data of the newspack blocks to.
54 */
55function enqueue_newspack_blocks_data( $handle ) {
56    $newspack_blocks_data = wp_json_encode(
57        array(
58            'posts_rest_url'          => rest_url( 'newspack-blocks/v1/newspack-blocks-posts' ),
59            // The editor store loads posts for several blocks at once through the batch endpoint.
60            'posts_batch_rest_url'    => rest_url( 'newspack-blocks/v1/newspack-blocks-posts-batch' ),
61            'posts_batch_max_queries' => \Newspack_Blocks_API::POSTS_BATCH_MAX_QUERIES,
62            'specific_posts_rest_url' => rest_url( 'newspack-blocks/v1/newspack-blocks-specific-posts' ),
63            // Define URL to core one to make autocomplete working for newspack-blocks installed via jetpack-mu-wpcom.
64            'authors_rest_url'        => rest_url() . 'wp/v2/users',
65            'custom_taxonomies'       => array(),
66        ),
67        JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP
68    );
69
70    wp_add_inline_script(
71        $handle,
72        "window.newspack_blocks_data = window.newspack_blocks_data ?? $newspack_blocks_data;",
73        'before'
74    );
75}
76
77/**
78 * Can be used to disable the Blog Posts Block.
79 */
80$disable_blog_posts_block = apply_filters( 'a8c_disable_blog_posts_block', false );
81if ( ! $disable_blog_posts_block ) {
82    require_once __DIR__ . '/synced-newspack-blocks/blocks/homepage-articles/view.php';
83
84    add_action(
85        'newspack_blocks_render_homepage_articles',
86        function () {
87            if ( ! is_admin() ) {
88                \jetpack_mu_wpcom_enqueue_assets( 'newspack-blocks-blog-posts-view', array( 'js', 'css' ) );
89            }
90        }
91    );
92
93    add_action(
94        'enqueue_block_editor_assets',
95        function () {
96            $handle = \jetpack_mu_wpcom_enqueue_assets( 'newspack-blocks-blog-posts-editor', array( 'js', 'css' ) );
97            enqueue_newspack_blocks_data( $handle );
98            wp_set_script_translations( $handle, 'jetpack-mu-wpcom' );
99        }
100    );
101}
102
103/**
104 * Can be used to disable the Post Carousel Block.
105 */
106$disable_posts_carousel_block = apply_filters( 'a8c_disable_posts_carousel_block', false ) || apply_filters( 'a8c_disable_post_carousel_block', false );
107if ( ! $disable_posts_carousel_block ) {
108    require_once __DIR__ . '/synced-newspack-blocks/blocks/carousel/view.php';
109
110    add_action(
111        'newspack_blocks_render_post_carousel',
112        function () {
113            if ( ! is_admin() ) {
114                \jetpack_mu_wpcom_enqueue_assets( 'newspack-blocks-carousel-view', array( 'js', 'css' ) );
115            }
116        }
117    );
118
119    add_action(
120        'enqueue_block_editor_assets',
121        function () {
122            $handle = \jetpack_mu_wpcom_enqueue_assets( 'newspack-blocks-carousel-editor', array( 'js', 'css' ) );
123            enqueue_newspack_blocks_data( $handle );
124            wp_set_script_translations( $handle, 'jetpack-mu-wpcom' );
125        }
126    );
127}