| 1 |
<?php |
| 2 |
/** |
| 3 |
* Archives shortcode |
| 4 |
* |
| 5 |
* @author bubel & nickmomrik |
| 6 |
* [archives limit=10] |
| 7 |
* |
| 8 |
* @package automattic/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 = (int) $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 |
|