PluginProbe
Gutenberg Essential Blocks – Page Builder for Gutenberg Blocks & Patterns / 6.4.1
Gutenberg Essential Blocks – Page Builder for Gutenberg Blocks & Patterns v6.4.1
6.4.3 6.4.2 6.4.1 6.4.0 6.3.0 6.2.1 6.2.0 6.1.4 6.1.3 6.1.2 6.1.1 6.1.0 trunk 1.0.0 1.1.0 1.1.1 1.1.2 1.2.0 1.3.0 1.3.1 2.0.0 2.0.1 2.0.2 2.1.0 2.2.0 All 198 releases
essential-blocks / includes / API / PostBlock.php

PostBlock.php in Gutenberg Essential Blocks – Page Builder for Gutenberg Blocks & Patterns 6.4.1, at includes/API/PostBlock.php

176 lines 7.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace EssentialBlocks\API;
4
5 use EssentialBlocks\Utils\Helper;
6 use EssentialBlocks\Blocks\PostGrid as PostGridBlock;
7 use EssentialBlocks\Blocks\PostCarousel as PostCarouselBlock;
8
9 class PostBlock extends Base
10 {
11 /**
12 * Register REST Routes
13 * Supports both GET (backward compatibility) and POST (firewall-friendly) methods
14 * @return void
15 */
16 public function register()
17 {
18 // GET method for backward compatibility
19 $this->get( 'queries', [
20 'callback' => [ $this, 'get_posts' ]
21 ] );
22
23 // POST method for better firewall compatibility (7G/8G)
24 $this->post( 'queries', [
25 'callback' => [ $this, 'get_posts' ]
26 ] );
27 }
28
29 /**
30 * Handle post queries for both GET and POST requests
31 * POST method helps avoid 7G/8G firewall 403 errors
32 *
33 * @param WP_REST_Request $request
34 * @return WP_REST_Response|WP_Error
35 */
36 public function get_posts( $request )
37 {
38 $block_type = $request->has_param( 'block_type' ) ? $request->get_param( 'block_type' ) : 'post-grid';
39
40 // Handle both GET and POST requests
41 $query_data = '';
42 $attributes_data = '';
43 $page_number = 0;
44 $query_filter = '';
45 $query_param_string = '';
46
47 if ( $request->get_method() === 'POST' ) {
48 // Handle POST request data
49 $body = $request->get_body();
50 $post_data = json_decode( $body, true );
51
52 if ( ! $post_data ) {
53 $post_data = $request->get_params();
54 }
55
56 $query_data = isset( $post_data['query_data'] ) ? sanitize_text_field( $post_data['query_data'] ) : '';
57 $attributes_data = isset( $post_data['attributes'] ) ? sanitize_text_field( $post_data['attributes'] ) : '';
58 $page_number = isset( $post_data['pageNumber'] ) ? (int) sanitize_text_field( $post_data['pageNumber'] ) - 1 : 0;
59 $query_filter = isset( $post_data['query_filter'] ) ? sanitize_text_field( $post_data['query_filter'] ) : '';
60 $query_param_string = isset( $post_data['query_param_string'] ) ? sanitize_text_field( $post_data['query_param_string'] ) : '';
61 } else {
62 // Handle GET request (backward compatibility)
63 $query_data = sanitize_text_field( $request->get_param( 'query_data' ) );
64 $attributes_data = sanitize_text_field( $request->get_param( 'attributes' ) );
65 $page_number = isset( $request[ 'pageNumber' ] ) ? (int) sanitize_text_field( $request[ 'pageNumber' ] ) - 1 : 0;
66 }
67
68 if ( empty( $query_data ) || empty( $attributes_data ) ) {
69 return new \WP_Error( 'invalid_request', 'Invalid request parameters', array( 'status' => 400 ) );
70 }
71
72 // Validate JSON data
73 $query = json_decode( $query_data, true );
74 $attributes = json_decode( $attributes_data, true );
75
76 if ( json_last_error() !== JSON_ERROR_NONE ) {
77 return new \WP_Error( 'invalid_json', 'Invalid JSON data provided', array( 'status' => 400 ) );
78 }
79
80 $query = ( is_object( $query ) || is_array( $query ) ) ? (array) $query : [ ];
81 $attributes = ( is_object( $attributes ) || is_array( $attributes ) ) ? (array) $attributes : [ ];
82 $pageNumber = $page_number;
83 //Check if param is empty
84 if ( ! is_array( $query ) || ! is_array( $attributes ) ) {
85 wp_send_json_error( "Invalid request param" );
86 }
87
88 if ( isset( $query[ 'per_page' ] ) && isset( $query[ 'offset' ] ) ) {
89 $query[ 'offset' ] = (int) $query[ 'offset' ] + ( (int) $query[ 'per_page' ] * (int) $pageNumber );
90 }
91
92 $_template_name = 'carousel-markup';
93 $block_object = PostCarouselBlock::get_instance();
94 if ( $block_type === 'post-grid' ) {
95 // Handle taxonomy and category filtering for both GET and POST
96 $taxonomy = '';
97 $category = '';
98 $query_type = '';
99 $search_key = '';
100
101 if ( $request->get_method() === 'POST' ) {
102 // Parse query_param_string for POST requests
103 if ( ! empty( $query_param_string ) ) {
104 parse_str( ltrim( $query_param_string, '&' ), $parsed_params );
105 $taxonomy = isset( $parsed_params['taxonomy'] ) ? sanitize_text_field( $parsed_params['taxonomy'] ) : '';
106 $category = isset( $parsed_params['category'] ) ? sanitize_text_field( $parsed_params['category'] ) : '';
107 $query_type = isset( $parsed_params['query_type'] ) ? sanitize_text_field( $parsed_params['query_type'] ) : '';
108 $search_key = isset( $parsed_params['s'] ) ? sanitize_text_field( $parsed_params['s'] ) : '';
109 }
110 } else {
111 // Handle GET request parameters
112 $taxonomy = $request->get_param( 'taxonomy' );
113 $category = $request->get_param( 'category' );
114 $query_type = $request->get_param( 'query_type' );
115 $search_key = $request->get_param( 's' );
116 }
117
118 if ( ! empty( $taxonomy ) && ! empty( $category ) ) {
119 $category_term = get_term_by( 'slug', $category, $taxonomy );
120 if ( $category_term ) {
121 $catString = wp_json_encode( [ [
122 "label" => $category_term->name,
123 "value" => $category_term->term_id
124 ] ] );
125 $filterQuery = [
126 $taxonomy => [
127 "name" => $category,
128 "slug" => $category,
129 "value" => $catString
130 ]
131 ];
132 $query[ "taxonomies" ] = array_merge( $query[ 'taxonomies' ] ?? [ ], $filterQuery );
133 }
134 }
135
136 if ( $query_type === 'search' && ! empty( $search_key ) ) {
137 $query[ "s" ] = $search_key;
138 }
139
140 $_template_name = 'grid-markup';
141 $block_object = PostGridBlock::get_instance();
142 $attributes = wp_parse_args( $attributes, $block_object->get_default_attributes() );
143 }
144
145 $result = $block_object->get_posts( $query, true );
146 $posts = [ ];
147 if ( isset( $result->posts ) && is_array( $result->posts ) && count( $result->posts ) > 0 ) {
148 $posts = $result->posts;
149 }
150 $posts_count = 0;
151 if ( isset( $result->found_posts ) ) {
152 $posts_count = $result->found_posts;
153 }
154
155 if ( empty( $posts ) ) {
156 return false;
157 }
158
159 ob_start();
160 Helper::views( 'post-partials/' . $_template_name, array_merge( $attributes, [
161 'posts' => $posts,
162 'block_object' => $block_object,
163 'source' => isset( $query[ 'source' ] ) ? $query[ 'source' ] : 'post',
164 'headerMeta' => ! empty( $attributes[ 'headerMeta' ] ) ? json_decode( $attributes[ 'headerMeta' ] ) : [ ],
165 'footerMeta' => ! empty( $attributes[ 'footerMeta' ] ) ? json_decode( $attributes[ 'footerMeta' ] ) : [ ]
166 ] ) );
167
168 $response = rest_ensure_response( ob_get_clean() );
169 $response->set_headers( [
170 'x-wp-total' => $posts_count
171 ] );
172
173 return $response;
174 }
175 }
176