| 1 |
<?php |
| 2 |
/** |
| 3 |
* Elementor Render Class. |
| 4 |
* |
| 5 |
* This class contains render logics & output. |
| 6 |
* |
| 7 |
* @package RadiusTheme\SB |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace RadiusTheme\SB\Elementor\Render; |
| 11 |
|
| 12 |
use RadiusTheme\SB\Helpers\BuilderFns; |
| 13 |
use WP_Query; |
| 14 |
use Elementor\Icons_Manager; |
| 15 |
use RadiusTheme\SB\Helpers\Fns; |
| 16 |
use RadiusTheme\SB\Models\QueryArgs; |
| 17 |
use RadiusTheme\SB\Traits\SingletonTrait; |
| 18 |
use RadiusTheme\SB\Traits\ActionBtnTraits; |
| 19 |
use RadiusTheme\SB\Elementor\Helper\RenderHelpers; |
| 20 |
|
| 21 |
// Do not allow directly accessing this file. |
| 22 |
if ( ! defined( 'ABSPATH' ) ) { |
| 23 |
exit( 'This script cannot be accessed directly.' ); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Elementor Render Class. |
| 28 |
*/ |
| 29 |
class Render { |
| 30 |
/** |
| 31 |
* Singleton Trait. |
| 32 |
*/ |
| 33 |
use SingletonTrait; |
| 34 |
|
| 35 |
/** |
| 36 |
* Action button trait. |
| 37 |
*/ |
| 38 |
use ActionBtnTraits; |
| 39 |
|
| 40 |
/** |
| 41 |
* HTML. |
| 42 |
* |
| 43 |
* @var string |
| 44 |
*/ |
| 45 |
private $html = null; |
| 46 |
|
| 47 |
/** |
| 48 |
* Settings array |
| 49 |
* |
| 50 |
* @var array |
| 51 |
*/ |
| 52 |
public $get_settings; |
| 53 |
|
| 54 |
/** |
| 55 |
* Get elementor settings. |
| 56 |
* |
| 57 |
* @return array |
| 58 |
*/ |
| 59 |
public function get_settings_for_display() { |
| 60 |
return $this->get_settings; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Container Wrapper HTML. |
| 65 |
* |
| 66 |
* @param string $content The content. |
| 67 |
* @param array $settings Settings. |
| 68 |
* @param string $class Class. |
| 69 |
* @param bool $fullwidth Fullwidth check. |
| 70 |
* |
| 71 |
* @return string |
| 72 |
*/ |
| 73 |
public function container( $content, $settings, $class = '', $fullwidth = false ) { |
| 74 |
$layout_id = 'rtsb-container-' . absint( wp_rand() ); |
| 75 |
$container_attr = null; |
| 76 |
$cart_txt_class = ''; |
| 77 |
$container_class = 'rtsb-elementor-container products rtsb-pos-r ' . esc_attr( $class ); |
| 78 |
|
| 79 |
$metas = RenderHelpers::meta_dataset( $settings ); |
| 80 |
|
| 81 |
$layout = $metas['layout']; |
| 82 |
$animation = $metas['hover_animation']; |
| 83 |
|
| 84 |
$is_cat = preg_match( '/category/', $layout ); |
| 85 |
$is_carousel = preg_match( '/slider/', $layout ); |
| 86 |
|
| 87 |
if ( ! $is_cat ) { |
| 88 |
$cart_txt_class = $metas['show_cart_text'] ? ' has-cart-text' : ' no-cart-text'; |
| 89 |
} |
| 90 |
|
| 91 |
if ( $is_carousel ) { |
| 92 |
$fullwidth = true; |
| 93 |
} |
| 94 |
|
| 95 |
$badge_class = [ |
| 96 |
'position' => $metas['badge_position'], |
| 97 |
'alignment' => $metas['badge_alignment'], |
| 98 |
]; |
| 99 |
|
| 100 |
if ( ! in_array( $layout, array_keys( Fns::free_layouts() ), true ) ) { |
| 101 |
$layout = RenderHelpers::set_default_layout( $layout ); |
| 102 |
} |
| 103 |
|
| 104 |
$d_col = 0 === $metas['d_cols'] ? RenderHelpers::default_columns( $layout ) : $metas['d_cols']; |
| 105 |
$t_col = 0 === $metas['t_cols'] ? 2 : $metas['t_cols']; |
| 106 |
$m_col = 0 === $metas['m_cols'] ? 1 : $metas['m_cols']; |
| 107 |
|
| 108 |
if ( $fullwidth ) { |
| 109 |
$d_col = 12; |
| 110 |
$t_col = 12; |
| 111 |
$m_col = 12; |
| 112 |
} |
| 113 |
|
| 114 |
$pagination_attr = ( $is_carousel || $is_cat ? '' : RenderHelpers::pagination_data( $settings ) ); |
| 115 |
|
| 116 |
if ( ! in_array( 'clear-btn', $metas['visibility'], true ) ) { |
| 117 |
$container_class .= ' no-clear-btn'; |
| 118 |
} |
| 119 |
|
| 120 |
$container_attr .= ' data-layout="' . esc_attr( $layout ) . '" data-desktop-col="' . absint( $d_col ) . '" data-tab-col="' . absint( $t_col ) . '" data-mobile-col="' . absint( $m_col ) . '" ' . $pagination_attr; |
| 121 |
$container_class .= ' badge-' . esc_attr( $badge_class['position'] ) . ' badge-' . esc_attr( $badge_class['alignment'] ) . esc_attr( $cart_txt_class ) . ' img-hover-' . esc_attr( $animation ) . ' excerpt-' . esc_attr( $metas['cat_excerpt_position'] ); |
| 122 |
|
| 123 |
if ( $metas['show_overlay'] ) { |
| 124 |
$container_class .= ' has-overlay'; |
| 125 |
} |
| 126 |
|
| 127 |
$this->html = '<div class="' . esc_attr( $container_class ) . '" id="' . esc_attr( $layout_id ) . '" ' . $container_attr . '>'; |
| 128 |
|
| 129 |
if ( ! empty( $settings['query_products'] ) && count( $settings['query_products'] ) && 'yes' === $settings['show_section_title'] ) { |
| 130 |
$this->html .= '<div class="rtsb-section-title-wrapper">'; |
| 131 |
$this->html .= '<' . esc_attr( $settings['section_title_tag'] ) . ' class="rtsb-section-title" >'; |
| 132 |
$this->html .= esc_html( $settings['section_title_text'] ); |
| 133 |
$this->html .= '</' . esc_attr( $settings['section_title_tag'] ) . '>'; |
| 134 |
$this->html .= '</div>'; |
| 135 |
} |
| 136 |
|
| 137 |
$this->html .= $content; |
| 138 |
|
| 139 |
$this->html .= '</div><!-- .rtsb-elementor-container -->'; |
| 140 |
|
| 141 |
// Action button icon reset. |
| 142 |
$this->action_button_icon_set_default(); |
| 143 |
|
| 144 |
$script_generator = []; |
| 145 |
$script_generator['layout'] = $layout_id; |
| 146 |
$script_generator['rand'] = absint( wp_rand() ); |
| 147 |
$script_generator['isCarousel'] = $is_carousel; |
| 148 |
|
| 149 |
add_action( |
| 150 |
'wp_footer', |
| 151 |
static function () use ( $script_generator ) { |
| 152 |
RenderHelpers::register_scripts( $script_generator ); |
| 153 |
} |
| 154 |
); |
| 155 |
|
| 156 |
return $this->html; |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Row Wrapper HTML. |
| 161 |
* |
| 162 |
* @param string $content The content. |
| 163 |
* @param array $settings Settings. |
| 164 |
* |
| 165 |
* @return string |
| 166 |
*/ |
| 167 |
public function row( $content, $settings ) { |
| 168 |
$masonry_class = null; |
| 169 |
$loader_class = ' rtsb-pre-loader'; |
| 170 |
|
| 171 |
$layout = $settings['layout']; |
| 172 |
$grid_type = $settings['grid_type']; |
| 173 |
$is_carousel = preg_match( '/slider/', $layout ); |
| 174 |
|
| 175 |
if ( 'even' === $grid_type ) { |
| 176 |
$masonry_class = ' rtsb-even'; |
| 177 |
} elseif ( 'masonry' === $grid_type ) { |
| 178 |
$masonry_class = ' rtsb-masonry'; |
| 179 |
} |
| 180 |
|
| 181 |
if ( ! rtsb()->has_pro() || $is_carousel ) { |
| 182 |
$masonry_class = ' rtsb-even'; |
| 183 |
} |
| 184 |
|
| 185 |
$row_start_class = apply_filters( 'rtsb/custom/layout/row/classes', esc_attr( 'rtsb-row rtsb-content-loader element-loading rtsb-' . $layout . $masonry_class . $loader_class ), $settings ); |
| 186 |
$row_start = '<div data-title="' . esc_html__( 'Loading ...', 'shopbuilder' ) . '" class="' . $row_start_class . '">'; |
| 187 |
$this->html = apply_filters( 'rtsb/custom/layout/row/start', $row_start, $row_start_class ); |
| 188 |
|
| 189 |
if ( $is_carousel ) { |
| 190 |
$slider_data = RenderHelpers::slider_data( $settings ); |
| 191 |
|
| 192 |
$this->html .= '<div class="rtsb-col-xs-12 ' . esc_attr( $slider_data['class'] ) . '" ' . $slider_data['data'] . '>'; |
| 193 |
$this->html .= '<div class="swiper-wrapper">'; |
| 194 |
} |
| 195 |
|
| 196 |
$this->html .= $content; |
| 197 |
|
| 198 |
if ( $is_carousel ) { |
| 199 |
$this->html .= '</div><!-- .swiper-wrapper -->'; |
| 200 |
|
| 201 |
$left_icon = $settings['left_arrow_icon']; |
| 202 |
$right_icon = $settings['right_arrow_icon']; |
| 203 |
|
| 204 |
if ( ! empty( $settings['slider_nav'] ) ) { |
| 205 |
$this->html .= |
| 206 |
'<div class="swiper-nav"> |
| 207 |
<div class="swiper-arrow swiper-button-next"> |
| 208 |
' . Fns::icons_manager( $right_icon ) . ' |
| 209 |
</div> |
| 210 |
<div class="swiper-arrow swiper-button-prev"> |
| 211 |
' . Fns::icons_manager( $left_icon ) . ' |
| 212 |
</div> |
| 213 |
</div>'; |
| 214 |
} |
| 215 |
|
| 216 |
$this->html .= ! empty( $settings['slider_dot'] ) ? '<div class="swiper-pagination rtsb-pos-s"></div>' : ''; |
| 217 |
|
| 218 |
$this->html .= '</div><!-- .rtsb-carousel-slider -->'; |
| 219 |
} |
| 220 |
|
| 221 |
$this->html .= RenderHelpers::pre_loader( $layout ); |
| 222 |
|
| 223 |
$row_end = '</div><!-- .rtsb-row -->'; |
| 224 |
|
| 225 |
$this->html .= apply_filters( 'rtsb/custom/layout/row/end', $row_end ); |
| 226 |
|
| 227 |
return $this->html; |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* Product loop. |
| 232 |
* |
| 233 |
* @param object $query The query. |
| 234 |
* @param array $settings Settings. |
| 235 |
* @param string $template Template. |
| 236 |
* |
| 237 |
* @return string|null |
| 238 |
*/ |
| 239 |
public function product_loop( $query, $settings, $template ) { |
| 240 |
$html = null; |
| 241 |
|
| 242 |
if ( empty( $query ) ) { |
| 243 |
return null; |
| 244 |
} |
| 245 |
|
| 246 |
while ( $query->have_posts() ) { |
| 247 |
$query->the_post(); |
| 248 |
|
| 249 |
// Loop arg. |
| 250 |
$arg = $this->arg_dataset( $settings, get_the_ID(), $settings['lazy_load'] ); |
| 251 |
|
| 252 |
// Get template. |
| 253 |
$html .= Fns::load_template( $template . $settings['layout'], $arg, true ); |
| 254 |
} |
| 255 |
|
| 256 |
return $html; |
| 257 |
} |
| 258 |
|
| 259 |
/** |
| 260 |
* Product loop. |
| 261 |
* |
| 262 |
* @param object $query The query. |
| 263 |
* @param array $settings Settings. |
| 264 |
* @param string $template Template. |
| 265 |
* @param bool $is_single Single category. |
| 266 |
* |
| 267 |
* @return string|null |
| 268 |
*/ |
| 269 |
public function category_loop( $query, $settings, $template, $is_single = false ) { |
| 270 |
$html = null; |
| 271 |
$term_link = null; |
| 272 |
|
| 273 |
if ( $is_single ) { |
| 274 |
// Loop arg. |
| 275 |
$arg = $this->cat_arg_dataset( $settings, $query, true ); |
| 276 |
|
| 277 |
// Get template. |
| 278 |
$html .= Fns::load_template( $template . $settings['layout'], $arg, true ); |
| 279 |
} else { |
| 280 |
foreach ( $query as $term ) { |
| 281 |
// Loop arg. |
| 282 |
$arg = $this->cat_arg_dataset( $settings, $term ); |
| 283 |
|
| 284 |
// Get template. |
| 285 |
$html .= Fns::load_template( $template . $settings['layout'], $arg, true ); |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
return $html; |
| 290 |
} |
| 291 |
|
| 292 |
/** |
| 293 |
* Render Product View. |
| 294 |
* |
| 295 |
* @param string $template Template name. |
| 296 |
* @param array $settings Control settings. |
| 297 |
* |
| 298 |
* @return string |
| 299 |
*/ |
| 300 |
public function product_view( $template, $settings ) { |
| 301 |
$this->get_settings = $settings; |
| 302 |
|
| 303 |
$metas = RenderHelpers::meta_dataset( $settings ); |
| 304 |
$is_carousel = preg_match( '/slider/', $metas['layout'] ); |
| 305 |
$pagination = $metas['pagination']; |
| 306 |
|
| 307 |
// Query Args. |
| 308 |
$args = ( new QueryArgs() )->buildArgs( $metas, 'product', $is_carousel ); |
| 309 |
|
| 310 |
// Query. |
| 311 |
$product_query = new WP_Query( $args ); |
| 312 |
|
| 313 |
if ( $product_query->have_posts() ) { |
| 314 |
// Row. |
| 315 |
$this->row( $this->product_loop( $product_query, $metas, $template ), $metas ); |
| 316 |
|
| 317 |
// Pagination. |
| 318 |
if ( $pagination ) { |
| 319 |
$this->html .= RenderHelpers::render_pagination( |
| 320 |
$product_query, |
| 321 |
$metas, |
| 322 |
$settings['posts_limit'], |
| 323 |
$settings['pagination_per_page'] |
| 324 |
); |
| 325 |
} |
| 326 |
} else { |
| 327 |
$this->html .= '<p>' . esc_html__( 'No products found', 'shopbuilder' ) . '</p>'; |
| 328 |
} |
| 329 |
|
| 330 |
wp_reset_postdata(); |
| 331 |
|
| 332 |
// Container. |
| 333 |
$this->container( $this->html, $settings, 'rtsb-products-container', $is_carousel ); |
| 334 |
|
| 335 |
return $this->html; |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
/** |
| 340 |
* Render Product View. |
| 341 |
* |
| 342 |
* @param string $template Template name. |
| 343 |
* @param array $settings Control settings. |
| 344 |
* |
| 345 |
* @return string |
| 346 |
*/ |
| 347 |
public function wc_loop_for_product_view( $template, $settings ) { |
| 348 |
global $wp_query, $post; |
| 349 |
|
| 350 |
$this->get_settings = $settings; |
| 351 |
|
| 352 |
$metas = RenderHelpers::meta_dataset( $settings ); |
| 353 |
$pagination = $metas['pagination']; |
| 354 |
|
| 355 |
$page_type = BuilderFns::builder_type( get_the_ID() ); |
| 356 |
$is_preview = BuilderFns::is_builder_preview() && array_key_exists( $page_type, BuilderFns::builder_page_types() ); |
| 357 |
$posts_per_page = wc_get_default_products_per_row() * wc_get_default_product_rows_per_page(); |
| 358 |
|
| 359 |
if ( $is_preview ) { |
| 360 |
$main_query = clone $wp_query; |
| 361 |
$main_post = clone $post; |
| 362 |
|
| 363 |
$ordering = ( new \WC_Query() )->get_catalog_ordering_args(); |
| 364 |
$wp_query_args = [ |
| 365 |
'post_type' => 'product', |
| 366 |
'posts_per_page' => $posts_per_page, |
| 367 |
]; |
| 368 |
|
| 369 |
if ( ! empty( $ordering['orderby'] ) ) { |
| 370 |
$wp_query_args['orderby'] = $ordering['orderby']; |
| 371 |
} |
| 372 |
|
| 373 |
if ( ! empty( $ordering['order'] ) ) { |
| 374 |
$wp_query_args['order'] = $ordering['order']; |
| 375 |
} |
| 376 |
|
| 377 |
$wp_query = new \WP_Query( $wp_query_args ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
| 378 |
wc_set_loop_prop( 'total', $wp_query->found_posts ); |
| 379 |
wc_set_loop_prop( 'total_pages', $wp_query->max_num_pages ); |
| 380 |
} |
| 381 |
|
| 382 |
// column_per_row. |
| 383 |
if ( woocommerce_product_loop() ) { |
| 384 |
if ( wc_get_loop_prop( 'total' ) ) { |
| 385 |
// Row. |
| 386 |
$this->row( $this->product_loop( $wp_query, $metas, $template ), $metas ); |
| 387 |
} |
| 388 |
|
| 389 |
// Pagination. |
| 390 |
if ( $pagination ) { |
| 391 |
$this->html .= RenderHelpers::render_pagination( |
| 392 |
$wp_query, |
| 393 |
$metas, |
| 394 |
wc_get_loop_prop( 'total' ), |
| 395 |
$posts_per_page |
| 396 |
); |
| 397 |
} |
| 398 |
} else { |
| 399 |
$this->html .= '<p>' . esc_html__( 'No products found', 'shopbuilder' ) . '</p>'; |
| 400 |
} |
| 401 |
|
| 402 |
if ( $is_preview ) { |
| 403 |
$wp_query = $main_query; |
| 404 |
$post = $main_post; |
| 405 |
|
| 406 |
wp_reset_query(); |
| 407 |
wp_reset_postdata(); |
| 408 |
} |
| 409 |
|
| 410 |
// Container. |
| 411 |
$this->container( $this->html, $settings, 'rtsb-products-container' ); |
| 412 |
|
| 413 |
return $this->html; |
| 414 |
} |
| 415 |
|
| 416 |
/** |
| 417 |
* Render Product View. |
| 418 |
* |
| 419 |
* @param string $template Template name. |
| 420 |
* @param array $settings Control settings. |
| 421 |
* |
| 422 |
* @return string |
| 423 |
*/ |
| 424 |
public function setup_loop_for_product_view( $template, $settings ) { |
| 425 |
if ( empty( $settings['query_products'] ) ) { |
| 426 |
return $this->html; |
| 427 |
} |
| 428 |
|
| 429 |
$html = null; |
| 430 |
|
| 431 |
$metas = RenderHelpers::meta_dataset( $settings ); |
| 432 |
|
| 433 |
foreach ( $settings['query_products'] as $_product ) { |
| 434 |
$post_object = get_post( $_product->get_id() ); |
| 435 |
|
| 436 |
setup_postdata( $GLOBALS['post'] =& $post_object ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited, Squiz.PHP.DisallowMultipleAssignments.Found |
| 437 |
|
| 438 |
// Loop arg. |
| 439 |
$arg = $this->arg_dataset( $metas, $_product->get_id() ); |
| 440 |
// Get template. |
| 441 |
$html .= Fns::load_template( $template . $settings['layout'], $arg, true ); |
| 442 |
|
| 443 |
$this->row( $html, $metas ); |
| 444 |
} |
| 445 |
|
| 446 |
// Container. |
| 447 |
$this->container( $this->html, $settings, 'rtsb-products-container' ); |
| 448 |
|
| 449 |
wp_reset_postdata(); |
| 450 |
|
| 451 |
return $this->html; |
| 452 |
|
| 453 |
} |
| 454 |
/** |
| 455 |
* Render Category View. |
| 456 |
* |
| 457 |
* @param string $template Template name. |
| 458 |
* @param array $settings Control settings. |
| 459 |
* @param bool $is_single Single category. |
| 460 |
* |
| 461 |
* @return string |
| 462 |
*/ |
| 463 |
public function category_view( $template, $settings, $is_single = false ) { |
| 464 |
$this->get_settings = $settings; |
| 465 |
|
| 466 |
$metas = RenderHelpers::meta_dataset( $settings ); |
| 467 |
$taxonomy = $metas['category_source']; |
| 468 |
$term = $metas['category_term']; |
| 469 |
|
| 470 |
// Query. |
| 471 |
if ( $is_single ) { |
| 472 |
if ( empty( $term ) ) { |
| 473 |
return '<p>' . esc_html__( 'Please select a category.', 'shopbuilder' ) . '</p>'; |
| 474 |
} |
| 475 |
|
| 476 |
$category_query = get_term( $term, $taxonomy ); |
| 477 |
} else { |
| 478 |
// Query Args. |
| 479 |
$args = ( new QueryArgs() )->buildArgs( $metas, 'category' ); |
| 480 |
|
| 481 |
$category_query = get_terms( $args ); |
| 482 |
} |
| 483 |
|
| 484 |
if ( ! empty( $category_query ) ) { |
| 485 |
// Row. |
| 486 |
$this->row( $this->category_loop( $category_query, $metas, $template, $is_single ), $metas ); |
| 487 |
} else { |
| 488 |
$this->html .= '<p>' . esc_html__( 'No categories found', 'shopbuilder' ) . '</p>'; |
| 489 |
} |
| 490 |
|
| 491 |
// Container. |
| 492 |
$this->container( $this->html, $settings, 'rtsb-categories-container', $is_single ); |
| 493 |
|
| 494 |
return $this->html; |
| 495 |
} |
| 496 |
|
| 497 |
/** |
| 498 |
* Render Category View. |
| 499 |
* |
| 500 |
* @param string $template Template name. |
| 501 |
* @param array $settings Control settings. |
| 502 |
* |
| 503 |
* @return string |
| 504 |
*/ |
| 505 |
public function social_share_view( $template, $settings ) { |
| 506 |
$this->get_settings = $settings; |
| 507 |
$share_items = []; |
| 508 |
|
| 509 |
foreach ( $settings['share_platforms'] as $platform ) { |
| 510 |
$share_items[] = [ |
| 511 |
'share_items' => $platform['share_items'], |
| 512 |
'share_text' => $platform['share_text'], |
| 513 |
]; |
| 514 |
} |
| 515 |
|
| 516 |
if ( ! empty( $share_items ) && is_array( $share_items ) ) { |
| 517 |
$arg['p_id'] = get_the_ID(); |
| 518 |
$arg['share_items'] = $share_items; |
| 519 |
$arg['preset'] = $settings['layout']; |
| 520 |
$arg['show_icon'] = $settings['show_share_icon']; |
| 521 |
$arg['raw_settings'] = $this->get_settings_for_display(); |
| 522 |
$this->html = Fns::load_template( $template, $arg, true ); |
| 523 |
} else { |
| 524 |
$this->html = '<p>' . esc_html__( 'Please select a social sharing platform.', 'shopbuilder' ) . '</p>'; |
| 525 |
} |
| 526 |
|
| 527 |
return $this->html; |
| 528 |
} |
| 529 |
|
| 530 |
/** |
| 531 |
* Builds an array with meta values. |
| 532 |
* |
| 533 |
* @param array $meta Meta values. |
| 534 |
* @param int $post_id Post ID. |
| 535 |
* @param bool $lazy_load Lazy Load. |
| 536 |
* @return array |
| 537 |
*/ |
| 538 |
public function arg_dataset( array $meta, int $post_id, bool $lazy_load = false ) { |
| 539 |
if ( ( empty( $meta ) && ! $post_id ) || ! in_array( get_post_type( $post_id ),['product_variation','product'] ) ) { |
| 540 |
return []; |
| 541 |
} |
| 542 |
global $post; |
| 543 |
|
| 544 |
$arg = []; |
| 545 |
$_product = wc_get_product( $post_id ); |
| 546 |
$p_type = $_product->get_type(); |
| 547 |
|
| 548 |
$arg['class'] = null; |
| 549 |
$arg['grid'] = null; |
| 550 |
$arg['anchor_class'] = null; |
| 551 |
$arg['hover_img_html'] = null; |
| 552 |
$arg['s_link'] = []; |
| 553 |
$arg['add_to_cart'] = ''; |
| 554 |
$arg['p_id'] = $post_id; |
| 555 |
$arg['p_link'] = get_permalink(); |
| 556 |
$arg['p_sale'] = ''; |
| 557 |
$arg['raw_settings'] = $this->get_settings_for_display(); |
| 558 |
$cart_icon_html = ''; |
| 559 |
|
| 560 |
$layout = $meta['layout']; |
| 561 |
|
| 562 |
$arg['target'] = '_self'; |
| 563 |
$arg['f_img'] = $meta['f_img']; |
| 564 |
$arg['items'] = $meta['visibility']; |
| 565 |
$arg['title_tag'] = $meta['title_tag']; |
| 566 |
$arg['title_class'] = 'product-title rtsb-text-limit limit-' . $meta['title_limit'] . ( ! $meta['title_link'] ? ' no-link' : '' ); |
| 567 |
$arg['title'] = Fns::text_truncation( get_the_title(), $meta['title_limit_custom'] ); |
| 568 |
$arg['excerpt_limit'] = $meta['excerpt_limit']; |
| 569 |
$arg['excerpt'] = wpautop( Fns::text_truncation( do_shortcode( $post->post_excerpt ), $meta['excerpt_limit_custom'] ) ); |
| 570 |
$arg['title_link'] = $meta['title_link']; |
| 571 |
$arg['image_link'] = $meta['image_link']; |
| 572 |
$arg['out_of_stock'] = $meta['out_of_stock_badge_text']; |
| 573 |
$arg['action_btn_preset'] = $meta['btn_preset']; |
| 574 |
$arg['action_btn_position'] = $meta['btn_position']; |
| 575 |
|
| 576 |
$is_list = preg_match( '/list/', $layout ); |
| 577 |
$is_carousel = preg_match( '/slider/', $layout ); |
| 578 |
|
| 579 |
$d_col = 0 === $meta['d_cols'] ? RenderHelpers::default_columns( $layout ) : $meta['d_cols']; |
| 580 |
$t_col = 0 === $meta['t_cols'] ? 2 : $meta['t_cols']; |
| 581 |
$m_col = 0 === $meta['m_cols'] ? 1 : $meta['m_cols']; |
| 582 |
|
| 583 |
$d_col = 5 === $d_col ? '24' : round( 12 / $d_col ); |
| 584 |
$t_col = 5 === $t_col ? '24' : round( 12 / $t_col ); |
| 585 |
$m_col = 5 === $m_col ? '24' : round( 12 / $m_col ); |
| 586 |
|
| 587 |
if ( ! $is_carousel ) { |
| 588 |
$arg['grid'] .= 'rtsb-col-md-' . $d_col . ' rtsb-col-sm-' . $t_col . ' rtsb-col-xs-' . $m_col . ' '; |
| 589 |
$arg['class'] .= ' even-grid-item '; |
| 590 |
} else { |
| 591 |
$arg['grid'] .= 'rtsb-slide-item swiper-slide '; |
| 592 |
} |
| 593 |
|
| 594 |
$arg['class'] .= 'rtsb-product'; |
| 595 |
|
| 596 |
if ( $is_list ) { |
| 597 |
$arg['class'] .= ' rtsb-product-list'; |
| 598 |
} |
| 599 |
|
| 600 |
$arg['class'] .= ' animated fadeIn'; |
| 601 |
|
| 602 |
if ( in_array( 'badges', $meta['visibility'], true ) ) { |
| 603 |
$arg['p_sale'] = Fns::get_sale_badge( |
| 604 |
$meta['sale_badge_type'], |
| 605 |
$meta['sale_badge_text'], |
| 606 |
$meta['out_of_stock_badge_text'] |
| 607 |
); |
| 608 |
} |
| 609 |
|
| 610 |
$arg['badge_class'] = RenderHelpers::badge_class( $meta['badge_preset'] ); |
| 611 |
|
| 612 |
if ( ! $meta['show_cart_text'] ) { |
| 613 |
$meta['add_to_cart_text'] = ''; |
| 614 |
} |
| 615 |
|
| 616 |
if ( in_array( 'add_to_cart', $meta['visibility'], true ) ) { |
| 617 |
if ( ! empty( $meta['add_to_cart_icon'] ) ) { |
| 618 |
$cart_icon_html .= Fns::icons_manager( $meta['add_to_cart_icon'], 'cart-icon' ); |
| 619 |
} |
| 620 |
|
| 621 |
if ( ! empty( $meta['add_to_cart_success_icon'] ) ) { |
| 622 |
$cart_icon_html .= Fns::icons_manager( $meta['add_to_cart_success_icon'], 'cart-success-icon' ); |
| 623 |
} |
| 624 |
|
| 625 |
$cart_icon = '<span class="icon">' . $cart_icon_html . '</span>'; |
| 626 |
|
| 627 |
if ( $_product->is_purchasable() ) { |
| 628 |
if ( $_product->is_in_stock() ) { |
| 629 |
// TODO:: Replace add to cart with WC hooks |
| 630 |
$arg['add_to_cart'] = $this->render_add_to_cart_button( |
| 631 |
get_permalink(), |
| 632 |
$post_id, |
| 633 |
$p_type, |
| 634 |
$meta['add_to_cart_text'], |
| 635 |
$cart_icon, |
| 636 |
$meta['add_to_cart_alignment'] |
| 637 |
); |
| 638 |
} |
| 639 |
} |
| 640 |
} |
| 641 |
|
| 642 |
if ( in_array( 'quick_view', $meta['visibility'], true ) ) { |
| 643 |
add_filter( 'rtsb/module/quick_view/button_params', [ $this, 'button_classes' ] ); |
| 644 |
add_filter( 'rtsb/module/quick_view/icon_html', [ $this, 'quick_view_icon' ] ); |
| 645 |
} |
| 646 |
|
| 647 |
if ( in_array( 'compare', $meta['visibility'], true ) ) { |
| 648 |
add_filter( 'rtsb/module/compare/button_params', [ $this, 'button_classes' ] ); |
| 649 |
add_filter( 'rtsb/module/compare/icon_html', [ $this, 'compare_icon' ] ); |
| 650 |
} |
| 651 |
|
| 652 |
if ( in_array( 'wishlist', $meta['visibility'], true ) ) { |
| 653 |
add_filter( 'rtsb/module/wishlist/button_params', [ $this, 'button_classes' ] ); |
| 654 |
add_filter( 'rtsb/module/wishlist/icon_html', [ $this, 'wishlist_icon' ] ); |
| 655 |
} |
| 656 |
|
| 657 |
$arg['img_html'] = $meta['f_img'] ? Fns::get_product_image_html( |
| 658 |
'product', |
| 659 |
$post_id, |
| 660 |
$meta['f_img_size'], |
| 661 |
$meta['default_img_id'], |
| 662 |
$meta['custom_img_size'], |
| 663 |
$lazy_load |
| 664 |
) : null; |
| 665 |
|
| 666 |
$gallery_ids = $_product->get_gallery_image_ids(); |
| 667 |
|
| 668 |
if ( $meta['hover_img'] && ! empty( $gallery_ids ) ) { |
| 669 |
$arg['class'] .= ' rtsb-double-img'; |
| 670 |
$arg['hover_img_html'] = Fns::get_product_image_html( |
| 671 |
'product', |
| 672 |
$gallery_ids[0], |
| 673 |
$meta['f_img_size'], |
| 674 |
null, |
| 675 |
$meta['custom_img_size'], |
| 676 |
$lazy_load, |
| 677 |
true |
| 678 |
); |
| 679 |
} |
| 680 |
|
| 681 |
$arg['hover_btn_text'] = $meta['hover_btn_text']; |
| 682 |
|
| 683 |
if ( $meta['show_hover_btn_icon'] && ! empty( $meta['hover_btn_icon'] ) ) { |
| 684 |
$arg['hover_btn_icon'] = '<span class="icon">' . Fns::icons_manager( $meta['hover_btn_icon'], 'hover-btn-icon' ) . '</span>'; |
| 685 |
} |
| 686 |
|
| 687 |
return $arg; |
| 688 |
} |
| 689 |
|
| 690 |
/** |
| 691 |
* Builds an array with meta values. |
| 692 |
* |
| 693 |
* @param array $meta Meta values. |
| 694 |
* @param Object $query Category object. |
| 695 |
* @param bool $fullwidth Fullwidth check. |
| 696 |
* @param bool $lazy_load Lazy Load. |
| 697 |
* @return array |
| 698 |
*/ |
| 699 |
public function cat_arg_dataset( $meta, $query = null, $fullwidth = false, $lazy_load = false ) { |
| 700 |
if ( empty( $meta ) ) { |
| 701 |
return []; |
| 702 |
} |
| 703 |
|
| 704 |
$arg = []; |
| 705 |
|
| 706 |
$arg['class'] = null; |
| 707 |
$arg['grid'] = null; |
| 708 |
$arg['anchor_class'] = null; |
| 709 |
$arg['p_sale'] = null; |
| 710 |
$arg['count'] = max( $query->count, 0 ); |
| 711 |
$arg['count_position'] = $meta['count_position']; |
| 712 |
$arg['excerpt_position'] = $meta['cat_excerpt_position']; |
| 713 |
$arg['cat_link'] = get_term_link( $query ); |
| 714 |
$arg['target'] = '_self'; |
| 715 |
$arg['raw_settings'] = $this->get_settings_for_display(); |
| 716 |
|
| 717 |
if ( ! empty( $meta['custom_link']['url'] ) ) { |
| 718 |
$arg['cat_link'] = $meta['custom_link']['url']; |
| 719 |
$arg['target'] = $meta['custom_link']['is_external'] ? '_blank' : '_self'; |
| 720 |
} |
| 721 |
|
| 722 |
$title = $query->name; |
| 723 |
$excerpt = $query->description; |
| 724 |
|
| 725 |
if ( in_array( 'badges', $meta['visibility'], true ) ) { |
| 726 |
$arg['p_sale'] = $meta['custom_badge_text']; |
| 727 |
} |
| 728 |
|
| 729 |
$arg['badge_class'] = RenderHelpers::badge_class( $meta['badge_preset'] ); |
| 730 |
|
| 731 |
if ( $arg['count'] >= 0 ) { |
| 732 |
$prefix = ! empty( $meta['before_count'] ) ? $meta['before_count'] : ''; |
| 733 |
$suffix = ! empty( $meta['after_count'] ) ? $meta['after_count'] : ''; |
| 734 |
|
| 735 |
$arg['count'] = $prefix . $arg['count'] . $suffix; |
| 736 |
} |
| 737 |
|
| 738 |
if ( $meta['show_custom_title'] && ! empty( $meta['cat_custom_title'] ) ) { |
| 739 |
$title = $meta['cat_custom_title']; |
| 740 |
} |
| 741 |
|
| 742 |
if ( $meta['show_custom_excerpt'] && ! empty( $meta['cat_custom_excerpt'] ) ) { |
| 743 |
$excerpt = $meta['cat_custom_excerpt']; |
| 744 |
} |
| 745 |
|
| 746 |
$layout = $meta['layout']; |
| 747 |
|
| 748 |
$arg['c_img'] = $meta['c_img']; |
| 749 |
$arg['items'] = $meta['visibility']; |
| 750 |
$arg['title_tag'] = $meta['title_tag']; |
| 751 |
$arg['title_class'] = 'category-title rtsb-text-limit limit-' . $meta['title_limit'] . ( ! $meta['title_link'] ? ' no-link' : '' ); |
| 752 |
$arg['title'] = Fns::text_truncation( $title, $meta['title_limit_custom'] ); |
| 753 |
$arg['excerpt_limit'] = $meta['excerpt_limit']; |
| 754 |
$arg['excerpt'] = wpautop( Fns::text_truncation( $excerpt, $meta['excerpt_limit_custom'] ) ); |
| 755 |
$arg['title_link'] = $meta['title_link']; |
| 756 |
$arg['image_link'] = $meta['image_link']; |
| 757 |
|
| 758 |
$d_col = 0 === $meta['d_cols'] ? RenderHelpers::default_columns( $layout ) : absint( $meta['d_cols'] ); |
| 759 |
$t_col = 0 === $meta['t_cols'] ? 2 : absint( $meta['t_cols'] ); |
| 760 |
$m_col = 0 === $meta['m_cols'] ? 1 : absint( $meta['m_cols'] ); |
| 761 |
|
| 762 |
$d_col = 5 === $d_col ? '24' : round( 12 / $d_col ); |
| 763 |
$t_col = 5 === $t_col ? '24' : round( 12 / $t_col ); |
| 764 |
$m_col = 5 === $m_col ? '24' : round( 12 / $m_col ); |
| 765 |
|
| 766 |
if ( ! $fullwidth ) { |
| 767 |
$arg['grid'] .= 'rtsb-col-md-' . $d_col . ' rtsb-col-sm-' . $t_col . ' rtsb-col-xs-' . $m_col . ' '; |
| 768 |
$arg['class'] .= ' even-grid-item '; |
| 769 |
} else { |
| 770 |
$arg['grid'] .= 'rtsb-col-xs-12 '; |
| 771 |
} |
| 772 |
|
| 773 |
$arg['class'] .= 'rtsb-category-grid'; |
| 774 |
|
| 775 |
$arg['img_html'] = $meta['c_img'] ? Fns::get_product_image_html( |
| 776 |
'category', |
| 777 |
$query->term_id, |
| 778 |
$meta['f_img_size'], |
| 779 |
$meta['default_img_id'], |
| 780 |
$meta['custom_img_size'], |
| 781 |
$lazy_load |
| 782 |
) : null; |
| 783 |
|
| 784 |
if ( $meta['show_custom_image'] && ! empty( $meta['custom_image'] ) ) { |
| 785 |
$arg['img_html'] = Fns::get_product_image_html( |
| 786 |
'custom', |
| 787 |
null, |
| 788 |
$meta['f_img_size'], |
| 789 |
$meta['custom_image'], |
| 790 |
$meta['custom_img_size'], |
| 791 |
$lazy_load |
| 792 |
); |
| 793 |
} |
| 794 |
|
| 795 |
$arg['excerpt_class'] = 'category-info' . ( 'block' === $meta['count_position'] ? ' block-count' : ' inline-count' ) . ( 'above' === $meta['cat_excerpt_position'] ? ' excerpt-above' : '' ) . ( empty( $arg['excerpt'] ) ? ' no-excerpt' : '' ); |
| 796 |
|
| 797 |
return $arg; |
| 798 |
} |
| 799 |
|
| 800 |
/** |
| 801 |
* Renders add to cart button. |
| 802 |
* |
| 803 |
* @param string $link Product link. |
| 804 |
* @param int $id Product ID. |
| 805 |
* @param string $type Product type. |
| 806 |
* @param string $text Custom text. |
| 807 |
* @param string $icon_html Custom icon. |
| 808 |
* @param string $alignment Icon alignment. |
| 809 |
* @return string |
| 810 |
*/ |
| 811 |
public function render_add_to_cart_button( $link, $id, $type, $text, $icon_html, $alignment ) { |
| 812 |
$content = null; |
| 813 |
$tooltip_attr = ''; |
| 814 |
|
| 815 |
if ( empty( $text ) ) { |
| 816 |
$tooltip_attr = ' title="' . esc_attr__( 'Add to Cart', 'shopbuilder' ) . '"'; |
| 817 |
|
| 818 |
if ( 'variable' === $type ) { |
| 819 |
$tooltip_attr = ' title="' . esc_attr__( 'Select Options', 'shopbuilder' ) . '"'; |
| 820 |
} |
| 821 |
} |
| 822 |
|
| 823 |
if ( 'variable' === $type ) { |
| 824 |
$content .= sprintf( |
| 825 |
'<div class="rtsb-wc-add-to-cart-wrap"> |
| 826 |
<a href="%1$s" data-quantity="1" class="rtsb-action-btn variable-product tipsy icon-%4$s %5$s" data-product_id="%3$s" rel="nofollow"%6$s>%7$s<span class="text">%2$s</span></a> |
| 827 |
</div>', |
| 828 |
esc_url( $link ), |
| 829 |
esc_html__( 'Select Options', 'shopbuilder' ), |
| 830 |
absint( $id ), |
| 831 |
esc_attr( $alignment ), |
| 832 |
empty( $text ) ? 'no-text' : 'has-text', |
| 833 |
$tooltip_attr, |
| 834 |
$icon_html, |
| 835 |
); |
| 836 |
|
| 837 |
return $content; |
| 838 |
} |
| 839 |
|
| 840 |
$content .= sprintf( |
| 841 |
'<a href="%1$s?add-to-cart=%2$d" data-quantity="1" class="rtsb-action-btn rtsb-add-to-cart-btn tipsy icon-%6$s %7$s" data-id="%2$d" data-type="%3$s"%8$s>%4$s<span class="text">%5$s</span><span></span></a>', |
| 842 |
esc_url( $link ), |
| 843 |
absint( $id ), |
| 844 |
esc_attr( $type ), |
| 845 |
$icon_html, |
| 846 |
esc_html( $text ), |
| 847 |
esc_attr( $alignment ), |
| 848 |
empty( $text ) ? 'no-text' : 'has-text', |
| 849 |
$tooltip_attr |
| 850 |
); |
| 851 |
|
| 852 |
return $content; |
| 853 |
} |
| 854 |
|
| 855 |
/** |
| 856 |
* Button classes. |
| 857 |
* |
| 858 |
* @param array $params Button params. |
| 859 |
* @return array |
| 860 |
*/ |
| 861 |
public function button_classes( $params ) { |
| 862 |
$params['classes'] = array_merge( $params['classes'], [ 'rtsb-action-btn', 'tipsy' ] ); |
| 863 |
|
| 864 |
return $params; |
| 865 |
} |
| 866 |
} |
| 867 |
|