Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 120
0.00% covered (danger)
0.00%
0 / 12
CRAP
0.00% covered (danger)
0.00%
0 / 1
Jetpack_VideoPress
0.00% covered (danger)
0.00%
0 / 117
0.00% covered (danger)
0.00%
0 / 12
1482
0.00% covered (danger)
0.00%
0 / 1
 init
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 on_init
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 enqueue_jwt_token_bridge
n/a
0 / 0
n/a
0 / 0
1
 media_new_page_admin_notice
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
6
 jetpack_module_deactivated
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 can
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
72
 enqueue_admin_styles
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 delete_video_wpcom
n/a
0 / 0
n/a
0 / 0
1
 enqueue_admin_scripts
0.00% covered (danger)
0.00%
0 / 39
0.00% covered (danger)
0.00%
0 / 1
12
 maybe_get_attached_url_for_videopress
n/a
0 / 0
n/a
0 / 0
1
 videopress_pluploder_config
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 should_override_media_uploader
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
12
 is_videopress_enabled
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 print_in_footer_open_media_add_new
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
30
 add_video_upload_mimes
n/a
0 / 0
n/a
0 / 0
1
 filter_video_mimes
n/a
0 / 0
n/a
0 / 0
1
 wp_mime_type_icon
n/a
0 / 0
n/a
0 / 0
1
 add_videopress_extenstion
