Completed
Push — branch-4.5-built ( c976d8...6f3e53 )
by
unknown
311:51 queued 304:04
created

Jetpack_VideoPress::add_media_new_notice()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 0
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * VideoPress in Jetpack
5
 *
6
 */
7
class Jetpack_VideoPress {
8
	/** @var string */
9
	public $module = 'videopress';
10
11
	/** @var int */
12
	public $version = 5;
13
14
	/**
15
	 * Singleton
16
	 */
17
	public static function init() {
18
		static $instance = false;
19
20
		if ( ! $instance ) {
21
			$instance = new Jetpack_VideoPress;
22
		}
23
24
		return $instance;
25
	}
26
27
	/**
28
	 * Jetpack_VideoPress constructor.
29
	 *
30
	 * Sets up the initializer and makes sure that videopress activates and deactivates properly.
31
	 */
32
	private function __construct() {
33
		//$this->version = time(); // <s>ghost</s> cache busters!
34
		add_action( 'init', array( $this, 'on_init' ) );
35
		add_action( 'jetpack_deactivate_module_videopress', array( $this, 'jetpack_module_deactivated' ) );
36
	}
37
38
	/**
39
	 * Fires on init
40
	 */
41
	public function on_init() {
42
		add_action( 'wp_enqueue_media', array( $this, 'enqueue_admin_scripts' ) );
43
		add_filter( 'plupload_default_settings', array( $this, 'videopress_pluploder_config' ) );
44
		add_filter( 'wp_get_attachment_url', array( $this, 'update_attachment_url_for_videopress' ), 10, 2 );
45
46
		if ( Jetpack::active_plan_supports( 'videopress' ) ) {
47
			add_filter( 'upload_mimes', array( $this, 'add_video_upload_mimes' ), 999 );
48
		}
49
50
		add_action( 'admin_print_footer_scripts', array( $this, 'print_in_footer_open_media_add_new' ) );
51
		add_action( 'admin_head', array( $this, 'enqueue_admin_styles' ) );
52
53
		add_filter( 'wp_mime_type_icon', array( $this, 'wp_mime_type_icon' ), 10, 3 );
54
55
		$this->add_media_new_notice();
56
57
		VideoPress_Scheduler::init();
58
		VideoPress_XMLRPC::init();
59
	}
60
61
	/**
62
	 * Runs when the VideoPress module is deactivated.
63
	 */
64
	public function jetpack_module_deactivated() {
65
		VideoPress_Options::delete_options();
66
	}
67
68
	/**
69
	 * A can of coke
70
	 *
71
	 * Similar to current_user_can, but internal to VideoPress. Returns
72
	 * true if the given VideoPress capability is allowed by the given user.
73
	 */
74
	public function can( $cap, $user_id = false ) {
75
		if ( ! $user_id ) {
76
			$user_id = get_current_user_id();
77
		}
78
79
		// Connection owners are allowed to do all the things.
80
		if ( $this->is_connection_owner( $user_id ) ) {
81
			return true;
82
		}
83
84
		// Additional and internal caps checks
85
86
		if ( ! user_can( $user_id, 'upload_files' ) ) {
87
			return false;
88
		}
89
90
		if ( 'edit_videos' == $cap && ! user_can( $user_id, 'edit_others_posts' ) ) {
91
			return false;
92
		}
93
94
		if ( 'delete_videos' == $cap && ! user_can( $user_id, 'delete_others_posts' ) ) {
95
			return false;
96
		}
97
98
		return true;
99
	}
100
101
	/**
102
	 * Returns true if the provided user is the Jetpack connection owner.
103
	 */
104
	public function is_connection_owner( $user_id = false ) {
105
		if ( ! $user_id ) {
106
			$user_id = get_current_user_id();
107
		}
108
109
		$user_token = Jetpack_Data::get_access_token( JETPACK_MASTER_USER );
110
111
		return $user_token && is_object( $user_token ) && isset( $user_token->external_user_id ) && $user_id === $user_token->external_user_id;
112
	}
113
114
	/**
115
	 * Add a notice to the top of the media-new.php to let the user know how to upload a video.
116
	 */
117
	public function add_media_new_notice() {
118
		global $pagenow;
119
120
		if ( $pagenow != 'media-new.php' ) {
121
			return;
122
		}
123
124
		$jitm = Jetpack_JITM::init();
125
126
		add_action( 'admin_enqueue_scripts', array( $jitm, 'jitm_enqueue_files' ) );
127
		add_action( 'admin_notices', array( $jitm, 'videopress_media_upload_warning_msg' ) );
128
	}
129
130
	/**
131
	 * Register and enqueue VideoPress admin styles.
132
	 */
133
	public function enqueue_admin_styles() {
134
		wp_register_style( 'videopress-admin', plugins_url( 'videopress-admin.css', __FILE__ ), array(), $this->version );
135
		wp_enqueue_style( 'videopress-admin' );
136
	}
137
138
	/**
139
	 * Register VideoPress admin scripts.
140
	 */
141
	public function enqueue_admin_scripts() {
142
		if ( did_action( 'videopress_enqueue_admin_scripts' ) ) {
143
			return;
144
		}
145
146
		if ( $this->should_override_media_uploader() ) {
147
			// We're going to replace the standard wp-plupload with our own ... messy, I know, but as of now the
148
			// hooks in it are not good enough for us to be able to override / add in just the code we need.
149
			// P.S. Please don't take this as an example of good behavior, this is a temporary fix until I
150
			// can get a more permanent action / filter system added into the core wp-plupload.js to make this
151
			// type of override unnecessary.
152
			wp_dequeue_script( 'wp-plupload' );
153
154
			wp_enqueue_script(
155
				'videopress-plupload',
156
				plugins_url( 'js/videopress-plupload.js', __FILE__ ),
157
				array(
158
					'jquery'
159
				),
160
				$this->version
161
			);
162
163
			wp_enqueue_script(
164
				'videopress-uploader',
165
				plugins_url( 'js/videopress-uploader.js', __FILE__ ),
166
				array(
167
					'videopress-plupload'
168
				),
169
				$this->version
170
			);
171
		}
172
173
		/**
174
		 * Fires after VideoPress scripts are enqueued in the dashboard.
175
		 *
176
		 * @since 2.5.0
177
		 */
178
		do_action( 'videopress_enqueue_admin_scripts' );
179
	}
180
181
	/**
182
	 * An override for the attachment url, which returns back the WPCOM videopress original url,
183
	 * if it is set to the the objects metadata. this allows us to show the original uploaded
184
	 * file on the WPCOM architecture, instead of the locally uplodaded file,
185
	 * which doeasn't exist.
186
	 *
187
	 * TODO: Fix this so that it will return a VideoPress process url, to ensure that it is in MP4 format.
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
188
	 *
189
	 * @param string $url
190
	 * @param int $post_id
191
	 *
192
	 * @return mixed
193
	 */
194
	public function update_attachment_url_for_videopress( $url, $post_id ) {
195
196
		if ( get_post_mime_type( $post_id ) === 'video/videopress' ) {
197
			$meta = wp_get_attachment_metadata( $post_id );
198
199
			if ( isset( $meta['original']['url'] ) ) {
200
				$url = $meta['original']['url'];
201
			}
202
		}
203
204
		return $url;
205
	}
206
207
	/**
208
	 * Modify the default plupload config to turn on videopress specific filters.
209
	 */
210
	public function videopress_pluploder_config( $config ) {
211
212
		if ( ! isset( $config['filters']['max_file_size'] ) ) {
213
			$config['filters']['max_file_size'] = wp_max_upload_size() . 'b';
214
		}
215
216
		$config['filters']['videopress_check_uploads'] = $config['filters']['max_file_size'];
217
218
		// We're doing our own check in the videopress_check_uploads filter.
219
		unset( $config['filters']['max_file_size'] );
220
221
		return $config;
222
	}
223
224
225
	/**
226
	 * Helper function to determine if the media uploader should be overridden.
227
	 *
228
	 * The rules are simple, only try to load the script when on the edit post or new post pages.
229
	 *
230
	 * @return bool
231
	 */
232
	protected function should_override_media_uploader() {
233
		global $pagenow;
234
235
		// Only load in the admin
236
		if ( ! is_admin() ) {
237
			return false;
238
		}
239
240
		$acceptable_pages = array(
241
			'post-new.php',
242
			'post.php',
243
			'upload.php',
244
			'customize.php',
245
		);
246
247
		// Only load on the post, new post, or upload pages.
248
		if ( !in_array( $pagenow, $acceptable_pages ) ) {
249
			return false;
250
		}
251
252
		$options = VideoPress_Options::get_options();
253
254
		return $options['shadow_blog_id'] > 0;
255
	}
256
257
	/**
258
	 * A work-around / hack to make it possible to go to the media library with the add new box open.
259
	 *
260
	 * @return bool
261
	 */
262
	public function print_in_footer_open_media_add_new() {
263
		global $pagenow;
264
265
		// Only load in the admin
266
		if ( ! is_admin() ) {
267
			return false;
268
		}
269
270
		if ( $pagenow !== 'upload.php' ) {
271
			return false;
272
		}
273
274
		if ( ! isset ( $_GET['action'] ) || $_GET['action'] !== 'add-new' ) {
275
			return false;
276
		}
277
278
		?>
279
			<script type="text/javascript">
280
				( function( $ ) {
281
					window.setTimeout( function() {
282
						$('#wp-media-grid .page-title-action').click();
283
					}, 500 );
284
285
				}( jQuery ) );
286
			</script>
287
		<?php
288
	}
289
290
	/**
291
	 * Changes the add new menu location, so that VideoPress will be enabled
292
	 * when a user clicks that button.
293
	 */
294
	public function change_add_new_menu_location() {
295
		$page = remove_submenu_page( 'upload.php', 'media-new.php' );
296
		add_submenu_page( 'upload.php', $page[0], $page[0], 'upload_files', 'upload.php?action=add-new');
297
	}
298
299
	/**
300
	 * Makes sure that all video mimes are added in, as multi site installs can remove them.
301
	 *
302
	 * @param array $existing_mimes
303
	 * @return array
304
	 */
305
	public function add_video_upload_mimes( $existing_mimes = array() ) {
306
		$mime_types = wp_get_mime_types();
307
		$video_types = array_filter( $mime_types, array( $this, 'filter_video_mimes' ) );
308
309
		foreach ( $video_types as $key => $value ) {
310
			$existing_mimes[ $key ] = $value;
311
		}
312
313
		return $existing_mimes;
314
	}
315
316
	/**
317
	 * Filter designed to get rid of non video mime types.
318
	 *
319
	 * @param string $value
320
	 * @return int
321
	 */
322
	public function filter_video_mimes( $value ) {
323
		return preg_match( '@^video/@', $value );
324
  }
325
326
	/**
327
	 * @param string $icon
328
	 * @param string $mime
329
	 * @param int $post_id
330
	 *
331
	 * @return string
332
	 */
333
	public function wp_mime_type_icon( $icon, $mime, $post_id ) {
334
335
		if ( $mime !== 'video/videopress' ) {
336
			return $icon;
337
		}
338
339
		$status = get_post_meta( $post_id, 'videopress_status', true );
340
341
		if ( $status === 'complete' ) {
342
			return $icon;
343
		}
344
345
		return plugins_url( 'images/media-video-processing-icon.png', JETPACK__PLUGIN_FILE );
346
	}
347
}
348
349
// Initialize the module.
350
Jetpack_VideoPress::init();
351