| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; // Exit if accessed directly. |
| 4 |
} |
| 5 |
|
| 6 |
// Register block and shortcode |
| 7 |
if ( function_exists( 'register_block_type' ) ) : |
| 8 |
register_block_type( |
| 9 |
'ew-block/ew-archive', |
| 10 |
array( |
| 11 |
'attributes' => array( |
| 12 |
'title' => array( |
| 13 |
'type' => 'string', |
| 14 |
'default' => 'Archives', |
| 15 |
), |
| 16 |
'limit' => array( |
| 17 |
'type' => 'number', |
| 18 |
'default' => 10, |
| 19 |
), |
| 20 |
'type' => array( |
| 21 |
'type' => 'string', |
| 22 |
'default' => 'monthly', |
| 23 |
), |
| 24 |
'post_type' => array( |
| 25 |
'type' => 'string', |
| 26 |
'default' => 'post', |
| 27 |
), |
| 28 |
'order' => array( |
| 29 |
'type' => 'string', |
| 30 |
'default' => 'asc', |
| 31 |
), |
| 32 |
'format' => array( |
| 33 |
'type' => 'string', |
| 34 |
'default' => 'html', |
| 35 |
), |
| 36 |
'before' => array( |
| 37 |
'type' => 'string', |
| 38 |
'default' => '', |
| 39 |
), |
| 40 |
'after' => array( |
| 41 |
'type' => 'string', |
| 42 |
'default' => '', |
| 43 |
), |
| 44 |
'show_post_count' => array( |
| 45 |
'type' => 'boolean', |
| 46 |
'default' => false, |
| 47 |
), |
| 48 |
'is_block' => array( |
| 49 |
'type' => 'boolean', |
| 50 |
'default' => true, |
| 51 |
), |
| 52 |
), |
| 53 |
'render_callback' => 'ew_archive_render_shortcode', |
| 54 |
) |
| 55 |
); |
| 56 |
endif; |
| 57 |
|
| 58 |
// Shortcode callback |
| 59 |
if ( ! function_exists( 'ew_archive_render_shortcode' ) ) : |
| 60 |
add_shortcode( 'ew-archive', 'ew_archive_render_shortcode' ); |
| 61 |
function ew_archive_render_shortcode( $atts ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- ew_ is this plugin's abbreviated prefix; wrapped in function_exists() guard. |
| 62 |
// Sanitize all attributes |
| 63 |
$instance = array( |
| 64 |
'title' => sanitize_text_field( $atts['title'] ?? '' ), |
| 65 |
'limit' => intval( $atts['limit'] ?? 10 ), |
| 66 |
'type' => sanitize_key( $atts['type'] ?? 'monthly' ), |
| 67 |
'post_type' => sanitize_key( $atts['post_type'] ?? 'post' ), |
| 68 |
'order' => sanitize_key( $atts['order'] ?? 'asc' ), |
| 69 |
'format' => sanitize_key( $atts['format'] ?? 'html' ), |
| 70 |
'before' => wp_kses_post( $atts['before'] ?? '' ), |
| 71 |
'after' => wp_kses_post( $atts['after'] ?? '' ), |
| 72 |
'show_post_count' => ! empty( $atts['show_post_count'] ) ? 1 : 0, |
| 73 |
'is_block' => ! empty( $atts['is_block'] ) ? true : false, |
| 74 |
); |
| 75 |
|
| 76 |
// Whitelist options |
| 77 |
$allowed_types = array( 'alpha', 'daily', 'monthly', 'postbypost', 'weekly', 'yearly' ); |
| 78 |
$allowed_orders = array( 'ASC', 'DESC', 'asc', 'desc' ); |
| 79 |
$allowed_formats = array( 'custom', 'html', 'option' ); |
| 80 |
|
| 81 |
$instance['type'] = in_array( $instance['type'], $allowed_types, true ) ? $instance['type'] : 'monthly'; |
| 82 |
$instance['order'] = in_array( $instance['order'], $allowed_orders, true ) ? $instance['order'] : 'asc'; |
| 83 |
$instance['format'] = in_array( $instance['format'], $allowed_formats, true ) ? $instance['format'] : 'html'; |
| 84 |
|
| 85 |
$ew_archive = new EW_Archives(); |
| 86 |
|
| 87 |
return $ew_archive->shortcode( $instance ); |
| 88 |
} |
| 89 |
endif; |
| 90 |
|