| Conditions | 8 |
| Paths | 12 |
| Total Lines | 66 |
| Code Lines | 33 |
| Lines | 0 |
| Ratio | 0 % |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 119 | public static function media_handle_upload( $url, $post_id, $post_data = array() ) { |
||
| 120 | |||
| 121 | $reflection = new ReflectionMethod( 'GFFormsModel', 'media_handle_upload' ); |
||
| 122 | |||
| 123 | /** |
||
| 124 | * If the method changes to public, use Gravity Forms' method |
||
| 125 | * @todo: If/when the method is public, remove the unneeded copied code. |
||
| 126 | */ |
||
| 127 | if( $reflection->isPublic() ) { |
||
| 128 | return parent::media_handle_upload( $url, $post_id, $post_data ); |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Original Gravity Forms code below: |
||
| 133 | * ================================== |
||
| 134 | */ |
||
| 135 | |||
| 136 | //WordPress Administration API required for the media_handle_upload() function |
||
| 137 | require_once( ABSPATH . 'wp-admin/includes/image.php' ); |
||
| 138 | |||
| 139 | $name = basename( $url ); |
||
| 140 | |||
| 141 | $file = self::copy_post_image( $url, $post_id ); |
||
| 142 | |||
| 143 | if ( ! $file ) { |
||
| 144 | return false; |
||
| 145 | } |
||
| 146 | |||
| 147 | $name_parts = pathinfo( $name ); |
||
| 148 | $name = trim( substr( $name, 0, - ( 1 + strlen( $name_parts['extension'] ) ) ) ); |
||
| 149 | |||
| 150 | $url = $file['url']; |
||
| 151 | $type = $file['type']; |
||
| 152 | $file = $file['file']; |
||
| 153 | $title = $name; |
||
| 154 | $content = ''; |
||
| 155 | |||
| 156 | // use image exif/iptc data for title and caption defaults if possible |
||
| 157 | if ( $image_meta = @wp_read_image_metadata( $file ) ) { |
||
| 158 | if ( trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) ) { |
||
| 159 | $title = $image_meta['title']; |
||
| 160 | } |
||
| 161 | if ( trim( $image_meta['caption'] ) ) { |
||
| 162 | $content = $image_meta['caption']; |
||
| 163 | } |
||
| 164 | } |
||
| 165 | |||
| 166 | // Construct the attachment array |
||
| 167 | $attachment = array_merge( |
||
| 168 | array( |
||
| 169 | 'post_mime_type' => $type, |
||
| 170 | 'guid' => $url, |
||
| 171 | 'post_parent' => $post_id, |
||
| 172 | 'post_title' => $title, |
||
| 173 | 'post_content' => $content, |
||
| 174 | ), $post_data |
||
| 175 | ); |
||
| 176 | |||
| 177 | // Save the data |
||
| 178 | $id = wp_insert_attachment( $attachment, $file, $post_id ); |
||
| 179 | if ( ! is_wp_error( $id ) ) { |
||
| 180 | wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) ); |
||
| 181 | } |
||
| 182 | |||
| 183 | return $id; |
||
| 184 | } |
||
| 185 | |||
| 187 | } |