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
|
|
|
add_action( 'admin_print_footer_scripts', array( $this, 'print_in_footer_open_media_add_new' ) ); |
47
|
|
|
add_action( 'admin_menu', array( $this,'change_add_new_menu_location' ), 999 ); |
48
|
|
|
add_action( 'admin_head', array( $this, 'enqueue_admin_styles' ) ); |
49
|
|
|
|
50
|
|
|
VideoPress_Scheduler::init(); |
51
|
|
|
VideoPress_XMLRPC::init(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Runs when the VideoPress module is deactivated. |
56
|
|
|
*/ |
57
|
|
|
public function jetpack_module_deactivated() { |
58
|
|
|
VideoPress_Options::delete_options(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* A can of coke |
63
|
|
|
* |
64
|
|
|
* Similar to current_user_can, but internal to VideoPress. Returns |
65
|
|
|
* true if the given VideoPress capability is allowed by the given user. |
66
|
|
|
*/ |
67
|
|
|
public function can( $cap, $user_id = false ) { |
68
|
|
|
if ( ! $user_id ) { |
69
|
|
|
$user_id = get_current_user_id(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
// Connection owners are allowed to do all the things. |
73
|
|
|
if ( $this->is_connection_owner( $user_id ) ) { |
74
|
|
|
return true; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
// Additional and internal caps checks |
78
|
|
|
|
79
|
|
|
if ( ! user_can( $user_id, 'upload_files' ) ) { |
80
|
|
|
return false; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if ( 'edit_videos' == $cap && ! user_can( $user_id, 'edit_others_posts' ) ) { |
84
|
|
|
return false; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
if ( 'delete_videos' == $cap && ! user_can( $user_id, 'delete_others_posts' ) ) { |
88
|
|
|
return false; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return true; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Returns true if the provided user is the Jetpack connection owner. |
96
|
|
|
*/ |
97
|
|
|
public function is_connection_owner( $user_id = false ) { |
98
|
|
|
if ( ! $user_id ) { |
99
|
|
|
$user_id = get_current_user_id(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$user_token = Jetpack_Data::get_access_token( JETPACK_MASTER_USER ); |
103
|
|
|
|
104
|
|
|
return $user_token && is_object( $user_token ) && isset( $user_token->external_user_id ) && $user_id === $user_token->external_user_id; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Register and enqueue VideoPress admin styles. |
109
|
|
|
*/ |
110
|
|
|
public function enqueue_admin_styles() { |
111
|
|
|
wp_register_style( 'videopress-admin', plugins_url( 'videopress-admin.css', __FILE__ ), array(), $this->version ); |
112
|
|
|
wp_enqueue_style( 'videopress-admin' ); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Register VideoPress admin scripts. |
117
|
|
|
*/ |
118
|
|
|
public function enqueue_admin_scripts() { |
119
|
|
|
if ( did_action( 'videopress_enqueue_admin_scripts' ) ) { |
120
|
|
|
return; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
if ( $this->should_override_media_uploader() ) { |
124
|
|
|
// We're going to replace the standard wp-plupload with our own ... messy, I know, but as of now the |
125
|
|
|
// hooks in it are not good enough for us to be able to override / add in just the code we need. |
126
|
|
|
// P.S. Please don't take this as an example of good behavior, this is a temporary fix until I |
127
|
|
|
// can get a more permanent action / filter system added into the core wp-plupload.js to make this |
128
|
|
|
// type of override unnecessary. |
129
|
|
|
wp_dequeue_script( 'wp-plupload' ); |
130
|
|
|
|
131
|
|
|
wp_enqueue_script( |
132
|
|
|
'videopress-plupload', |
133
|
|
|
plugins_url( 'js/videopress-plupload.js', __FILE__ ), |
134
|
|
|
array( |
135
|
|
|
'jquery' |
136
|
|
|
), |
137
|
|
|
$this->version |
138
|
|
|
); |
139
|
|
|
|
140
|
|
|
wp_enqueue_script( |
141
|
|
|
'videopress-uploader', |
142
|
|
|
plugins_url( 'js/videopress-uploader.js', __FILE__ ), |
143
|
|
|
array( |
144
|
|
|
'videopress-plupload' |
145
|
|
|
), |
146
|
|
|
$this->version |
147
|
|
|
); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Fires after VideoPress scripts are enqueued in the dashboard. |
152
|
|
|
* |
153
|
|
|
* @since 2.5.0 |
154
|
|
|
*/ |
155
|
|
|
do_action( 'videopress_enqueue_admin_scripts' ); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* An override for the attachment url, which returns back the WPCOM videopress original url, |
160
|
|
|
* if it is set to the the objects metadata. this allows us to show the original uploaded |
161
|
|
|
* file on the WPCOM architecture, instead of the locally uplodaded file, |
162
|
|
|
* which doeasn't exist. |
163
|
|
|
* |
164
|
|
|
* @param string $url |
165
|
|
|
* @param int $post_id |
166
|
|
|
* |
167
|
|
|
* @return mixed |
168
|
|
|
*/ |
169
|
|
|
public function update_attachment_url_for_videopress( $url, $post_id ) { |
170
|
|
|
|
171
|
|
|
if ( get_post_mime_type( $post_id ) === 'video/videopress' ) { |
172
|
|
|
$meta = wp_get_attachment_metadata( $post_id ); |
173
|
|
|
|
174
|
|
|
if ( isset( $meta['original']['url'] ) ) { |
175
|
|
|
$url = $meta['original']['url']; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
return $url; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Modify the default plupload config to turn on videopress specific filters. |
184
|
|
|
*/ |
185
|
|
|
public function videopress_pluploder_config( $config ) { |
186
|
|
|
|
187
|
|
|
if ( ! isset( $config['filters']['max_file_size'] ) ) { |
188
|
|
|
$config['filters']['max_file_size'] = wp_max_upload_size() . 'b'; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
$config['filters']['videopress_check_uploads'] = $config['filters']['max_file_size']; |
192
|
|
|
|
193
|
|
|
// We're doing our own check in the videopress_check_uploads filter. |
194
|
|
|
unset( $config['filters']['max_file_size'] ); |
195
|
|
|
|
196
|
|
|
return $config; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Helper function to determine if the media uploader should be overridden. |
202
|
|
|
* |
203
|
|
|
* The rules are simple, only try to load the script when on the edit post or new post pages. |
204
|
|
|
* |
205
|
|
|
* @return bool |
206
|
|
|
*/ |
207
|
|
|
protected function should_override_media_uploader() { |
208
|
|
|
global $pagenow; |
209
|
|
|
|
210
|
|
|
// Only load in the admin |
211
|
|
|
if ( ! is_admin() ) { |
212
|
|
|
return false; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
// Only load on the post, new post, or upload pages. |
216
|
|
|
if ( $pagenow !== 'post-new.php' && $pagenow !== 'post.php' && $pagenow !== 'upload.php' ) { |
217
|
|
|
return false; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
$options = VideoPress_Options::get_options(); |
221
|
|
|
|
222
|
|
|
return $options['shadow_blog_id'] > 0; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* A work-around / hack to make it possible to go to the media library with the add new box open. |
227
|
|
|
* |
228
|
|
|
* @return bool |
229
|
|
|
*/ |
230
|
|
|
public function print_in_footer_open_media_add_new() { |
231
|
|
|
global $pagenow; |
232
|
|
|
|
233
|
|
|
// Only load in the admin |
234
|
|
|
if ( ! is_admin() ) { |
235
|
|
|
return false; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
if ( $pagenow !== 'upload.php' ) { |
239
|
|
|
return false; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
if ( ! isset ( $_GET['action'] ) || $_GET['action'] !== 'add-new' ) { |
243
|
|
|
return false; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
?> |
247
|
|
|
<script type="text/javascript"> |
248
|
|
|
( function( $ ) { |
249
|
|
|
window.setTimeout( function() { |
250
|
|
|
$('#wp-media-grid .page-title-action').click(); |
251
|
|
|
}, 500 ); |
252
|
|
|
|
253
|
|
|
}( jQuery ) ); |
254
|
|
|
</script> |
255
|
|
|
<?php |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Changes the add new menu location, so that VideoPress will be enabled |
260
|
|
|
* when a user clicks that button. |
261
|
|
|
*/ |
262
|
|
|
public function change_add_new_menu_location() { |
263
|
|
|
$page = remove_submenu_page( 'upload.php', 'media-new.php' ); |
264
|
|
|
|
265
|
|
|
add_submenu_page( 'upload.php', $page[0], $page[0], 'upload_files', 'upload.php?action=add-new'); |
266
|
|
|
} |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
// Initialize the module. |
270
|
|
|
Jetpack_VideoPress::init(); |
271
|
|
|
|