Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
13.33% covered (danger)
13.33%
4 / 30
25.00% covered (danger)
25.00%
1 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Publicize_Assets
13.33% covered (danger)
13.33%
4 / 30
25.00% covered (danger)
25.00%
1 / 4
89.77
0.00% covered (danger)
0.00%
0 / 1
 configure
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 should_enqueue_block_editor_scripts
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 enqueue_block_editor_scripts
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
30
 register_wp_build_polyfills
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * Publicize_Assets.
4 *
5 * @package automattic/jetpack-publicize
6 */
7
8namespace Automattic\Jetpack\Publicize;
9
10use Automattic\Jetpack\Assets;
11use Automattic\Jetpack\WP_Build_Polyfills\WP_Build_Polyfills;
12
13/**
14 * Publicize_Assets class.
15 */
16class Publicize_Assets {
17
18    /**
19     * Initialize the class.
20     */
21    public static function configure() {
22        Publicize_Script_Data::configure();
23
24        add_action( 'enqueue_block_editor_assets', array( __CLASS__, 'enqueue_block_editor_scripts' ), 15 );
25    }
26
27    /**
28     * Whether to enqueue the block editor scripts.
29     *
30     * @return boolean True if the criteria are met.
31     */
32    public static function should_enqueue_block_editor_scripts() {
33
34        $post_type = get_post_type();
35
36        if ( empty( $post_type ) || ! post_type_supports( $post_type, 'publicize' ) ) {
37            return false;
38        }
39
40        return Publicize_Utils::current_user_can_access_publicize_data();
41    }
42
43    /**
44     * Enqueue block editor scripts and styles.
45     */
46    public static function enqueue_block_editor_scripts() {
47        if ( ! self::should_enqueue_block_editor_scripts() ) {
48            return;
49        }
50
51        // We don't want to render the Social UI in Jetpack sidebar
52        // if Jetpack is old, which has it bundled.
53        if ( defined( 'JETPACK__VERSION' ) && ( version_compare( (string) JETPACK__VERSION, '14.5-a.1', '<' ) ) ) {
54            return;
55        }
56
57        $script_to_load = class_exists( 'Jetpack' ) ? 'block-editor-jetpack' : 'block-editor-social';
58
59        self::register_wp_build_polyfills();
60
61        // Dequeue the old Social assets.
62        wp_dequeue_script( 'jetpack-social-editor' );
63        wp_dequeue_style( 'jetpack-social-editor' );
64
65        Assets::register_script(
66            'jetpack-social-editor',
67            sprintf( '../build/%s.js', $script_to_load ),
68            __FILE__,
69            array(
70                'in_footer'  => true,
71                'textdomain' => 'jetpack-publicize-pkg',
72                'enqueue'    => true,
73            )
74        );
75    }
76
77    /**
78     * Register the wp-build polyfills Social's block editor bundles request.
79     *
80     * Runs only where enqueue_block_editor_scripts() loads those bundles, as the editor has no `page` slug
81     * to gate on. Keep it there: below WP 7.1 it can replace Core's wp-private-apis and wp-rich-text editor-wide.
82     */
83    public static function register_wp_build_polyfills() {
84        if ( ! class_exists( WP_Build_Polyfills::class ) ) {
85            return;
86        }
87
88        WP_Build_Polyfills::register(
89            'jetpack-social',
90            array( 'wp-theme', 'wp-private-apis', 'wp-rich-text' )
91        );
92    }
93}