Completed
Push — add/carousel-lightbox-single-i... ( 204ac6...43c884 )
by
unknown
09:26
created

Jetpack_VideoPress::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
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_menu', array( $this,'change_add_new_menu_location' ), 999 );
52
		add_action( 'admin_head', array( $this, 'enqueue_admin_styles' ) );
53
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
	 * @param string $url
170
	 * @param int $post_id
171
	 *
172
	 * @return mixed
173
	 */
174
	public function update_attachment_url_for_videopress( $url, $post_id ) {
175
176
		if ( get_post_mime_type( $post_id ) === 'video/videopress' ) {
177
			$meta = wp_get_attachment_metadata( $post_id );
178
179
			if ( isset( $meta['original']['url'] ) ) {
180
				$url = $meta['original']['url'];
181
			}
182
		}
183
184
		return $url;
185
	}
186
187
	/**
188
	 * Modify the default plupload config to turn on videopress specific filters.
189
	 */
190
	public function videopress_pluploder_config( $config ) {
191
192
		if ( ! isset( $config['filters']['max_file_size'] ) ) {
193
			$config['filters']['max_file_size'] = wp_max_upload_size() . 'b';
194
		}
195
196
		$config['filters']['videopress_check_uploads'] = $config['filters']['max_file_size'];
197
198
		// We're doing our own check in the videopress_check_uploads filter.
199
		unset( $config['filters']['max_file_size'] );
200
201
		return $config;
202
	}
203
204
205
	/**
206
	 * Helper function to determine if the media uploader should be overridden.
207
	 *
208
	 * The rules are simple, only try to load the script when on the edit post or new post pages.
209
	 *
210
	 * @return bool
211
	 */
212
	protected function should_override_media_uploader() {
213
		global $pagenow;
214
215
		// Only load in the admin
216
		if ( ! is_admin() ) {
217
			return false;
218
		}
219
220
		// Only load on the post, new post, or upload pages.
221
		if ( $pagenow !== 'post-new.php' && $pagenow !== 'post.php' && $pagenow !== 'upload.php' ) {
222
			return false;
223
		}
224
225
		$options = VideoPress_Options::get_options();
226
227
		return $options['shadow_blog_id'] > 0;
228
	}
229
230
	/**
231
	 * A work-around / hack to make it possible to go to the media library with the add new box open.
232
	 *
233
	 * @return bool
234
	 */
235
	public function print_in_footer_open_media_add_new() {
236
		global $pagenow;
237
238
		// Only load in the admin
239
		if ( ! is_admin() ) {
240
			return false;
241
		}
242
243
		if ( $pagenow !== 'upload.php' ) {
244
			return false;
245
		}
246
247
		if ( ! isset ( $_GET['action'] ) || $_GET['action'] !== 'add-new' ) {
248
			return false;
249
		}
250
251
		?>
252
			<script type="text/javascript">
253
				( function( $ ) {
254
					window.setTimeout( function() {
255
						$('#wp-media-grid .page-title-action').click();
256
					}, 500 );
257
258
				}( jQuery ) );
259
			</script>
260
		<?php
261
	}
262
263
	/**
264
	 * Changes the add new menu location, so that VideoPress will be enabled
265
	 * when a user clicks that button.
266
	 */
267
	public function change_add_new_menu_location() {
268
		$page = remove_submenu_page( 'upload.php', 'media-new.php' );
269
270
		add_submenu_page( 'upload.php', $page[0], $page[0], 'upload_files', 'upload.php?action=add-new');
271
	}
272
273
	/**
274
	 * Makes sure that all video mimes are added in, as multi site installs can remove them.
275
	 *
276
	 * @param array $existing_mimes
277
	 * @return array
278
	 */
279
	public function add_video_upload_mimes( $existing_mimes = array() ) {
280
		$mime_types = wp_get_mime_types();
281
		$video_types = array_filter( $mime_types, array( $this, 'filter_video_mimes' ) );
282
283
		foreach ( $video_types as $key => $value ) {
284
			$existing_mimes[ $key ] = $value;
285
		}
286
287
		return $existing_mimes;
288
	}
289
290
	/**
291
	 * Filter designed to get rid of non video mime types.
292
	 *
293
	 * @param string $value
294
	 * @return int
295
	 */
296
	public function filter_video_mimes( $value ) {
297
		return preg_match( '@^video/@', $value );
298
	}
299
}
300
301
// Initialize the module.
302
Jetpack_VideoPress::init();
303