Completed
Push — fix/videopress-default-hd ( f03c07 )
by
unknown
22:34 queued 13:43
created

Jetpack_VideoPress::enqueue_admin_scripts()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 37
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 21
nc 5
nop 0
dl 0
loc 37
rs 8.5806
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
			wp_enqueue_script( 'videopress-uploader', plugins_url( 'js/videopress-uploader.js', __FILE__ ), array(
113
				'jquery',
114
				'wp-plupload'
115
			), $this->version );
116
		}
117
118
		wp_enqueue_style( 'videopress-admin', plugins_url( 'videopress-admin.css', __FILE__ ), array(), $this->version );
119
120
		$caps = array();
121
		foreach ( array( 'edit_videos', 'delete_videos' ) as $cap ) {
122
			$caps[ $cap ] = $this->can( $cap );
123
		}
124
125
		// Leaving these as we may need to encorporate them somewhere else
126
		$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...
127
			'selectVideoFile'         => __( 'Please select a video file to upload.', 'jetpack' ),
128
			'videoUploading'          => __( 'Your video is uploading... Please do not close this window.', 'jetpack' ),
129
			'unknownError'            => __( 'An unknown error has occurred. Please try again later.', 'jetpack' ),
130
			'videoUploaded'           => __( 'Your video has successfully been uploaded. It will appear in your VideoPress Library shortly.', 'jetpack' ),
131
			'VideoPressLibraryRouter' => __( 'VideoPress Library', 'jetpack' ),
132
			'uploadVideoRouter'       => __( 'Upload a Video', 'jetpack' ),
133
			'insertVideoButton'       => __( 'Insert Video', 'jetpack' ),
134
		);
135
136
		/**
137
		 * Fires after VideoPress scripts are enqueued in the dashboard.
138
		 *
139
		 * @since 2.5.0
140
		 */
141
		do_action( 'videopress_enqueue_admin_scripts' );
142
	}
143
144
	/**
145
	 * An override for the attachment url, which returns back the WPCOM videopress original url,
146
	 * if it is set to the the objects metadata. this allows us to show the original uploaded
147
	 * file on the WPCOM architecture, instead of the locally uplodaded file,
148
	 * which doeasn't exist.
149
	 *
150
	 * @param string $url
151
	 * @param int $post_id
152
	 *
153
	 * @return mixed
154
	 */
155
	public function update_attachment_url_for_videopress( $url, $post_id ) {
156
157
		if ( get_post_mime_type( $post_id ) === 'video/videopress' ) {
158
			$meta = wp_get_attachment_metadata( $post_id );
159
160
			if ( isset( $meta['original']['url'] ) ) {
161
				$url = $meta['original']['url'];
162
			}
163
		}
164
165
		return $url;
166
	}
167
168
	/**
169
	 * Modify the default plupload config to turn on videopress specific filters.
170
	 */
171
	public function videopress_pluploder_config( $config ) {
172
173
		if ( ! isset( $config['filters']['max_file_size'] ) ) {
174
			$config['filters']['max_file_size'] = wp_max_upload_size() . 'b';
175
		}
176
177
		$config['filters']['videopress_check_uploads'] = $config['filters']['max_file_size'];
178
179
		// We're doing our own check in the videopress_check_uploads filter.
180
		unset( $config['filters']['max_file_size'] );
181
182
		return $config;
183
	}
184
185
186
	/**
187
	 * Helper function to determine if the media uploader should be overridden.
188
	 *
189
	 * The rules are simple, only try to load the script when on the edit post or new post pages.
190
	 *
191
	 * @return bool
192
	 */
193
	protected function should_override_media_uploader() {
194
		global $pagenow;
195
196
		// Only load in the admin
197
		if ( ! is_admin() ) {
198
			return false;
199
		}
200
201
		// Only load on the post, new post, or upload pages.
202
		if ( $pagenow !== 'post-new.php' && $pagenow !== 'post.php' && $pagenow !== 'upload.php' ) {
203
			return false;
204
		}
205
206
		$options = VideoPress_Options::get_options();
207
208
		return $options['shadow_blog_id'] > 0;
209
	}
210
211
}
212
213
// Initialize the module.
214
Jetpack_VideoPress::init();
215