| 1 |
<?php |
| 2 |
namespace UltimatePostKit\Modules\AmoxGrid; |
| 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 |
|
| 16 |
parent::__construct(); |
| 17 |
|
| 18 |
add_action( 'wp_ajax_nopriv_upk_amox_grid_loadmore_posts', [ $this, 'callback_ajax_loadmore_posts' ] ); |
| 19 |
add_action( 'wp_ajax_upk_amox_grid_loadmore_posts', [ $this, 'callback_ajax_loadmore_posts' ] ); |
| 20 |
} |
| 21 |
|
| 22 |
public function get_name() { |
| 23 |
return 'amox-grid'; |
| 24 |
} |
| 25 |
|
| 26 |
public function get_widgets() { |
| 27 |
|
| 28 |
$widgets = [ |
| 29 |
'Amox_Grid', |
| 30 |
]; |
| 31 |
|
| 32 |
return $widgets; |
| 33 |
} |
| 34 |
|
| 35 |
public function callback_ajax_loadmore_posts() { |
| 36 |
// Verify the front-end nonce (sent by UltimatePostKitConfig.nonce) before |
| 37 |
// processing this public load-more request. |
| 38 |
if ( ! check_ajax_referer( 'upk-site', 'nonce', false ) ) { |
| 39 |
wp_send_json_error( array( 'message' => esc_html__( 'Security check failed.', 'ultimate-post-kit' ) ), 403 ); |
| 40 |
} |
| 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['posts_source'] ?? 'post'; |
| 50 |
|
| 51 |
$settings = array_merge( |
| 52 |
[ |
| 53 |
'posts_source' => 'post', |
| 54 |
'posts_orderby' => 'date', |
| 55 |
'posts_order' => 'DESC', |
| 56 |
'posts_ignore_sticky_posts' => 'no', |
| 57 |
'posts_only_with_featured_image' => 'no', |
| 58 |
'posts_select_date' => '', |
| 59 |
'posts_exclude_by' => [], |
| 60 |
'posts_include_by' => [], |
| 61 |
'posts_per_page' => isset( $_POST['per_page'] ) ? absint( $_POST['per_page'] ) : 0, |
| 62 |
'posts_offset' => isset( $_POST['offset'] ) ? absint( $_POST['offset'] ) : 0, |
| 63 |
], |
| 64 |
$settings |
| 65 |
); |
| 66 |
|
| 67 |
// Fill display flags the request may have omitted (see trait) before the render loop reads them. |
| 68 |
$settings = array_merge( $this->loadmore_display_defaults(), $settings ); |
| 69 |
|
| 70 |
$ajaxposts = $this->query_args( $settings ); |
| 71 |
|
| 72 |
ob_start(); |
| 73 |
$found_posts = false; |
| 74 |
|
| 75 |
if ( $ajaxposts->have_posts() ) { |
| 76 |
|
| 77 |
$i = 0; |
| 78 |
while ( $ajaxposts->have_posts() ) { |
| 79 |
$ajaxposts->the_post(); |
| 80 |
$found_posts = true; |
| 81 |
$i++; |
| 82 |
|
| 83 |
$title = get_the_title(); |
| 84 |
$post_link = esc_url( get_permalink() ); |
| 85 |
$meta_sep = $settings['meta_separator'] ?? '.'; |
| 86 |
$title_tag = Utils::get_valid_html_tag($settings['title_tags']); |
| 87 |
|
| 88 |
?> |
| 89 |
<div <?php if ( ! empty( $settings['global_link'] ) && $settings['global_link'] === 'yes' ) { printf( 'onclick="window.open(\'%s\', \'_self\')"', esc_url( $post_link ) ); } ?> class="upk-item"> |
| 90 |
<div class="upk-img-wrap"> |
| 91 |
<?php $this->render_image( get_post_thumbnail_id(), 'large' ); ?> |
| 92 |
</div> |
| 93 |
|
| 94 |
<div class="upk-content"> |
| 95 |
<?php if ( $settings['show_category'] === 'yes' ) : ?> |
| 96 |
<div class="upk-category"> |
| 97 |
<?php echo wp_kses_post( upk_get_category( $post_type ) ); ?> |
| 98 |
</div> |
| 99 |
<?php endif; ?> |
| 100 |
|
| 101 |
<?php if ( $settings['show_title'] === 'yes' ) : ?> |
| 102 |
<<?php echo esc_attr( $title_tag ); ?> class="upk-title"> |
| 103 |
<a |
| 104 |
href="<?php echo esc_url( $post_link ); ?>" |
| 105 |
title="<?php echo esc_attr( $title ); ?>" |
| 106 |
class="title-animation-<?php echo esc_attr( $settings['title_style'] ?? 'underline' ); ?>" |
| 107 |
<?php echo $settings['upk_link_new_tab'] === 'yes' ? 'target="_blank"' : '' ?> |
| 108 |
> |
| 109 |
<?php echo esc_html( $title ); ?> |
| 110 |
</a> |
| 111 |
</<?php echo esc_attr( $title_tag ); ?>> |
| 112 |
<?php endif; ?> |
| 113 |
|
| 114 |
<?php if ( |
| 115 |
$settings['show_comments'] === 'yes' || |
| 116 |
$settings['show_date'] === 'yes' || |
| 117 |
$settings['show_reading_time'] === 'yes' |
| 118 |
) : ?> |
| 119 |
<div class="upk-meta upk-flex-inline upk-flex-middle"> |
| 120 |
<?php if ( $settings['show_date'] === 'yes' ) : ?> |
| 121 |
<div class="upk-date"> |
| 122 |
<?php |
| 123 |
if ( $settings['show_date'] === 'yes' && $settings['human_diff_time'] !== 'yes' ) { |
| 124 |
echo esc_html( get_the_date() ); |
| 125 |
} |
| 126 |
if ( $settings['human_diff_time'] === 'yes' ) { |
| 127 |
echo esc_html( ultimate_post_kit_post_time_diff( $settings['human_diff_time_short'] !== 'yes' ? 'short' : '' ) ); |
| 128 |
} |
| 129 |
?> |
| 130 |
</div> |
| 131 |
|
| 132 |
<?php if ( $settings['show_time'] === 'yes' ) : ?> |
| 133 |
<div class="upk-post-time"> |
| 134 |
<i class="upk-icon-clock" aria-hidden="true"></i> |
| 135 |
<?php echo esc_html( get_the_time() ); ?> |
| 136 |
</div> |
| 137 |
<?php endif; ?> |
| 138 |
<?php endif; ?> |
| 139 |
|
| 140 |
<?php if ( |
| 141 |
function_exists( '_is_upk_pro_activated' ) && |
| 142 |
_is_upk_pro_activated() && |
| 143 |
function_exists( 'ultimate_post_kit_reading_time' ) && |
| 144 |
( $settings['show_reading_time'] ?? '' ) === 'yes' |
| 145 |
) : ?> |
| 146 |
<div class="upk-reading-time" data-separator="<?php echo esc_html( $meta_sep ); ?>"> |
| 147 |
<?php echo esc_html( ultimate_post_kit_reading_time( get_the_content(), $settings['avg_reading_speed'], $settings['hide_seconds'] ?? 'no', $settings['hide_minutes'] ?? 'no' ) ); ?> |
| 148 |
</div> |
| 149 |
<?php endif; ?> |
| 150 |
|
| 151 |
<?php if ( $settings['show_comments'] === 'yes' ) : ?> |
| 152 |
<div data-separator="<?php echo esc_html( $meta_sep ); ?>"> |
| 153 |
<div class="upk-comments upk-flex upk-flex-middle"> |
| 154 |
<i class="upk-icon-post-comments" aria-hidden="true"></i> |
| 155 |
<span><?php echo esc_html( $this->upk_get_localized_comment_count( get_the_ID() ) ); ?></span> |
| 156 |
</div> |
| 157 |
</div> |
| 158 |
<?php endif; ?> |
| 159 |
</div> |
| 160 |
<?php endif; ?> |
| 161 |
</div> |
| 162 |
</div> |
| 163 |
<?php |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
wp_reset_postdata(); |
| 168 |
$markup = ob_get_clean(); |
| 169 |
|
| 170 |
wp_send_json( |
| 171 |
[ |
| 172 |
'success' => $found_posts, |
| 173 |
'markup' => $found_posts ? $markup : esc_html__( 'No more found', 'ultimate-post-kit' ), |
| 174 |
] |
| 175 |
); |
| 176 |
} |
| 177 |
} |
| 178 |
|