Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 67
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 / 14
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.30.3' );
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            'specific_posts_rest_url' => rest_url( 'newspack-blocks/v1/newspack-blocks-specific-posts' ),
60            // Define URL to core one to make autocomplete working for newspack-blocks installed via jetpack-mu-wpcom.
61            'authors_rest_url'        => rest_url() . 'wp/v2/users',
62            'custom_taxonomies'       => array(),
63        ),
64        JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP
65    );
66
67    wp_add_inline_script(
68        $handle,
69        "window.newspack_blocks_data = window.newspack_blocks_data ?? $newspack_blocks_data;",
70        'before'
71    );
72}
73
74/**
75 * Can be used to disable the Blog Posts Block.
76 */
77$disable_blog_posts_block = apply_filters( 'a8c_disable_blog_posts_block', false );
78if ( ! $disable_blog_posts_block ) {
79    require_once __DIR__ . '/synced-newspack-blocks/blocks/homepage-articles/view.php';
80
81    add_action(
82        'newspack_blocks_render_homepage_articles',
83        function () {
84            if ( ! is_admin() ) {
85                \jetpack_mu_wpcom_enqueue_assets( 'newspack-blocks-blog-posts-view', array( 'js', 'css' ) );
86            }
87        }
88    );
89
90    add_action(
91        'enqueue_block_editor_assets',
92        function () {
93            $handle = \jetpack_mu_wpcom_enqueue_assets( 'newspack-blocks-blog-posts-editor', array( 'js', 'css' ) );
94            enqueue_newspack_blocks_data( $handle );
95            wp_set_script_translations( $handle, 'jetpack-mu-wpcom' );
96        }
97    );
98}
99
100/**
101 * Can be used to disable the Post Carousel Block.
102 */
103$disable_posts_carousel_block = apply_filters( 'a8c_disable_posts_carousel_block', false ) || apply_filters( 'a8c_disable_post_carousel_block', false );
104if ( ! $disable_posts_carousel_block ) {
105    require_once __DIR__ . '/synced-newspack-blocks/blocks/carousel/view.php';
106
107    add_action(
108        'newspack_blocks_render_post_carousel',
109        function () {
110            if ( ! is_admin() ) {
111                \jetpack_mu_wpcom_enqueue_assets( 'newspack-blocks-carousel-view', array( 'js', 'css' ) );
112            }
113        }
114    );
115
116    add_action(
117        'enqueue_block_editor_assets',
118        function () {
119            $handle = \jetpack_mu_wpcom_enqueue_assets( 'newspack-blocks-carousel-editor', array( 'js', 'css' ) );
120            enqueue_newspack_blocks_data( $handle );
121            wp_set_script_translations( $handle, 'jetpack-mu-wpcom' );
122        }
123    );
124}