Conditions | 9 |
Paths | 36 |
Total Lines | 78 |
Code Lines | 56 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
25 | public function shortcode( $attr ) { |
||
26 | $post = get_post(); |
||
27 | |||
28 | if ( ! empty( $attr['ids'] ) ) { |
||
29 | // 'ids' is explicitly ordered, unless you specify otherwise. |
||
30 | if ( empty( $attr['orderby'] ) ) { |
||
31 | $attr['orderby'] = 'post__in'; |
||
32 | } |
||
33 | $attr['include'] = $attr['ids']; |
||
34 | } |
||
35 | |||
36 | $atts = shortcode_atts( array( |
||
37 | 'order' => 'ASC', |
||
38 | 'orderby' => 'menu_order ID', |
||
39 | 'id' => $post ? $post->ID : 0, |
||
40 | 'include' => '', |
||
41 | 'exclude' => '', |
||
42 | 'size' => array( $this->args['width'], $this->args['height'] ) |
||
43 | ), $attr, 'gallery' ); |
||
44 | |||
45 | $id = intval( $atts['id'] ); |
||
46 | |||
47 | if ( ! empty( $atts['include'] ) ) { |
||
48 | $attachments = get_posts( array( |
||
49 | 'include' => $atts['include'], |
||
50 | 'post_status' => 'inherit', |
||
51 | 'post_type' => 'attachment', |
||
52 | 'post_mime_type' => 'image', |
||
53 | 'order' => $atts['order'], |
||
54 | 'orderby' => $atts['orderby'], |
||
55 | 'fields' => 'ids', |
||
56 | ) ); |
||
57 | } elseif ( ! empty( $atts['exclude'] ) ) { |
||
58 | $attachments = get_children( array( |
||
59 | 'post_parent' => $id, |
||
60 | 'exclude' => $atts['exclude'], |
||
61 | 'post_status' => 'inherit', |
||
62 | 'post_type' => 'attachment', |
||
63 | 'post_mime_type' => 'image', |
||
64 | 'order' => $atts['order'], |
||
65 | 'orderby' => $atts['orderby'], |
||
66 | 'fields' => 'ids', |
||
67 | ) ); |
||
68 | } else { |
||
69 | $attachments = get_children( array( |
||
70 | 'post_parent' => $id, |
||
71 | 'post_status' => 'inherit', |
||
72 | 'post_type' => 'attachment', |
||
73 | 'post_mime_type' => 'image', |
||
74 | 'order' => $atts['order'], |
||
75 | 'orderby' => $atts['orderby'], |
||
76 | 'fields' => 'ids', |
||
77 | ) ); |
||
78 | } |
||
79 | |||
80 | if ( empty( $attachments ) ) { |
||
81 | return ''; |
||
82 | } |
||
83 | |||
84 | $urls = array(); |
||
85 | foreach ( $attachments as $attachment_id ) { |
||
86 | list( $url, $width, $height ) = wp_get_attachment_image_src( $attachment_id, $atts['size'], true ); |
||
87 | |||
88 | if ( ! $url ) { |
||
89 | continue; |
||
90 | } |
||
91 | |||
92 | $urls[] = array( |
||
93 | 'url' => $url, |
||
94 | 'width' => $width, |
||
95 | 'height' => $height, |
||
96 | ); |
||
97 | } |
||
98 | |||
99 | return $this->render( array( |
||
100 | 'images' => $urls, |
||
101 | ) ); |
||
102 | } |
||
103 | |||
140 |