Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Publicize_Assets
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 4
132
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
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        /** 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 / wp-rich-text handles the
82     * Social bundles depend on but WP < 7.0 does not ship (or ships with an incomplete
83     * allowlist).
84     *
85     * `wp-rich-text` is needed because the editor bundles reach `@wordpress/dataviews`
86     * (via `@wordpress/ui`), whose dataform controls unlock rich-text's `privateApis` at
87     * module scope. WP 6.9 exports none, so without the polyfill the bundle throws
88     * "Cannot unlock an undefined object" before the sidebar renders.
89     *
90     * Only the handles Social actually uses are requested, to keep the polyfill's
91     * force-replacement off any handle we don't need — `wp-notices` in particular, which
92     * Core registers adequately for these bundles.
93     */
94    public static function register_wp_build_polyfills() {
95        if ( ! class_exists( WP_Build_Polyfills::class ) ) {
96            return;
97        }
98
99        WP_Build_Polyfills::register(
100            'jetpack-social',
101            array( 'wp-theme', 'wp-private-apis', 'wp-rich-text' )
102        );
103    }
104}