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