Completed
Push — master-stable ( d7447c...e70f3b )
by
unknown
14:02 queued 06:14
created

should_override_media_uploader()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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