Completed
Push — fix/4.4.1-typos ( 3db521 )
by
unknown
25s
created

Jetpack_VideoPress::on_init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 0
dl 0
loc 16
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 since is_connection_owner should wait until the user is initialized by $wp->init();
40
	 */
41
	public function on_init() {
42
		// Only the connection owner can configure this module.
43
		if ( $this->is_connection_owner() ) {
44
			Jetpack::enable_module_configurable( $this->module );
45
		}
46
47
		add_action( 'wp_enqueue_media', array( $this, 'enqueue_admin_scripts' ) );
48
49
		add_filter( 'plupload_default_settings', array( $this, 'videopress_pluploder_config' ) );
50
51
		add_filter( 'videopress_shortcode_options', array( $this, 'videopress_shortcode_options' ) );
52
		add_filter( 'wp_get_attachment_url', array( $this, 'update_attachment_url_for_videopress' ), 10, 2 );
53
54
		VideoPress_Scheduler::init();
55
		VideoPress_XMLRPC::init();
56
	}
57
58
	/**
59
	 * Runs when the VideoPress module is deactivated.
60
	 */
61
	public function jetpack_module_deactivated() {
62
		VideoPress_Options::delete_options();
63
	}
64
65
	/**
66
	 * A can of coke
67
	 *
68
	 * Similar to current_user_can, but internal to VideoPress. Returns
69
	 * true if the given VideoPress capability is allowed by the given user.
70
	 */
71
	public function can( $cap, $user_id = false ) {
72
		if ( ! $user_id ) {
73
			$user_id = get_current_user_id();
74
		}
75
76
		// Connection owners are allowed to do all the things.
77
		if ( $this->is_connection_owner( $user_id ) ) {
78
			return true;
79
		}
80
81
		// Additional and internal caps checks
82
83
		if ( ! user_can( $user_id, 'upload_files' ) ) {
84
			return false;
85
		}
86
87
		if ( 'edit_videos' == $cap && ! user_can( $user_id, 'edit_others_posts' ) ) {
88
			return false;
89
		}
90
91
		if ( 'delete_videos' == $cap && ! user_can( $user_id, 'delete_others_posts' ) ) {
92
			return false;
93
		}
94
95
		return true;
96
	}
97
98
	/**
99
	 * Returns true if the provided user is the Jetpack connection owner.
100
	 */
101
	public function is_connection_owner( $user_id = false ) {
102
		if ( ! $user_id ) {
103
			$user_id = get_current_user_id();
104
		}
105
106
		$user_token = Jetpack_Data::get_access_token( JETPACK_MASTER_USER );
107
108
		return $user_token && is_object( $user_token ) && isset( $user_token->external_user_id ) && $user_id === $user_token->external_user_id;
109
	}
110
111
	/**
112
	 * Filters the VideoPress shortcode options, makes sure that
113
	 * the settings set in Jetpack's VideoPress module are applied.
114
	 */
115
	public function videopress_shortcode_options( $options ) {
116
		$videopress_options = VideoPress_Options::get_options();
117
118
		if ( false === $options['freedom'] ) {
119
			$options['freedom'] = $videopress_options['freedom'];
120
		}
121
122
		$options['hd'] = $videopress_options['hd'];
123
124
		return $options;
125
	}
126
127
	/**
128
	 * Register VideoPress admin scripts.
129
	 */
130
	public function enqueue_admin_scripts() {
131
		if ( did_action( 'videopress_enqueue_admin_scripts' ) ) {
132
			return;
133
		}
134
135
		if ( $this->should_override_media_uploader() ) {
136
			wp_enqueue_script( 'videopress-uploader', plugins_url( 'js/videopress-uploader.js', __FILE__ ), array(
137
				'jquery',
138
				'wp-plupload'
139
			), $this->version );
140
		}
141
142
		wp_enqueue_style( 'videopress-admin', plugins_url( 'videopress-admin.css', __FILE__ ), array(), $this->version );
143
144
		$caps = array();
145
		foreach ( array( 'edit_videos', 'delete_videos' ) as $cap ) {
146
			$caps[ $cap ] = $this->can( $cap );
147
		}
148
149
		// Leaving these as we may need to encorporate them somewhere else
150
		$l10n = array(
0 ignored issues
show
Unused Code introduced by
$l10n is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
151
			'selectVideoFile'         => __( 'Please select a video file to upload.', 'jetpack' ),
152
			'videoUploading'          => __( 'Your video is uploading... Please do not close this window.', 'jetpack' ),
153
			'unknownError'            => __( 'An unknown error has occurred. Please try again later.', 'jetpack' ),
154
			'videoUploaded'           => __( 'Your video has successfully been uploaded. It will appear in your VideoPress Library shortly.', 'jetpack' ),
155
			'VideoPressLibraryRouter' => __( 'VideoPress Library', 'jetpack' ),
156
			'uploadVideoRouter'       => __( 'Upload a Video', 'jetpack' ),
157
			'insertVideoButton'       => __( 'Insert Video', 'jetpack' ),
158
		);
159
160
		/**
161
		 * Fires after VideoPress scripts are enqueued in the dashboard.
162
		 *
163
		 * @since 2.5.0
164
		 */
165
		do_action( 'videopress_enqueue_admin_scripts' );
166
	}
167
168
	/**
169
	 * An override for the attachment url, which returns back the WPCOM videopress original url,
170
	 * if it is set to the the objects metadata. this allows us to show the original uploaded
171
	 * file on the WPCOM architecture, instead of the locally uplodaded file,
172
	 * which doeasn't exist.
173
	 *
174
	 * @param string $url
175
	 * @param int $post_id
176
	 *
177
	 * @return mixed
178
	 */
179
	public function update_attachment_url_for_videopress( $url, $post_id ) {
180
181
		if ( get_post_mime_type( $post_id ) === 'video/videopress' ) {
182
			$meta = wp_get_attachment_metadata( $post_id );
183
184
			if ( isset( $meta['original']['url'] ) ) {
185
				$url = $meta['original']['url'];
186
			}
187
		}
188
189
		return $url;
190
	}
191
192
	/**
193
	 * Modify the default plupload config to turn on videopress specific filters.
194
	 */
195
	public function videopress_pluploder_config( $config ) {
196
197
		if ( ! isset( $config['filters']['max_file_size'] ) ) {
198
			$config['filters']['max_file_size'] = wp_max_upload_size() . 'b';
199
		}
200
201
		$config['filters']['videopress_check_uploads'] = $config['filters']['max_file_size'];
202
203
		// We're doing our own check in the videopress_check_uploads filter.
204
		unset( $config['filters']['max_file_size'] );
205
206
		return $config;
207
	}
208
209
210
	/**
211
	 * Helper function to determine if the media uploader should be overridden.
212
	 *
213
	 * The rules are simple, only try to load the script when on the edit post or new post pages.
214
	 *
215
	 * @return bool
216
	 */
217
	protected function should_override_media_uploader() {
218
		global $pagenow;
219
220
		// Only load in the admin
221
		if ( ! is_admin() ) {
222
			return false;
223
		}
224
225
		// Only load on the post, new post, or upload pages.
226
		if ( $pagenow !== 'post-new.php' && $pagenow !== 'post.php' && $pagenow !== 'upload.php' ) {
227
			return false;
228
		}
229
230
		$options = VideoPress_Options::get_options();
231
232
		return $options['shadow_blog_id'] > 0;
233
	}
234
235
}
236
237
// Initialize the module.
238
Jetpack_VideoPress::init();
239