| 1 |
<?php |
| 2 |
namespace UltimatePostKit\Modules\AlexGrid; |
| 3 |
|
| 4 |
use UltimatePostKit\Base\Ultimate_Post_Kit_Module_Base; |
| 5 |
use UltimatePostKit\Traits\Global_Widget_Functions; |
| 6 |
use Elementor\Icons_Manager; |
| 7 |
use UltimatePostKit\Utils; |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 10 |
|
| 11 |
class Module extends Ultimate_Post_Kit_Module_Base { |
| 12 |
|
| 13 |
use Global_Widget_Functions; |
| 14 |
|
| 15 |
public function __construct() { |
| 16 |
parent::__construct(); |
| 17 |
|
| 18 |
add_action('wp_ajax_nopriv_upk_alex_grid_loadmore_posts', [$this, 'callback_ajax_loadmore_posts']); |
| 19 |
add_action('wp_ajax_upk_alex_grid_loadmore_posts', [$this, 'callback_ajax_loadmore_posts']); |
| 20 |
} |
| 21 |
|
| 22 |
public function get_name() { |
| 23 |
return 'alex-grid'; |
| 24 |
} |
| 25 |
|
| 26 |
public function get_widgets() { |
| 27 |
|
| 28 |
$widgets = [ |
| 29 |
'Alex_Grid', |
| 30 |
]; |
| 31 |
|
| 32 |
return $widgets; |
| 33 |
} |
| 34 |
|
| 35 |
public function callback_ajax_loadmore_posts() { |
| 36 |
|
| 37 |
// Security: Verify nonce |
| 38 |
if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'upk-site' ) ) { |
| 39 |
wp_send_json_error( [ 'message' => esc_html__( 'Security verification failed', 'ultimate-post-kit' ) ], 403 ); |
| 40 |
wp_die(); |
| 41 |
} |
| 42 |
|
| 43 |
$settings = []; |
| 44 |
|
| 45 |
if ( isset( $_POST['settings'] ) && is_array( $_POST['settings'] ) ) { |
| 46 |
$settings = map_deep( wp_unslash( $_POST['settings'] ), 'sanitize_text_field' ); |
| 47 |
} |
| 48 |
|
| 49 |
$post_type = $settings['post_source'] ?? 'post'; |
| 50 |
|
| 51 |
// Security: Enforce query limits to prevent DoS |
| 52 |
$per_page = isset( $_POST['per_page'] ) ? absint( $_POST['per_page'] ) : 6; |
| 53 |
$per_page = min( $per_page, 50 ); // Maximum 50 posts per request |
| 54 |
$offset = isset( $_POST['offset'] ) ? absint( $_POST['offset'] ) : 0; |
| 55 |
$offset = min( $offset, 1000 ); // Maximum offset of 1000 |
| 56 |
|
| 57 |
// Security: Whitelist allowed post types |
| 58 |
$allowed_post_types = [ 'post', 'page' ]; |
| 59 |
$allowed_post_types = apply_filters( 'upk_alex_grid_allowed_post_types', $allowed_post_types ); |
| 60 |
$post_type = in_array( $post_type, $allowed_post_types, true ) ? $post_type : 'post'; |
| 61 |
|
| 62 |
// Security: Whitelist orderby values |
| 63 |
$allowed_orderby = [ 'date', 'title', 'modified', 'rand', 'comment_count', 'menu_order' ]; |
| 64 |
$posts_orderby = isset( $settings['posts_orderby'] ) && in_array( $settings['posts_orderby'], $allowed_orderby, true ) ? $settings['posts_orderby'] : 'date'; |
| 65 |
|
| 66 |
// Security: Whitelist order values |
| 67 |
$posts_order = isset( $settings['posts_order'] ) && in_array( strtoupper( $settings['posts_order'] ), [ 'ASC', 'DESC' ], true ) ? strtoupper( $settings['posts_order'] ) : 'DESC'; |
| 68 |
|
| 69 |
$settings = array_merge( |
| 70 |
[ |
| 71 |
'posts_source' => $post_type, |
| 72 |
'posts_orderby' => $posts_orderby, |
| 73 |
'posts_order' => $posts_order, |
| 74 |
'posts_ignore_sticky_posts' => 'no', |
| 75 |
'posts_only_with_featured_image' => 'no', |
| 76 |
'posts_select_date' => '', |
| 77 |
'posts_exclude_by' => [], |
| 78 |
'posts_include_by' => [], |
| 79 |
'posts_per_page' => $per_page, |
| 80 |
'posts_offset' => $offset, |
| 81 |
], |
| 82 |
$settings |
| 83 |
); |
| 84 |
|
| 85 |
$ajaxposts = $this->query_args( $settings ); |
| 86 |
|
| 87 |
// Security: Override post_status to ensure only published posts are shown |
| 88 |
if ( ! current_user_can( 'edit_posts' ) ) { |
| 89 |
$ajaxposts->query_vars['post_status'] = 'publish'; |
| 90 |
} |
| 91 |
|
| 92 |
ob_start(); |
| 93 |
$found_posts = false; |
| 94 |
|
| 95 |
if ($ajaxposts->have_posts()) : |
| 96 |
while ($ajaxposts->have_posts()) : |
| 97 |
$ajaxposts->the_post(); |
| 98 |
$found_posts = true; |
| 99 |
|
| 100 |
$title = get_the_title(); |
| 101 |
$post_link = esc_url(get_permalink()); |
| 102 |
$image_src = wp_get_attachment_image_url(get_post_thumbnail_id(), 'large'); |
| 103 |
$image_src = $image_src ? esc_url($image_src) : esc_url(\Elementor\Utils::get_placeholder_image_src()); |
| 104 |
$category = wp_kses_post(upk_get_category($post_type)); |
| 105 |
$author_url = esc_url(get_author_posts_url(get_the_author_meta('ID'))); |
| 106 |
$author_name = esc_html(get_the_author()); |
| 107 |
$title_tag = Utils::get_valid_html_tag($settings['title_tags'] ); |
| 108 |
|
| 109 |
$meta_separator = isset( $settings['meta_separator'] ) ? $settings['meta_separator'] : '|'; |
| 110 |
|
| 111 |
$onclick = ''; |
| 112 |
if (!empty($settings['global_link']) && $settings['global_link'] === 'yes') { |
| 113 |
$onclick = ' onclick="window.open(\'' . $post_link . '\', \'_self\')"'; |
| 114 |
} |
| 115 |
|
| 116 |
$date = ''; |
| 117 |
if (!empty($settings['human_diff_time']) && $settings['human_diff_time'] === 'yes') { |
| 118 |
$date = ultimate_post_kit_post_time_diff(($settings['human_diff_time_short'] === 'yes') ? 'short' : ''); |
| 119 |
} else { |
| 120 |
$date = esc_html(get_the_date()); |
| 121 |
} |
| 122 |
|
| 123 |
$format_icons = [ |
| 124 |
'aside' => 'upk-icon-aside', |
| 125 |
'gallery' => 'upk-icon-gallery', |
| 126 |
'link' => 'upk-icon-link', |
| 127 |
'image' => 'upk-icon-image', |
| 128 |
'quote' => 'upk-icon-quote', |
| 129 |
'status' => 'upk-icon-status', |
| 130 |
'video' => 'upk-icon-video', |
| 131 |
'audio' => 'upk-icon-music', |
| 132 |
'chat' => 'upk-icon-chat', |
| 133 |
]; |
| 134 |
|
| 135 |
$post_format_icon = isset($format_icons[get_post_format()]) ? $format_icons[get_post_format()] : 'upk-icon-post'; |
| 136 |
|
| 137 |
?> |
| 138 |
<div <?php if ( ! empty( $settings['global_link'] ) && $settings['global_link'] === 'yes' ) { printf( 'onclick="window.open(\'%s\', \'_self\')"', esc_url( $post_link ) ); } ?> class="upk-item"> |
| 139 |
<div class="upk-image-wrap"> |
| 140 |
<img class="upk-img" src="<?php echo esc_url( $image_src ); ?>" alt="<?php echo esc_attr($title); ?>"> |
| 141 |
|
| 142 |
<?php if ( |
| 143 |
$settings['show_author'] === 'yes' || |
| 144 |
$settings['show_date'] === 'yes' || |
| 145 |
$settings['show_time'] === 'yes' || |
| 146 |
$settings['show_reading_time'] === 'yes' |
| 147 |
) : ?> |
| 148 |
<div class="upk-meta"> |
| 149 |
<?php if ($settings['show_author'] === 'yes') : ?> |
| 150 |
<div class="upk-author-img"><?php echo wp_kses_post(get_avatar(get_the_author_meta('ID'), 48)); ?></div> |
| 151 |
<?php endif; ?> |
| 152 |
|
| 153 |
<div> |
| 154 |
<?php if ($settings['show_author'] === 'yes') : ?> |
| 155 |
<div class="upk-author-name"> |
| 156 |
<a href="<?php echo esc_url( $author_url ); ?>"><?php echo esc_html( $author_name ); ?></a> |
| 157 |
</div> |
| 158 |
<?php endif; ?> |
| 159 |
|
| 160 |
<div class="upk-flex upk-flex-middle upk-date-reading-wrap"> |
| 161 |
<?php if ( $settings['show_date'] === 'yes' ) : ?> |
| 162 |
<div class="upk-date"><?php echo $date; ?></div> |
| 163 |
<?php if ( $settings['show_time'] === 'yes' ) : ?> |
| 164 |
<div class="upk-post-time" data-separator="<?php echo esc_attr( $meta_separator ); ?>"> |
| 165 |
<i class="upk-icon-clock" aria-hidden="true"></i><?php echo esc_html( get_the_time() ); ?> |
| 166 |
</div> |
| 167 |
<?php endif; ?> |
| 168 |
<?php endif; ?> |
| 169 |
|
| 170 |
<?php if ( function_exists( '_is_upk_pro_activated' ) && _is_upk_pro_activated() && $settings['show_reading_time'] === 'yes' ) : ?> |
| 171 |
<div class="upk-reading-time" data-separator="<?php echo esc_attr( $meta_separator ); ?>"> |
| 172 |
<?php echo ultimate_post_kit_reading_time( get_the_content(), $settings['avg_reading_speed'], $settings['hide_seconds'] ?? 'no', $settings['hide_minutes'] ?? 'no' ); ?> |
| 173 |
</div> |
| 174 |
<?php endif; ?> |
| 175 |
</div> |
| 176 |
</div> |
| 177 |
</div> |
| 178 |
<?php endif; ?> |
| 179 |
|
| 180 |
<?php if ($settings['show_post_format'] === 'yes') : ?> |
| 181 |
<div class="upk-post-format"> |
| 182 |
<a href="<?php echo esc_url( $post_link ); ?>"> |
| 183 |
<i class="<?php echo esc_attr($post_format_icon); ?>" aria-hidden="true"></i> |
| 184 |
</a> |
| 185 |
</div> |
| 186 |
<?php endif; ?> |
| 187 |
</div> |
| 188 |
|
| 189 |
<div class="upk-content-wrap"> |
| 190 |
<div class="upk-content"> |
| 191 |
<?php if ($settings['show_category'] === 'yes') : ?> |
| 192 |
<div class="upk-category"><?php echo wp_kses_post( $category ); ?></div> |
| 193 |
<?php endif; ?> |
| 194 |
|
| 195 |
<?php if ($settings['show_title'] === 'yes') : ?> |
| 196 |
<<?php echo esc_attr( $title_tag ); ?> class="upk-title"> |
| 197 |
<a class="title-animation-<?php echo esc_attr($settings['title_style']); ?>" |
| 198 |
href="<?php echo esc_url( $post_link ); ?>" |
| 199 |
title="<?php echo esc_attr($title); ?>" |
| 200 |
<?php echo $settings['upk_link_new_tab'] === 'yes' ? 'target="_blank"' : ''; ?> |
| 201 |
> |
| 202 |
<?php echo esc_html($title); ?> |
| 203 |
</a> |
| 204 |
</<?php echo esc_attr( $title_tag); ?>> |
| 205 |
<?php endif; ?> |
| 206 |
</div> |
| 207 |
|
| 208 |
<?php if ($settings['show_readmore'] === 'yes') : ?> |
| 209 |
<div class="upk-button-wrap"> |
| 210 |
<a href="<?php echo esc_url( $post_link ); ?>" |
| 211 |
class="upk-readmore" |
| 212 |
target="<?php echo ($settings['upk_link_new_tab'] === 'yes') ? '_blank' : '_self'; ?>"> |
| 213 |
<span class="upk-readmore-icon"><span></span></span> |
| 214 |
</a> |
| 215 |
</div> |
| 216 |
<?php endif; ?> |
| 217 |
</div> |
| 218 |
</div> |
| 219 |
<?php |
| 220 |
endwhile; |
| 221 |
endif; |
| 222 |
|
| 223 |
wp_reset_postdata(); |
| 224 |
$markup = ob_get_clean(); |
| 225 |
|
| 226 |
wp_send_json( |
| 227 |
[ |
| 228 |
'success' => $found_posts, |
| 229 |
'markup' => $found_posts ? $markup : esc_html__('No more found', 'ultimate-post-kit'), |
| 230 |
] |
| 231 |
); |
| 232 |
} |
| 233 |
} |
| 234 |
|