Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
16.13% covered (danger)
16.13%
5 / 31
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Publicize_Assets
16.13% covered (danger)
16.13%
5 / 31
0.00% covered (danger)
0.00%
0 / 4
82.39
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
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 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
83.33% covered (warning)
83.33%
5 / 6
0.00% covered (danger)
0.00%
0 / 1
2.02
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        /** This filter is documented in projects/packages/publicize/src/class-publicize-base.php */
41        $capability = apply_filters( 'jetpack_publicize_capability', 'publish_posts' );
42
43        return current_user_can( $capability );
44    }
45
46    /**
47     * Enqueue block editor scripts and styles.
48     */
49    public static function enqueue_block_editor_scripts() {
50        if ( ! self::should_enqueue_block_editor_scripts() ) {
51            return;
52        }
53
54        // We don't want to render the Social UI in Jetpack sidebar
55        // if Jetpack is old, which has it bundled.
56        if ( defined( 'JETPACK__VERSION' ) && ( version_compare( (string) JETPACK__VERSION, '14.5-a.1', '<' ) ) ) {
57            return;
58        }
59
60        $script_to_load = class_exists( 'Jetpack' ) ? 'block-editor-jetpack' : 'block-editor-social';
61
62        self::register_wp_build_polyfills();
63
64        // Dequeue the old Social assets.
65        wp_dequeue_script( 'jetpack-social-editor' );
66        wp_dequeue_style( 'jetpack-social-editor' );
67
68        Assets::register_script(
69            'jetpack-social-editor',
70            sprintf( '../build/%s.js', $script_to_load ),
71            __FILE__,
72            array(
73                'in_footer'  => true,
74                'textdomain' => 'jetpack-publicize-pkg',
75                'enqueue'    => true,
76            )
77        );
78    }
79
80    /**
81     * Register polyfills for the wp-theme / wp-private-apis handles the Social bundles
82     * depend on but WP < 7.0 does not ship (or ships with an incomplete allowlist).
83     *
84     * Only the two handles Social actually uses are requested, to keep the polyfill's
85     * `wp-private-apis` force-replacement off any handle we don't need.
86     */
87    public static function register_wp_build_polyfills() {
88        if ( ! class_exists( WP_Build_Polyfills::class ) ) {
89            return;
90        }
91
92        WP_Build_Polyfills::register(
93            'jetpack-social',
94            array( 'wp-theme', 'wp-private-apis' )
95        );
96    }
97}