Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
41.03% covered (danger)
41.03%
16 / 39
0.00% covered (danger)
0.00%
0 / 3
CRAP
n/a
0 / 0
wpcomsh_coblocks_plugins_loaded
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
30
wpcomsh_coblocks_there_can_be_only_one_opentable_block
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
wpcomsh_coblocks_font_preset_variable
88.89% covered (warning)
88.89%
16 / 18
0.00% covered (danger)
0.00%
0 / 1
10.14
1<?php
2/**
3 * Customizations for CoBlocks
4 *
5 * CoBlocks is automatically installed as part of the WordPress.com on Atomic migration process.
6 * We need to adjust some CoBlocks behaviors to provide tailored WordPress.com experience.
7 *
8 * @package coblocks
9 */
10
11/**
12 * Makes coblock modifications once all plugins have been loaded.
13 */
14function wpcomsh_coblocks_plugins_loaded() {
15    if ( defined( 'COBLOCKS_VERSION' ) ) {
16        add_filter( 'render_block', 'wpcomsh_coblocks_font_preset_variable', 10, 2 );
17    }
18
19    // Disable CoBlocks block-patterns.
20    // See https://github.com/godaddy-wordpress/coblocks/pull/1659
21    if (
22        class_exists( 'CoBlocks_Block_Patterns' )
23        && defined( 'CoBlocks_Block_Patterns::POST_TYPE' )
24    ) {
25        $instance = CoBlocks_Block_Patterns::register();
26
27        remove_action( 'admin_enqueue_scripts', array( $instance, 'conditional_load_patterns' ) );
28
29        if ( is_wp_version_compatible( '5.5' ) ) {
30            remove_action( 'init', array( $instance, 'register_post_type' ) );
31            remove_action( 'init', array( $instance, 'register_type_taxonomy' ) );
32            remove_action( 'init', array( $instance, 'register_category_taxonomy' ) );
33            remove_action( 'init', array( $instance, 'load_block_patterns' ) );
34            remove_action( 'rest_insert_' . CoBlocks_Block_Patterns::POST_TYPE, array( $instance, 'add_taxonomies_on_insert_post' ), 10 );
35
36            remove_filter( 'coblocks_layout_selector_categories', array( $instance, 'load_categories' ) );
37            remove_filter( 'coblocks_layout_selector_layouts', array( $instance, 'load_layouts' ) );
38        }
39    }
40}
41add_action( 'plugins_loaded', 'wpcomsh_coblocks_plugins_loaded' );
42
43/**
44 * Disable Coblocks' OpenTable block
45 * Jetpack already ships with a similar block.
46 */
47function wpcomsh_coblocks_there_can_be_only_one_opentable_block() {
48    wp_add_inline_script(
49        'coblocks-editor',
50        <<<'SCRIPT'
51        document.addEventListener( 'DOMContentLoaded', function() {
52            if ( wp.blocks.getBlockType( 'jetpack/opentable' ) && wp.blocks.getBlockType( 'coblocks/opentable' ) ) {
53                wp.blocks.unregisterBlockType( 'coblocks/opentable' );
54            }
55        } );
56        SCRIPT
57    );
58}
59// Use later priority to give coblocks plenty of time to enqueue its scripts.
60add_action( 'enqueue_block_editor_assets', 'wpcomsh_coblocks_there_can_be_only_one_opentable_block', 99 );
61
62/**
63 * CoBlocks 3.x saves a font preset's slug as the wrapper's inline font-family; point it at the preset's variable.
64 *
65 * @param string $block_content Rendered block HTML.
66 * @param array  $block         Parsed block.
67 * @return string
68 */
69function wpcomsh_coblocks_font_preset_variable( $block_content, $block ) {
70    $slug = $block['attrs']['fontFamily'] ?? null;
71    if ( ! is_string( $slug ) || '' === $slug ) {
72        return $block_content;
73    }
74
75    $tags = new WP_HTML_Tag_Processor( $block_content );
76    if ( ! $tags->next_tag() || ! $tags->has_class( 'has-custom-font' ) ) {
77        return $block_content;
78    }
79
80    $style = $tags->get_attribute( 'style' );
81    if ( ! is_string( $style ) ) {
82        return $block_content;
83    }
84
85    $declarations = explode( ';', $style );
86    foreach ( $declarations as $i => $declaration ) {
87        $parts = explode( ':', $declaration, 2 );
88        if ( 2 !== count( $parts ) || 'font-family' !== trim( $parts[0] ) || trim( $parts[1] ) !== $slug ) {
89            continue;
90        }
91        // The fallback keeps CoBlocks 2.x real font names (e.g. Raleway) rendering when no preset matches.
92        $declarations[ $i ] = 'font-family:var(--wp--preset--font-family--' . _wp_to_kebab_case( $slug ) . ', ' . $slug . ')';
93        $tags->set_attribute( 'style', implode( ';', $declarations ) );
94        return $tags->get_updated_html();
95    }
96
97    return $block_content;
98}