| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
exit; // Exit if accessed directly. |
| 5 |
} |
| 6 |
|
| 7 |
// Hook the post rendering to the block |
| 8 |
if ( function_exists( 'register_block_type' ) ) : |
| 9 |
register_block_type( |
| 10 |
'ew-block/ew-category', |
| 11 |
array( |
| 12 |
'attributes' => array( |
| 13 |
'title' => array( |
| 14 |
'type' => 'string', |
| 15 |
'default' => 'Categories', // No translation here |
| 16 |
), |
| 17 |
'taxonomy' => array( |
| 18 |
'type' => 'string', |
| 19 |
'default' => 'category', |
| 20 |
), |
| 21 |
'style' => array( |
| 22 |
'type' => 'string', |
| 23 |
'default' => '', |
| 24 |
), |
| 25 |
'include' => array( |
| 26 |
'type' => 'string', |
| 27 |
'default' => '', |
| 28 |
), |
| 29 |
'exclude' => array( // phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_exclude -- Required wp_list_categories() parameter, not a WP_Query post__not_in clause. |
| 30 |
'type' => 'string', |
| 31 |
'default' => '', |
| 32 |
), |
| 33 |
'exclude_tree' => array( |
| 34 |
'type' => 'string', |
| 35 |
'default' => '', |
| 36 |
), |
| 37 |
'child_of' => array( |
| 38 |
'type' => 'string', |
| 39 |
'default' => '', |
| 40 |
), |
| 41 |
'current_category' => array( |
| 42 |
'type' => 'string', |
| 43 |
'default' => '', |
| 44 |
), |
| 45 |
'search' => array( |
| 46 |
'type' => 'string', |
| 47 |
'default' => '', |
| 48 |
), |
| 49 |
'hierarchical' => array( |
| 50 |
'type' => 'boolean', |
| 51 |
'default' => true, |
| 52 |
), |
| 53 |
'hide_empty' => array( |
| 54 |
'type' => 'boolean', |
| 55 |
'default' => true, |
| 56 |
), |
| 57 |
'order' => array( |
| 58 |
'type' => 'string', |
| 59 |
'default' => 'ASC', |
| 60 |
), |
| 61 |
'orderby' => array( |
| 62 |
'type' => 'string', |
| 63 |
'default' => 'name', |
| 64 |
), |
| 65 |
'depth' => array( |
| 66 |
'type' => 'number', |
| 67 |
'default' => 0, |
| 68 |
), |
| 69 |
'number' => array( |
| 70 |
'type' => 'number', |
| 71 |
'default' => 10, |
| 72 |
), |
| 73 |
'feed' => array( |
| 74 |
'type' => 'string', |
| 75 |
'default' => '', |
| 76 |
), |
| 77 |
'feed_type' => array( |
| 78 |
'type' => 'string', |
| 79 |
'default' => '', |
| 80 |
), |
| 81 |
'feed_image' => array( |
| 82 |
'type' => 'string', |
| 83 |
'default' => '', |
| 84 |
), |
| 85 |
'use_desc_for_title' => array( |
| 86 |
'type' => 'boolean', |
| 87 |
'default' => false, |
| 88 |
), |
| 89 |
'show_count' => array( |
| 90 |
'type' => 'boolean', |
| 91 |
'default' => false, |
| 92 |
), |
| 93 |
'is_block' => array( |
| 94 |
'type' => 'boolean', |
| 95 |
'default' => true, |
| 96 |
), |
| 97 |
), |
| 98 |
'render_callback' => 'ew_category_render_shortcode', |
| 99 |
) |
| 100 |
); |
| 101 |
endif; |
| 102 |
|
| 103 |
if ( ! function_exists( 'ew_category_render_shortcode' ) ) : |
| 104 |
add_shortcode( 'ew-category', 'ew_category_render_shortcode' ); |
| 105 |
function ew_category_render_shortcode( $atts ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- ew_ is this plugin's abbreviated prefix; wrapped in function_exists() guard. |
| 106 |
$instance = array(); |
| 107 |
|
| 108 |
// Title |
| 109 |
$instance['title'] = isset( $atts['title'] ) && 'Categories' === $atts['title'] |
| 110 |
? esc_html__( 'Categories', 'essential-widgets' ) |
| 111 |
: sanitize_text_field( $atts['title'] ); |
| 112 |
|
| 113 |
// Sanitize & validate inputs |
| 114 |
$instance['taxonomy'] = sanitize_key( $atts['taxonomy'] ?? 'category' ); |
| 115 |
$instance['style'] = in_array( $atts['style'] ?? 'list', ['list','none'], true ) ? $atts['style'] : 'list'; |
| 116 |
$instance['include'] = preg_replace('/[^0-9,]/', '', $atts['include'] ?? ''); |
| 117 |
$instance['exclude'] = preg_replace('/[^0-9,]/', '', $atts['exclude'] ?? ''); // phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_exclude -- Required wp_list_categories() parameter. |
| 118 |
$instance['exclude_tree'] = preg_replace('/[^0-9,]/', '', $atts['exclude_tree'] ?? ''); |
| 119 |
$instance['child_of'] = absint( $atts['child_of'] ?? 0 ); |
| 120 |
$instance['current_category'] = absint( $atts['current_category'] ?? 0 ); |
| 121 |
$instance['search'] = sanitize_text_field( $atts['search'] ?? '' ); |
| 122 |
$instance['hierarchical'] = isset( $atts['hierarchical'] ) ? (bool) $atts['hierarchical'] : true; |
| 123 |
$instance['hide_empty'] = isset( $atts['hide_empty'] ) ? (bool) $atts['hide_empty'] : true; |
| 124 |
$instance['order'] = in_array( $atts['order'] ?? 'ASC', ['ASC','DESC'], true ) ? $atts['order'] : 'ASC'; |
| 125 |
$instance['orderby'] = in_array( $atts['orderby'] ?? 'name', ['count','ID','name','slug','term_group'], true ) ? $atts['orderby'] : 'name'; |
| 126 |
$instance['depth'] = absint( $atts['depth'] ?? 0 ); |
| 127 |
$instance['number'] = absint( $atts['number'] ?? 10 ); |
| 128 |
$instance['feed'] = sanitize_text_field( $atts['feed'] ?? '' ); |
| 129 |
$feed_type = $atts['feed_type'] ?? ''; |
| 130 |
$instance['feed_type'] = in_array( $feed_type, ['', 'atom','rdf','rss','rss2'], true ) ? $feed_type : ''; |
| 131 |
$instance['feed_image'] = esc_url_raw( $atts['feed_image'] ?? '' ); |
| 132 |
$instance['use_desc_for_title'] = isset( $atts['use_desc_for_title'] ) ? (bool) $atts['use_desc_for_title'] : false; |
| 133 |
$instance['show_count'] = isset( $atts['show_count'] ) ? (bool) $atts['show_count'] : false; |
| 134 |
$instance['is_block'] = isset( $atts['is_block'] ) ? (bool) $atts['is_block'] : true; |
| 135 |
|
| 136 |
// Generate output via widget shortcode method |
| 137 |
$ew_category = new EW_Categories(); |
| 138 |
return $ew_category->shortcode( $instance ); |
| 139 |
} |
| 140 |
endif; |
| 141 |
|