PluginProbe
Post Grid Gutenberg Blocks – PostX / 5.0.13
Post Grid Gutenberg Blocks – PostX v5.0.13
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.13, at classes/Blocks.php

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