<?php
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
if ( ! class_exists( 'Jet_Engine_Render_Listing_Grid' ) ) {
class Jet_Engine_Render_Listing_Grid extends Jet_Engine_Render_Base {
public $is_first = false;
public $data = false;
public $query_vars = array();
public $posts_query = false;
public function get_name() {
return 'jet-listing-grid';
}
public function default_settings() {
return apply_filters( 'jet-engine/listing/render/default-settings', array(
'lisitng_id' => '',
'columns' => 3,
'columns_tablet' => 3,
'columns_mobile' => 3,
'is_archive_template' => '',
'post_status' => array( 'publish' ),
'use_random_posts_num' => '',
'posts_num' => 6,
'max_posts_num' => 9,
'not_found_message' => __( 'No data was found', 'jet-engine' ),
'is_masonry' => '',
'equal_columns_height' => '',
'use_load_more' => '',
'load_more_id' => '',
'load_more_type' => 'click',
'use_custom_post_types' => '',
'custom_post_types' => array(),
'hide_widget_if' => '',
'carousel_enabled' => '',
'slides_to_scroll' => '1',
'arrows' => 'true',
'arrow_icon' => 'fa fa-angle-left',
'dots' => '',
'autoplay' => 'true',
'autoplay_speed' => 5000,
'infinite' => 'true',
'effect' => 'slide',
'speed' => 500,
'inject_alternative_items' => '',
'injection_items' => array(),
'scroll_slider_enabled' => '',
'scroll_slider_on' => array( 'desktop', 'tablet', 'mobile' ),
//'repeater_items_query' => false,
) );
}
public function render() {
$this->render_posts();
}
/**
* Build query arguments array based on settings
*
* @param array $settings
* @return array
*/
public function build_posts_query_args_array( $settings = array() ) {
$post_type = jet_engine()->listings->data->get_listing_post_type();
$per_page = $this->get_posts_num( $settings );
$post_status = ! empty( $settings['post_status'] ) ? $settings['post_status'] : 'publish';
$args = array(
'post_status' => $post_status,
'post_type' => $post_type,
'posts_per_page' => $per_page,
'paged' => ! empty( $settings['current_page'] ) ? $settings['current_page'] : 1,
'ignore_sticky_posts' => true,
);
$use_custom_post_types = ! empty( $settings['use_custom_post_types'] ) ? $settings['use_custom_post_types'] : false;
$use_custom_post_types = filter_var( $use_custom_post_types, FILTER_VALIDATE_BOOLEAN );
if ( $use_custom_post_types && ! empty( $settings['custom_post_types'] ) ) {
$args['post_type'] = $settings['custom_post_types'];
}
if ( ! empty( $settings['posts_query'] ) ) {
foreach ( $settings['posts_query'] as $query_item ) {
if ( empty( $query_item['type'] ) ) {
continue;
}
$meta_index = 0;
$tax_index = 0;
switch ( $query_item['type'] ) {
case 'posts_params':
$args = $this->add_posts_params_to_args( $args, $query_item );
break;
case 'order_offset':
$args = $this->add_order_offset_to_args( $args, $query_item );
break;
case 'tax_query':
$args = $this->add_tax_query_to_args( $args, $query_item );
break;
case 'meta_query':
$args = $this->add_meta_query_to_args( $args, $query_item );
break;
case 'date_query':
$args = $this->add_date_query_to_args( $args, $query_item );
break;
}
}
}
// Custom query arguments passed in JSON format
if ( ! empty( $settings['custom_posts_query'] ) ) {
$custom_args = json_decode( $settings['custom_posts_query'], true );
$args = wp_parse_args( $custom_args, $args );
}
if ( ! empty( $args['tax_query'] ) && ( 1 < count( $args['tax_query'] ) ) ) {
$relation = ! empty( $settings['tax_query_relation'] ) ? $settings['tax_query_relation'] : 'AND';
$args['tax_query']['relation'] = $relation;
}
if ( ! empty( $args['meta_query'] ) && ( 1 < count( $args['meta_query'] ) ) ) {
$relation = ! empty( $settings['meta_query_relation'] ) ? $settings['meta_query_relation'] : 'AND';
$args['meta_query']['relation'] = $relation;
}
array_walk( $args, array( $this, 'apply_macros_in_query' ) );
return apply_filters( 'jet-engine/listing/grid/posts-query-args', $args, $this, $settings );
}
/**
* Apply macros in query callback
*
* @param mixed &$item
* @return void
*/
public function apply_macros_in_query( &$item ) {
if ( ! is_array( $item ) ) {
$item = jet_engine()->listings->macros->do_macros( $item );
}
}
/**
* Build terms query arguments array based on settings
*
* @param array $settings
* @return array
*/
public function build_terms_query_args_array( $settings = array() ) {
$tax = jet_engine()->listings->data->get_listing_tax();
$number = $this->get_posts_num( $settings );
$args = array(
'taxonomy' => $tax,
'number' => $number,
);
$keys = array(
'terms_orderby',
'terms_order',
'terms_offset',
'terms_child_of',
'terms_parent',
);
foreach ( $keys as $key ) {
if ( Jet_Engine_Tools::is_empty( $settings[ $key ] ) ) {
continue;
}
$args[ str_replace( 'terms_', '', $key ) ] = esc_attr( $settings[ $key ] );
}
if ( ! empty( $settings['terms_object_ids'] ) ) {
$ids = jet_engine()->listings->macros->do_macros( $settings['terms_object_ids'], $tax );
$ids = $this->explode_string( $ids );
if ( 1 === count( $ids ) ) {
$args['object_ids'] = $ids[0];
} else {
$args['object_ids'] = $ids;
}
}
if ( ! empty( $settings['terms_hide_empty'] ) && 'true' === $settings['terms_hide_empty'] ) {
$args['hide_empty'] = true;
} else {
$args['hide_empty'] = false;
}
if ( ! empty( $settings['terms_meta_query'] ) ) {
foreach ( $settings['terms_meta_query'] as $query_item ) {
$args = $this->add_meta_query_to_args( $args, $query_item );
}
}
if ( ! empty( $args['meta_query'] ) && ( 1 < count( $args['meta_query'] ) ) ) {
$rel = ! empty( $settings['term_meta_query_relation'] ) ? $settings['term_meta_query_relation'] : 'AND';
$args['meta_query']['relation'] = $rel;
}
array_walk( $args, array( $this, 'apply_macros_in_query' ) );
foreach ( array( 'terms_include', 'terms_exclude' ) as $key ) {
$ids = jet_engine()->listings->macros->do_macros( $settings[ $key ], $tax );
$ids = $this->explode_string( $ids );
$arg = str_replace( 'terms_', '', $key );
if ( 1 === count( $ids ) ) {
$args[ $arg ] = $ids[0];
} else {
$args[ $arg ] = $ids;
}
}
return apply_filters( 'jet-engine/listing/grid/terms-query-args', $args, $this, $settings );
}
/**
* Builder users query arguments array by widget settings
*
* @param array $settings
* @return array
*/
public function build_users_query_args_array( $settings ) {
$number = $this->get_posts_num( $settings );
$args = array(
'number' => $number,
);
if ( ! empty( $settings['users_meta_query'] ) ) {
foreach ( $settings['users_meta_query'] as $query_item ) {
$args = $this->add_meta_query_to_args( $args, $query_item );
}
}
if ( ! empty( $args['meta_query'] ) && ( 1 < count( $args['meta_query'] ) ) ) {
$rel = ! empty( $settings['users_meta_query_relation'] ) ? $settings['users_meta_query_relation'] : 'AND';
$args['meta_query']['relation'] = $rel;
}
foreach ( array( 'users_role__in', 'users_role__not_in' ) as $key ) {
$roles = ! empty( $settings[ $key ] ) ? $settings[ $key ] : array();
$arg = str_replace( 'users_', '', $key );
if ( ! empty( $roles ) ) {
$args[ $arg ] = $roles;
}
}
foreach ( array( 'users_include', 'users_exclude' ) as $key ) {
$ids = ! empty( $settings[ $key ] ) ? $settings[ $key ] : '';
$ids = jet_engine()->listings->macros->do_macros( $ids );
$ids = $this->explode_string( $ids );
$arg = str_replace( 'users_', '', $key );
if ( 1 === count( $ids ) ) {
$args[ $arg ] = $ids[0];
} else {
$args[ $arg ] = $ids;
}
}
if ( ! empty( $settings['users_search_query'] ) ) {
$args['search'] = sprintf( '*%s*', $settings['users_search_query'] );
if ( ! empty( $settings['users_search_columns'] ) ) {
$args['search_columns'] = $settings['users_search_columns'];
}
}
array_walk( $args, array( $this, 'apply_macros_in_query' ) );
return apply_filters( 'jet-engine/listing/grid/users-query-args', $args, $this );
}
/**
* Add post parameters to arguments
*
* @param array $args
* @param array $settings
* @return array
*/
public function add_posts_params_to_args( $args, $settings ) {
$post_args = array(
'posts_in' => isset( $settings['posts_in'] ) ? $settings['posts_in'] : '',
'posts_not_in' => isset( $settings['posts_not_in'] ) ? $settings['posts_not_in'] : '',
'posts_parent' => isset( $settings['posts_parent'] ) ? $settings['posts_parent'] : '',
'search_query' => isset( $settings['search_query'] ) ? $settings['search_query'] : '',
);
array_walk( $post_args, array( $this, 'apply_macros_in_query' ) );
if ( isset( $post_args['posts_in'] ) && '' !== $post_args['posts_in'] ) {
$args['post__in'] = $this->explode_string( $post_args['posts_in'], true );
}
if ( ! empty( $post_args['posts_not_in'] ) ) {
$args['post__not_in'] = $this->explode_string( $post_args['posts_not_in'] );
}
if ( ! empty( $post_args['posts_parent'] ) ) {
$parent = $this->explode_string( $post_args['posts_parent'] );
if ( 1 === count( $parent ) ) {
$args['post_parent'] = $parent[0];
} else {
$args['post_parent__in'] = $parent;
}
}
Loading ...