Complex classes like VideoPress_Edit_Attachment 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 VideoPress_Edit_Attachment, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | class VideoPress_Edit_Attachment { |
||
| 8 | |||
| 9 | /** |
||
| 10 | * Singleton method to initialize the object only once. |
||
| 11 | * |
||
| 12 | * @return VideoPress_Edit_Attachment |
||
| 13 | */ |
||
| 14 | public static function init() { |
||
| 23 | |||
| 24 | /** |
||
| 25 | * VideoPress_Edit_Attachment constructor. |
||
| 26 | * |
||
| 27 | * Adds in appropriate actions for attachment fields editor, meta boxes and saving. |
||
| 28 | */ |
||
| 29 | public function __construct() { |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @param string $post_type |
||
|
|
|||
| 40 | * @param object $post |
||
| 41 | */ |
||
| 42 | public function configure_meta_boxes( $post_type = 'unknown', $post = NULL ) { |
||
| 43 | if ( NULL == $post ) { |
||
| 44 | $post = (object) array ( 'ID' => 0 ); |
||
| 45 | } |
||
| 46 | |||
| 47 | if ( 'attachment' != $post_type ) { |
||
| 48 | return; |
||
| 49 | } |
||
| 50 | |||
| 51 | // If this has not been processed by videopress, we can skip the rest. |
||
| 52 | if ( ! is_videopress_attachment( $post->ID ) ) { |
||
| 53 | return; |
||
| 54 | } |
||
| 55 | |||
| 56 | add_meta_box( 'videopress-media-info', __( 'VideoPress Information', 'jetpack' ), array( $this, 'videopress_information_box' ), 'attachment', 'side', 'core' ); |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @param array $post |
||
| 61 | * @param array|null $attachment |
||
| 62 | * |
||
| 63 | * @return array |
||
| 64 | */ |
||
| 65 | public function save_fields( $post, $attachment = null ) { |
||
| 66 | if ( $attachment === null && isset( $_POST['attachment'] ) ) { |
||
| 67 | $attachment = $_POST['attachment']; |
||
| 68 | } |
||
| 69 | |||
| 70 | if ( ! isset( $attachment['is_videopress_attachment'] ) || $attachment['is_videopress_attachment'] !== 'yes' ) { |
||
| 71 | return $post; |
||
| 72 | } |
||
| 73 | |||
| 74 | $post_id = absint( $post['ID'] ); |
||
| 75 | |||
| 76 | $meta = wp_get_attachment_metadata( $post_id ); |
||
| 77 | |||
| 78 | // If this has not been processed by videopress, we can skip the rest. |
||
| 79 | if ( ! is_videopress_attachment( $post['ID'] ) ) { |
||
| 80 | return $post; |
||
| 81 | } |
||
| 82 | |||
| 83 | $values = array(); |
||
| 84 | |||
| 85 | // Add the video title & description in, so that we save it properly. |
||
| 86 | if ( isset( $_POST['post_title'] ) ) { |
||
| 87 | $values['title'] = trim( strip_tags( $_POST['post_title'] ) ); |
||
| 88 | } |
||
| 89 | |||
| 90 | if ( isset( $_POST['post_excerpt'] ) ) { |
||
| 91 | $values['description'] = trim( strip_tags( $_POST['post_excerpt'] ) ); |
||
| 92 | } |
||
| 93 | |||
| 94 | if ( isset( $attachment['rating'] ) ) { |
||
| 95 | $rating = $attachment['rating']; |
||
| 96 | |||
| 97 | if ( ! empty( $rating ) && in_array( $rating, array( 'G', 'PG-13', 'R-17', 'X-18' ) ) ) { |
||
| 98 | $values['rating'] = $rating; |
||
| 99 | } |
||
| 100 | } |
||
| 101 | |||
| 102 | // We set a default here, as if it isn't selected, then we'll turn it off. |
||
| 103 | $values['display_embed'] = 0; |
||
| 104 | if ( isset( $attachment['display_embed'] ) ) { |
||
| 105 | $display_embed = $attachment['display_embed']; |
||
| 106 | |||
| 107 | $values['display_embed'] = 'on' === $display_embed ? 1 : 0; |
||
| 108 | } |
||
| 109 | |||
| 110 | $args = array( |
||
| 111 | 'method' => 'POST', |
||
| 112 | ); |
||
| 113 | |||
| 114 | $guid = get_post_meta( $post_id, 'videopress_guid', true ); |
||
| 115 | |||
| 116 | $endpoint = "videos/{$guid}"; |
||
| 117 | $result = Jetpack_Client::wpcom_json_api_request_as_blog( $endpoint, Jetpack_Client::WPCOM_JSON_API_VERSION, $args, $values ); |
||
| 118 | |||
| 119 | if ( is_wp_error( $result ) ) { |
||
| 120 | $post['errors']['videopress']['errors'][] = __( 'There was an issue saving your updates to the VideoPress service. Please try again later.', 'jetpack' ); |
||
| 121 | return $post; |
||
| 122 | } |
||
| 123 | |||
| 124 | if ( isset( $values['display_embed'] ) ) { |
||
| 125 | $meta['videopress']['display_embed'] = $values['display_embed']; |
||
| 126 | } |
||
| 127 | |||
| 128 | if ( isset( $values['rating'] ) ) { |
||
| 129 | $meta['videopress']['rating'] = $values['rating']; |
||
| 130 | } |
||
| 131 | |||
| 132 | wp_update_attachment_metadata( $post_id, $meta ); |
||
| 133 | |||
| 134 | $response = json_decode( $result['body'], true ); |
||
| 135 | |||
| 136 | if ( 'true' !== $response ) { |
||
| 137 | return $post; |
||
| 138 | } |
||
| 139 | |||
| 140 | return $post; |
||
| 141 | } |
||
| 142 | |||
| 143 | |||
| 144 | /** |
||
| 145 | * Get the upload api path. |
||
| 146 | * |
||
| 147 | * @param string $guid |
||
| 148 | * @return string |
||
| 149 | */ |
||
| 150 | public function make_video_api_path( $guid ) { |
||
| 159 | |||
| 160 | |||
| 161 | /** |
||
| 162 | * Creates an array of video fields to edit based on transcoded videos. |
||
| 163 | * |
||
| 164 | * @param array $fields video fields of interest |
||
| 165 | * @param stdClass $post post object |
||
| 166 | * @return array modified version of video fields for administrative interface display |
||
| 167 | */ |
||
| 168 | public function fields_to_edit( $fields, $post ) { |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @param stdClass $post |
||
| 235 | */ |
||
| 236 | public function videopress_information_box( $post ) { |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Build HTML to display a form checkbox for embedcode display preference |
||
| 342 | * |
||
| 343 | * @param object $info database row from the videos table |
||
| 344 | * @return string input element of type checkbox set to checked state based on stored embed preference |
||
| 345 | */ |
||
| 346 | protected function display_embed_choice( $info ) { |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Build HTML to display a form input radio button for video ratings |
||
| 357 | * |
||
| 358 | * @param object $info database row from the videos table |
||
| 359 | * @return string input elements of type radio with existing stored value selected |
||
| 360 | */ |
||
| 361 | protected function display_rating( $info ) { |
||
| 384 | } |
||
| 385 | |||
| 387 | VideoPress_Edit_Attachment::init(); |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.