| 1 |
<?php |
| 2 |
/** |
| 3 |
* Render class for Advanced Heading widget. |
| 4 |
* |
| 5 |
* @package RadiusTheme\SB |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace RadiusTheme\SB\Elementor\Widgets\General\PostGrid; |
| 9 |
|
| 10 |
use DateTime; |
| 11 |
use DateTimeZone; |
| 12 |
use RadiusTheme\SB\Elementor\Helper\ControlHelper; |
| 13 |
use RadiusTheme\SB\Elementor\Helper\PostHelpers; |
| 14 |
use RadiusTheme\SB\Elementor\Helper\RenderHelpers; |
| 15 |
use RadiusTheme\SB\Helpers\Fns; |
| 16 |
use RadiusTheme\SB\Elementor\Render\GeneralAddons; |
| 17 |
use RadiusTheme\SB\Models\PostQueryArgs; |
| 18 |
use WP_Query; |
| 19 |
|
| 20 |
// Do not allow directly accessing this file. |
| 21 |
if ( ! defined( 'ABSPATH' ) ) { |
| 22 |
exit( 'This script cannot be accessed directly.' ); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Render class. |
| 27 |
* |
| 28 |
* @package RadiusTheme\SB |
| 29 |
*/ |
| 30 |
class Render extends GeneralAddons { |
| 31 |
/** |
| 32 |
* Main render function for displaying content. |
| 33 |
* |
| 34 |
* @param array $data Data to be passed to the template. |
| 35 |
* @param array $settings Widget settings. |
| 36 |
* |
| 37 |
* @return string |
| 38 |
*/ |
| 39 |
public function display_content( $data, $settings ) { |
| 40 |
$data['content'] = ''; |
| 41 |
$this->settings = $settings; |
| 42 |
$data = wp_parse_args( $this->get_template_args( $data ), $data ); |
| 43 |
$data = apply_filters( 'rtsb/general/post_grid/args/' . $data['unique_name'], $data ); |
| 44 |
$metas = PostHelpers::post_meta_dataset( $this->settings, $data['template'], $this->settings ); |
| 45 |
|
| 46 |
$args = ( new PostQueryArgs() )->buildArgs( $metas ); |
| 47 |
/** |
| 48 |
* Before Post query hook. |
| 49 |
*/ |
| 50 |
do_action( 'rtsb/elements/render/before_post_grid_query', $this->settings, $args ); |
| 51 |
|
| 52 |
$query = new WP_Query( $args ); |
| 53 |
|
| 54 |
if ( $query->have_posts() ) { |
| 55 |
$data['post_query'] = $query; |
| 56 |
while ( $query->have_posts() ) { |
| 57 |
$query->the_post(); |
| 58 |
$data['grid_classes'] = $this->content_classes; |
| 59 |
|
| 60 |
$data = array_merge( |
| 61 |
$data, |
| 62 |
PostHelpers::get_post_arg_dataset( $this->settings ) |
| 63 |
); |
| 64 |
$data['content'] .= Fns::load_template( $data['template'], $data, true ); |
| 65 |
} |
| 66 |
} else { |
| 67 |
$data['content'] = $this->no_posts_msg(); |
| 68 |
} |
| 69 |
|
| 70 |
wp_reset_postdata(); |
| 71 |
|
| 72 |
unset( $args['tax_query'] ); |
| 73 |
|
| 74 |
/** |
| 75 |
* After Product query hook. |
| 76 |
*/ |
| 77 |
do_action( 'rtsb/elements/render/after_post_grid_query', $settings, $args ); |
| 78 |
return $this->addon_view( $data, $settings ); |
| 79 |
} |
| 80 |
/** |
| 81 |
* Retrieves template arguments based on widget settings. |
| 82 |
* |
| 83 |
* @param array $data Data to be passed to the template. |
| 84 |
* |
| 85 |
* @return array |
| 86 |
*/ |
| 87 |
private function get_template_args( $data ) { |
| 88 |
if ( ! empty( $data['content_class'] ) ) { |
| 89 |
$this->content_classes[] = $data['content_class']; |
| 90 |
} |
| 91 |
$this->content_classes = is_array( $this->content_classes ) ? implode( ' ', $this->content_classes ) : $this->content_classes; |
| 92 |
|
| 93 |
return [ |
| 94 |
'title_limit' => $this->settings['title_limit'] ?? 'default', |
| 95 |
'excerpt_limit' => $this->settings['excerpt_limit'] ?? 'default', |
| 96 |
'title_tag' => $this->settings['title_tag'] ?? 'h2', |
| 97 |
'title_limit_custom' => $this->settings['title_limit_custom'] ?? '', |
| 98 |
'excerpt_limit_custom' => $this->settings['excerpt_limit_custom'] ?? '200', |
| 99 |
'show_title' => ! empty( $this->settings['show_title'] ), |
| 100 |
'show_short_desc' => ! empty( $this->settings['show_short_desc'] ), |
| 101 |
'show_post_thumbnail' => ! empty( $this->settings['show_post_thumbnail'] ), |
| 102 |
'show_categories' => ! empty( $this->settings['show_categories'] ), |
| 103 |
'show_tags' => ! empty( $this->settings['show_tags'] ), |
| 104 |
'show_dates' => ! empty( $this->settings['show_dates'] ), |
| 105 |
'show_comment' => ! empty( $this->settings['show_comment'] ), |
| 106 |
'show_reading_time' => ! empty( $this->settings['show_reading_time'] ), |
| 107 |
'show_post_views' => ! empty( $this->settings['show_post_views'] ), |
| 108 |
'show_author' => ! empty( $this->settings['show_author'] ), |
| 109 |
'show_read_more_btn' => ! empty( $this->settings['show_read_more_btn'] ), |
| 110 |
'title_link' => ! empty( $this->settings['title_link'] ), |
| 111 |
'image_link' => ! empty( $this->settings['image_link'] ), |
| 112 |
'show_author_icon' => ! empty( $this->settings['show_author_icon'] ), |
| 113 |
'show_date_icon' => ! empty( $this->settings['show_date_icon'] ), |
| 114 |
'show_comment_icon' => ! empty( $this->settings['show_comment_icon'] ), |
| 115 |
'show_reading_time_icon' => ! empty( $this->settings['show_reading_time_icon'] ), |
| 116 |
'show_post_views_icon' => ! empty( $this->settings['show_post_views_icon'] ), |
| 117 |
'author_icon_html' => $this->render_meta_icon( $this->settings['author_icon'] ?? '', 'author-icon' ), |
| 118 |
'date_icon_html' => $this->render_meta_icon( $this->settings['date_icon'] ?? '', 'date-icon' ), |
| 119 |
'comment_icon_html' => $this->render_meta_icon( $this->settings['comment_icon'] ?? '', 'comment-icon' ), |
| 120 |
'reading_time_icon_html' => $this->render_meta_icon( $this->settings['reading_time_icon'] ?? '', 'reading-time-icon' ), |
| 121 |
'post_views_icon_html' => $this->render_meta_icon( $this->settings['post_views_icon'] ?? '', 'post-views-icon' ), |
| 122 |
'button_text' => $this->settings['button_text'] ?? '', |
| 123 |
'button_icon' => $this->settings['button_icon'] ?? '', |
| 124 |
'button_icon_position' => $this->settings['button_icon_position'] ?? 'right', |
| 125 |
'meta_separator' => $this->settings['meta_separator'] ?? '', |
| 126 |
'item_class' => $this->generate_post_item_class(), |
| 127 |
]; |
| 128 |
} |
| 129 |
/** |
| 130 |
* Gets row arguments. |
| 131 |
* |
| 132 |
* @return array Row arguments. |
| 133 |
*/ |
| 134 |
protected function get_row_args() { |
| 135 |
$limit = ( empty( $this->settings['posts_limit'] ) || '-1' === $this->settings['posts_limit'] ) ? 10000000 : $this->settings['posts_limit']; |
| 136 |
$posts_per_page = $this->settings['pagination_per_page'] ?? ''; |
| 137 |
|
| 138 |
return [ |
| 139 |
'posts_limit' => $limit, |
| 140 |
'posts_per_page' => $posts_per_page, |
| 141 |
]; |
| 142 |
} |
| 143 |
/** |
| 144 |
* Function to render icon. |
| 145 |
* |
| 146 |
* @param string $position icon position ('left' or 'right'). |
| 147 |
* @param array $icon Position of the separator in settings. |
| 148 |
* @param string $icon_position icon position. |
| 149 |
* |
| 150 |
* @return string |
| 151 |
*/ |
| 152 |
public static function render_icon( $position, $icon, $icon_position ) { |
| 153 |
$html = ''; |
| 154 |
|
| 155 |
// Render the left icon. |
| 156 |
if ( $position === $icon_position ) { |
| 157 |
$html .= '<span class="icon">' . Fns::icons_manager( $icon ) . '</span>'; |
| 158 |
} |
| 159 |
|
| 160 |
return $html; |
| 161 |
} |
| 162 |
/** |
| 163 |
* Function to render meta icon. |
| 164 |
* |
| 165 |
* @param array $icon Position of the separator in settings. |
| 166 |
* @param string $class icon class. |
| 167 |
* |
| 168 |
* @return string |
| 169 |
*/ |
| 170 |
public static function render_meta_icon( $icon, $class ) { |
| 171 |
$html = ''; |
| 172 |
// Render the left icon. |
| 173 |
|
| 174 |
$html .= '<span class="icon ' . esc_attr( $class ) . '">' . Fns::icons_manager( $icon ) . '</span>'; |
| 175 |
|
| 176 |
return $html; |
| 177 |
} |
| 178 |
/** |
| 179 |
* Function to generate item class. |
| 180 |
* |
| 181 |
* @return string |
| 182 |
*/ |
| 183 |
public function generate_post_item_class() { |
| 184 |
$classes = 'rtsb-post-grid-item '; |
| 185 |
if ( empty( $this->settings['image_link'] ) ) { |
| 186 |
$classes .= 'no-img-linkable '; |
| 187 |
} |
| 188 |
if ( ! empty( $this->settings['image_hover_effect'] ) ) { |
| 189 |
$classes .= $this->settings['image_hover_effect']; |
| 190 |
} |
| 191 |
return $classes; |
| 192 |
} |
| 193 |
} |
| 194 |
|