Conditions | 13 |
Paths | 528 |
Total Lines | 119 |
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 //phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
||
84 | public function shortcode_callback( $attr ) { |
||
85 | $post_id = get_the_ID(); |
||
86 | |||
87 | $attr = shortcode_atts( |
||
88 | array( |
||
89 | 'trans' => 'fade', |
||
90 | 'order' => 'ASC', |
||
91 | 'orderby' => 'menu_order ID', |
||
92 | 'id' => $post_id, |
||
93 | 'include' => '', |
||
94 | 'exclude' => '', |
||
95 | 'autostart' => true, |
||
96 | 'size' => '', |
||
97 | ), |
||
98 | $attr, |
||
99 | 'slideshow' |
||
100 | ); |
||
101 | |||
102 | if ( 'rand' === strtolower( $attr['order'] ) ) { |
||
103 | $attr['orderby'] = 'none'; |
||
104 | } |
||
105 | |||
106 | $attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] ); |
||
107 | if ( ! $attr['orderby'] ) { |
||
108 | $attr['orderby'] = 'menu_order ID'; |
||
109 | } |
||
110 | |||
111 | if ( ! $attr['size'] ) { |
||
112 | $attr['size'] = 'full'; |
||
113 | } |
||
114 | |||
115 | // Don't restrict to the current post if include. |
||
116 | $post_parent = ( empty( $attr['include'] ) ) ? intval( $attr['id'] ) : null; |
||
117 | |||
118 | $attachments = get_posts( |
||
119 | array( |
||
120 | 'post_status' => 'inherit', |
||
121 | 'post_type' => 'attachment', |
||
122 | 'post_mime_type' => 'image', |
||
123 | 'posts_per_page' => - 1, |
||
124 | 'post_parent' => $post_parent, |
||
125 | 'order' => $attr['order'], |
||
126 | 'orderby' => $attr['orderby'], |
||
127 | 'include' => $attr['include'], |
||
128 | 'exclude' => $attr['exclude'], |
||
129 | 'suppress_filters' => false, |
||
130 | ) |
||
131 | ); |
||
132 | |||
133 | if ( count( $attachments ) < 1 ) { |
||
134 | return false; |
||
135 | } |
||
136 | |||
137 | $gallery_instance = sprintf( 'gallery-%d-%d', $attr['id'], ++$this->instance_count ); |
||
138 | |||
139 | $gallery = array(); |
||
140 | foreach ( $attachments as $attachment ) { |
||
141 | $attachment_image_src = wp_get_attachment_image_src( $attachment->ID, $attr['size'] ); |
||
142 | $attachment_image_src = $attachment_image_src[0]; // [url, width, height]. |
||
143 | $attachment_image_title = get_the_title( $attachment->ID ); |
||
144 | $attachment_image_alt = get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ); |
||
145 | /** |
||
146 | * Filters the Slideshow slide caption. |
||
147 | * |
||
148 | * @module shortcodes |
||
149 | * |
||
150 | * @since 2.3.0 |
||
151 | * |
||
152 | * @param string wptexturize( strip_tags( $attachment->post_excerpt ) ) Post excerpt. |
||
153 | * @param string $attachment ->ID Attachment ID. |
||
154 | */ |
||
155 | $caption = apply_filters( 'jetpack_slideshow_slide_caption', wptexturize( wp_strip_all_tags( $attachment->post_excerpt ) ), $attachment->ID ); |
||
156 | |||
157 | $gallery[] = (object) array( |
||
158 | 'src' => (string) esc_url_raw( $attachment_image_src ), |
||
159 | 'id' => (string) $attachment->ID, |
||
160 | 'title' => (string) esc_attr( $attachment_image_title ), |
||
161 | 'alt' => (string) esc_attr( $attachment_image_alt ), |
||
162 | 'caption' => (string) $caption, |
||
163 | 'itemprop' => 'image', |
||
164 | ); |
||
165 | } |
||
166 | |||
167 | $color = Jetpack_Options::get_option( 'slideshow_background_color', 'black' ); |
||
168 | $autostart = $attr['autostart'] ? $attr['autostart'] : 'true'; |
||
169 | $js_attr = array( |
||
170 | 'gallery' => $gallery, |
||
171 | 'selector' => $gallery_instance, |
||
172 | 'trans' => $attr['trans'] ? $attr['trans'] : 'fade', |
||
173 | 'autostart' => $autostart, |
||
174 | 'color' => $color, |
||
175 | ); |
||
176 | |||
177 | // Show a link to the gallery in feeds. |
||
178 | if ( is_feed() ) { |
||
179 | return sprintf( |
||
180 | '<a href="%s">%s</a>', |
||
181 | esc_url( get_permalink( $post_id ) . '#' . $gallery_instance . '-slideshow' ), |
||
182 | esc_html__( 'Click to view slideshow.', 'jetpack' ) |
||
183 | ); |
||
184 | } |
||
185 | |||
186 | if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) { |
||
187 | // Load the styles and use the rendering method from the Slideshow block. |
||
188 | Jetpack_Gutenberg::load_styles_as_required( 'slideshow' ); |
||
189 | |||
190 | $amp_args = array( |
||
191 | 'ids' => wp_list_pluck( $gallery, 'id' ), |
||
192 | ); |
||
193 | |||
194 | if ( 'true' == $autostart ) { // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison -- attribute can be stored as boolean or string. |
||
195 | $amp_args['autoplay'] = true; |
||
196 | } |
||
197 | |||
198 | return Slideshow\render_amp( $amp_args ); |
||
199 | } |
||
200 | |||
201 | return $this->slideshow_js( $js_attr ); |
||
202 | } |
||
203 | |||
326 |