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