| 1 |
<?php |
| 2 |
/** |
| 3 |
* Publicize_Assets. |
| 4 |
* |
| 5 |
* @package automattic/jetpack-publicize |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Automattic\Jetpack\Publicize; |
| 9 |
|
| 10 |
use Automattic\Jetpack\Assets; |
| 11 |
use Automattic\Jetpack\WP_Build_Polyfills\WP_Build_Polyfills; |
| 12 |
|
| 13 |
/** |
| 14 |
* Publicize_Assets class. |
| 15 |
*/ |
| 16 |
class 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 |
} |
| 94 |
|