Completed
Push — update/g-translate-wording ( 6a68cc...2381fe )
by
unknown
47:05 queued 38:01
created

Jetpack_VideoPress::videopress_shortcode_options()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A Jetpack_VideoPress::enqueue_admin_styles() 0 4 1
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
		VideoPress_Scheduler::init();
56
		VideoPress_XMLRPC::init();
57
	}
58
59
	/**
60
	 * Runs when the VideoPress module is deactivated.
61
	 */
62
	public function jetpack_module_deactivated() {
63
		VideoPress_Options::delete_options();
64
	}
65
66
	/**
67
	 * A can of coke
68
	 *
69
	 * Similar to current_user_can, but internal to VideoPress. Returns
70
	 * true if the given VideoPress capability is allowed by the given user.
71
	 */
72
	public function can( $cap, $user_id = false ) {
73
		if ( ! $user_id ) {
74
			$user_id = get_current_user_id();
75
		}
76
77
		// Connection owners are allowed to do all the things.
78
		if ( $this->is_connection_owner( $user_id ) ) {
79
			return true;
80
		}
81
82
		// Additional and internal caps checks
83
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 );
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
			// We're going to replace the standard wp-plupload with our own ... messy, I know, but as of now the
130
			// hooks in it are not good enough for us to be able to override / add in just the code we need.
131
			// P.S. Please don't take this as an example of good behavior, this is a temporary fix until I
132
			// can get a more permanent action / filter system added into the core wp-plupload.js to make this
133
			// type of override unnecessary.
134
			wp_dequeue_script( 'wp-plupload' );
135
136
			wp_enqueue_script(
137
				'videopress-plupload',
138
				plugins_url( 'js/videopress-plupload.js', __FILE__ ),
139
				array(
140
					'jquery'
141
				),
142
				$this->version
143
			);
144
145
			wp_enqueue_script(
146
				'videopress-uploader',
147
				plugins_url( 'js/videopress-uploader.js', __FILE__ ),
148
				array(
149
					'videopress-plupload'
150
				),
151
				$this->version
152
			);
153
		}
154
155
		/**
156
		 * Fires after VideoPress scripts are enqueued in the dashboard.
157
		 *
158
		 * @since 2.5.0
159
		 */
160
		do_action( 'videopress_enqueue_admin_scripts' );
161
	}
162
163
	/**
164
	 * An override for the attachment url, which returns back the WPCOM videopress original url,
165
	 * if it is set to the the objects metadata. this allows us to show the original uploaded
166
	 * file on the WPCOM architecture, instead of the locally uplodaded file,
167
	 * which doeasn't exist.
168
	 *
169
	 * 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...
170
	 *
171
	 * @param string $url
172
	 * @param int $post_id
173
	 *
174
	 * @return mixed
175
	 */
176
	public function update_attachment_url_for_videopress( $url, $post_id ) {
177
178
		if ( get_post_mime_type( $post_id ) === 'video/videopress' ) {
179
			$meta = wp_get_attachment_metadata( $post_id );
180
181
			if ( isset( $meta['original']['url'] ) ) {
182
				$url = $meta['original']['url'];
183
			}
184
		}
185
186
		return $url;
187
	}
188
189
	/**
190
	 * Modify the default plupload config to turn on videopress specific filters.
191
	 */
192
	public function videopress_pluploder_config( $config ) {
193
194
		if ( ! isset( $config['filters']['max_file_size'] ) ) {
195
			$config['filters']['max_file_size'] = wp_max_upload_size() . 'b';
196
		}
197
198
		$config['filters']['videopress_check_uploads'] = $config['filters']['max_file_size'];
199
200
		// We're doing our own check in the videopress_check_uploads filter.
201
		unset( $config['filters']['max_file_size'] );
202
203
		return $config;
204
	}
205
206
207
	/**
208
	 * Helper function to determine if the media uploader should be overridden.
209
	 *
210
	 * The rules are simple, only try to load the script when on the edit post or new post pages.
211
	 *
212
	 * @return bool
213
	 */
214
	protected function should_override_media_uploader() {
215
		global $pagenow;
216
217
		// Only load in the admin
218
		if ( ! is_admin() ) {
219
			return false;
220
		}
221
222
		$acceptable_pages = array(
223
			'post-new.php',
224
			'post.php',
225
			'upload.php',
226
			'customize.php',
227
		);
228
229
		// Only load on the post, new post, or upload pages.
230
		if ( !in_array( $pagenow, $acceptable_pages ) ) {
231
			return false;
232
		}
233
234
		$options = VideoPress_Options::get_options();
235
236
		return $options['shadow_blog_id'] > 0;
237
	}
238
239
	/**
240
	 * A work-around / hack to make it possible to go to the media library with the add new box open.
241
	 *
242
	 * @return bool
243
	 */
244
	public function print_in_footer_open_media_add_new() {
245
		global $pagenow;
246
247
		// Only load in the admin
248
		if ( ! is_admin() ) {
249
			return false;
250
		}
251
252
		if ( $pagenow !== 'upload.php' ) {
253
			return false;
254
		}
255
256
		if ( ! isset ( $_GET['action'] ) || $_GET['action'] !== 'add-new' ) {
257
			return false;
258
		}
259
260
		?>
261
			<script type="text/javascript">
262
				( function( $ ) {
263
					window.setTimeout( function() {
264
						$('#wp-media-grid .page-title-action').click();
265
					}, 500 );
266
267
				}( jQuery ) );
268
			</script>
269
		<?php
270
	}
271
272
	/**
273
	 * Changes the add new menu location, so that VideoPress will be enabled
274
	 * when a user clicks that button.
275
	 */
276
	public function change_add_new_menu_location() {
277
		$page = remove_submenu_page( 'upload.php', 'media-new.php' );
278
		add_submenu_page( 'upload.php', $page[0], $page[0], 'upload_files', 'upload.php?action=add-new');
279
	}
280
281
	/**
282
	 * Makes sure that all video mimes are added in, as multi site installs can remove them.
283
	 *
284
	 * @param array $existing_mimes
285
	 * @return array
286
	 */
287
	public function add_video_upload_mimes( $existing_mimes = array() ) {
288
		$mime_types = wp_get_mime_types();
289
		$video_types = array_filter( $mime_types, array( $this, 'filter_video_mimes' ) );
290
291
		foreach ( $video_types as $key => $value ) {
292
			$existing_mimes[ $key ] = $value;
293
		}
294
295
		return $existing_mimes;
296
	}
297
298
	/**
299
	 * Filter designed to get rid of non video mime types.
300
	 *
301
	 * @param string $value
302
	 * @return int
303
	 */
304
	public function filter_video_mimes( $value ) {
305
		return preg_match( '@^video/@', $value );
306
  }
307
308
	/**
309
	 * @param string $icon
310
	 * @param string $mime
311
	 * @param int $post_id
312
	 *
313
	 * @return string
314
	 */
315
	public function wp_mime_type_icon( $icon, $mime, $post_id ) {
316
317
		if ( $mime !== 'video/videopress' ) {
318
			return $icon;
319
		}
320
321
		$status = get_post_meta( $post_id, 'videopress_status', true );
322
323
		if ( $status === 'complete' ) {
324
			return $icon;
325
		}
326
327
		return get_site_url() . '/wp-content/plugins/jetpack/images/media-video-processing-icon.png';
328
	}
329
}
330
331
// Initialize the module.
332
Jetpack_VideoPress::init();
333