Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 47
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
External_Media
0.00% covered (danger)
0.00%
0 / 47
0.00% covered (danger)
0.00%
0 / 3
56
0.00% covered (danger)
0.00%
0 / 1
 init
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
6
 enqueue_block_editor_assets
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
2
 get_data
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
12
 site_supports_next_default_size
n/a
0 / 0
n/a
0 / 0
1
1<?php
2/**
3 * Register the external media to both WP Admin and Editor.
4 *
5 * @package automattic/jetpack-external-media
6 */
7
8namespace Automattic\Jetpack\External_Media;
9
10use Automattic\Jetpack\Assets;
11use Automattic\Jetpack\Connection\Initial_State as Connection_Initial_State;
12use Automattic\Jetpack\Constants;
13use Automattic\Jetpack\External_Connections;
14use Automattic\Jetpack\Status\Host;
15use Jetpack_Options;
16
17/**
18 * Class External_Media
19 */
20class External_Media {
21    const PACKAGE_VERSION = '0.8.24';
22    const BASE_DIR        = __DIR__ . '/';
23    const BASE_FILE       = __FILE__;
24
25    /**
26     * Add hooks and filters.
27     */
28    public static function init() {
29        // Load external media import page on WordPress.com sites first.
30        // We will continue to enable the feature on all sites.
31        $host = new Host();
32        if ( $host->is_wpcom_platform() ) {
33            require_once __DIR__ . '/features/admin/external-media-import.php';
34        }
35
36        // @todo this current approach results in a console warning in the editor related to adding the css to the iframe incorrectly.
37        // It has been temporarily added back to prevent performance issues in the site editor. See pdWQjU-1rA-p2 for more details.
38        add_action( 'enqueue_block_editor_assets', array( __CLASS__, 'enqueue_block_editor_assets' ) );
39
40        External_Connections::add_settings_for_service(
41            'media',
42            array(
43                'service'      => 'google_photos',
44                'title'        => __( 'Google Photos', 'jetpack-external-media' ),
45                'description'  => __( 'Access photos stored in your Google Photos library.', 'jetpack-external-media' ),
46                'support_link' => array(
47                    'wpcom'   => 'https://wordpress.com/support/google-photos/',
48                    'jetpack' => 'using-your-google-photos-with-jetpack/',
49                ),
50            )
51        );
52    }
53
54    /**
55     * Enqueue block editor assets.
56     */
57    public static function enqueue_block_editor_assets() {
58        $assets_base_path = 'build/';
59        $asset_name       = 'jetpack-external-media-editor';
60
61        Assets::register_script(
62            $asset_name,
63            $assets_base_path . "$asset_name/$asset_name.js",
64            self::BASE_FILE,
65            array(
66                'enqueue'    => true,
67                'textdomain' => 'jetpack-external-media',
68            )
69        );
70
71        wp_add_inline_script(
72            $asset_name,
73            sprintf( 'var JetpackExternalMediaData = %s;', wp_json_encode( self::get_data(), JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ) ),
74            'before'
75        );
76
77        Connection_Initial_State::render_script( $asset_name );
78    }
79
80    /**
81     * Get the initial state data.
82     *
83     * @return array
84     */
85    private static function get_data() {
86        $host = new Host();
87        if ( $host->is_wpcom_simple() ) {
88            $blog_id = get_current_blog_id();
89        } else {
90            $blog_id = Jetpack_Options::get_option( 'id', 0 );
91        }
92
93        $jetpack_ai_enabled = false;
94        if ( $host->is_wpcom_platform() ) {
95            $jetpack_ai_enabled = true;
96        }
97
98        return array(
99            'wpcomBlogId'    => $blog_id,
100            'pluginBasePath' => plugins_url( '', Constants::get_constant( 'JETPACK__PLUGIN_FILE' ) ),
101            'ai-assistant'   => array(
102                'is-enabled' => apply_filters( 'jetpack_ai_enabled', $jetpack_ai_enabled ),
103            ),
104        );
105    }
106
107    /**
108     * Check whether the environment supports the newer default size of elements, gradually introduced starting with WP 6.4.
109     *
110     * @since jetpack-14.0
111     * @deprecated since 0.8.0
112     * @return bool
113     */
114    public static function site_supports_next_default_size() {
115        _deprecated_function( __METHOD__, 'jetpack-external-media-0.8.0' );
116        return true;
117    }
118}