Conditions | 10 |
Paths | 25 |
Total Lines | 84 |
Code Lines | 35 |
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 |
||
19 | public static function copy_post_image( $url, $post_id ) { |
||
20 | |||
21 | $reflection = new ReflectionMethod( 'GFFormsModel', 'copy_post_image' ); |
||
22 | |||
23 | /** |
||
24 | * If the method changes to public, use Gravity Forms' method |
||
25 | * @todo: If/when the method is public, remove the unneeded copied code. |
||
26 | */ |
||
27 | if( $reflection->isPublic() ) { |
||
|
|||
28 | return parent::copy_post_image( $url, $post_id ); |
||
29 | } |
||
30 | |||
31 | /** |
||
32 | * Original Gravity Forms code below: |
||
33 | * ================================== |
||
34 | */ |
||
35 | |||
36 | $time = current_time( 'mysql' ); |
||
37 | |||
38 | if ( $post = get_post( $post_id ) ) { |
||
39 | if ( substr( $post->post_date, 0, 4 ) > 0 ) { |
||
40 | $time = $post->post_date; |
||
41 | } |
||
42 | } |
||
43 | |||
44 | //making sure there is a valid upload folder |
||
45 | if ( ! ( ( $upload_dir = wp_upload_dir( $time ) ) && false === $upload_dir['error'] ) ) { |
||
46 | return false; |
||
47 | } |
||
48 | |||
49 | $form_id = get_post_meta( $post_id, '_gform-form-id', true ); |
||
50 | |||
51 | /** |
||
52 | * Filter the media upload location. |
||
53 | * |
||
54 | * @param array $upload_dir The current upload directory’s path and url. |
||
55 | * @param int $form_id The ID of the form currently being processed. |
||
56 | * @param int $post_id The ID of the post created from the entry currently being processed. |
||
57 | */ |
||
58 | $upload_dir = gf_apply_filters( 'gform_media_upload_path', $form_id, $upload_dir, $form_id, $post_id ); |
||
59 | |||
60 | if ( ! file_exists( $upload_dir['path'] ) ) { |
||
61 | if ( ! wp_mkdir_p( $upload_dir['path'] ) ) { |
||
62 | return false; |
||
63 | } |
||
64 | } |
||
65 | |||
66 | $name = basename( $url ); |
||
67 | $filename = wp_unique_filename( $upload_dir['path'], $name ); |
||
68 | |||
69 | // the destination path |
||
70 | $new_file = $upload_dir['path'] . "/$filename"; |
||
71 | |||
72 | // the source path |
||
73 | $y = substr( $time, 0, 4 ); |
||
74 | $m = substr( $time, 5, 2 ); |
||
75 | $target_root = RGFormsModel::get_upload_path( $form_id ) . "/$y/$m/"; |
||
76 | $target_root_url = RGFormsModel::get_upload_url( $form_id ) . "/$y/$m/"; |
||
77 | $upload_root_info = array( 'path' => $target_root, 'url' => $target_root_url ); |
||
78 | $upload_root_info = gf_apply_filters( 'gform_upload_path', $form_id, $upload_root_info, $form_id ); |
||
79 | $path = str_replace( $upload_root_info['url'], $upload_root_info['path'], $url ); |
||
80 | |||
81 | // copy the file to the destination path |
||
82 | if ( ! copy( $path, $new_file ) ) { |
||
83 | return false; |
||
84 | } |
||
85 | |||
86 | // Set correct file permissions |
||
87 | $stat = stat( dirname( $new_file ) ); |
||
88 | $perms = $stat['mode'] & 0000666; |
||
89 | @ chmod( $new_file, $perms ); |
||
90 | |||
91 | // Compute the URL |
||
92 | $url = $upload_dir['url'] . "/$filename"; |
||
93 | |||
94 | if ( is_multisite() ) { |
||
95 | delete_transient( 'dirsize_cache' ); |
||
96 | } |
||
97 | |||
98 | $type = wp_check_filetype( $new_file ); |
||
99 | |||
100 | return array( 'file' => $new_file, 'url' => $url, 'type' => $type['type'] ); |
||
101 | |||
102 | } |
||
103 | |||
187 | } |