| 1 |
<?php |
| 2 |
namespace UltimatePostKit\Modules\MapleGrid; |
| 3 |
use UltimatePostKit\Traits\Global_Widget_Functions; |
| 4 |
|
| 5 |
use UltimatePostKit\Base\Ultimate_Post_Kit_Module_Base; |
| 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_maple_grid_loadmore_posts', [ $this, 'callback_ajax_loadmore_posts' ] ); |
| 18 |
add_action( 'wp_ajax_upk_maple_grid_loadmore_posts', [ $this, 'callback_ajax_loadmore_posts' ] ); |
| 19 |
} |
| 20 |
|
| 21 |
public function get_name() { |
| 22 |
return 'maple-grid'; |
| 23 |
} |
| 24 |
|
| 25 |
public function get_widgets() { |
| 26 |
|
| 27 |
$widgets = [ |
| 28 |
'Maple_Grid', |
| 29 |
]; |
| 30 |
|
| 31 |
return $widgets; |
| 32 |
} |
| 33 |
|
| 34 |
public function callback_ajax_loadmore_posts() { |
| 35 |
// Verify the front-end nonce (sent by UltimatePostKitConfig.nonce) before |
| 36 |
// processing this public load-more request. |
| 37 |
if ( ! check_ajax_referer( 'upk-site', 'nonce', false ) ) { |
| 38 |
wp_send_json_error( array( 'message' => esc_html__( 'Security check failed.', 'ultimate-post-kit' ) ), 403 ); |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
$settings = []; |
| 43 |
|
| 44 |
if ( isset( $_POST['settings'] ) && is_array( $_POST['settings'] ) ) { |
| 45 |
$settings = map_deep( wp_unslash( $_POST['settings'] ), 'sanitize_text_field' ); |
| 46 |
} |
| 47 |
|
| 48 |
$post_type = $settings['posts_source'] ?? 'post'; |
| 49 |
|
| 50 |
$settings = array_merge( |
| 51 |
[ |
| 52 |
'posts_source' => 'post', |
| 53 |
'posts_orderby' => 'date', |
| 54 |
'posts_order' => 'DESC', |
| 55 |
'posts_ignore_sticky_posts' => 'no', |
| 56 |
'posts_only_with_featured_image' => 'no', |
| 57 |
'posts_select_date' => '', |
| 58 |
'posts_exclude_by' => [], |
| 59 |
'posts_include_by' => [], |
| 60 |
'posts_per_page' => isset( $_POST['per_page'] ) ? absint( $_POST['per_page'] ) : 0, |
| 61 |
'posts_offset' => isset( $_POST['offset'] ) ? absint( $_POST['offset'] ) : 0, |
| 62 |
], |
| 63 |
$settings |
| 64 |
); |
| 65 |
|
| 66 |
// Fill display flags the request may have omitted (see trait) before the render loop reads them. |
| 67 |
$settings = array_merge( $this->loadmore_display_defaults(), $settings ); |
| 68 |
|
| 69 |
$ajaxposts = $this->query_args( $settings ); |
| 70 |
|
| 71 |
ob_start(); |
| 72 |
$found_posts = false; |
| 73 |
|
| 74 |
if ( $ajaxposts->have_posts() ) { |
| 75 |
$placeholder = \Elementor\Utils::get_placeholder_image_src(); |
| 76 |
|
| 77 |
while ( $ajaxposts->have_posts() ) { |
| 78 |
$ajaxposts->the_post(); |
| 79 |
$found_posts = true; |
| 80 |
|
| 81 |
$title = get_the_title(); |
| 82 |
$post_link = esc_url( get_permalink() ); |
| 83 |
$author_url = esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ); |
| 84 |
$author_name = esc_html( get_the_author() ); |
| 85 |
|
| 86 |
$image_src = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large' ); |
| 87 |
$image_src = $image_src ? $image_src[0] : $placeholder; |
| 88 |
|
| 89 |
$meta_sep = $settings['meta_separator'] ?? '-'; |
| 90 |
$grid_style = $settings['grid_style'] ?? '1'; |
| 91 |
$title_tag = Utils::get_valid_html_tag($settings['title_tags']); |
| 92 |
|
| 93 |
?> |
| 94 |
<div <?php if ( ! empty( $settings['global_link'] ) && $settings['global_link'] === 'yes' ) { printf( 'onclick="window.open(\'%s\', \'_self\')"', esc_url( $post_link ) ); } ?> class="upk-item"> |
| 95 |
<div class="upk-item-box"> |
| 96 |
<div class="upk-image-wrap"> |
| 97 |
<div class="upk-main-image"> |
| 98 |
<img class="upk-img" src="<?php echo esc_url( $image_src ); ?>" alt="<?php echo esc_attr( $title ); ?>"> |
| 99 |
</div> |
| 100 |
|
| 101 |
<?php if ( $settings['show_post_format'] === 'yes' && $grid_style !== '3' ) : ?> |
| 102 |
<div class="upk-post-format"> |
| 103 |
<a href="<?php echo esc_url($post_link); ?>"> |
| 104 |
<?php if ( has_post_format( 'aside' ) ) : ?> |
| 105 |
<i class="upk-icon-aside" aria-hidden="true"></i> |
| 106 |
<?php elseif ( has_post_format( 'gallery' ) ) : ?> |
| 107 |
<i class="upk-icon-gallery" aria-hidden="true"></i> |
| 108 |
<?php elseif ( has_post_format( 'link' ) ) : ?> |
| 109 |
<i class="upk-icon-link" aria-hidden="true"></i> |
| 110 |
<?php elseif ( has_post_format( 'image' ) ) : ?> |
| 111 |
<i class="upk-icon-image" aria-hidden="true"></i> |
| 112 |
<?php elseif ( has_post_format( 'quote' ) ) : ?> |
| 113 |
<i class="upk-icon-quote" aria-hidden="true"></i> |
| 114 |
<?php elseif ( has_post_format( 'status' ) ) : ?> |
| 115 |
<i class="upk-icon-status" aria-hidden="true"></i> |
| 116 |
<?php elseif ( has_post_format( 'video' ) ) : ?> |
| 117 |
<i class="upk-icon-video" aria-hidden="true"></i> |
| 118 |
<?php elseif ( has_post_format( 'audio' ) ) : ?> |
| 119 |
<i class="upk-icon-music" aria-hidden="true"></i> |
| 120 |
<?php elseif ( has_post_format( 'chat' ) ) : ?> |
| 121 |
<i class="upk-icon-chat" aria-hidden="true"></i> |
| 122 |
<?php else : ?> |
| 123 |
<i class="upk-icon-post" aria-hidden="true"></i> |
| 124 |
<?php endif; ?> |
| 125 |
</a> |
| 126 |
</div> |
| 127 |
<?php endif; ?> |
| 128 |
|
| 129 |
<?php if ( ( $settings['show_category'] ?? 'yes' ) === 'yes' && $grid_style !== '3' ) : ?> |
| 130 |
<div class="upk-category"> |
| 131 |
<?php echo wp_kses_post(upk_get_category( $post_type )); ?> |
| 132 |
</div> |
| 133 |
<?php endif; ?> |
| 134 |
</div> |
| 135 |
|
| 136 |
<div class="upk-content"> |
| 137 |
<div class="upk-content-inner"> |
| 138 |
<?php if ( ( $settings['show_author_avatar'] ?? 'yes' ) === 'yes' || ( $settings['show_author_name'] ?? 'yes' ) === 'yes' || ( $settings['show_date'] ?? 'yes' ) === 'yes' || ( $settings['show_reading_time'] ?? 'no' ) === 'yes' ) : ?> |
| 139 |
<div class="upk-meta"> |
| 140 |
<?php if ( $grid_style !== '3' && ( $settings['show_author_avatar'] == 'yes' || $settings['show_author_name'] == 'yes' ) ) : ?> |
| 141 |
<div class="upk-author"> |
| 142 |
<?php if ($settings['show_author_avatar'] == 'yes') : ?> |
| 143 |
<?php echo wp_kses_post(get_avatar(get_the_author_meta('ID'), 36)); ?> |
| 144 |
<?php endif; ?> |
| 145 |
|
| 146 |
<?php if ($settings['show_author_name'] == 'yes') : ?> |
| 147 |
<a class="author-name" href="<?php echo esc_url( get_author_posts_url(get_the_author_meta('ID')) ); ?>"> |
| 148 |
<?php echo esc_html( get_the_author() ); ?> |
| 149 |
</a> |
| 150 |
<?php endif; ?> |
| 151 |
</div> |
| 152 |
<?php endif; ?> |
| 153 |
|
| 154 |
<?php if ( ( $settings['show_date'] ?? 'yes' ) === 'yes' ) : ?> |
| 155 |
<div class="upk-blog-date" data-separator="<?php echo esc_html( $meta_sep ); ?>"> |
| 156 |
<div class="upk-blog-date"> |
| 157 |
<a class="date" href="#"> |
| 158 |
<i class="upk-icon-calendar" aria-hidden="true"></i> |
| 159 |
<?php |
| 160 |
if ( ( $settings['human_diff_time'] ?? 'no' ) === 'yes' ) { |
| 161 |
echo esc_html( ultimate_post_kit_post_time_diff( ( ( $settings['human_diff_time_short'] ?? 'no' ) === 'yes' ) ? 'short' : '' ) ); |
| 162 |
} else { |
| 163 |
echo get_the_date(); |
| 164 |
} |
| 165 |
?> |
| 166 |
</a> |
| 167 |
</div> |
| 168 |
<?php if ( ( $settings['show_time'] ?? 'no' ) === 'yes' ) : ?> |
| 169 |
<div class="upk-post-time"> |
| 170 |
<i class="upk-icon-clock" aria-hidden="true"></i> |
| 171 |
<?php echo esc_html( get_the_time() ); ?> |
| 172 |
</div> |
| 173 |
<?php endif; ?> |
| 174 |
</div> |
| 175 |
<?php endif; ?> |
| 176 |
|
| 177 |
<?php if ( function_exists( '_is_upk_pro_activated' ) && _is_upk_pro_activated() && function_exists( 'ultimate_post_kit_reading_time' ) && ( $settings['show_reading_time'] ?? '' ) === 'yes' ) : ?> |
| 178 |
<div class="upk-reading-time" data-separator="<?php echo esc_html( $meta_sep ); ?>"> |
| 179 |
<?php echo esc_html( ultimate_post_kit_reading_time( get_the_content(), $settings['avg_reading_speed'], $settings['hide_seconds'] ?? 'no', $settings['hide_minutes'] ?? 'no' ) ); ?> |
| 180 |
</div> |
| 181 |
<?php endif; ?> |
| 182 |
</div> |
| 183 |
<?php endif; ?> |
| 184 |
|
| 185 |
<?php if ( ! isset( $settings['show_title'] ) || $settings['show_title'] === 'yes' ) : ?> |
| 186 |
<<?php echo esc_attr( $title_tag ); ?> class="upk-title"> |
| 187 |
<a |
| 188 |
href="<?php echo esc_url($post_link); ?>" |
| 189 |
title="<?php echo esc_attr( $title ); ?>" |
| 190 |
class="title-animation-<?php echo esc_attr( $settings['title_style'] ); ?>" |
| 191 |
<?php echo $settings['upk_link_new_tab'] === 'yes' ? 'target="_blank"' : ''; ?> |
| 192 |
> |
| 193 |
<?php echo esc_html( $title ); ?> |
| 194 |
</a> |
| 195 |
</<?php echo esc_attr( $title_tag ); ?>> |
| 196 |
<?php endif; ?> |
| 197 |
|
| 198 |
<?php if ( ( $settings['show_excerpt'] ?? 'yes' ) === 'yes' && $grid_style !== '3' ) : ?> |
| 199 |
<div class="upk-text"> |
| 200 |
<?php |
| 201 |
if ( has_excerpt() ) { |
| 202 |
the_excerpt(); |
| 203 |
} else { |
| 204 |
if ( function_exists( 'ultimate_post_kit_custom_excerpt' ) ) { |
| 205 |
echo wp_kses_post( ultimate_post_kit_custom_excerpt( ultimate_post_kit_clamp_excerpt_length( $settings['excerpt_length'] ?? 20, 20 ), false, '' ) ); |
| 206 |
} else { |
| 207 |
echo esc_html( wp_trim_words( wp_strip_all_tags( get_the_content() ), ultimate_post_kit_clamp_excerpt_length( $settings['excerpt_length'] ?? 20, 20 ) ) ); |
| 208 |
} |
| 209 |
} |
| 210 |
?> |
| 211 |
</div> |
| 212 |
<?php endif; ?> |
| 213 |
</div> |
| 214 |
</div> |
| 215 |
</div> |
| 216 |
</div> |
| 217 |
<?php |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
wp_reset_postdata(); |
| 222 |
$markup = ob_get_clean(); |
| 223 |
|
| 224 |
wp_send_json( |
| 225 |
[ |
| 226 |
'success' => $found_posts, |
| 227 |
'markup' => $found_posts ? $markup : esc_html__( 'No more found', 'ultimate-post-kit' ), |
| 228 |
] |
| 229 |
); |
| 230 |
|
| 231 |
} |
| 232 |
} |