Complex classes like Jetpack_VideoPress often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Jetpack_VideoPress, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
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() { |
||
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() { |
||
37 | |||
38 | /** |
||
39 | * Fires on init |
||
40 | */ |
||
41 | public function on_init() { |
||
60 | |||
61 | /** |
||
62 | * Runs when the VideoPress module is deactivated. |
||
63 | */ |
||
64 | public function jetpack_module_deactivated() { |
||
67 | |||
68 | /** |
||
69 | * A can of coke |
||
70 | * |
||
71 | * Similar to current_user_can, but internal to VideoPress. Returns |
||
72 | * true if the given VideoPress capability is allowed by the given user. |
||
73 | */ |
||
74 | public function can( $cap, $user_id = false ) { |
||
100 | |||
101 | /** |
||
102 | * Returns true if the provided user is the Jetpack connection owner. |
||
103 | */ |
||
104 | public function is_connection_owner( $user_id = false ) { |
||
113 | |||
114 | /** |
||
115 | * Register and enqueue VideoPress admin styles. |
||
116 | */ |
||
117 | public function enqueue_admin_styles() { |
||
121 | |||
122 | /** |
||
123 | * Register VideoPress admin scripts. |
||
124 | */ |
||
125 | public function enqueue_admin_scripts() { |
||
126 | if ( did_action( 'videopress_enqueue_admin_scripts' ) ) { |
||
127 | return; |
||
128 | } |
||
129 | |||
130 | if ( $this->should_override_media_uploader() ) { |
||
131 | wp_enqueue_script( |
||
132 | 'videopress-plupload', |
||
133 | Jetpack::get_file_url_for_environment( |
||
134 | '_inc/build/videopress/js/videopress-plupload.min.js', |
||
135 | 'modules/videopress/js/videopress-plupload.js' |
||
136 | ), |
||
137 | array( |
||
138 | 'jquery', |
||
139 | 'wp-plupload' |
||
140 | ), |
||
141 | $this->version |
||
142 | ); |
||
143 | |||
144 | wp_enqueue_script( |
||
145 | 'videopress-uploader', |
||
146 | Jetpack::get_file_url_for_environment( |
||
147 | '_inc/build/videopress/js/videopress-uploader.min.js', |
||
148 | 'modules/videopress/js/videopress-uploader.js' |
||
149 | ), |
||
150 | array( |
||
151 | 'videopress-plupload' |
||
152 | ), |
||
153 | $this->version |
||
154 | ); |
||
155 | |||
156 | wp_enqueue_script( |
||
157 | 'media-video-widget-extensions', |
||
158 | Jetpack::get_file_url_for_environment( |
||
159 | '_inc/build/videopress/js/media-video-widget-extensions.min.js', |
||
160 | 'modules/videopress/js/media-video-widget-extensions.js' |
||
161 | ), |
||
162 | array(), |
||
163 | $this->version, |
||
164 | true |
||
165 | ); |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Fires after VideoPress scripts are enqueued in the dashboard. |
||
170 | * |
||
171 | * @since 2.5.0 |
||
172 | */ |
||
173 | do_action( 'videopress_enqueue_admin_scripts' ); |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * An override for the attachment url, which returns back the WPCOM VideoPress processed url. |
||
178 | * |
||
179 | * This is an action proxy to the videopress_get_attachment_url() utility function. |
||
180 | * |
||
181 | * @param string $url |
||
182 | * @param int $post_id |
||
183 | * |
||
184 | * @return string |
||
185 | */ |
||
186 | public function update_attachment_url_for_videopress( $url, $post_id ) { |
||
187 | if ( $videopress_url = videopress_get_attachment_url( $post_id ) ) { |
||
188 | return $videopress_url; |
||
189 | } |
||
190 | |||
191 | return $url; |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * Modify the default plupload config to turn on videopress specific filters. |
||
196 | */ |
||
197 | public function videopress_pluploder_config( $config ) { |
||
198 | |||
199 | if ( ! isset( $config['filters']['max_file_size'] ) ) { |
||
200 | $config['filters']['max_file_size'] = wp_max_upload_size() . 'b'; |
||
201 | } |
||
202 | |||
203 | $config['filters']['videopress_check_uploads'] = $config['filters']['max_file_size']; |
||
204 | |||
205 | // We're doing our own check in the videopress_check_uploads filter. |
||
206 | unset( $config['filters']['max_file_size'] ); |
||
207 | |||
208 | return $config; |
||
209 | } |
||
210 | |||
211 | |||
212 | /** |
||
213 | * Helper function to determine if the media uploader should be overridden. |
||
214 | * |
||
215 | * The rules are simple, only try to load the script when on the edit post or new post pages. |
||
216 | * |
||
217 | * @return bool |
||
218 | */ |
||
219 | protected function should_override_media_uploader() { |
||
220 | global $pagenow; |
||
221 | |||
222 | // Only load in the admin |
||
223 | if ( ! is_admin() ) { |
||
224 | return false; |
||
225 | } |
||
226 | |||
227 | $acceptable_pages = array( |
||
228 | 'post-new.php', |
||
229 | 'post.php', |
||
230 | 'upload.php', |
||
231 | 'customize.php', |
||
232 | ); |
||
233 | |||
234 | // Only load on the post, new post, or upload pages. |
||
235 | if ( !in_array( $pagenow, $acceptable_pages ) ) { |
||
236 | return false; |
||
237 | } |
||
238 | |||
239 | $options = VideoPress_Options::get_options(); |
||
240 | |||
241 | return $options['shadow_blog_id'] > 0; |
||
242 | } |
||
243 | |||
244 | /** |
||
245 | * A work-around / hack to make it possible to go to the media library with the add new box open. |
||
246 | * |
||
247 | * @return bool |
||
248 | */ |
||
249 | public function print_in_footer_open_media_add_new() { |
||
250 | global $pagenow; |
||
251 | |||
252 | // Only load in the admin |
||
253 | if ( ! is_admin() ) { |
||
254 | return false; |
||
255 | } |
||
256 | |||
257 | if ( $pagenow !== 'upload.php' ) { |
||
258 | return false; |
||
259 | } |
||
260 | |||
261 | if ( ! isset ( $_GET['action'] ) || $_GET['action'] !== 'add-new' ) { |
||
262 | return false; |
||
263 | } |
||
264 | |||
265 | ?> |
||
266 | <script type="text/javascript"> |
||
267 | ( function( $ ) { |
||
268 | window.setTimeout( function() { |
||
269 | $('#wp-media-grid .page-title-action').click(); |
||
270 | }, 500 ); |
||
271 | |||
272 | }( jQuery ) ); |
||
273 | </script> |
||
274 | <?php |
||
275 | } |
||
276 | |||
277 | /** |
||
278 | * Makes sure that all video mimes are added in, as multi site installs can remove them. |
||
279 | * |
||
280 | * @param array $existing_mimes |
||
281 | * @return array |
||
282 | */ |
||
283 | public function add_video_upload_mimes( $existing_mimes = array() ) { |
||
296 | |||
297 | /** |
||
298 | * Filter designed to get rid of non video mime types. |
||
299 | * |
||
300 | * @param string $value |
||
301 | * @return int |
||
302 | */ |
||
303 | public function filter_video_mimes( $value ) { |
||
306 | |||
307 | /** |
||
308 | * @param string $icon |
||
309 | * @param string $mime |
||
310 | * @param int $post_id |
||
311 | * |
||
312 | * @return string |
||
313 | */ |
||
314 | public function wp_mime_type_icon( $icon, $mime, $post_id ) { |
||
328 | |||
329 | /** |
||
330 | * @param array $extensions |
||
331 | * |
||
332 | * @return array |
||
333 | */ |
||
334 | public function add_videopress_extenstion( $extensions ) { |
||
339 | } |
||
340 | |||
341 | // Initialize the module. |
||
342 | Jetpack_VideoPress::init(); |
||
343 |