Conditions | 27 |
Paths | 433 |
Total Lines | 95 |
Code Lines | 78 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
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 |
||
124 | public static function paginate_links( $args = '' ) { |
||
125 | $defaults = array( |
||
126 | 'base' => '%_%', // http://example.com/all_posts.php%_% : %_% is replaced by format (below) |
||
127 | 'format' => '?page=%#%', // ?page=%#% : %#% is replaced by the page number |
||
128 | 'total' => 1, |
||
129 | 'current' => 0, |
||
130 | 'show_all' => false, |
||
131 | 'prev_next' => true, |
||
132 | 'prev_text' => __( '« Previous' ), |
||
133 | 'next_text' => __( 'Next »' ), |
||
134 | 'end_size' => 1, |
||
135 | 'mid_size' => 2, |
||
136 | 'type' => 'array', |
||
137 | 'add_args' => false, // array of query args to add |
||
138 | 'add_fragment' => '', |
||
139 | ); |
||
140 | $args = wp_parse_args( $args, $defaults ); |
||
141 | // Who knows what else people pass in $args |
||
142 | $args['total'] = intval( (int) $args['total'] ); |
||
143 | if ( $args['total'] < 2 ) { |
||
144 | return array(); |
||
145 | } |
||
146 | $args['current'] = (int) $args['current']; |
||
147 | $args['end_size'] = 0 < (int) $args['end_size'] ? (int) $args['end_size'] : 1; // Out of bounds? Make it the default. |
||
148 | $args['mid_size'] = 0 <= (int) $args['mid_size'] ? (int) $args['mid_size'] : 2; |
||
149 | $args['add_args'] = is_array( $args['add_args'] ) ? $args['add_args'] : false; |
||
150 | $page_links = array(); |
||
151 | $dots = false; |
||
152 | if ( $args['prev_next'] && $args['current'] && 1 < $args['current'] ) { |
||
153 | $link = str_replace( '%_%', 2 === absint( $args['current'] ) ? '' : $args['format'], $args['base'] ); |
||
154 | $link = str_replace( '%#%', $args['current'] - 1, $link ); |
||
155 | if ( $args['add_args'] ) { |
||
156 | $link = add_query_arg( $args['add_args'], $link ); |
||
157 | } |
||
158 | $link .= $args['add_fragment']; |
||
159 | $link = untrailingslashit( $link ); |
||
160 | $page_links[] = array( |
||
161 | 'class' => 'prev page-numbers', |
||
162 | 'link' => esc_url( apply_filters( 'paginate_links', $link ) ), |
||
163 | 'title' => $args['prev_text'], |
||
164 | ); |
||
165 | } |
||
166 | for ( $n = 1; $n <= $args['total']; $n++ ) { |
||
167 | $n_display = number_format_i18n( $n ); |
||
168 | if ( absint( $args['current'] ) === $n ) { |
||
169 | $page_links[] = array( |
||
170 | 'class' => 'page-number page-numbers current', |
||
171 | 'title' => $n_display, |
||
172 | 'text' => $n_display, |
||
173 | 'name' => $n_display, |
||
174 | 'current' => true, |
||
175 | ); |
||
176 | $dots = true; |
||
177 | } else { |
||
178 | if ( $args['show_all'] || ( $n <= $args['end_size'] || ( $args['current'] && $n >= $args['current'] - $args['mid_size'] && $n <= $args['current'] + $args['mid_size'] ) || $n > $args['total'] - $args['end_size'] ) ) { |
||
179 | $link = str_replace( '%_%', 1 === absint( $n ) ? '' : $args['format'], $args['base'] ); |
||
180 | $link = str_replace( '%#%', $n, $link ); |
||
181 | $link = trailingslashit( $link ) . ltrim( $args['add_fragment'], '/' ); |
||
182 | if ( $args['add_args'] ) { |
||
183 | $link = rtrim( add_query_arg( $args['add_args'], $link ), '/' ); |
||
184 | } |
||
185 | $link = str_replace( ' ', '+', $link ); |
||
186 | $link = untrailingslashit( $link ); |
||
187 | $page_links[] = array( |
||
188 | 'class' => 'page-number page-numbers', |
||
189 | 'link' => esc_url( apply_filters( 'paginate_links', $link ) ), |
||
190 | 'title' => $n_display, |
||
191 | 'name' => $n_display, |
||
192 | 'current' => absint( $args['current'] ) === $n, |
||
193 | ); |
||
194 | $dots = true; |
||
195 | } elseif ( $dots && ! $args['show_all'] ) { |
||
196 | $page_links[] = array( |
||
197 | 'class' => 'dots', |
||
198 | 'title' => __( '…' ), |
||
199 | ); |
||
200 | $dots = false; |
||
201 | } |
||
202 | } |
||
203 | } |
||
204 | if ( $args['prev_next'] && $args['current'] && ( $args['current'] < $args['total'] || -1 === intval( $args['total'] ) ) ) { |
||
205 | $link = str_replace( '%_%', $args['format'], $args['base'] ); |
||
206 | $link = str_replace( '%#%', $args['current'] + 1, $link ); |
||
207 | if ( $args['add_args'] ) { |
||
208 | $link = add_query_arg( $args['add_args'], $link ); |
||
209 | } |
||
210 | $link = untrailingslashit( trailingslashit( $link ) . $args['add_fragment'] ); |
||
211 | $page_links[] = array( |
||
212 | 'class' => 'next page-numbers', |
||
213 | 'link' => esc_url( apply_filters( 'paginate_links', $link ) ), |
||
214 | 'title' => $args['next_text'], |
||
215 | ); |
||
216 | } |
||
217 | return $page_links; |
||
218 | } |
||
219 | |||
308 |