n/a
0 / 0
n/a
0 / 0
1
1<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2
3use Automattic\Jetpack\Assets;
4use Automattic\Jetpack\VideoPress\Attachment_Handler;
5use Automattic\Jetpack\VideoPress\Jwt_Token_Bridge;
6use Automattic\Jetpack\VideoPress\Options as VideoPress_Options;
7
8if ( ! defined( 'ABSPATH' ) ) {
9    exit( 0 );
10}
11
12/**
13 * VideoPress in Jetpack
14 */
15class Jetpack_VideoPress {
16    /**
17     * Module name.
18     *
19     * @var string
20     */
21    public $module = 'videopress';
22
23    /**
24     * Singleton
25     */
26    public static function init() {
27        static $instance = false;
28
29        if ( ! $instance ) {
30            $instance = new Jetpack_VideoPress();
31        }
32
33        return $instance;
34    }
35
36    /**
37     * Jetpack_VideoPress constructor.
38     *
39     * Sets up the initializer and makes sure that videopress activates and deactivates properly.
40     */
41    private function __construct() {
42        add_action( 'init', array( $this, 'on_init' ) );
43        add_action( 'jetpack_deactivate_module_videopress', array( $this, 'jetpack_module_deactivated' ) );
44    }
45
46    /**
47     * Fires on init
48     */
49    public function on_init() {
50        add_action( 'wp_enqueue_media', array( $this, 'enqueue_admin_scripts' ) );
51        add_filter( 'plupload_default_settings', array( $this, 'videopress_pluploder_config' ) );
52
53        add_action( 'admin_print_footer_scripts', array( $this, 'print_in_footer_open_media_add_new' ) );
54        add_action( 'admin_head', array( $this, 'enqueue_admin_styles' ) );
55
56        VideoPress_Scheduler::init();
57
58        if ( $this->is_videopress_enabled() ) {
59            add_action( 'admin_notices', array( $this, 'media_new_page_admin_notice' ) );
60        }
61    }
62
63    /**
64     * Enqueues the jwt bridge script.
65     *
66     * @deprecated 11.3
67     */
68    public function enqueue_jwt_token_bridge() {
69        _deprecated_function( __METHOD__, 'jetpack-11.3', 'Automattic\Jetpack\VideoPress\Jwt_Token_Bridge::enqueue_jwt_token_bridge' );
70        return Jwt_Token_Bridge::enqueue_jwt_token_bridge();
71    }
72
73    /**
74     * The media-new.php page isn't supported for uploading to VideoPress.
75     *
76     * There is either a technical reason for this (bulk uploader isn't overridable),
77     * or it is an intentional way to give site owners an option for uploading videos that bypass VideoPress.
78     */
79    public function media_new_page_admin_notice() {
80        global $pagenow;
81        if ( 'media-new.php' !== $pagenow ) {
82            return;
83        }
84
85        $message = sprintf(
86            wp_kses(
87                /* translators: %s is the url to the Media Library */
88                __( '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' ),
89                array( 'a' => array( 'href' => array() ) )
90            ),
91            esc_url( admin_url( 'upload.php?mode=grid&action=add-new' ) )
92        );
93        wp_admin_notice(
94            $message,
95            array(
96                'type'        => 'warning',
97                'dismissible' => true,
98            )
99        );
100    }
101
102    /**
103     * Runs when the VideoPress module is deactivated.
104     */
105    public function jetpack_module_deactivated() {
106        VideoPress_Options::delete_options();
107    }
108
109    /**
110     * Similar to current_user_can, but internal to VideoPress.
111     *
112     * @param string $cap Capability name.
113     * @param int    $user_id User ID.
114     * @return bool Returns true if the given VideoPress capability is allowed by the given user.
115     */
116    public function can( $cap, $user_id = false ) {
117        if ( ! $user_id ) {
118            $user_id = get_current_user_id();
119        }
120
121        // Connection owners are allowed to do all the things.
122        if ( Jetpack::connection()->is_connection_owner( $user_id ) ) {
123            return true;
124        }
125
126        // Additional and internal caps checks
127        if ( ! user_can( $user_id, 'upload_files' ) ) {
128            return false;
129        }
130
131        if ( 'edit_videos' === $cap && ! user_can( $user_id, 'edit_others_posts' ) ) {
132            return false;
133        }
134
135        if ( 'delete_videos' === $cap && ! user_can( $user_id, 'delete_others_posts' ) ) {
136            return false;
137        }
138
139        return true;
140    }
141
142    /**
143     * Register and enqueue VideoPress admin styles.
144     */
145    public function enqueue_admin_styles() {
146        wp_register_style( 'videopress-admin', plugins_url( 'videopress-admin.css', __FILE__ ), array(), JETPACK__VERSION );
147        wp_enqueue_style( 'videopress-admin' );
148    }
149
150    /**
151     * Attempts to delete a VideoPress video from wp.com.
152     * Will block the deletion from continuing if certain errors return from the wp.com API.
153     *
154     * @param Boolean $delete if the deletion should occur or not (unused).
155     * @param WP_Post $post the post object.
156     *
157     * @deprecated 11.3
158     *
159     * @return null|WP_Error|Boolean null if deletion should continue.
160     */
161    public function delete_video_wpcom( $delete, $post ) {
162        _deprecated_function( __METHOD__, 'jetpack-11.3', 'Automattic\Jetpack\VideoPress\Attachment_Handler::delete_video_wpcom' );
163        return Attachment_Handler::delete_video_wpcom( $delete, $post );
164    }
165
166    /**
167     * Register VideoPress admin scripts.
168     */
169    public function enqueue_admin_scripts() {
170        if ( did_action( 'videopress_enqueue_admin_scripts' ) ) {
171            return;
172        }
173
174        if ( $this->should_override_media_uploader() ) {
175            wp_enqueue_script(
176                'videopress-plupload',
177                Assets::get_file_url_for_environment(
178                    '_inc/build/videopress/js/videopress-plupload.min.js',
179                    'modules/videopress/js/videopress-plupload.js'
180                ),
181                array(
182                    'jquery',
183                    'wp-plupload',
184                ),
185                JETPACK__VERSION,
186                true
187            );
188
189            wp_enqueue_script(
190                'videopress-uploader',
191                Assets::get_file_url_for_environment(
192                    '_inc/build/videopress/js/videopress-uploader.min.js',
193                    'modules/videopress/js/videopress-uploader.js'
194                ),
195                array(
196                    'videopress-plupload',
197                ),
198                JETPACK__VERSION,
199                true
200            );
201
202            wp_enqueue_script(
203                'media-video-widget-extensions',
204                Assets::get_file_url_for_environment(
205                    '_inc/build/videopress/js/media-video-widget-extensions.min.js',
206                    'modules/videopress/js/media-video-widget-extensions.js'
207                ),
208                array(),
209                JETPACK__VERSION,
210                true
211            );
212        }
213
214        /**
215         * Fires after VideoPress scripts are enqueued in the dashboard.
216         *
217         * @since 2.5.0
218         */
219        do_action( 'videopress_enqueue_admin_scripts' );
220    }
221
222    /**
223     * Returns the VideoPress URL for the give post id, otherwise returns the provided default.
224     *
225     * This is an attachment-based filter handler.
226     *
227     * @deprecated 11.3
228     *
229     * @param string $default The default return value if post id is not a VideoPress video.
230     * @param int    $post_id The post id for the current attachment.
231     */
232    public function maybe_get_attached_url_for_videopress( $default, $post_id ) {
233        _deprecated_function( __METHOD__, 'jetpack-11.3', 'Automattic\Jetpack\VideoPress\Attachment_Handler::maybe_get_attached_url_for_videopress' );
234        return Attachment_Handler::maybe_get_attached_url_for_videopress( $default, $post_id );
235    }
236
237    /**
238     * Modify the default plupload config to turn on VideoPress specific filters.
239     *
240     * @param array $config The plupload config.
241     */
242    public function videopress_pluploder_config( $config ) {
243
244        if ( ! isset( $config['filters']['max_file_size'] ) ) {
245            $config['filters']['max_file_size'] = wp_max_upload_size() . 'b';
246        }
247
248        $config['filters']['videopress_check_uploads'] = $config['filters']['max_file_size'];
249
250        // We're doing our own check in the videopress_check_uploads filter.
251        unset( $config['filters']['max_file_size'] );
252
253        return $config;
254    }
255
256    /**
257     * Helper function to determine if the media uploader should be overridden.
258     *
259     * The rules are simple, only try to load the script when on the edit post or new post pages.
260     *
261     * @return bool
262     */
263    protected function should_override_media_uploader() {
264        global $pagenow;
265
266        // Only load in the admin
267        if ( ! is_admin() ) {
268            return false;
269        }
270
271        $acceptable_pages = array(
272            'post-new.php',
273            'post.php',
274            'upload.php',
275            'customize.php',
276        );
277
278        // Only load on the post, new post, or upload pages.
279        if ( ! in_array( $pagenow, $acceptable_pages, true ) ) {
280            return false;
281        }
282
283        return $this->is_videopress_enabled();
284    }
285
286    /**
287     * Detects if VideoPress is enabled.
288     *
289     * @return bool
290     */
291    protected function is_videopress_enabled() {
292        $options = VideoPress_Options::get_options();
293
294        return $options['shadow_blog_id'] > 0;
295    }
296
297    /**
298     * A work-around / hack to make it possible to go to the media library with the add new box open.
299     *
300     * @return bool
301     */
302    public function print_in_footer_open_media_add_new() {
303        global $pagenow;
304
305        // Only load in the admin
306        if ( ! is_admin() ) {
307            return false;
308        }
309
310        if ( $pagenow !== 'upload.php' ) {
311            return false;
312        }
313
314        if ( ! isset( $_GET['action'] ) || $_GET['action'] !== 'add-new' ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
315            return false;
316        }
317
318        ?>
319            <script type="text/javascript">
320                ( function( $ ) {
321                    window.setTimeout( function() {
322                        $('#wp-media-grid .page-title-action').click();
323                    }, 500 );
324
325                }( jQuery ) );
326            </script>
327        <?php
328    }
329
330    /**
331     * Makes sure that all video mimes are added in, as multi site installs can remove them.
332     *
333     * @deprecated 11.3
334     *
335     * @param array $existing_mimes Mime types to extend/filter.
336     * @return array
337     */
338    public function add_video_upload_mimes( $existing_mimes = array() ) {
339        _deprecated_function( __METHOD__, 'jetpack-11.3', 'Automattic\Jetpack\VideoPress\Attachment_Handler::add_video_upload_mimes' );
340        return Attachment_Handler::add_video_upload_mimes( $existing_mimes );
341    }
342
343    /**
344     * Filter designed to get rid of non video mime types.
345     *
346     * @deprecated 11.3
347     *
348     * @param string $value Mime type to filter.
349     * @return int
350     */
351    public function filter_video_mimes( $value ) {
352        _deprecated_function( __METHOD__, 'jetpack-11.3', 'Automattic\Jetpack\VideoPress\Attachment_Handler::filter_video_mimes' );
353        return Attachment_Handler::filter_video_mimes( $value );
354    }
355
356    /**
357     * Filter the mime type icon.
358     *
359     * @param string $icon Icon path.
360     * @param string $mime Mime type.
361     * @param int    $post_id Post ID.
362     *
363     * @deprecated 11.3
364     *
365     * @return string
366     */
367    public function wp_mime_type_icon( $icon, $mime, $post_id ) {
368        _deprecated_function( __METHOD__, 'jetpack-11.3', 'Automattic\Jetpack\VideoPress\Attachment_Handler::wp_mime_type_icon' );
369        return Attachment_Handler::wp_mime_type_icon( $icon, $mime, $post_id );
370    }
371
372    /**
373     * Filter the list of supported video formats.
374     *
375     * @param array $extensions Supported video formats.
376     *
377     * @deprecated 11.3
378     *
379     * @return array
380     */
381    public function add_videopress_extenstion( $extensions ) {
382        _deprecated_function( __METHOD__, 'jetpack-11.3', 'Automattic\Jetpack\VideoPress\Attachment_Handler::add_videopress_extenstion' );
383        return Attachment_Handler::add_videopress_extenstion( $extensions );
384    }
385}
386
387// Initialize the module.
388Jetpack_VideoPress::init();