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_menu', array( $this,'change_add_new_menu_location' ), 999 ); |
||
52 | add_action( 'admin_head', array( $this, 'enqueue_admin_styles' ) ); |
||
53 | |||
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() { |
||
63 | VideoPress_Options::delete_options(); |
||
64 | } |
||
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 ) { |
||
73 | if ( ! $user_id ) { |
||
74 | $user_id = get_current_user_id(); |
||
75 | } |
||
76 | |||
77 | // Connection owners are allowed to do all the things. |
||
78 | if ( $this->is_connection_owner( $user_id ) ) { |
||
79 | return true; |
||
80 | } |
||
81 | |||
82 | // Additional and internal caps checks |
||
83 | |||
84 | if ( ! user_can( $user_id, 'upload_files' ) ) { |
||
85 | return false; |
||
86 | } |
||
87 | |||
88 | if ( 'edit_videos' == $cap && ! user_can( $user_id, 'edit_others_posts' ) ) { |
||
89 | return false; |
||
90 | } |
||
91 | |||
92 | if ( 'delete_videos' == $cap && ! user_can( $user_id, 'delete_others_posts' ) ) { |
||
93 | return false; |
||
94 | } |
||
95 | |||
96 | return true; |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Returns true if the provided user is the Jetpack connection owner. |
||
101 | */ |
||
102 | public function is_connection_owner( $user_id = false ) { |
||
103 | if ( ! $user_id ) { |
||
104 | $user_id = get_current_user_id(); |
||
105 | } |
||
106 | |||
107 | $user_token = Jetpack_Data::get_access_token( JETPACK_MASTER_USER ); |
||
108 | |||
109 | return $user_token && is_object( $user_token ) && isset( $user_token->external_user_id ) && $user_id === $user_token->external_user_id; |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Register and enqueue VideoPress admin styles. |
||
114 | */ |
||
115 | public function enqueue_admin_styles() { |
||
116 | wp_register_style( 'videopress-admin', plugins_url( 'videopress-admin.css', __FILE__ ), array(), $this->version ); |
||
117 | wp_enqueue_style( 'videopress-admin' ); |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * Register VideoPress admin scripts. |
||
122 | */ |
||
123 | public function enqueue_admin_scripts() { |
||
124 | if ( did_action( 'videopress_enqueue_admin_scripts' ) ) { |
||
125 | return; |
||
126 | } |
||
127 | |||
128 | if ( $this->should_override_media_uploader() ) { |
||
129 | // We're going to replace the standard wp-plupload with our own ... messy, I know, but as of now the |
||
130 | // hooks in it are not good enough for us to be able to override / add in just the code we need. |
||
131 | // P.S. Please don't take this as an example of good behavior, this is a temporary fix until I |
||
132 | // can get a more permanent action / filter system added into the core wp-plupload.js to make this |
||
133 | // type of override unnecessary. |
||
134 | wp_dequeue_script( 'wp-plupload' ); |
||
135 | |||
136 | wp_enqueue_script( |
||
137 | 'videopress-plupload', |
||
138 | plugins_url( 'js/videopress-plupload.js', __FILE__ ), |
||
139 | array( |
||
140 | 'jquery' |
||
141 | ), |
||
142 | $this->version |
||
143 | ); |
||
144 | |||
145 | wp_enqueue_script( |
||
146 | 'videopress-uploader', |
||
147 | plugins_url( 'js/videopress-uploader.js', __FILE__ ), |
||
148 | array( |
||
149 | 'videopress-plupload' |
||
150 | ), |
||
151 | $this->version |
||
152 | ); |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * Fires after VideoPress scripts are enqueued in the dashboard. |
||
157 | * |
||
158 | * @since 2.5.0 |
||
159 | */ |
||
160 | do_action( 'videopress_enqueue_admin_scripts' ); |
||
161 | } |
||
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 | * @param string $url |
||
170 | * @param int $post_id |
||
171 | * |
||
172 | * @return mixed |
||
173 | */ |
||
174 | public function update_attachment_url_for_videopress( $url, $post_id ) { |
||
186 | |||
187 | /** |
||
188 | * Modify the default plupload config to turn on videopress specific filters. |
||
189 | */ |
||
190 | public function videopress_pluploder_config( $config ) { |
||
203 | |||
204 | |||
205 | /** |
||
206 | * Helper function to determine if the media uploader should be overridden. |
||
207 | * |
||
208 | * The rules are simple, only try to load the script when on the edit post or new post pages. |
||
209 | * |
||
210 | * @return bool |
||
211 | */ |
||
212 | protected function should_override_media_uploader() { |
||
229 | |||
230 | /** |
||
231 | * A work-around / hack to make it possible to go to the media library with the add new box open. |
||
232 | * |
||
233 | * @return bool |
||
234 | */ |
||
235 | public function print_in_footer_open_media_add_new() { |
||
262 | |||
263 | /** |
||
264 | * Changes the add new menu location, so that VideoPress will be enabled |
||
265 | * when a user clicks that button. |
||
266 | */ |
||
267 | public function change_add_new_menu_location() { |
||
272 | |||
273 | /** |
||
274 | * Makes sure that all video mimes are added in, as multi site installs can remove them. |
||
275 | * |
||
276 | * @param array $existing_mimes |
||
277 | * @return array |
||
278 | */ |
||
279 | public function add_video_upload_mimes( $existing_mimes = array() ) { |
||
289 | |||
290 | /** |
||
291 | * Filter designed to get rid of non video mime types. |
||
292 | * |
||
293 | * @param string $value |
||
294 | * @return int |
||
295 | */ |
||
296 | public function filter_video_mimes( $value ) { |
||
299 | } |
||
300 | |||
303 |