| 1 |
<?php |
| 2 |
namespace UltimatePostKit\Modules\EliteGrid; |
| 3 |
|
| 4 |
use UltimatePostKit\Base\Ultimate_Post_Kit_Module_Base; |
| 5 |
use UltimatePostKit\Traits\Global_Widget_Functions; |
| 6 |
use UltimatePostKit\Utils; |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 9 |
|
| 10 |
class Module extends Ultimate_Post_Kit_Module_Base { |
| 11 |
|
| 12 |
use Global_Widget_Functions; |
| 13 |
|
| 14 |
public function __construct() { |
| 15 |
parent::__construct(); |
| 16 |
|
| 17 |
add_action( 'wp_ajax_nopriv_upk_elite_grid_loadmore_posts', [ $this, 'callback_ajax_loadmore_posts' ] ); |
| 18 |
add_action( 'wp_ajax_upk_elite_grid_loadmore_posts', [ $this, 'callback_ajax_loadmore_posts' ] ); |
| 19 |
} |
| 20 |
|
| 21 |
public function get_name() { |
| 22 |
return 'elite-grid'; |
| 23 |
} |
| 24 |
|
| 25 |
public function get_widgets() { |
| 26 |
|
| 27 |
$widgets = [ |
| 28 |
'Elite_Grid', |
| 29 |
]; |
| 30 |
|
| 31 |
return $widgets; |
| 32 |
} |
| 33 |
|
| 34 |
public function callback_ajax_loadmore_posts() { |
| 35 |
|
| 36 |
$settings = []; |
| 37 |
|
| 38 |
if ( isset( $_POST['settings'] ) && is_array( $_POST['settings'] ) ) { |
| 39 |
$settings = map_deep( wp_unslash( $_POST['settings'] ), 'sanitize_text_field' ); |
| 40 |
} |
| 41 |
|
| 42 |
$post_type = $settings['posts_source'] ?? 'post'; |
| 43 |
|
| 44 |
$settings = array_merge( |
| 45 |
[ |
| 46 |
'posts_source' => 'post', |
| 47 |
'posts_orderby' => 'date', |
| 48 |
'posts_order' => 'DESC', |
| 49 |
'posts_ignore_sticky_posts' => 'no', |
| 50 |
'posts_only_with_featured_image' => 'no', |
| 51 |
'posts_select_date' => '', |
| 52 |
'posts_exclude_by' => [], |
| 53 |
'posts_include_by' => [], |
| 54 |
'posts_per_page' => isset( $_POST['per_page'] ) ? absint( $_POST['per_page'] ) : 0, |
| 55 |
'posts_offset' => isset( $_POST['offset'] ) ? absint( $_POST['offset'] ) : 0, |
| 56 |
], |
| 57 |
$settings |
| 58 |
); |
| 59 |
|
| 60 |
$ajaxposts = $this->query_args($settings); |
| 61 |
|
| 62 |
ob_start(); |
| 63 |
$found_posts = false; |
| 64 |
|
| 65 |
if ( $ajaxposts->have_posts() ) { |
| 66 |
|
| 67 |
$placeholder = \Elementor\Utils::get_placeholder_image_src(); |
| 68 |
$meta_sep = $settings['meta_separator'] ?? '|'; |
| 69 |
|
| 70 |
while ( $ajaxposts->have_posts() ) { |
| 71 |
|
| 72 |
$ajaxposts->the_post(); |
| 73 |
$found_posts = true; |
| 74 |
|
| 75 |
$title = get_the_title(); |
| 76 |
$post_link = esc_url( get_permalink() ); |
| 77 |
$author_url = esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ); |
| 78 |
$author_name = esc_html( get_the_author() ); |
| 79 |
$title_tag = Utils::get_valid_html_tag($settings['title_tags']); |
| 80 |
|
| 81 |
$image_src = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large' ); |
| 82 |
$image_src = $image_src ? $image_src[0] : $placeholder; |
| 83 |
|
| 84 |
$onclick = ''; |
| 85 |
if ( ! empty( $settings['global_link'] ) && $settings['global_link'] === 'yes' ) { |
| 86 |
$onclick = 'onclick="window.open(\'' . esc_url( $post_link ) . '\', \'_self\')"'; |
| 87 |
} |
| 88 |
|
| 89 |
?> |
| 90 |
<div <?php echo $onclick; ?> class="upk-item"> |
| 91 |
<div class="upk-item-box"> |
| 92 |
<div class="upk-image-wrap"> |
| 93 |
<div class="upk-main-image"> |
| 94 |
<img class="upk-img" src="<?php echo esc_url($image_src); ?>" alt="<?php echo esc_attr($title); ?>"> |
| 95 |
</div> |
| 96 |
|
| 97 |
<?php if ($settings['show_author'] === 'yes') : ?> |
| 98 |
<div class="upk-author-wrap"> |
| 99 |
<div class="upk-author-img-wrap"> |
| 100 |
<?php echo get_avatar(get_the_author_meta('ID'), 48); ?> |
| 101 |
</div> |
| 102 |
<div class="upk-author-info-warp"> |
| 103 |
<span class="author-name"> |
| 104 |
<a class="name" href="<?php echo $author_url; ?>"> |
| 105 |
<?php echo $author_name; ?> |
| 106 |
</a> |
| 107 |
</span> |
| 108 |
<span class="author-depertment"> |
| 109 |
<?php |
| 110 |
$aid = get_the_author_meta('ID'); |
| 111 |
echo ucwords(get_user_role($aid)); |
| 112 |
?> |
| 113 |
</span> |
| 114 |
</div> |
| 115 |
</div> |
| 116 |
<?php endif; ?> |
| 117 |
|
| 118 |
<?php if ($settings['show_category'] === 'yes' || $settings['show_date'] === 'yes') : ?> |
| 119 |
<div class="upk-meta-wrap"> |
| 120 |
<?php if ($settings['show_date'] === 'yes' || $settings['show_reading_time'] === 'yes') : ?> |
| 121 |
<div class="upk-flex upk-flex-middle upk-date-reading-wrap"> |
| 122 |
<?php if ($settings['show_date'] === 'yes') : ?> |
| 123 |
<div class="upk-date"> |
| 124 |
<?php |
| 125 |
if ($settings['human_diff_time'] === 'yes') { |
| 126 |
echo esc_html( |
| 127 |
ultimate_post_kit_post_time_diff( |
| 128 |
($settings['human_diff_time_short'] == 'yes') ? 'short' : '' |
| 129 |
) |
| 130 |
); |
| 131 |
} else { |
| 132 |
echo esc_html(get_the_date()); |
| 133 |
} |
| 134 |
?> |
| 135 |
</div> |
| 136 |
<?php if ($settings['show_time'] === 'yes' && $settings['human_diff_time'] !== 'yes') : ?> |
| 137 |
<div class="upk-post-time"> |
| 138 |
<i class="upk-icon-clock" aria-hidden="true"></i> |
| 139 |
<?php echo esc_html(get_the_time()); ?> |
| 140 |
</div> |
| 141 |
<?php endif; ?> |
| 142 |
<?php endif; ?> |
| 143 |
|
| 144 |
<?php if (function_exists('ultimate_post_kit_reading_time') && $settings['show_reading_time'] === 'yes') : ?> |
| 145 |
<?php $speed = (int)($settings['avg_reading_speed'] ?? 200); ?> |
| 146 |
<div class="upk-reading-time" data-separator="<?php echo esc_attr($meta_sep); ?>"> |
| 147 |
<?php echo esc_html( ultimate_post_kit_reading_time( get_the_content(), $speed, $settings['hide_seconds'] ?? 'no', $settings['hide_minutes'] ?? 'no' ) ); ?> |
| 148 |
</div> |
| 149 |
<?php endif; ?> |
| 150 |
</div> |
| 151 |
<?php endif; ?> |
| 152 |
|
| 153 |
<?php if ($settings['show_category'] === 'yes') : ?> |
| 154 |
<div class="upk-category"><?php echo upk_get_category($post_type); ?></div> |
| 155 |
<?php endif; ?> |
| 156 |
</div> |
| 157 |
<?php endif; ?> |
| 158 |
</div> |
| 159 |
|
| 160 |
<div class="upk-content"> |
| 161 |
<div> |
| 162 |
<?php if (!isset($settings['show_title']) || $settings['show_title'] === 'yes') : ?> |
| 163 |
<<?php echo esc_attr( $title_tag ); ?> class="upk-title"> |
| 164 |
<a |
| 165 |
href="<?php echo $post_link; ?>" |
| 166 |
title="<?php echo esc_attr($title); ?>" |
| 167 |
class="title-animation-<?php echo esc_attr($settings['title_style']); ?>" |
| 168 |
<?php echo $settings['upk_link_new_tab'] === 'yes' ? 'target="_blank"' : ''; ?> |
| 169 |
> |
| 170 |
<?php echo esc_html($title); ?> |
| 171 |
</a> |
| 172 |
</<?php echo esc_attr( $title_tag ); ?>> |
| 173 |
<?php endif; ?> |
| 174 |
|
| 175 |
<?php if ($settings['show_excerpt'] === 'yes') : ?> |
| 176 |
<div class="upk-text-wrap"> |
| 177 |
<div class="upk-text"> |
| 178 |
<?php |
| 179 |
echo esc_html( |
| 180 |
wp_trim_words( |
| 181 |
get_the_excerpt(), |
| 182 |
absint($settings['excerpt_length'] ?? 20) |
| 183 |
) |
| 184 |
); |
| 185 |
?> |
| 186 |
</div> |
| 187 |
</div> |
| 188 |
<?php endif; ?> |
| 189 |
|
| 190 |
<?php if ( |
| 191 |
$settings['show_category'] === 'yes' || |
| 192 |
$settings['show_date'] === 'yes' || |
| 193 |
$settings['show_reading_time'] === 'yes' |
| 194 |
) : ?> |
| 195 |
<div class="upk-meta-list upk-flex upk-flex-middle"> |
| 196 |
<?php if ($settings['show_category'] === 'yes') : ?> |
| 197 |
<div class="upk-category"><?php echo upk_get_category($post_type); ?></div> |
| 198 |
<?php endif; ?> |
| 199 |
|
| 200 |
<?php if ($settings['show_date'] === 'yes') : ?> |
| 201 |
<div class="upk-date"> |
| 202 |
<?php |
| 203 |
if ($settings['human_diff_time'] === 'yes') { |
| 204 |
echo esc_html( |
| 205 |
ultimate_post_kit_post_time_diff( |
| 206 |
($settings['human_diff_time_short'] == 'yes') ? 'short' : '' |
| 207 |
) |
| 208 |
); |
| 209 |
} else { |
| 210 |
echo esc_html(get_the_date()); |
| 211 |
} |
| 212 |
?> |
| 213 |
</div> |
| 214 |
<?php if ($settings['show_time'] === 'yes' && $settings['human_diff_time'] !== 'yes') : ?> |
| 215 |
<div class="upk-post-time"> |
| 216 |
<i class="upk-icon-clock" aria-hidden="true"></i> |
| 217 |
<?php echo esc_html(get_the_time()); ?> |
| 218 |
</div> |
| 219 |
<?php endif; ?> |
| 220 |
<?php endif; ?> |
| 221 |
|
| 222 |
<?php if (function_exists('ultimate_post_kit_reading_time') && $settings['show_reading_time'] === 'yes') : ?> |
| 223 |
<?php $speed = (int)($settings['avg_reading_speed'] ?? 200); ?> |
| 224 |
<div class="upk-reading-time" data-separator="<?php echo esc_attr($meta_sep); ?>"> |
| 225 |
<?php echo esc_html(ultimate_post_kit_reading_time(get_the_content(), $speed, $settings['hide_seconds'] ?? 'no', $settings['hide_minutes'] ?? 'no' ) ); ?> |
| 226 |
</div> |
| 227 |
<?php endif; ?> |
| 228 |
</div> |
| 229 |
<?php endif; ?> |
| 230 |
</div> |
| 231 |
</div> |
| 232 |
</div> |
| 233 |
</div> |
| 234 |
<?php |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
wp_reset_postdata(); |
| 239 |
$markup = ob_get_clean(); |
| 240 |
|
| 241 |
wp_send_json( |
| 242 |
[ |
| 243 |
'success' => $found_posts, |
| 244 |
'markup' => $found_posts ? $markup : esc_html__( 'No more found', 'ultimate-post-kit' ), |
| 245 |
] |
| 246 |
); |
| 247 |
} |
| 248 |
} |
| 249 |
|