Completed
Push — enhance/add-video-mimetypes ( 6a02c0 )
by
unknown
08:57
created

Jetpack_VideoPress::jetpack_module_deactivated()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
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
52
		add_action( 'admin_menu', array( $this,'change_add_new_menu_location' ), 999 );
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 VideoPress admin scripts.
114
	 */
115
	public function enqueue_admin_scripts() {
116
		if ( did_action( 'videopress_enqueue_admin_scripts' ) ) {
117
			return;
118
		}
119
120
		if ( $this->should_override_media_uploader() ) {
121
			// We're going to replace the standard wp-plupload with our own ... messy, I know, but as of now the
122
			// hooks in it are not good enough for us to be able to override / add in just the code we need.
123
			// P.S. Please don't take this as an example of good behavior, this is a temporary fix until I
124
			// can get a more permanent action / filter system added into the core wp-plupload.js to make this
125
			// type of override unnecessary.
126
			wp_dequeue_script( 'wp-plupload' );
127
128
			wp_enqueue_script(
129
				'videopress-plupload',
130
				plugins_url( 'js/videopress-plupload.js', __FILE__ ),
131
				array(
132
					'jquery'
133
				),
134
				$this->version
135
			);
136
137
			wp_enqueue_script(
138
				'videopress-uploader',
139
				plugins_url( 'js/videopress-uploader.js', __FILE__ ),
140
				array(
141
					'videopress-plupload'
142
				),
143
				$this->version
144
			);
145
		}
146
147
		wp_enqueue_style( 'videopress-admin', plugins_url( 'videopress-admin.css', __FILE__ ), array(), $this->version );
148
149
		/**
150
		 * Fires after VideoPress scripts are enqueued in the dashboard.
151
		 *
152
		 * @since 2.5.0
153
		 */
154
		do_action( 'videopress_enqueue_admin_scripts' );
155
	}
156
157
	/**
158
	 * An override for the attachment url, which returns back the WPCOM videopress original url,
159
	 * if it is set to the the objects metadata. this allows us to show the original uploaded
160
	 * file on the WPCOM architecture, instead of the locally uplodaded file,
161
	 * which doeasn't exist.
162
	 *
163
	 * @param string $url
164
	 * @param int $post_id
165
	 *
166
	 * @return mixed
167
	 */
168
	public function update_attachment_url_for_videopress( $url, $post_id ) {
169
170
		if ( get_post_mime_type( $post_id ) === 'video/videopress' ) {
171
			$meta = wp_get_attachment_metadata( $post_id );
172
173
			if ( isset( $meta['original']['url'] ) ) {
174
				$url = $meta['original']['url'];
175
			}
176
		}
177
178
		return $url;
179
	}
180
181
	/**
182
	 * Modify the default plupload config to turn on videopress specific filters.
183
	 */
184
	public function videopress_pluploder_config( $config ) {
185
186
		if ( ! isset( $config['filters']['max_file_size'] ) ) {
187
			$config['filters']['max_file_size'] = wp_max_upload_size() . 'b';
188
		}
189
190
		$config['filters']['videopress_check_uploads'] = $config['filters']['max_file_size'];
191
192
		// We're doing our own check in the videopress_check_uploads filter.
193
		unset( $config['filters']['max_file_size'] );
194
195
		return $config;
196
	}
197
198
199
	/**
200
	 * Helper function to determine if the media uploader should be overridden.
201
	 *
202
	 * The rules are simple, only try to load the script when on the edit post or new post pages.
203
	 *
204
	 * @return bool
205
	 */
206
	protected function should_override_media_uploader() {
207
		global $pagenow;
208
209
		// Only load in the admin
210
		if ( ! is_admin() ) {
211
			return false;
212
		}
213
214
		// Only load on the post, new post, or upload pages.
215
		if ( $pagenow !== 'post-new.php' && $pagenow !== 'post.php' && $pagenow !== 'upload.php' ) {
216
			return false;
217
		}
218
219
		$options = VideoPress_Options::get_options();
220
221
		return $options['shadow_blog_id'] > 0;
222
	}
223
224
	/**
225
	 * A work-around / hack to make it possible to go to the media library with the add new box open.
226
	 *
227
	 * @return bool
228
	 */
229
	public function print_in_footer_open_media_add_new() {
230
		global $pagenow;
231
232
		// Only load in the admin
233
		if ( ! is_admin() ) {
234
			return false;
235
		}
236
237
		if ( $pagenow !== 'upload.php' ) {
238
			return false;
239
		}
240
241
		if ( ! isset ( $_GET['action'] ) || $_GET['action'] !== 'add-new' ) {
242
			return false;
243
		}
244
245
		?>
246
			<script type="text/javascript">
247
				( function( $ ) {
248
					window.setTimeout( function() {
249
						$('#wp-media-grid .page-title-action').click();
250
					}, 500 );
251
252
				}( jQuery ) );
253
			</script>
254
		<?php
255
	}
256
257
	/**
258
	 * Changes the add new menu location, so that VideoPress will be enabled
259
	 * when a user clicks that button.
260
	 */
261
	public function change_add_new_menu_location() {
262
		$page = remove_submenu_page( 'upload.php', 'media-new.php' );
263
264
		add_submenu_page( 'upload.php', $page[0], $page[0], 'upload_files', 'upload.php?action=add-new');
265
	}
266
267
	/**
268
	 * Makes sure that all video mimes are added in, as multi site installs can remove them.
269
	 *
270
	 * @param array $existing_mimes
271
	 * @return array
272
	 */
273
	public function add_video_upload_mimes( $existing_mimes = array() ) {
274
		$mime_types = wp_get_mime_types();
275
		$video_types = array_filter( $mime_types, array( $this, 'filter_video_mimes' ) );
276
277
		foreach ( $video_types as $key => $value ) {
278
			$existing_mimes[ $key ] = $value;
279
		}
280
281
		return $existing_mimes;
282
	}
283
284
	/**
285
	 * Filter designed to get rid of non video mime types.
286
	 *
287
	 * @param string $value
288
	 * @return int
289
	 */
290
	public function filter_video_mimes( $value ) {
291
		return preg_match( '@^video/@', $value );
292
	}
293
}
294
295
// Initialize the module.
296
Jetpack_VideoPress::init();
297