Completed
Push — add/jetpack-data-methods ( d7e5a6...2c93cb )
by
unknown
07:30
created

modules/videopress/class.jetpack-videopress.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * VideoPress in Jetpack
5
 */
6
class Jetpack_VideoPress {
7
	/** @var string */
8
	public $module = 'videopress';
9
10
	/** @var int */
11
	public $version = 5;
12
13
	/**
14
	 * Singleton
15
	 */
16
	public static function init() {
17
		static $instance = false;
18
19
		if ( ! $instance ) {
20
			$instance = new Jetpack_VideoPress();
21
		}
22
23
		return $instance;
24
	}
25
26
	/**
27
	 * Jetpack_VideoPress constructor.
28
	 *
29
	 * Sets up the initializer and makes sure that videopress activates and deactivates properly.
30
	 */
31
	private function __construct() {
32
		// $this->version = time(); // <s>ghost</s> cache busters!
33
		add_action( 'init', array( $this, 'on_init' ) );
34
		add_action( 'jetpack_deactivate_module_videopress', array( $this, 'jetpack_module_deactivated' ) );
35
	}
36
37
	/**
38
	 * Fires on init
39
	 */
40
	public function on_init() {
41
		add_action( 'wp_enqueue_media', array( $this, 'enqueue_admin_scripts' ) );
42
		add_filter( 'plupload_default_settings', array( $this, 'videopress_pluploder_config' ) );
43
		add_filter( 'wp_get_attachment_url', array( $this, 'update_attachment_url_for_videopress' ), 10, 2 );
44
45
		if ( Jetpack_Plan::supports( 'videopress' ) ) {
46
			add_filter( 'upload_mimes', array( $this, 'add_video_upload_mimes' ), 999 );
47
		}
48
49
		add_action( 'admin_print_footer_scripts', array( $this, 'print_in_footer_open_media_add_new' ) );
50
		add_action( 'admin_head', array( $this, 'enqueue_admin_styles' ) );
51
52
		add_filter( 'wp_mime_type_icon', array( $this, 'wp_mime_type_icon' ), 10, 3 );
53
54
		add_filter( 'wp_video_extensions', array( $this, 'add_videopress_extenstion' ) );
55
56
		VideoPress_Scheduler::init();
57
		VideoPress_XMLRPC::init();
58
	}
59
60
	/**
61
	 * Runs when the VideoPress module is deactivated.
62
	 */
63
	public function jetpack_module_deactivated() {
64
		VideoPress_Options::delete_options();
65
	}
66
67
	/**
68
	 * A can of coke
69
	 *
70
	 * Similar to current_user_can, but internal to VideoPress. Returns
71
	 * true if the given VideoPress capability is allowed by the given user.
72
	 */
73
	public function can( $cap, $user_id = false ) {
74
		if ( ! $user_id ) {
75
			$user_id = get_current_user_id();
76
		}
77
78
		// Connection owners are allowed to do all the things.
79
		if ( $this->is_connection_owner( $user_id ) ) {
80
			return true;
81
		}
82
83
		// Additional and internal caps checks
84
		if ( ! user_can( $user_id, 'upload_files' ) ) {
85
			return false;
86
		}
87
88
		if ( 'edit_videos' == $cap && ! user_can( $user_id, 'edit_others_posts' ) ) {
89
			return false;
90
		}
91
92
		if ( 'delete_videos' == $cap && ! user_can( $user_id, 'delete_others_posts' ) ) {
93
			return false;
94
		}
95
96
		return true;
97
	}
98
99
	/**
100
	 * Returns true if the provided user is the Jetpack connection owner.
101
	 */
102
	public function is_connection_owner( $user_id = false ) {
103
		if ( ! $user_id ) {
104
			$user_id = get_current_user_id();
105
		}
106
107
		$user_token = Jetpack_Data::get_access_token( JETPACK_MASTER_USER );
0 ignored issues
show
Deprecated Code introduced by
The method Jetpack_Data::get_access_token() has been deprecated with message: 7.5 Use Connection_Manager instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
108
109
		return $user_token && is_object( $user_token ) && isset( $user_token->external_user_id ) && $user_id === $user_token->external_user_id;
110
	}
111
112
	/**
113
	 * Register and enqueue VideoPress admin styles.
114
	 */
115
	public function enqueue_admin_styles() {
116
		wp_register_style( 'videopress-admin', plugins_url( 'videopress-admin.css', __FILE__ ), array(), $this->version );
117
		wp_enqueue_style( 'videopress-admin' );
118
	}
119
120
	/**
121
	 * Register VideoPress admin scripts.
122
	 */
123
	public function enqueue_admin_scripts() {
124
		if ( did_action( 'videopress_enqueue_admin_scripts' ) ) {
125
			return;
126
		}
127
128
		if ( $this->should_override_media_uploader() ) {
129
			wp_enqueue_script(
130
				'videopress-plupload',
131
				Jetpack::get_file_url_for_environment(
132
					'_inc/build/videopress/js/videopress-plupload.min.js',
133
					'modules/videopress/js/videopress-plupload.js'
134
				),
135
				array(
136
					'jquery',
137
					'wp-plupload',
138
				),
139
				$this->version
140
			);
141
142
			wp_enqueue_script(
143
				'videopress-uploader',
144
				Jetpack::get_file_url_for_environment(
145
					'_inc/build/videopress/js/videopress-uploader.min.js',
146
					'modules/videopress/js/videopress-uploader.js'
147
				),
148
				array(
149
					'videopress-plupload',
150
				),
151
				$this->version
152
			);
153
154
			wp_enqueue_script(
155
				'media-video-widget-extensions',
156
				Jetpack::get_file_url_for_environment(
157
					'_inc/build/videopress/js/media-video-widget-extensions.min.js',
158
					'modules/videopress/js/media-video-widget-extensions.js'
159
				),
160
				array(),
161
				$this->version,
162
				true
163
			);
164
		}
165
166
		/**
167
		 * Fires after VideoPress scripts are enqueued in the dashboard.
168
		 *
169
		 * @since 2.5.0
170
		 */
171
		do_action( 'videopress_enqueue_admin_scripts' );
172
	}
173
174
	/**
175
	 * An override for the attachment url, which returns back the WPCOM VideoPress processed url.
176
	 *
177
	 * This is an action proxy to the videopress_get_attachment_url() utility function.
178
	 *
179
	 * @param string $url
180
	 * @param int    $post_id
181
	 *
182
	 * @return string
183
	 */
184
	public function update_attachment_url_for_videopress( $url, $post_id ) {
185
		if ( $videopress_url = videopress_get_attachment_url( $post_id ) ) {
186
			return $videopress_url;
187
		}
188
189
		return $url;
190
	}
191
192
	/**
193
	 * Modify the default plupload config to turn on videopress specific filters.
194
	 */
195
	public function videopress_pluploder_config( $config ) {
196
197
		if ( ! isset( $config['filters']['max_file_size'] ) ) {
198
			$config['filters']['max_file_size'] = wp_max_upload_size() . 'b';
199
		}
200
201
		$config['filters']['videopress_check_uploads'] = $config['filters']['max_file_size'];
202
203
		// We're doing our own check in the videopress_check_uploads filter.
204
		unset( $config['filters']['max_file_size'] );
205
206
		return $config;
207
	}
208
209
210
	/**
211
	 * Helper function to determine if the media uploader should be overridden.
212
	 *
213
	 * The rules are simple, only try to load the script when on the edit post or new post pages.
214
	 *
215
	 * @return bool
216
	 */
217
	protected function should_override_media_uploader() {
218
		global $pagenow;
219
220
		// Only load in the admin
221
		if ( ! is_admin() ) {
222
			return false;
223
		}
224
225
		$acceptable_pages = array(
226
			'post-new.php',
227
			'post.php',
228
			'upload.php',
229
			'customize.php',
230
		);
231
232
		// Only load on the post, new post, or upload pages.
233
		if ( ! in_array( $pagenow, $acceptable_pages ) ) {
234
			return false;
235
		}
236
237
		$options = VideoPress_Options::get_options();
238
239
		return $options['shadow_blog_id'] > 0;
240
	}
241
242
	/**
243
	 * A work-around / hack to make it possible to go to the media library with the add new box open.
244
	 *
245
	 * @return bool
246
	 */
247
	public function print_in_footer_open_media_add_new() {
248
		global $pagenow;
249
250
		// Only load in the admin
251
		if ( ! is_admin() ) {
252
			return false;
253
		}
254
255
		if ( $pagenow !== 'upload.php' ) {
256
			return false;
257
		}
258
259
		if ( ! isset( $_GET['action'] ) || $_GET['action'] !== 'add-new' ) {
260
			return false;
261
		}
262
263
		?>
264
			<script type="text/javascript">
265
				( function( $ ) {
266
					window.setTimeout( function() {
267
						$('#wp-media-grid .page-title-action').click();
268
					}, 500 );
269
270
				}( jQuery ) );
271
			</script>
272
		<?php
273
	}
274
275
	/**
276
	 * Makes sure that all video mimes are added in, as multi site installs can remove them.
277
	 *
278
	 * @param array $existing_mimes
279
	 * @return array
280
	 */
281
	public function add_video_upload_mimes( $existing_mimes = array() ) {
282
		$mime_types  = wp_get_mime_types();
283
		$video_types = array_filter( $mime_types, array( $this, 'filter_video_mimes' ) );
284
285
		foreach ( $video_types as $key => $value ) {
286
			$existing_mimes[ $key ] = $value;
287
		}
288
289
		// Make sure that videopress mimes are considered videos.
290
		$existing_mimes['videopress'] = 'video/videopress';
291
292
		return $existing_mimes;
293
	}
294
295
	/**
296
	 * Filter designed to get rid of non video mime types.
297
	 *
298
	 * @param string $value
299
	 * @return int
300
	 */
301
	public function filter_video_mimes( $value ) {
302
		return preg_match( '@^video/@', $value );
303
	}
304
305
	/**
306
	 * @param string $icon
307
	 * @param string $mime
308
	 * @param int    $post_id
309
	 *
310
	 * @return string
311
	 */
312
	public function wp_mime_type_icon( $icon, $mime, $post_id ) {
313
314
		if ( $mime !== 'video/videopress' ) {
315
			return $icon;
316
		}
317
318
		$status = get_post_meta( $post_id, 'videopress_status', true );
319
320
		if ( $status === 'complete' ) {
321
			return $icon;
322
		}
323
324
		return 'https://wordpress.com/wp-content/mu-plugins/videopress/images/media-video-processing-icon.png';
325
	}
326
327
	/**
328
	 * @param array $extensions
329
	 *
330
	 * @return array
331
	 */
332
	public function add_videopress_extenstion( $extensions ) {
333
		$extensions[] = 'videopress';
334
335
		return $extensions;
336
	}
337
}
338
339
// Initialize the module.
340
Jetpack_VideoPress::init();
341