PluginProbe
Post Grid Gutenberg Blocks – PostX / 5.0.6
Post Grid Gutenberg Blocks – PostX v5.0.6
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.6, at classes/Blocks.php

708 lines 31.6 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_share_count_callback' ) ); // share Count save .
40 add_action( 'wp_ajax_nopriv_ultp_share_count', array( $this, 'ultp_share_count_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 $is_adv = isset( $_POST['isAdv'] ) ? ultimate_post()->ultp_rest_sanitize_params( $_POST['isAdv'] ) : false;
288 $filterValue = isset( $_POST['filterValue'] ) ?
289 (
290 is_array( $_POST['filterValue'] ) ?
291 ultimate_post()->ultp_rest_sanitize_params( $_POST['filterValue'] ) :
292 sanitize_text_field( $_POST['filterValue'] )
293 ) :
294 '';
295
296 $filterType = isset( $_POST['filterType'] ) ? sanitize_text_field( $_POST['filterType'] ) : '';
297 $filterShow = isset( $_POST['filterShow'] ) ? sanitize_text_field( $_POST['filterShow'] ) : false;
298 $checkFilter = isset( $_POST['checkFilter'] ) ? sanitize_text_field( $_POST['checkFilter'] ) : false;
299 $author = isset( $_POST['author'] ) ? sanitize_text_field( $_POST['author'] ) : false;
300 $orderby = isset( $_POST['orderby'] ) ? sanitize_text_field( $_POST['orderby'] ) : 'date';
301 $order = isset( $_POST['order'] ) ? sanitize_text_field( $_POST['order'] ) : 'DESC';
302 $search = isset( $_POST['search'] ) ? sanitize_text_field( $_POST['search'] ) : '';
303 $adv_sort = isset( $_POST['adv_sort'] ) ? sanitize_text_field( $_POST['adv_sort'] ) : '';
304
305 $adv_filter_data = array(
306 'is_adv' => filter_var( $is_adv, FILTER_VALIDATE_BOOLEAN ),
307 'filterShow' => filter_var( $filterShow, FILTER_VALIDATE_BOOLEAN ),
308 'checkFilter' => filter_var( $checkFilter, FILTER_VALIDATE_BOOLEAN ),
309 'author' => $author ? wp_json_encode( $author ) : false,
310 'orderby' => $orderby,
311 'order' => $order,
312 'search' => $search,
313 'adv_sort' => $adv_sort,
314 'notFirstLoad' => true,
315 );
316
317 $ultp_uniqueIds = isset( $_POST['ultpUniqueIds'] ) ? json_decode( stripslashes( sanitize_text_field( $_POST['ultpUniqueIds'] ) ), true ) : array();
318 $ultp_current_unique_posts = isset( $_POST['ultpCurrentUniquePosts'] ) ? json_decode( stripslashes( sanitize_text_field( $_POST['ultpCurrentUniquePosts'] ) ), true ) : array();
319
320 if ( $widgetBlockId ) {
321 $blocks = parse_blocks( get_option( 'widget_block' )[ $widgetBlockId ]['content'] );
322 $this->pagination_content_return( $blocks, $paged, $blockId, $blockRaw, $blockName, $builder, '', $filterValue, $filterType, $ultp_uniqueIds, $ultp_current_unique_posts, $widgetBlockId, '', $adv_filter_data );
323 } elseif ( $paged && $blockId && $postId && $blockName ) {
324 $post = get_post( $postId );
325 if ( has_blocks( $post->post_content ) ) {
326 $blocks = parse_blocks( $post->post_content );
327 $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 );
328 }
329 }
330 }
331
332 /**
333 * Filter Callback of the Blocks.
334 *
335 * @since v.1.0.0
336 *
337 * @return STRING The AJAX response.
338 */
339 public function ultp_filter_callback() {
340 if ( ! ( isset( $_REQUEST['wpnonce'] ) && wp_verify_nonce( sanitize_key( wp_unslash( $_REQUEST['wpnonce'] ) ), 'ultp-nonce' ) ) ) {
341 return;
342 }
343
344 $taxtype = isset( $_POST['taxtype'] ) ? sanitize_text_field( $_POST['taxtype'] ) : '';
345 if ( $taxtype ) {
346 $blockId = isset( $_POST['blockId'] ) ? sanitize_text_field( $_POST['blockId'] ) : '';
347 $postId = isset( $_POST['postId'] ) ? sanitize_text_field( $_POST['postId'] ) : '';
348 $taxonomy = isset( $_POST['taxonomy'] ) ? sanitize_text_field( $_POST['taxonomy'] ) : '';
349 $blockRaw = isset( $_POST['blockName'] ) ? sanitize_text_field( $_POST['blockName'] ) : '';
350 $blockName = str_replace( '_', '/', $blockRaw );
351 $post = get_post( $postId );
352 $widgetBlockId = isset( $_POST['widgetBlockId'] ) ? sanitize_text_field( $_POST['widgetBlockId'] ) : '';
353 $ultp_uniqueIds = isset( $_POST['ultpUniqueIds'] ) ? json_decode( stripslashes( sanitize_text_field( $_POST['ultpUniqueIds'] ) ), true ) : array();
354 $ultp_current_unique_posts = isset( $_POST['ultpCurrentUniquePosts'] ) ? json_decode( stripslashes( sanitize_text_field( $_POST['ultpCurrentUniquePosts'] ) ), true ) : array();
355 $toReturn = array();
356
357 if ( $widgetBlockId ) {
358 $blocks = parse_blocks( get_option( 'widget_block' )[ $widgetBlockId ]['content'] );
359 $data = $this->filter_content_return( $blocks, $blockId, $blockRaw, $blockName, $taxtype, $taxonomy, $postId, $toReturn, $widgetBlockId, array(), $ultp_uniqueIds, $ultp_current_unique_posts );
360 } elseif ( has_blocks( $post->post_content ) ) {
361 $blocks = parse_blocks( $post->post_content );
362 $data = $this->filter_content_return( $blocks, $blockId, $blockRaw, $blockName, $taxtype, $taxonomy, $postId, $toReturn, '', array(), $ultp_uniqueIds, $ultp_current_unique_posts );
363 }
364 return wp_send_json_success(
365 array(
366 'filteredData' => $data,
367 )
368 );
369 }
370 }
371
372 /**
373 * Advanced Filter Callback of the Blocks.
374 *
375 * @since v.3.2.4
376 *
377 * @return STRING The AJAX response.
378 */
379 public function ultp_adv_filter_callback() {
380 if ( ! ( isset( $_REQUEST['wpnonce'] ) && wp_verify_nonce( sanitize_key( wp_unslash( $_REQUEST['wpnonce'] ) ), 'ultp-nonce' ) ) ) {
381 return;
382 }
383
384 $blockId = isset( $_POST['blockId'] ) ? sanitize_text_field( $_POST['blockId'] ) : '';
385 $postId = isset( $_POST['postId'] ) ? sanitize_text_field( $_POST['postId'] ) : '';
386
387 $taxonomy = isset( $_POST['taxonomy'] ) ? ultimate_post()->ultp_rest_sanitize_params( $_POST['taxonomy'] ) : '[]';
388
389 $author = isset( $_POST['author'] ) ? ultimate_post()->ultp_rest_sanitize_params( $_POST['author'] ) : false;
390 $orderby = isset( $_POST['orderby'] ) ? sanitize_text_field( $_POST['orderby'] ) : 'title'; // default orderbyt title requested from support
391 $order = isset( $_POST['order'] ) ? sanitize_text_field( $_POST['order'] ) : 'DESC';
392 $search = isset( $_POST['search'] ) ? sanitize_text_field( $_POST['search'] ) : '';
393 $adv_sort = isset( $_POST['adv_sort'] ) ? sanitize_text_field( $_POST['adv_sort'] ) : '';
394
395 $adv_filter_data = array(
396 'is_adv' => true,
397 'filterShow' => true,
398 'checkFilter' => true,
399 'author' => $author ? wp_json_encode( $author ) : false,
400 'orderby' => $orderby,
401 'order' => $order,
402 'search' => $search,
403 'adv_sort' => $adv_sort,
404 );
405
406 $blockRaw = isset( $_POST['blockName'] ) ? sanitize_text_field( $_POST['blockName'] ) : '';
407 $blockName = str_replace( '_', '/', $blockRaw );
408 $post = get_post( $postId );
409 $widgetBlockId = isset( $_POST['widgetBlockId'] ) ? sanitize_text_field( $_POST['widgetBlockId'] ) : '';
410 $toReturn = array();
411
412 $ultp_uniqueIds = isset( $_POST['ultpUniqueIds'] ) ? json_decode( stripslashes( sanitize_text_field( $_POST['ultpUniqueIds'] ) ), true ) : array();
413 $ultp_current_unique_posts = isset( $_POST['ultpCurrentUniquePosts'] ) ? json_decode( stripslashes( sanitize_text_field( $_POST['ultpCurrentUniquePosts'] ) ), true ) : array();
414
415 if ( $widgetBlockId ) {
416 $blocks = parse_blocks( get_option( 'widget_block' )[ $widgetBlockId ]['content'] );
417 $data = $this->filter_content_return( $blocks, $blockId, $blockRaw, $blockName, 'multiTaxonomy', $taxonomy, $postId, $toReturn, $widgetBlockId, $adv_filter_data, $ultp_uniqueIds, $ultp_current_unique_posts );
418 } elseif ( has_blocks( $post->post_content ) ) {
419 $blocks = parse_blocks( $post->post_content );
420 $data = $this->filter_content_return( $blocks, $blockId, $blockRaw, $blockName, 'multiTaxonomy', $taxonomy, $postId, $toReturn, '', $adv_filter_data, $ultp_uniqueIds, $ultp_current_unique_posts );
421 }
422 return wp_send_json_success(
423 array(
424 'filteredData' => $data,
425 )
426 );
427 }
428
429 /**
430 * Pagination of the Blocks.
431 *
432 * @since v.1.0.0
433 *
434 * @return STRING The AJAX response.
435 */
436 public function ultp_pagination_callback() {
437 if ( ! ( isset( $_REQUEST['wpnonce'] ) && wp_verify_nonce( sanitize_key( wp_unslash( $_REQUEST['wpnonce'] ) ), 'ultp-nonce' ) ) ) {
438 return;
439 }
440
441 $paged = isset( $_POST['paged'] ) ? sanitize_text_field( $_POST['paged'] ) : '';
442 if ( $paged ) {
443 $blockId = isset( $_POST['blockId'] ) ? sanitize_text_field( $_POST['blockId'] ) : '';
444 $postId = isset( $_POST['postId'] ) ? sanitize_text_field( $_POST['postId'] ) : '';
445 $blockRaw = isset( $_POST['blockName'] ) ? sanitize_text_field( $_POST['blockName'] ) : '';
446 $builder = isset( $_POST['builder'] ) ? sanitize_text_field( $_POST['builder'] ) : '';
447 $blockName = str_replace( '_', '/', $blockRaw );
448 $post = get_post( $postId );
449 $widgetBlockId = isset( $_POST['widgetBlockId'] ) ? sanitize_text_field( $_POST['widgetBlockId'] ) : '';
450 $exclude_post_id = isset( $_POST['exclude'] ) ? sanitize_text_field( $_POST['exclude'] ) : '';
451
452 $is_adv = isset( $_POST['isAdv'] ) ? ultimate_post()->ultp_rest_sanitize_params( $_POST['isAdv'] ) : false;
453
454 $filterValue = array();
455
456 if ( isset( $_POST['filterValue'] ) ) {
457 if ( is_array( $_POST['filterValue'] ) ) {
458 $filterValue = ultimate_post()->ultp_rest_sanitize_params( $_POST['filterValue'] );
459 } else {
460 $decoded = json_decode( $_POST['filterValue'] );
461 if ( is_array( $decoded ) ) {
462 $filterValue = ultimate_post()->ultp_rest_sanitize_params( $decoded );
463 } elseif ( $decoded ) {
464 $filterValue = sanitize_text_field( $_POST['filterValue'] );
465 }
466 }
467 }
468
469 $filterType = isset( $_POST['filterType'] ) ? sanitize_text_field( $_POST['filterType'] ) : '';
470 $filterShow = isset( $_POST['filterShow'] ) ? sanitize_text_field( $_POST['filterShow'] ) : false;
471 $checkFilter = isset( $_POST['checkFilter'] ) ? sanitize_text_field( $_POST['checkFilter'] ) : false;
472 $author = isset( $_POST['author'] ) ? sanitize_text_field( $_POST['author'] ) : false;
473 $orderby = isset( $_POST['orderby'] ) ? sanitize_text_field( $_POST['orderby'] ) : 'title'; // default orderbyt title requested from support
474 $order = isset( $_POST['order'] ) ? sanitize_text_field( $_POST['order'] ) : 'DESC';
475 $search = isset( $_POST['search'] ) ? sanitize_text_field( $_POST['search'] ) : '';
476 $adv_sort = isset( $_POST['adv_sort'] ) ? sanitize_text_field( $_POST['adv_sort'] ) : '';
477
478 $adv_filter_data = array(
479 'is_adv' => filter_var( $is_adv, FILTER_VALIDATE_BOOLEAN ),
480 'filterShow' => filter_var( $filterShow, FILTER_VALIDATE_BOOLEAN ),
481 'checkFilter' => filter_var( $checkFilter, FILTER_VALIDATE_BOOLEAN ),
482 'author' => $author ? wp_json_encode( $author ) : false,
483 'orderby' => $orderby,
484 'order' => $order,
485 'search' => $search,
486 'adv_sort' => $adv_sort,
487 );
488
489 $ultp_uniqueIds = isset( $_POST['ultpUniqueIds'] ) ? json_decode( stripslashes( sanitize_text_field( $_POST['ultpUniqueIds'] ) ), true ) : array();
490 $ultp_current_unique_posts = isset( $_POST['ultpCurrentUniquePosts'] ) ? json_decode( stripslashes( sanitize_text_field( $_POST['ultpCurrentUniquePosts'] ) ), true ) : array();
491
492 if ( $widgetBlockId ) {
493 $blocks = parse_blocks( get_option( 'widget_block' )[ $widgetBlockId ]['content'] );
494 $this->pagination_content_return( $blocks, $paged, $blockId, $blockRaw, $blockName, $builder, '', $filterValue, $filterType, $ultp_uniqueIds, $ultp_current_unique_posts, $widgetBlockId, '', $adv_filter_data );
495 } elseif ( has_blocks( $post->post_content ) ) {
496 $blocks = parse_blocks( $post->post_content );
497 $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 );
498 }
499 }
500 }
501
502 /**
503 * Share Count callback.
504 *
505 * @since v.1.0.0
506 *
507 * @return void
508 */
509 public function ultp_share_count_callback() {
510 if ( ! ( isset( $_REQUEST['wpnonce'] ) && wp_verify_nonce( sanitize_key( wp_unslash( $_REQUEST['wpnonce'] ) ), 'ultp-nonce' ) ) ) {
511 return;
512 }
513
514 $post_id = isset( $_POST['postId'] ) ? absint( $_POST['postId'] ) : 0;
515
516 // Validate post ID
517 if ( ! $post_id ) {
518 wp_send_json_error( array( 'message' => 'Invalid post ID' ) );
519 return;
520 }
521
522 // Check if post exists
523 $post = get_post( $post_id );
524 if ( ! $post ) {
525 wp_send_json_error( array( 'message' => 'Post not found' ) );
526 return;
527 }
528
529 // Block private posts
530 if ( $post->post_status === 'private' && ! current_user_can( 'read_private_posts' ) ) {
531 wp_send_json_error( array( 'message' => 'Private post' ) );
532 return;
533 }
534
535 // Block draft/pending posts
536 if ( in_array( $post->post_status, array( 'draft', 'pending' ), true )
537 && ! current_user_can( 'edit_post', $post->ID ) ) {
538 wp_send_json_error( array( 'message' => 'Not allowed' ) );
539 return;
540 }
541
542 // Block password protected posts
543 if ( post_password_required( $post ) ) {
544 wp_send_json_error( array( 'message' => 'Password protected' ) );
545 return;
546 }
547
548 // Only allow published posts to have share count updated
549 if ( $post->post_status !== 'publish' ) {
550 wp_send_json_error( array( 'message' => 'Post not published' ) );
551 return;
552 }
553
554 // Rate limiting - 60 second cooldown per user per post
555 $user_identifier = is_user_logged_in() ? 'user_' . get_current_user_id() : 'ip_' . sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ?? '' ) );
556 $transient_key = 'ultp_share_' . $post_id . '_' . md5( $user_identifier );
557
558 if ( get_transient( $transient_key ) ) {
559 return; // Already shared within last 60 seconds
560 }
561
562 set_transient( $transient_key, true, 60 ); // 60 seconds cooldown
563
564 // Always increment by 1 from database - ignore POST shareCount
565 $current_count = get_post_meta( $post_id, 'share_count', true );
566 $current_count = $current_count ? absint( $current_count ) : 0;
567 $new_count = $current_count + 1;
568
569 update_post_meta( $post_id, 'share_count', $new_count );
570 }
571
572 /**
573 * Filter Callback of the Blocks.
574 *
575 * @since v.1.0.0
576 *
577 * @param ARRAY $blocks The blocks array.
578 * @param STRING $blockId The block ID.
579 * @param STRING $blockRaw The raw block name.
580 * @param STRING $blockName The block name.
581 * @param STRING $taxtype The taxonomy type.
582 * @param STRING|ARRAY $taxonomy The taxonomy.
583 * @param STRING $postId The post ID.
584 * @param ARRAY $toReturn The array to return.
585 * @param STRING $widgetBlockId The widget block ID.
586 * @param ARRAY $adv_filter_data The advanced filter data.
587 * @param ARRAY $ultp_uniqueIds The unique IDs.
588 * @param ARRAY $ultp_current_unique_posts The current unique posts.
589 * @return ARRAY
590 */
591 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() ) {
592 foreach ( $blocks as $key => $value ) {
593 if ( $blockName == $value['blockName'] ) {
594 if ( $value['attrs']['blockId'] == $blockId ) {
595 $objName = str_replace( ' ', '_', ucwords( str_replace( array( 'ultimate-post/', '-' ), array( '', ' ' ), $blockName ) ) );
596 $new_obj = '\ULTP\blocks\\' . $objName;
597 $objectBlock = new $new_obj();
598 $attr = $objectBlock->get_attributes( true );
599 if ( $taxonomy ) {
600
601 if ( isset( $adv_filter_data['is_adv'] ) && $adv_filter_data['is_adv'] ) {
602 $value['attrs']['queryTaxValue'] = wp_json_encode( $taxonomy );
603 $value['attrs']['queryRelation'] = 'AND';
604 $value['attrs']['queryAuthor'] = $adv_filter_data['author'];
605 $value['attrs']['queryOrderBy'] = $adv_filter_data['orderby'];
606 $value['attrs']['queryOrder'] = $adv_filter_data['order'];
607 $value['attrs']['querySearch'] = $adv_filter_data['search'];
608 $value['attrs']['queryQuick'] = $adv_filter_data['adv_sort'];
609 } else {
610 $value['attrs']['queryTaxValue'] = wp_json_encode( array( $taxonomy ) );
611 }
612
613 $value['attrs']['queryTax'] = $taxtype;
614 $value['attrs']['ajaxCall'] = true;
615 }
616 if ( isset( $value['attrs']['queryNumber'] ) ) {
617 $value['attrs']['queryNumber'] = $value['attrs']['queryNumber'];
618 }
619
620 if ( isset( $value['attrs']['queryUnique'] ) && $value['attrs']['queryUnique'] ) {
621 $value['attrs']['loadMoreQueryUnique'] = $ultp_uniqueIds;
622 $ultp_uniqueIds[ $value['attrs']['queryUnique'] ] = array_diff( $ultp_uniqueIds[ $value['attrs']['queryUnique'] ], $ultp_current_unique_posts );
623 $value['attrs']['savedQueryUnique'] = $ultp_uniqueIds;
624 $value['attrs']['ultp_current_unique_posts'] = $ultp_current_unique_posts;
625 }
626
627 $attr = array_merge( $attr, $value['attrs'] );
628
629 $filter_attributes = array();
630
631 $filter_attributes['isAdv'] = isset( $adv_filter_data['is_adv'] ) ? $adv_filter_data['is_adv'] : false;
632 $filter_attributes['queryTaxValue'] = $filter_attributes['isAdv'] ? wp_json_encode( $taxonomy ) : $taxonomy;
633 $filter_attributes['queryTax'] = $taxtype;
634
635 if ( $filter_attributes['isAdv'] ) {
636 $filter_attributes['queryAuthor'] = $adv_filter_data['author'];
637 $filter_attributes['queryOrderBy'] = $adv_filter_data['orderby'];
638 $filter_attributes['queryOrder'] = $adv_filter_data['order'];
639 $filter_attributes['querySearch'] = $adv_filter_data['search'];
640 $filter_attributes['queryQuick'] = $adv_filter_data['adv_sort'];
641 }
642
643 $toReturn = array(
644 'blocks' => $objectBlock->content( $attr, true ),
645 'notFound' => isset( $attr['notFoundMessage'] ) ? $attr['notFoundMessage'] : '',
646 'pagination' => $this->pagination_for_filter( $attr, $postId, $blockRaw, $filter_attributes ),
647 'paginationType' => $attr['paginationType'],
648 'paginationShow' => $attr['paginationShow'],
649 );
650 }
651 }
652 if ( ! empty( $value['innerBlocks'] ) ) {
653 $this->filter_content_return( $value['innerBlocks'], $blockId, $blockRaw, $blockName, $taxtype, $taxonomy, $postId, $toReturn, $widgetBlockId, $adv_filter_data, $ultp_uniqueIds, $ultp_current_unique_posts );
654 }
655 }
656 return $toReturn;
657 }
658
659 /**
660 * Pagination for filter callback
661 *
662 * @since v.2.8.9
663 *
664 * @param ARRAY $attr .
665 * @param STRING $postId .
666 * @param STRING $blockRaw .
667 * @param ARRAY $filter_attributes .
668 * @return STRING
669 */
670 public function pagination_for_filter( $attr, $postId, $blockRaw, $filter_attributes ) {
671 $attr['queryNumber'] = ultimate_post()->get_post_number( 4, $attr['queryNumber'], $attr['queryNumPosts'] );
672 $recent_posts = new \WP_Query( ultimate_post()->get_query( $attr ) );
673 $pageNum = ultimate_post()->get_page_number( $attr, $recent_posts->found_posts );
674
675 $datasets = ultimate_post()->get_adv_data_attrs( null, $filter_attributes );
676 $datasets .= ' data-for="ultp-block-' . sanitize_html_class( $attr['blockId'] ) . '" ';
677
678 $wraper_after = '';
679 $style = $pageNum == 1 ? 'style="display:none"' : '';
680
681 if ( $attr['paginationType'] == 'loadMore' ) {
682 $wraper_after .= '<div ' . $style . ' class="ultp-loadmore "' . '>';
683 $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>';
684 $wraper_after .= '</div>';
685 } elseif ( $attr['paginationType'] == 'navigation' ) {
686 $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 . '>';
687 $wraper_after .= ultimate_post()->next_prev();
688 $wraper_after .= '</div>';
689 } elseif ( $attr['paginationType'] == 'pagination' ) {
690 $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 . '>';
691
692 $wraper_after .= ultimate_post()->pagination(
693 $pageNum,
694 $attr['paginationNav'],
695 $attr['paginationText'],
696 $attr['paginationAjax'],
697 isset( $_SERVER['HTTP_REFERER'] ) ? esc_url_raw( $_SERVER['HTTP_REFERER'] ) : '',
698 $attr['blockId']
699 );
700
701 $wraper_after .= '</div>';
702 }
703 wp_reset_query();
704
705 return $wraper_after;
706 }
707 }
708