| 1 |
<?php |
| 2 |
|
| 3 |
// Hook the post rendering to the block |
| 4 |
if ( function_exists( 'register_block_type' ) ) : |
| 5 |
register_block_type( |
| 6 |
'ew-block/ew-post', |
| 7 |
array( |
| 8 |
'attributes' => array( |
| 9 |
'title' => array( |
| 10 |
'type' => 'string', |
| 11 |
'default' => esc_html__( 'Posts', 'essential-widgets' ), |
| 12 |
), |
| 13 |
'post_type' => array( |
| 14 |
'type' => 'array', |
| 15 |
'default' => array( 'post' ), |
| 16 |
'items' => array( 'type' => 'string' ), |
| 17 |
), |
| 18 |
'number' => array( |
| 19 |
'type' => 'number', |
| 20 |
'default' => 10, |
| 21 |
), |
| 22 |
'order' => array( |
| 23 |
'type' => 'string', |
| 24 |
'default' => 'desc', |
| 25 |
), |
| 26 |
'orderby' => array( |
| 27 |
'type' => 'string', |
| 28 |
'default' => 'date', |
| 29 |
), |
| 30 |
'show_date' => array( |
| 31 |
'type' => 'boolean', |
| 32 |
'default' => false, |
| 33 |
), |
| 34 |
'show_author' => array( |
| 35 |
'type' => 'boolean', |
| 36 |
'default' => false, |
| 37 |
), |
| 38 |
'is_block' => array( |
| 39 |
'type' => 'boolean', |
| 40 |
'default' => true, |
| 41 |
), |
| 42 |
), |
| 43 |
'render_callback' => 'ew_post_render_shortcode', |
| 44 |
) |
| 45 |
); |
| 46 |
endif; |
| 47 |
|
| 48 |
if ( ! function_exists( 'ew_post_render_shortcode' ) ) : |
| 49 |
add_shortcode( 'ew-post', 'ew_post_render_shortcode' ); |
| 50 |
function ew_post_render_shortcode( $atts ) { |
| 51 |
$instance['title'] = $atts['title']; |
| 52 |
$instance['post_type'] = $atts['post_type']; |
| 53 |
$instance['number'] = $atts['number']; |
| 54 |
$instance['show_date'] = $atts['show_date']; |
| 55 |
$instance['show_author'] = $atts['show_author']; |
| 56 |
$instance['order'] = $atts['order']; |
| 57 |
$instance['orderby'] = $atts['orderby']; |
| 58 |
$instance['is_block'] = $atts['is_block']; |
| 59 |
|
| 60 |
$ew_post = new EW_Posts(); |
| 61 |
|
| 62 |
return $ew_post->shortcode( $instance ); |
| 63 |
} |
| 64 |
endif; |
| 65 |
|
| 66 |
if ( ! function_exists( 'ew_post_list' ) ) : |
| 67 |
|
| 68 |
/** |
| 69 |
* Get nav menus for select option via custom REST API! |
| 70 |
* |
| 71 |
* @return array|null Array of nav menus object with label and value pair, * or null if none. |
| 72 |
*/ |
| 73 |
function ew_post_list() { |
| 74 |
$post_types = get_post_types( |
| 75 |
array( |
| 76 |
'public' => true, |
| 77 |
'hierarchical' => false, |
| 78 |
), |
| 79 |
'objects' |
| 80 |
); |
| 81 |
|
| 82 |
$post_list = array(); |
| 83 |
|
| 84 |
foreach ( $post_types as $post ) { |
| 85 |
$object = new stdClass(); |
| 86 |
$object->label = $post->labels->singular_name; |
| 87 |
$object->value = $post->name; |
| 88 |
$post_list[] = $object; |
| 89 |
} |
| 90 |
|
| 91 |
return $post_list; |
| 92 |
} |
| 93 |
|
| 94 |
add_action( |
| 95 |
'rest_api_init', |
| 96 |
function () { |
| 97 |
register_rest_route( |
| 98 |
'ew-rest/v1', |
| 99 |
'ew-post-list', |
| 100 |
array( |
| 101 |
'methods' => 'GET', |
| 102 |
'callback' => 'ew_post_list', |
| 103 |
'permission_callback' => '__return_true', // for public use |
| 104 |
) |
| 105 |
); |
| 106 |
} |
| 107 |
); |
| 108 |
endif; |
| 109 |
|