Conditions | 13 |
Paths | 449 |
Total Lines | 67 |
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 |
||
18 | function archives_shortcode( $atts ) { |
||
19 | if ( is_feed() ) { |
||
20 | return '[archives]'; |
||
21 | } |
||
22 | |||
23 | global $allowedposttags; |
||
24 | |||
25 | $default_atts = array( |
||
26 | 'type' => 'postbypost', |
||
27 | 'limit' => '', |
||
28 | 'format' => 'html', |
||
29 | 'showcount' => false, |
||
30 | 'before' => '', |
||
31 | 'after' => '', |
||
32 | 'order' => 'desc', |
||
33 | ); |
||
34 | |||
35 | $attr = shortcode_atts( $default_atts, $atts, 'archives' ); |
||
36 | |||
37 | if ( ! in_array( $attr['type'], array( 'yearly', 'monthly', 'daily', 'weekly', 'postbypost' ), true ) ) { |
||
38 | $attr['type'] = 'postbypost'; |
||
39 | } |
||
40 | |||
41 | if ( ! in_array( $attr['format'], array( 'html', 'option', 'custom' ), true ) ) { |
||
42 | $attr['format'] = 'html'; |
||
43 | } |
||
44 | |||
45 | $limit = intval( $attr['limit'] ); |
||
46 | // A Limit of 0 makes no sense so revert back to the default. |
||
47 | if ( empty( $limit ) ) { |
||
48 | $limit = ''; |
||
49 | } |
||
50 | |||
51 | $showcount = ( false !== $attr['showcount'] && 'false' !== $attr['showcount'] ) ? true : false; |
||
52 | $before = wp_kses( $attr['before'], $allowedposttags ); |
||
53 | $after = wp_kses( $attr['after'], $allowedposttags ); |
||
54 | |||
55 | // Get the archives. |
||
56 | $archives = wp_get_archives( |
||
57 | array( |
||
58 | 'type' => $attr['type'], |
||
59 | 'limit' => $limit, |
||
60 | 'format' => $attr['format'], |
||
61 | 'echo' => false, |
||
62 | 'show_post_count' => $showcount, |
||
63 | 'before' => $before, |
||
64 | 'after' => $after, |
||
65 | ) |
||
66 | ); |
||
67 | |||
68 | if ( 'asc' === $attr['order'] ) { |
||
69 | $archives = implode( "\n", array_reverse( explode( "\n", $archives ) ) ); |
||
70 | } |
||
71 | |||
72 | // Check to see if there are any archives. |
||
73 | if ( empty( $archives ) ) { |
||
74 | $archives = '<p>' . __( 'Your blog does not currently have any published posts.', 'jetpack' ) . '</p>'; |
||
75 | } elseif ( 'option' === $attr['format'] ) { |
||
76 | $is_amp = class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request(); |
||
77 | $change_attribute = $is_amp ? 'on="change:AMP.navigateTo(url=event.value)"' : 'onchange="document.location.href=this.options[this.selectedIndex].value;"'; |
||
78 | $archives = '<select name="archive-dropdown" ' . $change_attribute . '><option value="' . get_permalink() . '">--</option>' . $archives . '</select>'; |
||
79 | } elseif ( 'html' === $attr['format'] ) { |
||
80 | $archives = '<ul>' . $archives . '</ul>'; |
||
81 | } |
||
82 | |||
83 | return $archives; |
||
84 | } |
||
85 |