PluginProbe
Post Grid Gutenberg Blocks – PostX / 5.0.5
Post Grid Gutenberg Blocks – PostX v5.0.5
5.0.39 5.0.38 5.0.37 5.0.36 5.0.35 5.0.34 5.0.33 5.0.32 5.0.31 5.0.30 5.0.29 5.0.28 5.0.27 5.0.26 5.0.25 5.0.23 5.0.24 5.0.22 5.0.21 5.0.20 5.0.19 5.0.18 5.0.17 2.1.1 2.1.2 All 237 releases
ultimate-post / classes / Blocks.php

Blocks.php in Post Grid Gutenberg Blocks – PostX 5.0.5, at classes/Blocks.php

657 lines 30.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Require All Blocks and handle block ajax action
4 *
5 * @package ULTP\Blocks
6 * @since 4.1.11
7 */
8
9 namespace ULTP;
10
11 defined( 'ABSPATH' ) || exit;
12
13 /**
14 * Blocks class.
15 */
16 class Blocks {
17
18 /**
19 * Setup class.
20 *
21 * Registers AJAX actions and includes all block classes.
22 *
23 * @since 4.1.11
24 */
25 public function __construct() {
26 $this->include_all_blocks(); // Include Blocks .
27 add_action( 'wp_ajax_ultp_next_prev', array( $this, 'ultp_next_prev_callback' ) ); // Next Previous AJAX Call .
28 add_action( 'wp_ajax_nopriv_ultp_next_prev', array( $this, 'ultp_next_prev_callback' ) ); // Next Previous AJAX Call Logout User .
29
30 add_action( 'wp_ajax_ultp_filter', array( $this, 'ultp_filter_callback' ) ); // Next Previous AJAX Call .
31 add_action( 'wp_ajax_nopriv_ultp_filter', array( $this, 'ultp_filter_callback' ) ); // Next Previous AJAX Call Logout User .
32
33 add_action( 'wp_ajax_ultp_adv_filter', array( $this, 'ultp_adv_filter_callback' ) );
34 add_action( 'wp_ajax_nopriv_ultp_adv_filter', array( $this, 'ultp_adv_filter_callback' ) );
35
36 add_action( 'wp_ajax_ultp_pagination', array( $this, 'ultp_pagination_callback' ) ); // Page Number AJAX Call .
37 add_action( 'wp_ajax_nopriv_ultp_pagination', array( $this, 'ultp_pagination_callback' ) ); // Page Number AJAX Call Logout User .
38
39 add_action( 'wp_ajax_ultp_share_count', array( $this, 'ultp_shareCount_callback' ) ); // share Count save .
40 add_action( 'wp_ajax_nopriv_ultp_share_count', array( $this, 'ultp_shareCount_callback' ) ); // share Count save .
41 }
42
43 /**
44 * Require Blocks
45 *
46 * @since v.1.0.0
47 * @return NULL
48 */
49 public function include_all_blocks() {
50 spl_autoload_register(
51 function ( $class ) {
52 if ( strpos( $class, 'ULTP\blocks' ) === 0 ) {
53 $source = ULTP_PATH . 'blocks/' . explode( '\\', $class )[2] . '.php';
54 if ( file_exists( $source ) ) {
55 include_once $source;
56 } else {
57 $source = ULTP_PATH . 'addons/builder/blocks/' . explode( '\\', $class )[2] . '.php';
58 if ( file_exists( $source ) ) {
59 include_once $source;
60 }
61 }
62 }
63 }
64 );
65
66 $request = isset( $_POST['action'] ) ? sanitize_text_field( $_POST['action'] ) : '';
67 $get_action = isset( $_GET['action'] ) ? sanitize_text_field( $_GET['action'] ) : '';
68
69 if (
70 is_admin() &&
71 $request != 'et_fb_ajax_render_shortcode' && // Divi Module Check .
72 $get_action != 'elementor' && // Elementor Widget Check .
73 $request != 'elementor_ajax' // Elementor Widget Check .
74 ) {
75 return;
76 }
77
78 $settings = ultimate_post()->get_setting();
79 $blocks = array(
80 'post_list_1' => 'Post_List_1',
81 'post_list_2' => 'Post_List_2',
82 'post_list_3' => 'Post_List_3',
83 'post_list_4' => 'Post_List_4',
84 'post_grid_1' => 'Post_Grid_1',
85 'post_grid_2' => 'Post_Grid_2',
86 'post_grid_3' => 'Post_Grid_3',
87 'post_grid_4' => 'Post_Grid_4',
88 'post_grid_5' => 'Post_Grid_5',
89 'post_grid_6' => 'Post_Grid_6',
90 'post_grid_7' => 'Post_Grid_7',
91 'post_slider_1' => 'Post_Slider_1',
92 'post_slider_2' => 'Post_Slider_2',
93 'post_module_1' => 'Post_Module_1',
94 'post_module_2' => 'Post_Module_2',
95 'heading' => 'Heading',
96 'image' => 'Image',
97 'taxonomy' => 'Taxonomy',
98 'news_ticker' => 'News_Ticker',
99 'advanced_search' => 'Advanced_Search',
100 'advanced_filter' => 'Advanced_Filter',
101 'dark_Light' => 'Dark_Light',
102 'advanced_list' => 'Advanced_List',
103 'button' => 'Button',
104 'youtube_gallery' => 'Youtube_Gallery',
105 );
106
107 foreach ( $blocks as $id => $block ) {
108 if ( isset( $settings[ $id ] ) && $settings[ $id ] != 'yes' ) {
109 } else {
110 $obj = '\ULTP\blocks\\' . $block;
111 new $obj();
112 }
113 }
114
115 if ( isset( $settings['ultp_builder'] ) && $settings['ultp_builder'] == 'true' ) {
116 $builder_blocks = array(
117 'builder_archive_title' => 'Archive_Title',
118 'builder_post_title' => 'Post_Title',
119 'builder_post_content' => 'Post_Content',
120 'builder_post_featured_image' => 'Post_Featured_Image',
121 'builder_post_breadcrumb' => 'Post_Breadcrumb',
122 'builder_post_tag' => 'Post_Tag',
123 'builder_post_category' => 'Post_Category',
124 'builder_post_next_previous' => 'Next_Previous',
125 'builder_post_excerpt' => 'Post_Excerpt',
126 'builder_author_box' => 'Author_Box',
127 'builder_post_comments' => 'Post_Comments',
128 'builder_post_view_count' => 'Post_View_Count',
129 'builder_post_reading_time' => 'Post_Reading_Time',
130 'builder_post_comment_count' => 'Post_Comment_Count',
131 'builder_post_author_meta' => 'Post_Author_Meta',
132 'builder_post_date_meta' => 'Post_Date_Meta',
133 'builder_post_social_share' => 'Post_Social_Share',
134 'builder_advance_post_meta' => 'Advance_Post_Meta',
135 );
136 foreach ( $builder_blocks as $id => $block ) {
137 if ( isset( $settings[ $id ] ) && $settings[ $id ] != 'yes' ) {
138 } else {
139 $obj = '\ULTP\blocks\\' . $block;
140 new $obj();
141 }
142 }
143 }
144 }
145
146
147 /**
148 * Blocks Content Start.
149 *
150 * @since v.1.0.0
151 *
152 * @param ARRAY $blocks The blocks array.
153 * @param STRING $paged The current page number.
154 * @param STRING $blockId The block ID.
155 * @param STRING $blockRaw The raw block name.
156 * @param STRING $blockName The block name.
157 * @param STRING $builder The builder flag.
158 * @param STRING $postId The post ID.
159 * @param STRING|ARRAY $filterValue The filter value.
160 * @param STRING $filterType The filter type.
161 * @param ARRAY $ultp_uniqueIds Unique IDs for pagination.
162 * @param ARRAY $ultp_current_unique_posts Current unique posts.
163 * @param STRING $widgetBlockId The widget block ID.
164 * @param STRING $exclude_post_id The post ID to exclude.
165 * @param ARRAY $adv_filter_data Advanced filter data.
166 * @return STRING The AJAX response.
167 */
168 public function pagination_content_return( $blocks, $paged, $blockId, $blockRaw, $blockName, $builder, $postId, $filterValue, $filterType, $ultp_uniqueIds = array(), $ultp_current_unique_posts = array(), $widgetBlockId = '', $exclude_post_id = '', $adv_filter_data = array() ) {
169 foreach ( $blocks as $key => $value ) {
170 if ( $blockName == $value['blockName'] ) {
171 if ( $value['attrs']['blockId'] == $blockId ) {
172 $objName = str_replace( ' ', '_', ucwords( str_replace( array( 'ultimate-post/', '-' ), array( '', ' ' ), $blockName ) ) );
173 $new_obj = '\ULTP\blocks\\' . $objName;
174 $objectBlock = new $new_obj();
175 $attr = $objectBlock->get_attributes( true );
176
177 // Fix for grid blocks that do not support load more by default (pagination block)
178 if ( isset( $adv_filter_data['notFirstLoad'] ) && $adv_filter_data['notFirstLoad'] ) {
179 $attr['notFirstLoad'] = $adv_filter_data['notFirstLoad'];
180 }
181
182 $value['attrs']['paged'] = $paged;
183 if ( $builder ) {
184 $value['attrs']['builder'] = $builder;
185 }
186 if ( $postId ) {
187 $attr['current_post'] = $postId;
188 if ( get_post_type( $postId ) == 'ultp_builder' && ! $builder ) {
189 $attr['current_post'] = $exclude_post_id;
190 }
191 }
192 if ( isset( $value['attrs']['queryUnique'] ) && $value['attrs']['queryUnique'] ) {
193 $value['attrs']['loadMoreQueryUnique'] = $ultp_uniqueIds;
194 $ultp_uniqueIds[ $value['attrs']['queryUnique'] ] = array_diff( $ultp_uniqueIds[ $value['attrs']['queryUnique'] ], $ultp_current_unique_posts );
195 $value['attrs']['savedQueryUnique'] = $ultp_uniqueIds;
196 $value['attrs']['ultp_current_unique_posts'] = $ultp_current_unique_posts;
197 }
198 if ( isset( $value['attrs']['queryUnique'] ) && $value['attrs']['queryUnique'] && ( $value['attrs']['paginationType'] == 'loadMore' || $value['attrs']['paginationType'] == 'navigation' ) && isset( $ultp_uniqueIds ) && ! isset( $ultp_current_unique_posts ) ) {
199 die();
200 }
201
202 if ( $filterValue ) {
203 $value['attrs']['queryTaxValue'] = $adv_filter_data['is_adv'] ? wp_json_encode( $filterValue ) : wp_json_encode( array( $filterValue ) );
204 $value['attrs']['queryTax'] = $filterType;
205 $value['attrs']['checkFilter'] = true;
206 $value['attrs']['filterShow'] = $adv_filter_data['filterShow'];
207 $value['attrs']['queryAuthor'] = $adv_filter_data['author'];
208 $value['attrs']['queryOrderBy'] = $adv_filter_data['orderby'];
209 $value['attrs']['queryOrder'] = $adv_filter_data['order'];
210 $value['attrs']['querySearch'] = $adv_filter_data['search'];
211 $value['attrs']['queryQuick'] = $adv_filter_data['adv_sort'];
212
213 if ( $adv_filter_data['is_adv'] ) {
214 $value['attrs']['queryRelation'] = 'AND';
215 }
216 }
217 // Exclude Current Post From Pagination
218 if ( $exclude_post_id ) {
219 $queryArr = json_decode( $value['attrs']['queryExclude'] );
220 $queryArr[] = array(
221 'value' => $exclude_post_id,
222 'title' => '',
223 );
224 $value['attrs']['queryExclude'] = wp_json_encode( $queryArr );
225 }
226 $attr = array_merge( $attr, $value['attrs'] );
227 echo $objectBlock->content($attr, true); //phpcs:ignore
228 die();
229 }
230 }
231 if ( ! empty( $value['innerBlocks'] ) ) {
232 $this->pagination_content_return( $value['innerBlocks'], $paged, $blockId, $blockRaw, $blockName, $builder, $postId, $filterValue, $filterType, $ultp_uniqueIds, $ultp_current_unique_posts, $widgetBlockId, $exclude_post_id, $adv_filter_data );
233 }
234 }
235 }
236
237
238 /**
239 * Next Preview Callback of the Blocks.
240 *
241 * @since v.1.0.0
242 *
243 * @return STRING The AJAX response.
244 */
245 public function ultp_next_prev_callback() {
246 if ( ! ( isset( $_REQUEST['wpnonce'] ) && wp_verify_nonce( sanitize_key( wp_unslash( $_REQUEST['wpnonce'] ) ), 'ultp-nonce' ) ) ) {
247 return;
248 }
249
250 $paged = isset( $_POST['paged'] ) ? sanitize_text_field( $_POST['paged'] ) : '';
251 $blockId = isset( $_POST['blockId'] ) ? sanitize_text_field( $_POST['blockId'] ) : '';
252 $postId = isset( $_POST['postId'] ) ? sanitize_text_field( $_POST['postId'] ) : '';
253 $blockRaw = isset( $_POST['blockName'] ) ? sanitize_text_field( $_POST['blockName'] ) : '';
254 $builder = isset( $_POST['builder'] ) ? sanitize_text_field( $_POST['builder'] ) : '';
255 $blockName = str_replace( '_', '/', $blockRaw );
256 $widgetBlockId = isset( $_POST['widgetBlockId'] ) ? sanitize_text_field( $_POST['widgetBlockId'] ) : '';
257 $exclude_post_id = isset( $_POST['exclude'] ) ? sanitize_text_field( $_POST['exclude'] ) : '';
258
259 if ( $postId ) { // patch security issue
260 $post = get_post( (int) $postId );
261
262 if ( ! $post ) {
263 wp_send_json_error( array( 'message' => 'Post not found' ) );
264 return;
265 }
266
267 // Block private posts
268 if ( $post->post_status === 'private' && ! current_user_can( 'read_private_posts' ) ) {
269 wp_send_json_error( array( 'message' => 'Private post' ) );
270 return;
271 }
272
273 // Block draft/pending
274 if ( in_array( $post->post_status, array( 'draft', 'pending' ), true )
275 && ! current_user_can( 'edit_post', $post->ID ) ) {
276 wp_send_json_error( array( 'message' => 'Not allowed' ) );
277 return;
278 }
279
280 // Block password protected
281 if ( post_password_required( $post ) ) {
282 wp_send_json_error( array( 'message' => 'Password protected' ) );
283 return;
284 }
285 }
286
287
288 $is_adv = isset( $_POST['isAdv'] ) ? ultimate_post()->ultp_rest_sanitize_params( $_POST['isAdv'] ) : false;
289 $filterValue = isset( $_POST['filterValue'] ) ?
290 (
291 is_array( $_POST['filterValue'] ) ?
292 ultimate_post()->ultp_rest_sanitize_params( $_POST['filterValue'] ) :
293 sanitize_text_field( $_POST['filterValue'] )
294 ) :
295 '';
296
297 $filterType = isset( $_POST['filterType'] ) ? sanitize_text_field( $_POST['filterType'] ) : '';
298 $filterShow = isset( $_POST['filterShow'] ) ? sanitize_text_field( $_POST['filterShow'] ) : false;
299 $checkFilter = isset( $_POST['checkFilter'] ) ? sanitize_text_field( $_POST['checkFilter'] ) : false;
300 $author = isset( $_POST['author'] ) ? sanitize_text_field( $_POST['author'] ) : false;
301 $orderby = isset( $_POST['orderby'] ) ? sanitize_text_field( $_POST['orderby'] ) : 'date';
302 $order = isset( $_POST['order'] ) ? sanitize_text_field( $_POST['order'] ) : 'DESC';
303 $search = isset( $_POST['search'] ) ? sanitize_text_field( $_POST['search'] ) : '';
304 $adv_sort = isset( $_POST['adv_sort'] ) ? sanitize_text_field( $_POST['adv_sort'] ) : '';
305
306 $adv_filter_data = array(
307 'is_adv' => filter_var( $is_adv, FILTER_VALIDATE_BOOLEAN ),
308 'filterShow' => filter_var( $filterShow, FILTER_VALIDATE_BOOLEAN ),
309 'checkFilter' => filter_var( $checkFilter, FILTER_VALIDATE_BOOLEAN ),
310 'author' => $author ? wp_json_encode( $author ) : false,
311 'orderby' => $orderby,
312 'order' => $order,
313 'search' => $search,
314 'adv_sort' => $adv_sort,
315 'notFirstLoad' => true,
316 );
317
318 $ultp_uniqueIds = isset( $_POST['ultpUniqueIds'] ) ? json_decode( stripslashes( sanitize_text_field( $_POST['ultpUniqueIds'] ) ), true ) : array();
319 $ultp_current_unique_posts = isset( $_POST['ultpCurrentUniquePosts'] ) ? json_decode( stripslashes( sanitize_text_field( $_POST['ultpCurrentUniquePosts'] ) ), true ) : array();
320
321 if ( $widgetBlockId ) {
322 $blocks = parse_blocks( get_option( 'widget_block' )[ $widgetBlockId ]['content'] );
323 $this->pagination_content_return( $blocks, $paged, $blockId, $blockRaw, $blockName, $builder, '', $filterValue, $filterType, $ultp_uniqueIds, $ultp_current_unique_posts, $widgetBlockId, '', $adv_filter_data );
324 } elseif ( $paged && $blockId && $postId && $blockName ) {
325 $post = get_post( $postId );
326 if ( has_blocks( $post->post_content ) ) {
327 $blocks = parse_blocks( $post->post_content );
328 $this->pagination_content_return( $blocks, $paged, $blockId, $blockRaw, $blockName, $builder, $postId, $filterValue, $filterType, $ultp_uniqueIds, $ultp_current_unique_posts, '', $exclude_post_id, $adv_filter_data );
329 }
330 }
331 }
332
333 /**
334 * Filter Callback of the Blocks.
335 *
336 * @since v.1.0.0
337 *
338 * @return STRING The AJAX response.
339 */
340 public function ultp_filter_callback() {
341 if ( ! ( isset( $_REQUEST['wpnonce'] ) && wp_verify_nonce( sanitize_key( wp_unslash( $_REQUEST['wpnonce'] ) ), 'ultp-nonce' ) ) ) {
342 return;
343 }
344
345 $taxtype = isset( $_POST['taxtype'] ) ? sanitize_text_field( $_POST['taxtype'] ) : '';
346 if ( $taxtype ) {
347 $blockId = isset( $_POST['blockId'] ) ? sanitize_text_field( $_POST['blockId'] ) : '';
348 $postId = isset( $_POST['postId'] ) ? sanitize_text_field( $_POST['postId'] ) : '';
349 $taxonomy = isset( $_POST['taxonomy'] ) ? sanitize_text_field( $_POST['taxonomy'] ) : '';
350 $blockRaw = isset( $_POST['blockName'] ) ? sanitize_text_field( $_POST['blockName'] ) : '';
351 $blockName = str_replace( '_', '/', $blockRaw );
352 $post = get_post( $postId );
353 $widgetBlockId = isset( $_POST['widgetBlockId'] ) ? sanitize_text_field( $_POST['widgetBlockId'] ) : '';
354 $ultp_uniqueIds = isset( $_POST['ultpUniqueIds'] ) ? json_decode( stripslashes( sanitize_text_field( $_POST['ultpUniqueIds'] ) ), true ) : array();
355 $ultp_current_unique_posts = isset( $_POST['ultpCurrentUniquePosts'] ) ? json_decode( stripslashes( sanitize_text_field( $_POST['ultpCurrentUniquePosts'] ) ), true ) : array();
356 $toReturn = array();
357
358 if ( $widgetBlockId ) {
359 $blocks = parse_blocks( get_option( 'widget_block' )[ $widgetBlockId ]['content'] );
360 $data = $this->filter_content_return( $blocks, $blockId, $blockRaw, $blockName, $taxtype, $taxonomy, $postId, $toReturn, $widgetBlockId, array(), $ultp_uniqueIds, $ultp_current_unique_posts );
361 } elseif ( has_blocks( $post->post_content ) ) {
362 $blocks = parse_blocks( $post->post_content );
363 $data = $this->filter_content_return( $blocks, $blockId, $blockRaw, $blockName, $taxtype, $taxonomy, $postId, $toReturn, '', array(), $ultp_uniqueIds, $ultp_current_unique_posts );
364 }
365 return wp_send_json_success(
366 array(
367 'filteredData' => $data,
368 )
369 );
370 }
371 }
372
373 /**
374 * Advanced Filter Callback of the Blocks.
375 *
376 * @since v.3.2.4
377 *
378 * @return STRING The AJAX response.
379 */
380 public function ultp_adv_filter_callback() {
381 if ( ! ( isset( $_REQUEST['wpnonce'] ) && wp_verify_nonce( sanitize_key( wp_unslash( $_REQUEST['wpnonce'] ) ), 'ultp-nonce' ) ) ) {
382 return;
383 }
384
385 $blockId = isset( $_POST['blockId'] ) ? sanitize_text_field( $_POST['blockId'] ) : '';
386 $postId = isset( $_POST['postId'] ) ? sanitize_text_field( $_POST['postId'] ) : '';
387
388 $taxonomy = isset( $_POST['taxonomy'] ) ? ultimate_post()->ultp_rest_sanitize_params( $_POST['taxonomy'] ) : '[]';
389
390 $author = isset( $_POST['author'] ) ? ultimate_post()->ultp_rest_sanitize_params( $_POST['author'] ) : false;
391 $orderby = isset( $_POST['orderby'] ) ? sanitize_text_field( $_POST['orderby'] ) : 'date';
392 $order = isset( $_POST['order'] ) ? sanitize_text_field( $_POST['order'] ) : 'DESC';
393 $search = isset( $_POST['search'] ) ? sanitize_text_field( $_POST['search'] ) : '';
394 $adv_sort = isset( $_POST['adv_sort'] ) ? sanitize_text_field( $_POST['adv_sort'] ) : '';
395
396 $adv_filter_data = array(
397 'is_adv' => true,
398 'filterShow' => true,
399 'checkFilter' => true,
400 'author' => $author ? wp_json_encode( $author ) : false,
401 'orderby' => $orderby,
402 'order' => $order,
403 'search' => $search,
404 'adv_sort' => $adv_sort,
405 );
406
407 $blockRaw = isset( $_POST['blockName'] ) ? sanitize_text_field( $_POST['blockName'] ) : '';
408 $blockName = str_replace( '_', '/', $blockRaw );
409 $post = get_post( $postId );
410 $widgetBlockId = isset( $_POST['widgetBlockId'] ) ? sanitize_text_field( $_POST['widgetBlockId'] ) : '';
411 $toReturn = array();
412
413 $ultp_uniqueIds = isset( $_POST['ultpUniqueIds'] ) ? json_decode( stripslashes( sanitize_text_field( $_POST['ultpUniqueIds'] ) ), true ) : array();
414 $ultp_current_unique_posts = isset( $_POST['ultpCurrentUniquePosts'] ) ? json_decode( stripslashes( sanitize_text_field( $_POST['ultpCurrentUniquePosts'] ) ), true ) : array();
415
416 if ( $widgetBlockId ) {
417 $blocks = parse_blocks( get_option( 'widget_block' )[ $widgetBlockId ]['content'] );
418 $data = $this->filter_content_return( $blocks, $blockId, $blockRaw, $blockName, 'multiTaxonomy', $taxonomy, $postId, $toReturn, $widgetBlockId, $adv_filter_data, $ultp_uniqueIds, $ultp_current_unique_posts );
419 } elseif ( has_blocks( $post->post_content ) ) {
420 $blocks = parse_blocks( $post->post_content );
421 $data = $this->filter_content_return( $blocks, $blockId, $blockRaw, $blockName, 'multiTaxonomy', $taxonomy, $postId, $toReturn, '', $adv_filter_data, $ultp_uniqueIds, $ultp_current_unique_posts );
422 }
423 return wp_send_json_success(
424 array(
425 'filteredData' => $data,
426 )
427 );
428 }
429
430 /**
431 * Pagination of the Blocks.
432 *
433 * @since v.1.0.0
434 *
435 * @return STRING The AJAX response.
436 */
437 public function ultp_pagination_callback() {
438 if ( ! ( isset( $_REQUEST['wpnonce'] ) && wp_verify_nonce( sanitize_key( wp_unslash( $_REQUEST['wpnonce'] ) ), 'ultp-nonce' ) ) ) {
439 return;
440 }
441
442 $paged = isset( $_POST['paged'] ) ? sanitize_text_field( $_POST['paged'] ) : '';
443 if ( $paged ) {
444 $blockId = isset( $_POST['blockId'] ) ? sanitize_text_field( $_POST['blockId'] ) : '';
445 $postId = isset( $_POST['postId'] ) ? sanitize_text_field( $_POST['postId'] ) : '';
446 $blockRaw = isset( $_POST['blockName'] ) ? sanitize_text_field( $_POST['blockName'] ) : '';
447 $builder = isset( $_POST['builder'] ) ? sanitize_text_field( $_POST['builder'] ) : '';
448 $blockName = str_replace( '_', '/', $blockRaw );
449 $post = get_post( $postId );
450 $widgetBlockId = isset( $_POST['widgetBlockId'] ) ? sanitize_text_field( $_POST['widgetBlockId'] ) : '';
451 $exclude_post_id = isset( $_POST['exclude'] ) ? sanitize_text_field( $_POST['exclude'] ) : '';
452
453 $is_adv = isset( $_POST['isAdv'] ) ? ultimate_post()->ultp_rest_sanitize_params( $_POST['isAdv'] ) : false;
454
455 $filterValue = array();
456
457 if ( isset( $_POST['filterValue'] ) ) {
458 if ( is_array( $_POST['filterValue'] ) ) {
459 $filterValue = ultimate_post()->ultp_rest_sanitize_params( $_POST['filterValue'] );
460 } else {
461 $decoded = json_decode( $_POST['filterValue'] );
462 if ( is_array( $decoded ) ) {
463 $filterValue = ultimate_post()->ultp_rest_sanitize_params( $decoded );
464 } elseif ( $decoded ) {
465 $filterValue = sanitize_text_field( $_POST['filterValue'] );
466 }
467 }
468 }
469
470 $filterType = isset( $_POST['filterType'] ) ? sanitize_text_field( $_POST['filterType'] ) : '';
471 $filterShow = isset( $_POST['filterShow'] ) ? sanitize_text_field( $_POST['filterShow'] ) : false;
472 $checkFilter = isset( $_POST['checkFilter'] ) ? sanitize_text_field( $_POST['checkFilter'] ) : false;
473 $author = isset( $_POST['author'] ) ? sanitize_text_field( $_POST['author'] ) : false;
474 $orderby = isset( $_POST['orderby'] ) ? sanitize_text_field( $_POST['orderby'] ) : 'date';
475 $order = isset( $_POST['order'] ) ? sanitize_text_field( $_POST['order'] ) : 'DESC';
476 $search = isset( $_POST['search'] ) ? sanitize_text_field( $_POST['search'] ) : '';
477 $adv_sort = isset( $_POST['adv_sort'] ) ? sanitize_text_field( $_POST['adv_sort'] ) : '';
478
479 $adv_filter_data = array(
480 'is_adv' => filter_var( $is_adv, FILTER_VALIDATE_BOOLEAN ),
481 'filterShow' => filter_var( $filterShow, FILTER_VALIDATE_BOOLEAN ),
482 'checkFilter' => filter_var( $checkFilter, FILTER_VALIDATE_BOOLEAN ),
483 'author' => $author ? wp_json_encode( $author ) : false,
484 'orderby' => $orderby,
485 'order' => $order,
486 'search' => $search,
487 'adv_sort' => $adv_sort,
488 );
489
490 $ultp_uniqueIds = isset( $_POST['ultpUniqueIds'] ) ? json_decode( stripslashes( sanitize_text_field( $_POST['ultpUniqueIds'] ) ), true ) : array();
491 $ultp_current_unique_posts = isset( $_POST['ultpCurrentUniquePosts'] ) ? json_decode( stripslashes( sanitize_text_field( $_POST['ultpCurrentUniquePosts'] ) ), true ) : array();
492
493 if ( $widgetBlockId ) {
494 $blocks = parse_blocks( get_option( 'widget_block' )[ $widgetBlockId ]['content'] );
495 $this->pagination_content_return( $blocks, $paged, $blockId, $blockRaw, $blockName, $builder, '', $filterValue, $filterType, $ultp_uniqueIds, $ultp_current_unique_posts, $widgetBlockId, '', $adv_filter_data );
496 } elseif ( has_blocks( $post->post_content ) ) {
497 $blocks = parse_blocks( $post->post_content );
498 $this->pagination_content_return( $blocks, $paged, $blockId, $blockRaw, $blockName, $builder, $postId, $filterValue, $filterType, $ultp_uniqueIds, $ultp_current_unique_posts, '', $exclude_post_id, $adv_filter_data );
499 }
500 }
501 }
502
503 /**
504 * Share Count callback.
505 *
506 * @since v.1.0.0
507 *
508 * @return STRING The AJAX response.
509 */
510 public function ultp_shareCount_callback() {
511 if ( ! ( isset( $_REQUEST['wpnonce'] ) && wp_verify_nonce( sanitize_key( wp_unslash( $_REQUEST['wpnonce'] ) ), 'ultp-nonce' ) ) ) {
512 return;
513 }
514 $id = isset( $_POST['postId'] ) ? sanitize_text_field( $_POST['postId'] ) : '';
515 $count = isset( $_POST['shareCount'] ) ? sanitize_text_field( $_POST['shareCount'] ) : '';
516 $post_id = $id;
517 $new_count = $count + 1;
518 update_post_meta( $post_id, 'share_count', $new_count );
519 }
520
521 /**
522 * Filter Callback of the Blocks.
523 *
524 * @since v.1.0.0
525 *
526 * @param ARRAY $blocks The blocks array.
527 * @param STRING $blockId The block ID.
528 * @param STRING $blockRaw The raw block name.
529 * @param STRING $blockName The block name.
530 * @param STRING $taxtype The taxonomy type.
531 * @param STRING|ARRAY $taxonomy The taxonomy.
532 * @param STRING $postId The post ID.
533 * @param ARRAY $toReturn The array to return.
534 * @param STRING $widgetBlockId The widget block ID.
535 * @param ARRAY $adv_filter_data The advanced filter data.
536 * @param ARRAY $ultp_uniqueIds The unique IDs.
537 * @param ARRAY $ultp_current_unique_posts The current unique posts.
538 * @return ARRAY
539 */
540 public function filter_content_return( $blocks, $blockId, $blockRaw, $blockName, $taxtype, $taxonomy, $postId, &$toReturn, $widgetBlockId = '', $adv_filter_data = array(), $ultp_uniqueIds = array(), $ultp_current_unique_posts = array() ) {
541 foreach ( $blocks as $key => $value ) {
542 if ( $blockName == $value['blockName'] ) {
543 if ( $value['attrs']['blockId'] == $blockId ) {
544 $objName = str_replace( ' ', '_', ucwords( str_replace( array( 'ultimate-post/', '-' ), array( '', ' ' ), $blockName ) ) );
545 $new_obj = '\ULTP\blocks\\' . $objName;
546 $objectBlock = new $new_obj();
547 $attr = $objectBlock->get_attributes( true );
548 if ( $taxonomy ) {
549
550 if ( isset( $adv_filter_data['is_adv'] ) && $adv_filter_data['is_adv'] ) {
551 $value['attrs']['queryTaxValue'] = wp_json_encode( $taxonomy );
552 $value['attrs']['queryRelation'] = 'AND';
553 $value['attrs']['queryAuthor'] = $adv_filter_data['author'];
554 $value['attrs']['queryOrderBy'] = $adv_filter_data['orderby'];
555 $value['attrs']['queryOrder'] = $adv_filter_data['order'];
556 $value['attrs']['querySearch'] = $adv_filter_data['search'];
557 $value['attrs']['queryQuick'] = $adv_filter_data['adv_sort'];
558 } else {
559 $value['attrs']['queryTaxValue'] = wp_json_encode( array( $taxonomy ) );
560 }
561
562 $value['attrs']['queryTax'] = $taxtype;
563 $value['attrs']['ajaxCall'] = true;
564 }
565 if ( isset( $value['attrs']['queryNumber'] ) ) {
566 $value['attrs']['queryNumber'] = $value['attrs']['queryNumber'];
567 }
568
569 if ( isset( $value['attrs']['queryUnique'] ) && $value['attrs']['queryUnique'] ) {
570 $value['attrs']['loadMoreQueryUnique'] = $ultp_uniqueIds;
571 $ultp_uniqueIds[ $value['attrs']['queryUnique'] ] = array_diff( $ultp_uniqueIds[ $value['attrs']['queryUnique'] ], $ultp_current_unique_posts );
572 $value['attrs']['savedQueryUnique'] = $ultp_uniqueIds;
573 $value['attrs']['ultp_current_unique_posts'] = $ultp_current_unique_posts;
574 }
575
576 $attr = array_merge( $attr, $value['attrs'] );
577
578 $filter_attributes = array();
579
580 $filter_attributes['isAdv'] = isset( $adv_filter_data['is_adv'] ) ? $adv_filter_data['is_adv'] : false;
581 $filter_attributes['queryTaxValue'] = $filter_attributes['isAdv'] ? wp_json_encode( $taxonomy ) : $taxonomy;
582 $filter_attributes['queryTax'] = $taxtype;
583
584 if ( $filter_attributes['isAdv'] ) {
585 $filter_attributes['queryAuthor'] = $adv_filter_data['author'];
586 $filter_attributes['queryOrderBy'] = $adv_filter_data['orderby'];
587 $filter_attributes['queryOrder'] = $adv_filter_data['order'];
588 $filter_attributes['querySearch'] = $adv_filter_data['search'];
589 $filter_attributes['queryQuick'] = $adv_filter_data['adv_sort'];
590 }
591
592 $toReturn = array(
593 'blocks' => $objectBlock->content( $attr, true ),
594 'notFound' => isset( $attr['notFoundMessage'] ) ? $attr['notFoundMessage'] : '',
595 'pagination' => $this->pagination_for_filter( $attr, $postId, $blockRaw, $filter_attributes ),
596 'paginationType' => $attr['paginationType'],
597 'paginationShow' => $attr['paginationShow'],
598 );
599 }
600 }
601 if ( ! empty( $value['innerBlocks'] ) ) {
602 $this->filter_content_return( $value['innerBlocks'], $blockId, $blockRaw, $blockName, $taxtype, $taxonomy, $postId, $toReturn, $widgetBlockId, $adv_filter_data, $ultp_uniqueIds, $ultp_current_unique_posts );
603 }
604 }
605 return $toReturn;
606 }
607
608 /**
609 * Pagination for filter callback
610 *
611 * @since v.2.8.9
612 *
613 * @param ARRAY $attr .
614 * @param STRING $postId .
615 * @param STRING $blockRaw .
616 * @param ARRAY $filter_attributes .
617 * @return STRING
618 */
619 public function pagination_for_filter( $attr, $postId, $blockRaw, $filter_attributes ) {
620 $attr['queryNumber'] = ultimate_post()->get_post_number( 4, $attr['queryNumber'], $attr['queryNumPosts'] );
621 $recent_posts = new \WP_Query( ultimate_post()->get_query( $attr ) );
622 $pageNum = ultimate_post()->get_page_number( $attr, $recent_posts->found_posts );
623
624 $datasets = ultimate_post()->get_adv_data_attrs( null, $filter_attributes );
625 $datasets .= ' data-for="ultp-block-' . sanitize_html_class( $attr['blockId'] ) . '" ';
626
627 $wraper_after = '';
628 $style = $pageNum == 1 ? 'style="display:none"' : '';
629
630 if ( $attr['paginationType'] == 'loadMore' ) {
631 $wraper_after .= '<div ' . $style . ' class="ultp-loadmore "' . '>';
632 $wraper_after .= '<span class="ultp-loadmore-action" tabindex="0" role="button" data-pages="' . $pageNum . '" data-pagenum="1" data-blockid="' . $attr['blockId'] . '" data-blockname="' . $blockRaw . '" data-postid="' . $postId . '" ' . ultimate_post()->get_builder_attr( $attr['queryType'] ) . $datasets . '>' . ( isset( $attr['loadMoreText'] ) ? $attr['loadMoreText'] : 'Load More' ) . ' <span class="ultp-spin">' . ultimate_post()->get_svg_icon( 'refresh' ) . '</span></span>';
633 $wraper_after .= '</div>';
634 } elseif ( $attr['paginationType'] == 'navigation' ) {
635 $wraper_after .= '<div ' . $style . ' class="ultp-next-prev-wrap" data-pages="' . $pageNum . '" data-pagenum="1" data-blockid="' . $attr['blockId'] . '" data-blockname="' . $blockRaw . '" data-postid="' . $postId . '" ' . ultimate_post()->get_builder_attr( $attr['queryType'] ) . $datasets . '>';
636 $wraper_after .= ultimate_post()->next_prev();
637 $wraper_after .= '</div>';
638 } elseif ( $attr['paginationType'] == 'pagination' ) {
639 $wraper_after .= '<div class="ultp-pagination-wrap' . ( $attr['paginationAjax'] ? ' ultp-pagination-ajax-action' : '' ) . '" data-paged="1" data-blockid="' . $attr['blockId'] . '" data-postid="' . $postId . '" data-pages="' . $pageNum . '" data-blockname="' . $blockRaw . '" ' . ultimate_post()->get_builder_attr( $attr['queryType'] ) . $datasets . '>';
640
641 $wraper_after .= ultimate_post()->pagination(
642 $pageNum,
643 $attr['paginationNav'],
644 $attr['paginationText'],
645 $attr['paginationAjax'],
646 isset( $_SERVER['HTTP_REFERER'] ) ? esc_url_raw( $_SERVER['HTTP_REFERER'] ) : '',
647 $attr['blockId']
648 );
649
650 $wraper_after .= '</div>';
651 }
652 wp_reset_query();
653
654 return $wraper_after;
655 }
656 }
657