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-page / index.php

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

223 lines 6.8 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-page',
11 array(
12 'attributes' => array(
13 'title' => array(
14 'type' => 'string',
15 'default' => 'Pages', // No translation here
16 ),
17 'post_type' => array(
18 'type' => 'string',
19 'default' => 'page',
20 ),
21 'depth' => array(
22 'type' => 'number',
23 'default' => 0,
24 ),
25 'number' => array(
26 'type' => 'number',
27 'default' => 10,
28 ),
29 'offset' => array(
30 'type' => 'string',
31 'default' => '',
32 ),
33 'child_of' => array(
34 'type' => 'string',
35 'default' => '',
36 ),
37 'include' => array(
38 'type' => 'string',
39 'default' => '',
40 ),
41 'exclude' => array( // phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_exclude -- Required wp_list_pages() parameter, not a WP_Query post__not_in clause.
42 'type' => 'string',
43 'default' => '',
44 ),
45 'exclude_tree' => array(
46 'type' => 'string',
47 'default' => '',
48 ),
49
50 'meta_key' => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- Required wp_list_pages() parameter.
51 'type' => 'string',
52 'default' => '',
53 ),
54 'meta_value' => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value -- Required wp_list_pages() parameter.
55 'type' => 'string',
56 'default' => '',
57 ),
58 'authors' => array(
59 'type' => 'string',
60 'default' => '',
61 ),
62 'link_before' => array(
63 'type' => 'string',
64 'default' => '',
65 ),
66 'link_after' => array(
67 'type' => 'string',
68 'default' => '',
69 ),
70 'show_date' => array(
71 'type' => 'string',
72 'default' => '',
73 ),
74 'hierarchical' => array(
75 'type' => 'boolean',
76 'default' => true,
77 ),
78 'sort_column' => array(
79 'type' => 'string',
80 'default' => 'post_title',
81 ),
82 'sort_order' => array(
83 'type' => 'string',
84 'default' => 'ASC',
85 ),
86 'date_format' => array(
87 'type' => 'string',
88 'default' => '',
89 ),
90 'is_block' => array(
91 'type' => 'boolean',
92 'default' => true,
93 ),
94 ),
95 'render_callback' => 'ew_page_render_shortcode',
96 )
97 );
98 endif;
99
100 if ( ! function_exists( 'ew_page_render_shortcode' ) ) :
101 add_shortcode( 'ew-page', 'ew_page_render_shortcode' );
102
103 function ew_page_render_shortcode( $atts ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- ew_ is this plugin's abbreviated prefix; wrapped in function_exists() guard.
104
105 $atts = shortcode_atts(
106 array(
107 'title' => 'Pages',
108 'post_type' => 'page',
109 'depth' => 0,
110 'number' => 10,
111 'offset' => 0,
112 'child_of' => '',
113 'include' => '',
114 'exclude' => '', // phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_exclude -- Required wp_list_pages() parameter.
115 'exclude_tree' => '',
116 'meta_key' => '', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- Required wp_list_pages() parameter.
117 'meta_value' => '', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value -- Required wp_list_pages() parameter.
118 'authors' => '',
119 'link_before' => '',
120 'link_after' => '',
121 'show_date' => '',
122 'hierarchical' => true,
123 'sort_column' => 'post_title',
124 'sort_order' => 'ASC',
125 'date_format' => '',
126 'is_block' => true,
127 ),
128 $atts,
129 'ew-page'
130 );
131
132 $instance = array();
133
134 $instance['title'] = ( 'Pages' === $atts['title'] )
135 ? esc_html__( 'Pages', 'essential-widgets' )
136 : sanitize_text_field( $atts['title'] );
137
138 $instance['post_type'] = sanitize_key( $atts['post_type'] );
139
140 $instance['depth'] = absint( $atts['depth'] );
141 $instance['number'] = absint( $atts['number'] );
142 $instance['offset'] = absint( $atts['offset'] );
143
144 $instance['child_of'] = absint( $atts['child_of'] );
145 $instance['include'] = sanitize_text_field( $atts['include'] );
146 $instance['exclude'] = sanitize_text_field( $atts['exclude'] ); // phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_exclude -- Required wp_list_pages() parameter.
147 $instance['exclude_tree'] = sanitize_text_field( $atts['exclude_tree'] );
148
149 $instance['meta_key'] = sanitize_key( $atts['meta_key'] ); // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- Required wp_list_pages() parameter.
150 $instance['meta_value'] = sanitize_text_field( $atts['meta_value'] ); // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value -- Required wp_list_pages() parameter.
151
152 $instance['authors'] = sanitize_text_field( $atts['authors'] );
153
154 $instance['link_before'] = wp_kses_post( $atts['link_before'] );
155 $instance['link_after'] = wp_kses_post( $atts['link_after'] );
156
157 $instance['show_date'] = sanitize_text_field( $atts['show_date'] );
158 $instance['date_format'] = sanitize_text_field( $atts['date_format'] );
159
160 $instance['hierarchical'] = (bool) $atts['hierarchical'];
161 $instance['is_block'] = (bool) $atts['is_block'];
162
163 // Whitelist sorting values
164 $allowed_order = array( 'ASC', 'DESC' );
165 $instance['sort_order'] = in_array( strtoupper( $atts['sort_order'] ), $allowed_order, true )
166 ? strtoupper( $atts['sort_order'] )
167 : 'ASC';
168
169 $allowed_columns = array( 'post_title', 'menu_order', 'post_date', 'post_modified', 'ID' );
170 $instance['sort_column'] = in_array( $atts['sort_column'], $allowed_columns, true )
171 ? $atts['sort_column']
172 : 'post_title';
173
174 $ew_page = new EW_Pages();
175
176 return $ew_page->shortcode( $instance );
177 }
178 endif;
179
180 if ( ! function_exists( 'ew_page_list' ) ) :
181
182 /**
183 * Get nav menus for select option via custom REST API!
184 *
185 * @return array|null Array of nav menus object with label and value pair, * or null if none.
186 */
187 function ew_page_list() { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- ew_ is this plugin's abbreviated prefix; wrapped in function_exists() guard.
188 $post_types = get_post_types(
189 array(
190 'public' => true,
191 'hierarchical' => true,
192 ),
193 'objects'
194 );
195
196 $page_list = array();
197
198 foreach ( $post_types as $page ) {
199 $object = new stdClass();
200 $object->label = sanitize_text_field( $page->labels->singular_name );
201 $object->value = sanitize_key( $page->name );
202 $page_list[] = $object;
203 }
204
205 return $page_list;
206 }
207
208 add_action(
209 'rest_api_init',
210 function () {
211 register_rest_route(
212 'ew-rest/v1',
213 'ew-page-list',
214 array(
215 'methods' => 'GET',
216 'callback' => 'ew_page_list',
217 'permission_callback' => '__return_true', // for public use
218 )
219 );
220 }
221 );
222 endif;
223