← All changes
|
modules/videopress/class.jetpack-videopress.php
+122
-22
13.7.2
→
16.3-a.5
View file →
| @@ -1,10 +1,17 @@ | ||
| 1 | 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 | 2 | |
| 3 | 3 | use Automattic\Jetpack\Assets; |
| 4 | +use Automattic\Jetpack\Current_Plan; | |
| 5 | +use Automattic\Jetpack\Status; | |
| 4 | 6 | use Automattic\Jetpack\VideoPress\Attachment_Handler; |
| 5 | 7 | use Automattic\Jetpack\VideoPress\Jwt_Token_Bridge; |
| 6 | 8 | use Automattic\Jetpack\VideoPress\Options as VideoPress_Options; |
| 9 | + | |
| 10 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 11 | + exit( 0 ); | |
| 12 | +} | |
| 13 | + | |
| 7 | 14 | /** |
| 8 | 15 | * VideoPress in Jetpack |
| 9 | 16 | */ |
| 10 | 17 | class Jetpack_VideoPress { |
| @@ -46,14 +53,11 @@ | ||
| 46 | 53 | add_filter( 'plupload_default_settings', array( $this, 'videopress_pluploder_config' ) ); |
| 47 | 54 | |
| 48 | 55 | add_action( 'admin_print_footer_scripts', array( $this, 'print_in_footer_open_media_add_new' ) ); |
| 49 | 56 | add_action( 'admin_head', array( $this, 'enqueue_admin_styles' ) ); |
| 57 | + add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_media_new_scripts' ) ); | |
| 50 | 58 | |
| 51 | 59 | VideoPress_Scheduler::init(); |
| 52 | - | |
| 53 | - if ( $this->is_videopress_enabled() ) { | |
| 54 | - add_action( 'admin_notices', array( $this, 'media_new_page_admin_notice' ) ); | |
| 55 | - } | |
| 56 | 60 | } |
| 57 | 61 | |
| 58 | 62 | /** |
| 59 | 63 | * Enqueues the jwt bridge script. |
| @@ -65,33 +69,129 @@ | ||
| 65 | 69 | return Jwt_Token_Bridge::enqueue_jwt_token_bridge(); |
| 66 | 70 | } |
| 67 | 71 | |
| 68 | 72 | /** |
| 69 | - * The media-new.php page isn't supported for uploading to VideoPress. | |
| 73 | + * Enqueues the script that routes media-new.php video uploads to VideoPress. | |
| 70 | 74 | * |
| 71 | - * There is either a technical reason for this (bulk uploader isn't overridable), | |
| 72 | - * or it is an intentional way to give site owners an option for uploading videos that bypass VideoPress. | |
| 75 | + * The classic uploader on media-new.php builds a raw plupload.Uploader | |
| 76 | + * (via plupload-handlers) instead of wp.Uploader, so the override in | |
| 77 | + * videopress-plupload.js never engages there. This companion script | |
| 78 | + * registers the same videopress_check_uploads plupload filter and | |
| 79 | + * re-targets video uploads to VideoPress. The filter is injected into the | |
| 80 | + * uploader settings through the plupload_init filter, which | |
| 81 | + * media_upload_form() applies when it renders later in the request. | |
| 82 | + * | |
| 83 | + * @param string $hook_suffix The current admin page. | |
| 73 | 84 | */ |
| 74 | - public function media_new_page_admin_notice() { | |
| 75 | - global $pagenow; | |
| 76 | - if ( 'media-new.php' !== $pagenow ) { | |
| 85 | + public function enqueue_media_new_scripts( $hook_suffix ) { | |
| 86 | + if ( 'media-new.php' !== $hook_suffix ) { | |
| 77 | 87 | return; |
| 78 | 88 | } |
| 79 | 89 | |
| 80 | - $message = sprintf( | |
| 81 | - wp_kses( | |
| 82 | - /* translators: %s is the url to the Media Library */ | |
| 83 | - __( 'VideoPress uploads are not supported here. To upload to VideoPress, add your videos from the <a href="%s">Media Library</a> or the block editor using the Video block.', 'jetpack' ), | |
| 84 | - array( 'a' => array( 'href' => array() ) ) | |
| 90 | + if ( ! $this->is_videopress_enabled() ) { | |
| 91 | + return; | |
| 92 | + } | |
| 93 | + | |
| 94 | + // Version with the served file's mtime so browsers and page caches pick | |
| 95 | + // up new builds even when JETPACK__VERSION has not changed (branch | |
| 96 | + // testing, beta builds). | |
| 97 | + $script_relative = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG | |
| 98 | + ? 'modules/videopress/js/videopress-media-new.js' | |
| 99 | + : '_inc/build/videopress/js/videopress-media-new.min.js'; | |
| 100 | + $script_path = JETPACK__PLUGIN_DIR . $script_relative; | |
| 101 | + $script_version = file_exists( $script_path ) ? (string) filemtime( $script_path ) : JETPACK__VERSION; | |
| 102 | + | |
| 103 | + wp_enqueue_script( | |
| 104 | + 'videopress-media-new', | |
| 105 | + Assets::get_file_url_for_environment( | |
| 106 | + '_inc/build/videopress/js/videopress-media-new.min.js', | |
| 107 | + 'modules/videopress/js/videopress-media-new.js' | |
| 85 | 108 | ), |
| 86 | - esc_url( admin_url( 'upload.php' ) ) | |
| 109 | + array( | |
| 110 | + 'jquery', | |
| 111 | + 'plupload-handlers', | |
| 112 | + ), | |
| 113 | + $script_version, | |
| 114 | + true | |
| 87 | 115 | ); |
| 88 | - wp_admin_notice( | |
| 89 | - $message, | |
| 90 | - array( | |
| 91 | - 'type' => 'warning', | |
| 92 | - 'dismissible' => true, | |
| 93 | - ) | |
| 116 | + | |
| 117 | + wp_localize_script( 'videopress-media-new', 'videoPressMediaNew', $this->get_media_new_upload_limits() ); | |
| 118 | + | |
| 119 | + add_filter( 'plupload_init', array( $this, 'videopress_pluploder_config' ) ); | |
| 120 | + } | |
| 121 | + | |
| 122 | + /** | |
| 123 | + * Returns the free-plan upload limit data used by the media-new.php script. | |
| 124 | + * | |
| 125 | + * The free plan includes a single video upload, so the script needs to know | |
| 126 | + * whether the site has a paid VideoPress plan, whether the free upload has | |
| 127 | + * already been used, and where to send the user to upgrade. Mirrors the | |
| 128 | + * checks behind the VideoPress dashboard's upgrade notice: the paid check | |
| 129 | + * matches Admin_UI::initial_state()'s paidFeatures, and the used check | |
| 130 | + * matches the dashboard's VideoPress video count (video/videopress | |
| 131 | + * attachments). | |
| 132 | + * | |
| 133 | + * @return array | |
| 134 | + */ | |
| 135 | + public function get_media_new_upload_limits() { | |
| 136 | + $has_videopress_purchase = Current_Plan::supports( 'videopress-1tb-storage' ) | |
| 137 | + || Current_Plan::supports( 'videopress-unlimited-storage' ) | |
| 138 | + || ( function_exists( 'wpcom_site_has_feature' ) && wpcom_site_has_feature( 'videopress' ) ); | |
| 139 | + | |
| 140 | + $has_used_video = false; | |
| 141 | + if ( ! $has_videopress_purchase ) { | |
| 142 | + $videopress_videos = get_posts( | |
| 143 | + array( | |
| 144 | + 'post_type' => 'attachment', | |
| 145 | + 'post_status' => 'inherit', | |
| 146 | + 'post_mime_type' => 'video/videopress', | |
| 147 | + 'posts_per_page' => 1, | |
| 148 | + 'fields' => 'ids', | |
| 149 | + ) | |
| 150 | + ); | |
| 151 | + | |
| 152 | + $has_used_video = count( $videopress_videos ) > 0; | |
| 153 | + } | |
| 154 | + | |
| 155 | + $upgrade_url = sprintf( | |
| 156 | + 'https://wordpress.com/checkout/%s/jetpack_videopress?redirect_to=%s', | |
| 157 | + rawurlencode( ( new Status() )->get_site_suffix() ), | |
| 158 | + rawurlencode( admin_url( 'media-new.php' ) ) | |
| 159 | + ); | |
| 160 | + | |
| 161 | + $allowed_html = array( | |
| 162 | + 'a' => array( 'href' => array() ), | |
| 163 | + ); | |
| 164 | + | |
| 165 | + return array( | |
| 166 | + 'hasVideoPressPurchase' => $has_videopress_purchase, | |
| 167 | + 'hasUsedVideo' => $has_used_video, | |
| 168 | + 'strings' => array( | |
| 169 | + 'usedVideoUpload' => sprintf( | |
| 170 | + wp_kses( | |
| 171 | + /* translators: %s is the url to upgrade the VideoPress plan */ | |
| 172 | + __( 'You have used your free video upload. The free plan includes one video upload. <a href="%s">Upgrade now</a> to unlock unlimited videos, 1TB of storage, and more!', 'jetpack' ), | |
| 173 | + $allowed_html | |
| 174 | + ), | |
| 175 | + esc_url( $upgrade_url ) | |
| 176 | + ), | |
| 177 | + 'multipleVideos' => sprintf( | |
| 178 | + wp_kses( | |
| 179 | + /* translators: %s is the url to upgrade the VideoPress plan */ | |
| 180 | + __( 'The free plan includes one video upload. <a href="%s">Upgrade now</a> to upload more videos and unlock unlimited videos, 1TB of storage, and more!', 'jetpack' ), | |
| 181 | + $allowed_html | |
| 182 | + ), | |
| 183 | + esc_url( $upgrade_url ) | |
| 184 | + ), | |
| 185 | + 'multipleVideosSelected' => sprintf( | |
| 186 | + wp_kses( | |
| 187 | + /* translators: %s is the url to upgrade the VideoPress plan */ | |
| 188 | + __( 'Uploading multiple videos requires a paid plan. The free plan includes one video upload. <a href="%s">Upgrade now</a> to unlock unlimited videos, 1TB of storage, and more!', 'jetpack' ), | |
| 189 | + $allowed_html | |
| 190 | + ), | |
| 191 | + esc_url( $upgrade_url ) | |
| 192 | + ), | |
| 193 | + ), | |
| 94 | 194 | ); |
| 95 | 195 | } |
| 96 | 196 | |
| 97 | 197 | /** |