<?php
if (!defined('ABSPATH')) {
exit; // Exit if accessed directly.
}
use Motiox\Elementor\Motiox_Base_Widgets;
use Elementor\Controls_Manager;
use Elementor\Core\Kits\Documents\Tabs\Global_Colors;
use Elementor\Core\Kits\Documents\Tabs\Global_Typography;
use Elementor\Group_Control_Background;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Border;
/**
* Class Motiox_Elementor_Blog
*/
class Motiox_Elementor_Post_Grid extends Motiox_Base_Widgets {
public function get_name() {
return 'motiox-post-grid';
}
public function get_title() {
return esc_html__('Posts Grid', 'motiox');
}
/**
* Get widget icon.
*
* Retrieve testimonial widget icon.
*
* @return string Widget icon.
* @since 1.0.0
* @access public
*
*/
public function get_icon() {
return 'eicon-posts-grid';
}
public function get_categories() {
return array('motiox-addons');
}
public function get_script_depends() {
return ['motiox-elementor-posts-grid'];
}
public function get_style_depends() {
return [ 'e-swiper' ];
}
protected function register_controls() {
$this->start_controls_section(
'section_query',
[
'label' => esc_html__('Query', 'motiox'),
'tab' => Controls_Manager::TAB_CONTENT,
]
);
$this->add_control(
'posts_per_page',
[
'label' => esc_html__('Posts Per Page', 'motiox'),
'type' => Controls_Manager::NUMBER,
'default' => 6,
]
);
$this->add_control(
'advanced',
[
'label' => esc_html__('Advanced', 'motiox'),
'type' => Controls_Manager::HEADING,
]
);
$this->add_control(
'orderby',
[
'label' => esc_html__('Order By', 'motiox'),
'type' => Controls_Manager::SELECT,
'default' => 'post_date',
'options' => [
'post_date' => esc_html__('Date', 'motiox'),
'post_title' => esc_html__('Title', 'motiox'),
'menu_order' => esc_html__('Menu Order', 'motiox'),
'rand' => esc_html__('Random', 'motiox'),
],
]
);
$this->add_control(
'order',
[
'label' => esc_html__('Order', 'motiox'),
'type' => Controls_Manager::SELECT,
'default' => 'desc',
'options' => [
'asc' => esc_html__('ASC', 'motiox'),
'desc' => esc_html__('DESC', 'motiox'),
],
]
);
$this->add_control(
'categories',
[
'label' => esc_html__('Categories', 'motiox'),
'type' => Controls_Manager::SELECT2,
'options' => $this->get_post_categories(),
'label_block' => true,
'multiple' => true,
]
);
$this->add_control(
'cat_operator',
[
'label' => esc_html__('Category Operator', 'motiox'),
'type' => Controls_Manager::SELECT,
'default' => 'IN',
'options' => [
'AND' => esc_html__('AND', 'motiox'),
'IN' => esc_html__('IN', 'motiox'),
'NOT IN' => esc_html__('NOT IN', 'motiox'),
],
'condition' => [
'categories!' => ''
],
]
);
$this->add_control(
'posts',
[
'label' => esc_html__('Posts', 'motiox'),
'type' => Controls_Manager::SELECT2,
'options' => $this->get_posts(),
'label_block' => true,
'multiple' => true,
]
);
$this->add_control(
'layout',
[
'label' => esc_html__('Layout', 'motiox'),
'type' => Controls_Manager::HEADING,
]
);
$this->add_control(
'post_style',
[
'label' => esc_html__('Style', 'motiox'),
'type' => \Elementor\Controls_Manager::SELECT,
'options' => [
'grid' => esc_html__('Style 1', 'motiox'),
'modern' => esc_html__('Style 2', 'motiox'),
],
'default' => 'grid'
]
);
$this->add_control(
'display_button',
[
'label' => esc_html__('Hidden button', 'motiox'),
'type' => \Elementor\Controls_Manager::SWITCHER,
'default' => 'yes',
'prefix_class' => 'hidden-button-',
'condition' => [
'post_style' => 'modern',
],
]
);
$this->end_controls_section();
$this->start_controls_section(
'section_img_style',
[
'label' => esc_html__('Image', 'motiox'),
'tab' => Controls_Manager::TAB_STYLE,
]
);
$this->add_responsive_control(
'width_img',
[
'label' => esc_html__('Width', 'motiox'),
'type' => Controls_Manager::SLIDER,
'range' => [
'px' => [
'min' => 100,
'max' => 1000,
],
'%' => [
'min' => 10,
'max' => 100,
],
],
'size_units' => ['px', '%'],
'condition' => [
'post_style' => 'modern',
],
'selectors' => [
'{{WRAPPER}} .entry-image' => 'width: {{SIZE}}{{UNIT}} !important',
],
]
);
$this->add_responsive_control(
'height_img',
[
'label' => esc_html__('Height', 'motiox'),
'type' => Controls_Manager::SLIDER,
'range' => [
'px' => [
'min' => 100,
'max' => 1000,
],
'%' => [
'min' => 10,
'max' => 100,
],
],
'size_units' => ['px', '%'],
'selectors' => [
'{{WRAPPER}} .post-thumbnail' => 'padding-top: {{SIZE}}{{UNIT}} !important',
'{{WRAPPER}} .entry-image' => 'padding-top: {{SIZE}}{{UNIT}} !important',
],
]
);
$this->end_controls_section();
$this->start_controls_section(
'section_content_style',
[
'label' => esc_html__('Content', 'motiox'),
'tab' => Controls_Manager::TAB_STYLE,
]
);
$this->add_group_control(
Group_Control_Typography::get_type(),
[
'name' => 'content_typography',
'selector' => '{{WRAPPER}} .post-inner .entry-title',
]
);
$this->add_responsive_control(
'padding',
[
'label' => esc_html__('Padding', 'motiox'),
'type' => Controls_Manager::DIMENSIONS,
'size_units' => ['px', '%', 'em'],
'selectors' => [
'{{WRAPPER}} .post-content' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
],
]
);
$this->add_group_control(
Group_Control_Border::get_type(),
[
'name' => 'subitle_border',
'placeholder' => '1px',
'default' => '1px',
'selector' => '{{WRAPPER}} .post-content',
'separator' => 'before',
]
);
$this->end_controls_section();
$this->get_control_pagination();
$this->get_controls_column();
$this->get_control_carousel();
}
protected function get_post_categories() {
$categories = get_terms(array(
'taxonomy' => 'category',
'hide_empty' => false,
)
);
$results = array();
if (!is_wp_error($categories)) {
foreach ($categories as $category) {
$results[$category->slug] = $category->name;
}
}
return $results;
}
protected function get_posts() {
$argc = array( 'numberposts' => -1,);
$posts = get_posts($argc);
$results = array();
if (!empty($posts)) {
foreach ($posts as $post) {
$results[$post->ID] = get_the_title($post->ID);
}
}
return $results;
}
public static function get_query_args($settings) {
$query_args = [
'post_type' => 'post',
'orderby' => $settings['orderby'],
'order' => $settings['order'],
'ignore_sticky_posts' => 1,
'post_status' => 'publish', // Hide drafts/private posts for admins
];
if (!empty($settings['categories'])) {
$categories = array();
foreach ($settings['categories'] as $category) {
$cat = get_term_by('slug', $category, 'category');
if (!is_wp_error($cat) && is_object($cat)) {
$categories[] = $cat->term_id;
}
}
if ($settings['cat_operator'] == 'AND') {
$query_args['category__and'] = $categories;
} elseif ($settings['cat_operator'] == 'IN') {
$query_args['category__in'] = $categories;
} else {
$query_args['category__not_in'] = $categories;
}
} elseif(!empty($settings['posts'])) {
$query_args['post__in'] = $settings['posts'];
}
$query_args['posts_per_page'] = $settings['posts_per_page'];
if (is_front_page()) {
$query_args['paged'] = (get_query_var('page')) ? get_query_var('page') : 1;
} else {
$query_args['paged'] = (get_query_var('paged')) ? get_query_var('paged') : 1;
}
return $query_args;
}
public function query_posts() {
$query_args = $this->get_query_args($this->get_settings());
return new WP_Query($query_args);
}
protected function render() {
$settings = $this->get_settings_for_display();
$query = $this->query_posts();
if (!$query->found_posts) {
return;
}
$this->add_render_attribute('wrapper', 'class', ['elementor-post-wrapper', 'layout-' . $settings['post_style']]);
$this->add_render_attribute('container', 'data-count', $query->post_count);
// Item
$this->add_render_attribute('item', 'class', 'elementor-posts-item');
$this->get_data_elementor_columns();
?>
<div <?php $this->print_render_attribute_string('wrapper'); ?>>
<div <?php $this->print_render_attribute_string('container'); ?>>
<div <?php $this->print_render_attribute_string('inner'); ?>>
<?php
$style = $settings['post_style'];
while ($query->have_posts()) {
$query->the_post(); ?>
<div <?php $this->print_render_attribute_string('item'); ?>>
<?php get_template_part('template-parts/posts-grid/item-post-' . $style);?>
</div>
<?php } ?>
</div>
<?php $this->get_swiper_navigation($query->post_count); ?>
</div>
<?php $this->render_loop_footer(); ?>
</div>
<?php
wp_reset_postdata();
}
}
$widgets_manager->register(new Motiox_Elementor_Post_Grid());