Completed
Push — update/add_grunion_after_feedb... ( 614f06...9f6c1a )
by
unknown
07:04
created

archives.php ➔ archives_shortcode()   F

Complexity

Conditions 13
Paths 449

Size

Total Lines 67

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
nc 449
nop 1
dl 0
loc 67
rs 3.2922
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
/**
3
 * Archives shortcode
4
 *
5
 * @author bubel & nickmomrik
6
 * [archives limit=10]
7
 *
8
 * @package Jetpack
9
 */
10
11
add_shortcode( 'archives', 'archives_shortcode' );
12
13
/**
14
 * Display Archives shortcode.
15
 *
16
 * @param array $atts Shortcode attributes.
17
 */
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