| 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-author', |
| 11 |
array( |
| 12 |
'attributes' => array( |
| 13 |
'title' => array( |
| 14 |
'type' => 'string', |
| 15 |
'default' => 'Authors', // No translation here |
| 16 |
), |
| 17 |
'order' => array( |
| 18 |
'type' => 'string', |
| 19 |
'default' => 'ASC', |
| 20 |
), |
| 21 |
'orderby' => array( |
| 22 |
'type' => 'string', |
| 23 |
'default' => 'display_name', |
| 24 |
), |
| 25 |
'number' => array( |
| 26 |
'type' => 'number', |
| 27 |
'default' => 5, |
| 28 |
), |
| 29 |
'include' => array( |
| 30 |
'type' => 'string', |
| 31 |
'default' => '', |
| 32 |
), |
| 33 |
'exclude' => array( // phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_exclude -- Required wp_list_authors() parameter, not a WP_Query post__not_in clause. |
| 34 |
'type' => 'string', |
| 35 |
'default' => '', |
| 36 |
), |
| 37 |
'optioncount' => array( |
| 38 |
'type' => 'boolean', |
| 39 |
'default' => false, |
| 40 |
), |
| 41 |
'exclude_admin' => array( |
| 42 |
'type' => 'boolean', |
| 43 |
'default' => false, |
| 44 |
), |
| 45 |
'show_fullname' => array( |
| 46 |
'type' => 'boolean', |
| 47 |
'default' => false, |
| 48 |
), |
| 49 |
'hide_empty' => array( |
| 50 |
'type' => 'boolean', |
| 51 |
'default' => true, |
| 52 |
), |
| 53 |
'style' => array( |
| 54 |
'type' => 'string', |
| 55 |
'default' => 'list', |
| 56 |
), |
| 57 |
'html' => array( |
| 58 |
'type' => 'boolean', |
| 59 |
'default' => true, |
| 60 |
), |
| 61 |
'feed' => array( |
| 62 |
'type' => 'string', |
| 63 |
'default' => '', |
| 64 |
), |
| 65 |
'feed_type' => array( |
| 66 |
'type' => 'string', |
| 67 |
'default' => '', |
| 68 |
), |
| 69 |
'feed_image' => array( |
| 70 |
'type' => 'string', |
| 71 |
'default' => '', |
| 72 |
), |
| 73 |
'is_block' => array( |
| 74 |
'type' => 'boolean', |
| 75 |
'default' => true, |
| 76 |
), |
| 77 |
), |
| 78 |
'render_callback' => 'ew_author_render_shortcode', |
| 79 |
) |
| 80 |
); |
| 81 |
endif; |
| 82 |
|
| 83 |
if ( ! function_exists( 'ew_author_render_shortcode' ) ) : |
| 84 |
add_shortcode( 'ew-author', 'ew_author_render_shortcode' ); |
| 85 |
|
| 86 |
function ew_author_render_shortcode( $atts ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- ew_ is this plugin's abbreviated prefix; wrapped in function_exists() guard. |
| 87 |
|
| 88 |
$atts = shortcode_atts( |
| 89 |
array( |
| 90 |
'title' => 'Authors', |
| 91 |
'order' => 'ASC', |
| 92 |
'orderby' => 'display_name', |
| 93 |
'number' => 5, |
| 94 |
'include' => '', |
| 95 |
'exclude' => '', // phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_exclude -- Required wp_list_authors() parameter. |
| 96 |
'optioncount' => false, |
| 97 |
'exclude_admin' => false, |
| 98 |
'show_fullname' => false, |
| 99 |
'hide_empty' => true, |
| 100 |
'style' => 'list', |
| 101 |
'html' => true, |
| 102 |
'feed' => '', |
| 103 |
'feed_type' => '', |
| 104 |
'feed_image' => '', |
| 105 |
'is_block' => true, |
| 106 |
), |
| 107 |
$atts, |
| 108 |
'ew-author' |
| 109 |
); |
| 110 |
|
| 111 |
$instance = array(); |
| 112 |
|
| 113 |
// Title |
| 114 |
$instance['title'] = ( 'Authors' === $atts['title'] ) |
| 115 |
? esc_html__( 'Authors', 'essential-widgets' ) |
| 116 |
: sanitize_text_field( $atts['title'] ); |
| 117 |
|
| 118 |
// Whitelist order |
| 119 |
$allowed_order = array( 'ASC', 'DESC' ); |
| 120 |
$instance['order'] = in_array( strtoupper( $atts['order'] ), $allowed_order, true ) |
| 121 |
? strtoupper( $atts['order'] ) |
| 122 |
: 'ASC'; |
| 123 |
|
| 124 |
// Whitelist orderby |
| 125 |
$allowed_orderby = array( |
| 126 |
'display_name', |
| 127 |
'user_login', |
| 128 |
'user_nicename', |
| 129 |
'user_email', |
| 130 |
'ID', |
| 131 |
'post_count', |
| 132 |
); |
| 133 |
$instance['orderby'] = in_array( $atts['orderby'], $allowed_orderby, true ) |
| 134 |
? $atts['orderby'] |
| 135 |
: 'display_name'; |
| 136 |
|
| 137 |
// Numbers |
| 138 |
$instance['number'] = absint( $atts['number'] ); |
| 139 |
|
| 140 |
// Allow only IDs (numbers + commas) |
| 141 |
$instance['include'] = preg_replace( '/[^0-9,]/', '', sanitize_text_field( $atts['include'] ) ); |
| 142 |
$instance['exclude'] = preg_replace( '/[^0-9,]/', '', sanitize_text_field( $atts['exclude'] ) ); // phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_exclude -- Required wp_list_authors() parameter. |
| 143 |
|
| 144 |
// Booleans |
| 145 |
$instance['optioncount'] = (bool) $atts['optioncount']; |
| 146 |
$instance['exclude_admin'] = (bool) $atts['exclude_admin']; |
| 147 |
$instance['show_fullname'] = (bool) $atts['show_fullname']; |
| 148 |
$instance['hide_empty'] = (bool) $atts['hide_empty']; |
| 149 |
$instance['html'] = (bool) $atts['html']; |
| 150 |
$instance['is_block'] = (bool) $atts['is_block']; |
| 151 |
|
| 152 |
// Style whitelist |
| 153 |
$allowed_styles = array( 'list', 'dropdown' ); |
| 154 |
$instance['style'] = in_array( $atts['style'], $allowed_styles, true ) |
| 155 |
? $atts['style'] |
| 156 |
: 'list'; |
| 157 |
|
| 158 |
// Feed fields |
| 159 |
$instance['feed'] = sanitize_text_field( $atts['feed'] ); |
| 160 |
$instance['feed_type'] = sanitize_text_field( $atts['feed_type'] ); |
| 161 |
$instance['feed_image'] = sanitize_text_field( $atts['feed_image'] ); |
| 162 |
|
| 163 |
$ew_author = new EW_Authors(); |
| 164 |
|
| 165 |
return $ew_author->shortcode( $instance ); |
| 166 |
} |
| 167 |
endif; |
| 168 |
|