| Conditions | 10 |
| Paths | 2 |
| Total Lines | 45 |
| Code Lines | 18 |
| 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 |
||
| 119 | public function get_gallery( $post ) { |
||
| 120 | |||
| 121 | // @todo: the `gallery` shortcode has an `exclude` attribute which isn't |
||
| 122 | // checked at the moment. |
||
| 123 | |||
| 124 | // Prepare the return value. |
||
| 125 | $ids = array(); |
||
| 126 | |||
| 127 | // As the above for images in galleries. |
||
| 128 | // Code inspired by http://wordpress.stackexchange.com/questions/80408/how-to-get-page-post-gallery-attachment-images-in-order-they-are-set-in-backend |
||
| 129 | $pattern = get_shortcode_regex(); |
||
| 130 | |||
| 131 | if ( preg_match_all( '/' . $pattern . '/s', $post->post_content, $matches ) |
||
| 132 | && array_key_exists( 2, $matches ) |
||
| 133 | && in_array( 'gallery', $matches[2] ) |
||
| 134 | ) { |
||
| 135 | |||
| 136 | $keys = array_keys( $matches[2], 'gallery' ); |
||
| 137 | |||
| 138 | foreach ( $keys as $key ) { |
||
| 139 | $atts = shortcode_parse_atts( $matches[3][ $key ] ); |
||
| 140 | |||
| 141 | if ( is_array( $atts ) && array_key_exists( 'ids', $atts ) ) { |
||
| 142 | // gallery images insert explicitly by their ids. |
||
| 143 | |||
| 144 | foreach ( explode( ',', $atts['ids'] ) as $attachment_id ) { |
||
| 145 | // Since we do not check for actual image existence |
||
| 146 | // when generating the json content, check it now. |
||
| 147 | if ( wp_get_attachment_image_src( $attachment_id, 'full' ) ) { |
||
| 148 | $ids[ $attachment_id ] = true; |
||
| 149 | } |
||
| 150 | } |
||
| 151 | } else { |
||
| 152 | // gallery shortcode with no ids uses all the images |
||
| 153 | // attached to the post. |
||
| 154 | $images = get_attached_media( 'image', $post->ID ); |
||
| 155 | foreach ( $images as $attachment ) { |
||
| 156 | $ids[ $attachment->ID ] = true; |
||
| 157 | } |
||
| 158 | } |
||
| 159 | } |
||
| 160 | } |
||
| 161 | |||
| 162 | return array_keys( $ids ); |
||
| 163 | } |
||
| 164 | |||
| 166 |