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() { |
||
| 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 | if ( Jetpack::active_plan_supports( 'videopress' ) ) { |
||
| 47 | add_filter( 'upload_mimes', array( $this, 'add_video_upload_mimes' ), 999 ); |
||
| 48 | } |
||
| 49 | |||
| 50 | add_action( 'admin_print_footer_scripts', array( $this, 'print_in_footer_open_media_add_new' ) ); |
||
| 51 | add_action( 'admin_head', array( $this, 'enqueue_admin_styles' ) ); |
||
| 52 | |||
| 53 | add_filter( 'wp_mime_type_icon', array( $this, 'wp_mime_type_icon' ), 10, 3 ); |
||
| 54 | |||
| 55 | VideoPress_Scheduler::init(); |
||
| 56 | VideoPress_XMLRPC::init(); |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Runs when the VideoPress module is deactivated. |
||
| 61 | */ |
||
| 62 | public function jetpack_module_deactivated() { |
||
| 65 | |||
| 66 | /** |
||
| 67 | * A can of coke |
||
| 68 | * |
||
| 69 | * Similar to current_user_can, but internal to VideoPress. Returns |
||
| 70 | * true if the given VideoPress capability is allowed by the given user. |
||
| 71 | */ |
||
| 72 | public function can( $cap, $user_id = false ) { |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Returns true if the provided user is the Jetpack connection owner. |
||
| 101 | */ |
||
| 102 | public function is_connection_owner( $user_id = false ) { |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Register and enqueue VideoPress admin styles. |
||
| 114 | */ |
||
| 115 | public function enqueue_admin_styles() { |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Register VideoPress admin scripts. |
||
| 122 | */ |
||
| 123 | public function enqueue_admin_scripts() { |
||
| 162 | |||
| 163 | /** |
||
| 164 | * An override for the attachment url, which returns back the WPCOM videopress original url, |
||
| 165 | * if it is set to the the objects metadata. this allows us to show the original uploaded |
||
| 166 | * file on the WPCOM architecture, instead of the locally uplodaded file, |
||
| 167 | * which doeasn't exist. |
||
| 168 | * |
||
| 169 | * TODO: Fix this so that it will return a VideoPress process url, to ensure that it is in MP4 format. |
||
|
|
|||
| 170 | * |
||
| 171 | * @param string $url |
||
| 172 | * @param int $post_id |
||
| 173 | * |
||
| 174 | * @return mixed |
||
| 175 | */ |
||
| 176 | public function update_attachment_url_for_videopress( $url, $post_id ) { |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Modify the default plupload config to turn on videopress specific filters. |
||
| 191 | */ |
||
| 192 | public function videopress_pluploder_config( $config ) { |
||
| 205 | |||
| 206 | |||
| 207 | /** |
||
| 208 | * Helper function to determine if the media uploader should be overridden. |
||
| 209 | * |
||
| 210 | * The rules are simple, only try to load the script when on the edit post or new post pages. |
||
| 211 | * |
||
| 212 | * @return bool |
||
| 213 | */ |
||
| 214 | protected function should_override_media_uploader() { |
||
| 238 | |||
| 239 | /** |
||
| 240 | * A work-around / hack to make it possible to go to the media library with the add new box open. |
||
| 241 | * |
||
| 242 | * @return bool |
||
| 243 | */ |
||
| 244 | public function print_in_footer_open_media_add_new() { |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Changes the add new menu location, so that VideoPress will be enabled |
||
| 274 | * when a user clicks that button. |
||
| 275 | */ |
||
| 276 | public function change_add_new_menu_location() { |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Makes sure that all video mimes are added in, as multi site installs can remove them. |
||
| 283 | * |
||
| 284 | * @param array $existing_mimes |
||
| 285 | * @return array |
||
| 286 | */ |
||
| 287 | public function add_video_upload_mimes( $existing_mimes = array() ) { |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Filter designed to get rid of non video mime types. |
||
| 300 | * |
||
| 301 | * @param string $value |
||
| 302 | * @return int |
||
| 303 | */ |
||
| 304 | public function filter_video_mimes( $value ) { |
||
| 307 | |||
| 308 | /** |
||
| 309 | * @param string $icon |
||
| 310 | * @param string $mime |
||
| 311 | * @param int $post_id |
||
| 312 | * |
||
| 313 | * @return string |
||
| 314 | */ |
||
| 315 | public function wp_mime_type_icon( $icon, $mime, $post_id ) { |
||
| 329 | } |
||
| 330 | |||
| 333 |