PluginProbe
Essential Widgets / trunk
Essential Widgets vtrunk
3.2.1 3.3 3.2 trunk 1.0.0 1.0.1 1.1 1.2 1.2.1 1.2.2 1.3 1.4 1.4.1 1.5 1.6 1.7 1.7.1 1.7.2 1.7.3 1.7.4 1.8 1.9 2.0 2.1 2.2 All 31 releases
essential-widgets / includes / ew-block / blocks / ew-post / index.php

index.php in Essential Widgets trunk, at includes/ew-block/blocks/ew-post/index.php

120 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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-post',
11 array(
12 'attributes' => array(
13 'title' => array(
14 'type' => 'string',
15 'default' => 'Posts', // No translation here
16 ),
17 'post_type' => array(
18 'type' => 'array',
19 'default' => array( 'post' ),
20 'items' => array( 'type' => 'string' ),
21 ),
22 'number' => array(
23 'type' => 'number',
24 'default' => 10,
25 ),
26 'order' => array(
27 'type' => 'string',
28 'default' => 'desc',
29 ),
30 'orderby' => array(
31 'type' => 'string',
32 'default' => 'date',
33 ),
34 'show_date' => array(
35 'type' => 'boolean',
36 'default' => false,
37 ),
38 'show_author' => array(
39 'type' => 'boolean',
40 'default' => false,
41 ),
42 'is_block' => array(
43 'type' => 'boolean',
44 'default' => true,
45 ),
46 ),
47 'render_callback' => 'ew_post_render_shortcode',
48 )
49 );
50 endif;
51
52 if ( ! function_exists( 'ew_post_render_shortcode' ) ) :
53 add_shortcode( 'ew-post', 'ew_post_render_shortcode' );
54 function ew_post_render_shortcode( $atts ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- ew_ is this plugin's abbreviated prefix; wrapped in function_exists() guard.
55 $instance['title'] = isset( $atts['title'] ) && 'Posts' === $atts['title']
56 ? esc_html__( 'Posts', 'essential-widgets' )
57 : sanitize_text_field( $atts['title'] );
58 $instance['post_type'] = array_map('sanitize_key', (array) $atts['post_type']);
59 $instance['number'] = intval($atts['number']);
60 $instance['show_date'] = !empty($atts['show_date']);
61 $instance['show_author'] = !empty($atts['show_author']);
62 $instance['order'] = ( isset( $atts['order'] ) && in_array( $atts['order'], ['ASC','DESC'], true ) )
63 ? $atts['order']
64 : 'DESC';
65
66 $instance['orderby'] = ( isset( $atts['orderby'] ) && in_array( $atts['orderby'], ['author','name','none','type','date','ID','modified','parent','comment_count','menu_order','title'], true ) )
67 ? $atts['orderby']
68 : 'date';
69 $instance['is_block'] = !empty($atts['is_block']);
70
71 $ew_post = new EW_Posts();
72
73 return $ew_post->shortcode( $instance );
74 }
75 endif;
76
77 if ( ! function_exists( 'ew_post_list' ) ) :
78
79 /**
80 * Get nav menus for select option via custom REST API!
81 *
82 * @return array|null Array of nav menus object with label and value pair, * or null if none.
83 */
84 function ew_post_list() { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- ew_ is this plugin's abbreviated prefix; wrapped in function_exists() guard.
85 $post_types = get_post_types(
86 array(
87 'public' => true,
88 'hierarchical' => false,
89 ),
90 'objects'
91 );
92
93 $post_list = array();
94
95 foreach ( $post_types as $post ) {
96 $object = new stdClass();
97 $object->label = $post->labels->singular_name;
98 $object->value = $post->name;
99 $post_list[] = $object;
100 }
101
102 return $post_list;
103 }
104
105 add_action(
106 'rest_api_init',
107 function () {
108 register_rest_route(
109 'ew-rest/v1',
110 'ew-post-list',
111 array(
112 'methods' => 'GET',
113 'callback' => 'ew_post_list',
114 'permission_callback' => '__return_true', // for public use
115 )
116 );
117 }
118 );
119 endif;
120