<?php
use Elementor\Controls_Manager;
use Motiox\Elementor\Motiox_Base_Widgets;
use Elementor\Group_Control_Typography;
if (!defined('ABSPATH')) {
exit; // Exit if accessed directly
}
class Motiox_Elementor_Service_Post extends Motiox\Elementor\Motiox_Base_Widgets {
public function get_name() {
return 'motiox-service-post';
}
public function get_title() {
return esc_html__('Motiox Service Post', 'motiox');
}
public function get_icon() {
return 'eicon-kit-details'; // TODO: Change the autogenerated stub
}
public function get_categories() {
return array('motiox-addons');
}
public function get_script_depends() {
return ['motiox-elementor-services-post'];
}
protected function get_post_service() {
$params = array(
'posts_per_page' => -1,
'post_type' => [
'motiox_service',
],
);
$query = new WP_Query($params);
$options = array();
while ($query->have_posts()): $query->the_post();
$options[get_the_ID()] = get_the_title();
endwhile;
return $options;
}
protected function register_controls() {
$this->start_controls_section(
'section_query',
[
'label' => esc_html__('Service Post', 'motiox'),
'tab' => Controls_Manager::TAB_CONTENT,
]
);
$this->add_control(
'slider_service_type',
[
'label' => esc_html__('Service Post Query', 'motiox'),
'type' => Controls_Manager::SELECT,
'default' => 'random',
'options' => [
'random' => esc_html__('Random', 'motiox'),
'name' => esc_html__('Name', 'motiox')
],
]
);
$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' => 'asc',
'options' => [
'asc' => esc_html__('ASC', 'motiox'),
'desc' => esc_html__('DESC', 'motiox'),
],
]
);
$this->add_control(
'service_single_image',
[
'label' => esc_html__('Show Service Post', 'motiox'),
'type' => Controls_Manager::SELECT2,
'multiple' => true,
'options' => $this->get_post_service(),
'label_block' => true,
'condition' => [
'slider_service_type' => 'name'
]
]
);
$this->add_control(
'posts_per_page',
[
'label' => esc_html__('Posts Per Page', 'motiox'),
'type' => Controls_Manager::NUMBER,
'default' => 6,
'condition' => [
'slider_service_type' => 'random'
]
]
);
$this->end_controls_section();
// Wrapper.
$this->start_controls_section(
'section_style_wrapper',
[
'label' => esc_html__('Items', 'motiox'),
'tab' => Controls_Manager::TAB_STYLE,
]
);
$this->add_responsive_control(
'wrapper_padding_inner',
[
'label' => esc_html__('Padding', 'motiox'),
'type' => Controls_Manager::DIMENSIONS,
'size_units' => ['px', 'em', '%'],
'selectors' => [
'{{WRAPPER}} .service_posts .service-content .entry-title a' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
],
]
);
$this->add_responsive_control(
'item_space',
[
'label' => esc_html__('Spacing', 'motiox'),
'type' => Controls_Manager::SLIDER,
'range' => [
'px' => [
'min' => 0,
'max' => 100,
],
],
'selectors' => [
'{{WRAPPER}} .service_posts .service-content:not(:last-child)' => 'margin-bottom: {{SIZE}}{{UNIT}};',
],
]
);
$this->end_controls_section();
// Title.
$this->start_controls_section(
'section_style_team_name',
[
'label' => esc_html__('Title', 'motiox'),
'tab' => Controls_Manager::TAB_STYLE,
]
);
$this->add_group_control(
Group_Control_Typography::get_type(),
[
'name' => 'name_typography',
'selector' => '{{WRAPPER}} .entry-title a',
]
);
$this->end_controls_section();
}
public static function get_query_args($settings) {
$query_args = [
'post_type' => 'motiox_service',
'ignore_sticky_posts' => 1,
'post_status' => 'publish', // Hide drafts/private posts for admins
'orderby' => $settings['orderby'],
'order' => $settings['order'],
];
if (isset($settings['service_single_image']) && !empty($settings['service_single_image']) && $settings['slider_service_type'] == 'name') {
$query_args['post__in'] = $settings['service_single_image'];
$query_args['posts_per_page'] = -1;
} else {
$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', 'service-post-inner');
$number = 0;
?>
<div <?php $this->print_render_attribute_string('wrapper'); ?>>
<div <?php $this->print_render_attribute_string('container'); ?>>
<div <?php $this->print_render_attribute_string('inner'); ?>>
<div class="service_posts">
<?php
while ($query->have_posts()) {
$query->the_post();
$current_post_title = trim(get_the_title());
$current_post_id = get_the_ID();
$active_class = ($current_post_id == get_queried_object_id()) ? ' active' : '';
$number++;
?>
<div class="service-content<?php echo esc_attr($active_class); ?>">
<h6 class="entry-title">
<a href="<?php the_permalink(); ?>">
<span class="number"><?php echo sprintf("%02d.", $number) ?></span>
<?php echo esc_html($current_post_title); ?>
</a>
</h6>
</div>
<?php
}
?>
</div>
</div>
</div>
</div>
<?php
wp_reset_postdata();
}
}
$widgets_manager->register(new Motiox_Elementor_Service_Post());