jetpack
/
jetpack_vendor
/
automattic
/
jetpack-external-media
/
src
/
features
/
admin
/
external-media-import.php
jetpack
/
jetpack_vendor
/
automattic
/
jetpack-external-media
/
src
/
features
/
admin
Last commit date
external-media-import.php
9 months ago
external-media-import.php
166 lines
| 1 | <?php |
| 2 | /** |
| 3 | * The Jetpack external media import page. |
| 4 | * |
| 5 | * Adds Jetpack external media page to Media > Import. |
| 6 | * |
| 7 | * @package automattic/jetpack-external-media |
| 8 | */ |
| 9 | |
| 10 | namespace Automattic\Jetpack\External_Media; |
| 11 | |
| 12 | use Automattic\Jetpack\Assets; |
| 13 | use Automattic\Jetpack\Connection\Manager as Connection_Manager; |
| 14 | |
| 15 | if ( ! defined( 'ABSPATH' ) ) { |
| 16 | exit( 0 ); |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * Whether the current user is connected to WordPress.com. |
| 21 | */ |
| 22 | function is_current_user_connected() { |
| 23 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
| 24 | return true; |
| 25 | } |
| 26 | |
| 27 | return ( new Connection_Manager( 'jetpack' ) )->is_user_connected(); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Register the Jetpack external media page to Media > Import. |
| 32 | */ |
| 33 | function add_jetpack_external_media_import_page() { |
| 34 | /** |
| 35 | * The feature is enabled only when the current user is connected to WordPress.com. |
| 36 | */ |
| 37 | if ( ! is_current_user_connected() ) { |
| 38 | return; |
| 39 | } |
| 40 | |
| 41 | $external_media_import_page_hook = add_submenu_page( |
| 42 | 'upload.php', |
| 43 | __( 'Import Media', 'jetpack-external-media' ), |
| 44 | __( 'Import Media', 'jetpack-external-media' ), |
| 45 | 'upload_files', |
| 46 | 'jetpack_external_media_import_page', |
| 47 | __NAMESPACE__ . '\render_jetpack_external_media_import_page' |
| 48 | ); |
| 49 | |
| 50 | add_action( 'load-upload.php', __NAMESPACE__ . '\enqueue_jetpack_external_media_import_button' ); |
| 51 | add_action( "load-$external_media_import_page_hook", __NAMESPACE__ . '\enqueue_jetpack_external_media_import_page' ); |
| 52 | } |
| 53 | add_action( 'admin_menu', __NAMESPACE__ . '\add_jetpack_external_media_import_page' ); |
| 54 | |
| 55 | /** |
| 56 | * Enqueue the assets of the Jetpack external media import button. |
| 57 | */ |
| 58 | function enqueue_jetpack_external_media_import_button() { |
| 59 | $assets_base_path = 'build/'; |
| 60 | $asset_name = 'jetpack-external-media-import-button'; |
| 61 | |
| 62 | Assets::register_script( |
| 63 | $asset_name, |
| 64 | $assets_base_path . "$asset_name/$asset_name.js", |
| 65 | External_Media::BASE_FILE, |
| 66 | array( |
| 67 | 'in_footer' => true, |
| 68 | 'textdomain' => 'jetpack-external-media', |
| 69 | 'css_path' => $assets_base_path . "$asset_name/$asset_name.css", |
| 70 | ) |
| 71 | ); |
| 72 | |
| 73 | Assets::enqueue_script( $asset_name ); |
| 74 | wp_localize_script( |
| 75 | $asset_name, |
| 76 | 'JETPACK_EXTERNAL_MEDIA_IMPORT_BUTTON', |
| 77 | array( |
| 78 | 'href' => admin_url( 'upload.php?page=jetpack_external_media_import_page' ), |
| 79 | ) |
| 80 | ); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Enqueue the assets of the Jetpack external media page. |
| 85 | */ |
| 86 | function enqueue_jetpack_external_media_import_page() { |
| 87 | $assets_base_path = 'build/'; |
| 88 | $asset_name = 'jetpack-external-media-import-page'; |
| 89 | |
| 90 | Assets::register_script( |
| 91 | $asset_name, |
| 92 | $assets_base_path . "$asset_name/$asset_name.js", |
| 93 | External_Media::BASE_FILE, |
| 94 | array( |
| 95 | 'in_footer' => true, |
| 96 | 'textdomain' => 'jetpack-external-media', |
| 97 | ) |
| 98 | ); |
| 99 | |
| 100 | Assets::enqueue_script( $asset_name ); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Render the container of the Jetpack external media page. |
| 105 | */ |
| 106 | function render_jetpack_external_media_import_page() { |
| 107 | $title = __( 'Import Media', 'jetpack-external-media' ); |
| 108 | $description = __( 'WordPress allows you to import media from various platforms directly into the Media Library. To begin, select a platform from the options below:', 'jetpack-external-media' ); |
| 109 | $external_media_sources = array( |
| 110 | array( |
| 111 | 'slug' => 'google_photos', |
| 112 | 'name' => __( 'Google Photos', 'jetpack-external-media' ), |
| 113 | 'description' => __( 'Import media from your Google Photos account.', 'jetpack-external-media' ), |
| 114 | ), |
| 115 | array( |
| 116 | 'slug' => 'pexels', |
| 117 | 'name' => __( 'Pexels free photos', 'jetpack-external-media' ), |
| 118 | 'description' => __( 'Free stock photos, royalty free images shared by creators.', 'jetpack-external-media' ), |
| 119 | ), |
| 120 | array( |
| 121 | 'slug' => 'openverse', |
| 122 | 'name' => __( 'Openverse', 'jetpack-external-media' ), |
| 123 | 'description' => __( 'Explore more than 800 million creative works.', 'jetpack-external-media' ), |
| 124 | ), |
| 125 | ); |
| 126 | |
| 127 | ?> |
| 128 | <div id="jetpack-external-media-import" class="wrap"> |
| 129 | <h1><?php echo esc_html( $title ); ?></h1> |
| 130 | <div id="jetpack-external-media-import-notice"></div> |
| 131 | <p><?php echo esc_html( $description ); ?></p> |
| 132 | <table class="widefat importers striped"> |
| 133 | <?php |
| 134 | foreach ( $external_media_sources as $external_media_source ) { |
| 135 | $slug = $external_media_source['slug']; |
| 136 | $name = $external_media_source['name']; |
| 137 | $description = $external_media_source['description']; |
| 138 | $action = sprintf( |
| 139 | '<a aria-label="%1$s" style="cursor:pointer;" data-slug="%2$s">%3$s</a>', |
| 140 | /* translators: %s: The name of the external media source. */ |
| 141 | esc_attr( sprintf( __( 'Import %s', 'jetpack-external-media' ), $name ) ), |
| 142 | esc_attr( $slug ), |
| 143 | __( 'Import now', 'jetpack-external-media' ) |
| 144 | ); |
| 145 | |
| 146 | ?> |
| 147 | <tr class='importer-item'> |
| 148 | <td class='import-system'> |
| 149 | <span class='importer-title'><?php echo esc_html( $name ); ?></span> |
| 150 | <span class='importer-action'> |
| 151 | <?php echo $action; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- we escape things above. ?> |
| 152 | </span> |
| 153 | </td> |
| 154 | <td class='desc'> |
| 155 | <span class='importer-desc'><?php echo esc_html( $description ); ?></span> |
| 156 | </td> |
| 157 | </tr> |
| 158 | <?php |
| 159 | } |
| 160 | ?> |
| 161 | </table> |
| 162 | <div id="jetpack-external-media-import-modal"></div> |
| 163 | </div> |
| 164 | <?php |
| 165 | } |
| 166 |