| 1 |
<?php |
| 2 |
/** |
| 3 |
* Register the external media to both WP Admin and Editor. |
| 4 |
* |
| 5 |
* @package automattic/jetpack-external-media |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Automattic\Jetpack\External_Media; |
| 9 |
|
| 10 |
use Automattic\Jetpack\Assets; |
| 11 |
use Automattic\Jetpack\Connection\Initial_State as Connection_Initial_State; |
| 12 |
use Automattic\Jetpack\Constants; |
| 13 |
use Automattic\Jetpack\External_Connections; |
| 14 |
use Automattic\Jetpack\Status\Host; |
| 15 |
use Jetpack_Options; |
| 16 |
|
| 17 |
/** |
| 18 |
* Class External_Media |
| 19 |
*/ |
| 20 |
class External_Media { |
| 21 |
const PACKAGE_VERSION = '0.9.7'; |
| 22 |
const BASE_DIR = __DIR__ . '/'; |
| 23 |
const BASE_FILE = __FILE__; |
| 24 |
|
| 25 |
private const EDITOR_HANDLE = 'jetpack-external-media-editor'; |
| 26 |
|
| 27 |
/** |
| 28 |
* Add hooks and filters. |
| 29 |
*/ |
| 30 |
public static function init() { |
| 31 |
// Load external media import page on WordPress.com sites first. |
| 32 |
// We will continue to enable the feature on all sites. |
| 33 |
$host = new Host(); |
| 34 |
if ( $host->is_wpcom_platform() ) { |
| 35 |
require_once __DIR__ . '/features/admin/external-media-import.php'; |
| 36 |
} |
| 37 |
|
| 38 |
// Only the stylesheet goes into the iframed canvas: loading the script there hurt site editor performance. See pdWQjU-1rA-p2. |
| 39 |
add_action( 'enqueue_block_editor_assets', array( __CLASS__, 'enqueue_block_editor_assets' ) ); |
| 40 |
add_action( 'enqueue_block_assets', array( __CLASS__, 'enqueue_block_canvas_styles' ) ); |
| 41 |
|
| 42 |
External_Connections::add_settings_for_service( |
| 43 |
'media', |
| 44 |
array( |
| 45 |
'service' => 'google_photos', |
| 46 |
'title' => __( 'Google Photos', 'jetpack-external-media' ), |
| 47 |
'description' => __( 'Access photos stored in your Google Photos library.', 'jetpack-external-media' ), |
| 48 |
'support_link' => array( |
| 49 |
'wpcom' => 'https://wordpress.com/support/google-photos/', |
| 50 |
'jetpack' => 'using-your-google-photos-with-jetpack/', |
| 51 |
), |
| 52 |
) |
| 53 |
); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Enqueue block editor assets. |
| 58 |
*/ |
| 59 |
public static function enqueue_block_editor_assets() { |
| 60 |
self::register_editor_assets(); |
| 61 |
Assets::enqueue_script( self::EDITOR_HANDLE ); |
| 62 |
|
| 63 |
wp_add_inline_script( |
| 64 |
self::EDITOR_HANDLE, |
| 65 |
sprintf( 'var JetpackExternalMediaData = %s;', wp_json_encode( self::get_data(), JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ) ), |
| 66 |
'before' |
| 67 |
); |
| 68 |
|
| 69 |
Connection_Initial_State::render_script( self::EDITOR_HANDLE ); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Enqueue the editor stylesheet in the iframed editor canvas, where block placeholders render the media buttons. |
| 74 |
* |
| 75 |
* @since 0.9.5 |
| 76 |
*/ |
| 77 |
public static function enqueue_block_canvas_styles() { |
| 78 |
// Skip the front end, and the editor's parent document, where the editor script already brings the stylesheet. |
| 79 |
if ( ! is_admin() || wp_style_is( self::EDITOR_HANDLE ) ) { |
| 80 |
return; |
| 81 |
} |
| 82 |
|
| 83 |
self::register_editor_assets(); |
| 84 |
$style = wp_styles()->query( self::EDITOR_HANDLE ); |
| 85 |
if ( $style ) { |
| 86 |
// A separate handle drops the script-derived deps, which would load the editor UI styles into every canvas. |
| 87 |
wp_enqueue_style( self::EDITOR_HANDLE . '-canvas', $style->src, array(), $style->ver ); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Register the editor script and its stylesheet. |
| 93 |
*/ |
| 94 |
private static function register_editor_assets() { |
| 95 |
Assets::register_script( |
| 96 |
self::EDITOR_HANDLE, |
| 97 |
'build/' . self::EDITOR_HANDLE . '/' . self::EDITOR_HANDLE . '.js', |
| 98 |
self::BASE_FILE, |
| 99 |
array( |
| 100 |
'textdomain' => 'jetpack-external-media', |
| 101 |
) |
| 102 |
); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Get the initial state data. |
| 107 |
* |
| 108 |
* @return array |
| 109 |
*/ |
| 110 |
private static function get_data() { |
| 111 |
$host = new Host(); |
| 112 |
if ( $host->is_wpcom_simple() ) { |
| 113 |
$blog_id = get_current_blog_id(); |
| 114 |
} else { |
| 115 |
$blog_id = Jetpack_Options::get_option( 'id', 0 ); |
| 116 |
} |
| 117 |
|
| 118 |
$jetpack_ai_enabled = false; |
| 119 |
if ( $host->is_wpcom_platform() ) { |
| 120 |
$jetpack_ai_enabled = true; |
| 121 |
} |
| 122 |
|
| 123 |
return array( |
| 124 |
'wpcomBlogId' => $blog_id, |
| 125 |
'pluginBasePath' => plugins_url( '', Constants::get_constant( 'JETPACK__PLUGIN_FILE' ) ), |
| 126 |
'ai-assistant' => array( |
| 127 |
'is-enabled' => apply_filters( 'jetpack_ai_enabled', $jetpack_ai_enabled ), |
| 128 |
), |
| 129 |
); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Check whether the environment supports the newer default size of elements, gradually introduced starting with WP 6.4. |
| 134 |
* |
| 135 |
* @since jetpack-14.0 |
| 136 |
* @deprecated since 0.8.0 |
| 137 |
* @return bool |
| 138 |
*/ |
| 139 |
public static function site_supports_next_default_size() { |
| 140 |
_deprecated_function( __METHOD__, 'jetpack-external-media-0.8.0' ); |
| 141 |
return true; |
| 142 |
} |
| 143 |
} |
| 144 |
|