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