Completed
Push — master-stable ( e932ef...d7447c )
by
unknown
07:32
created

Jetpack_VideoPress::is_connection_owner()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 5
nc 8
nop 1
dl 0
loc 9
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 since is_connection_owner should wait until the user is initialized by $wp->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
		VideoPress_Scheduler::init();
47
		VideoPress_XMLRPC::init();
48
	}
49
50
	/**
51
	 * Runs when the VideoPress module is deactivated.
52
	 */
53
	public function jetpack_module_deactivated() {
54
		VideoPress_Options::delete_options();
55
	}
56
57
	/**
58
	 * A can of coke
59
	 *
60
	 * Similar to current_user_can, but internal to VideoPress. Returns
61
	 * true if the given VideoPress capability is allowed by the given user.
62
	 */
63
	public function can( $cap, $user_id = false ) {
64
		if ( ! $user_id ) {
65
			$user_id = get_current_user_id();
66
		}
67
68
		// Connection owners are allowed to do all the things.
69
		if ( $this->is_connection_owner( $user_id ) ) {
70
			return true;
71
		}
72
73
		// Additional and internal caps checks
74
75
		if ( ! user_can( $user_id, 'upload_files' ) ) {
76
			return false;
77
		}
78
79
		if ( 'edit_videos' == $cap && ! user_can( $user_id, 'edit_others_posts' ) ) {
80
			return false;
81
		}
82
83
		if ( 'delete_videos' == $cap && ! user_can( $user_id, 'delete_others_posts' ) ) {
84
			return false;
85
		}
86
87
		return true;
88
	}
89
90
	/**
91
	 * Returns true if the provided user is the Jetpack connection owner.
92
	 */
93
	public function is_connection_owner( $user_id = false ) {
94
		if ( ! $user_id ) {
95
			$user_id = get_current_user_id();
96
		}
97
98
		$user_token = Jetpack_Data::get_access_token( JETPACK_MASTER_USER );
99
100
		return $user_token && is_object( $user_token ) && isset( $user_token->external_user_id ) && $user_id === $user_token->external_user_id;
101
	}
102
103
	/**
104
	 * Register VideoPress admin scripts.
105
	 */
106
	public function enqueue_admin_scripts() {
107
		if ( did_action( 'videopress_enqueue_admin_scripts' ) ) {
108
			return;
109
		}
110
111
		if ( $this->should_override_media_uploader() ) {
112
			// We're going to replace the standard wp-plupload with our own ... messy, I know, but as of now the
113
			// hooks in it are not good enough for us to be able to override / add in just the code we need.
114
			// P.S. Please don't take this as an example of good behavior, this is a temporary fix until I
115
			// can get a more permanent action / filter system added into the core wp-plupload.js to make this
116
			// type of override unnecessary.
117
			wp_dequeue_script( 'wp-plupload' );
118
119
			wp_enqueue_script(
120
				'videopress-plupload',
121
				plugins_url( 'js/videopress-plupload.js', __FILE__ ),
122
				array(
123
					'jquery'
124
				),
125
				$this->version
126
			);
127
128
			wp_enqueue_script(
129
				'videopress-uploader',
130
				plugins_url( 'js/videopress-uploader.js', __FILE__ ),
131
				array(
132
					'videopress-plupload'
133
				),
134
				$this->version
135
			);
136
		}
137
138
		wp_enqueue_style( 'videopress-admin', plugins_url( 'videopress-admin.css', __FILE__ ), array(), $this->version );
139
140
		/**
141
		 * Fires after VideoPress scripts are enqueued in the dashboard.
142
		 *
143
		 * @since 2.5.0
144
		 */
145
		do_action( 'videopress_enqueue_admin_scripts' );
146
	}
147
148
	/**
149
	 * An override for the attachment url, which returns back the WPCOM videopress original url,
150
	 * if it is set to the the objects metadata. this allows us to show the original uploaded
151
	 * file on the WPCOM architecture, instead of the locally uplodaded file,
152
	 * which doeasn't exist.
153
	 *
154
	 * @param string $url
155
	 * @param int $post_id
156
	 *
157
	 * @return mixed
158
	 */
159
	public function update_attachment_url_for_videopress( $url, $post_id ) {
160
161
		if ( get_post_mime_type( $post_id ) === 'video/videopress' ) {
162
			$meta = wp_get_attachment_metadata( $post_id );
163
164
			if ( isset( $meta['original']['url'] ) ) {
165
				$url = $meta['original']['url'];
166
			}
167
		}
168
169
		return $url;
170
	}
171
172
	/**
173
	 * Modify the default plupload config to turn on videopress specific filters.
174
	 */
175
	public function videopress_pluploder_config( $config ) {
176
177
		if ( ! isset( $config['filters']['max_file_size'] ) ) {
178
			$config['filters']['max_file_size'] = wp_max_upload_size() . 'b';
179
		}
180
181
		$config['filters']['videopress_check_uploads'] = $config['filters']['max_file_size'];
182
183
		// We're doing our own check in the videopress_check_uploads filter.
184
		unset( $config['filters']['max_file_size'] );
185
186
		return $config;
187
	}
188
189
190
	/**
191
	 * Helper function to determine if the media uploader should be overridden.
192
	 *
193
	 * The rules are simple, only try to load the script when on the edit post or new post pages.
194
	 *
195
	 * @return bool
196
	 */
197
	protected function should_override_media_uploader() {
198
		global $pagenow;
199
200
		// Only load in the admin
201
		if ( ! is_admin() ) {
202
			return false;
203
		}
204
205
		// Only load on the post, new post, or upload pages.
206
		if ( $pagenow !== 'post-new.php' && $pagenow !== 'post.php' && $pagenow !== 'upload.php' ) {
207
			return false;
208
		}
209
210
		$options = VideoPress_Options::get_options();
211
212
		return $options['shadow_blog_id'] > 0;
213
	}
214
215
}
216
217
// Initialize the module.
218
Jetpack_VideoPress::init();
219