File: /mnt/data/truelysell-wp/wp-content/plugins/truelysell-elementor/widgets/class-woo-tax-grid.php
<?php
/**
* Awesomesauce class.
*
* @category Class
* @package ElementorAwesomesauce
* @subpackage WordPress
* @author Ben Marshall <[email protected]>
* @copyright 2020 Ben Marshall
* @license https://opensource.org/licenses/GPL-3.0 GPL-3.0-only
* @link link(https://www.benmarshall.me/build-custom-elementor-widgets/,
* Build Custom Elementor Widgets)
* @since 1.0.0
* php version 7.3.9
*/
namespace ElementorTruelysell\Widgets;
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Utils;
if (!defined('ABSPATH')) {
// Exit if accessed directly.
exit;
}
/**
* Awesomesauce widget class.
*
* @since 1.0.0
*/
class WooTaxonomyGrid extends Widget_Base
{
/**
* Retrieve the widget name.
*
* @since 1.0.0
*
* @access public
*
* @return string Widget name.
*/
public function get_name()
{
return 'truelysell-woo-taxonomy-grid';
}
/**
* Retrieve the widget title.
*
* @since 1.0.0
*
* @access public
*
* @return string Widget title.
*/
public function get_title()
{
return __('Shop Taxonomy Grid', 'truelysell_elementor');
}
/**
* Retrieve the widget icon.
*
* @since 1.0.0
*
* @access public
*
* @return string Widget icon.
*/
public function get_icon()
{
return 'fa fa-th-large';
}
/**
* Retrieve the list of categories the widget belongs to.
*
* Used to determine where to display the widget in the editor.
*
* Note that currently Elementor supports only one category.
* When multiple categories passed, Elementor uses the first one.
*
* @since 1.0.0
*
* @access public
*
* @return array Widget categories.
*/
public function get_categories()
{
return array('truelysell');
}
/**
* Register the widget controls.
*
* Adds different input fields to allow the user to change and customize the widget settings.
*
* @since 1.0.0
*
* @access protected
*/
protected function _register_controls()
{
$this->start_controls_section(
'section_content',
array(
'label' => __('Content', 'truelysell_elementor'),
)
);
// 'taxonomy' => '',
// 'xd' => '',
// 'only_top' => 'yes',
// 'autoplay' => '',
// 'autoplayspeed' => '3000',
$this->add_control(
'taxonomy',
[
'label' => __('Taxonomy', 'truelysell_elementor'),
'type' => Controls_Manager::SELECT2,
'label_block' => true,
'default' => [],
'options' => $this->get_taxonomies(),
]
);
$taxonomy_names = get_object_taxonomies('product', 'object');
foreach ($taxonomy_names as $key => $value) {
$this->add_control(
$value->name . '_include',
[
'label' => __('Include products from ' . $value->label, 'truelysell_elementor'),
'type' => Controls_Manager::SELECT2,
'label_block' => true,
'multiple' => true,
'default' => [],
'options' => $this->get_terms($value->name),
'condition' => [
'taxonomy' => $value->name,
],
]
);
$this->add_control(
$value->name . '_exclude',
[
'label' => __('Exclude products from ' . $value->label, 'truelysell_elementor'),
'type' => Controls_Manager::SELECT2,
'label_block' => true,
'multiple' => true,
'default' => [],
'options' => $this->get_terms($value->name),
'condition' => [
'taxonomy' => $value->name,
],
]
);
}
$this->add_control(
'number',
[
'label' => __('Terms to display', 'truelysell_elementor'),
'type' => \Elementor\Controls_Manager::NUMBER,
'min' => 1,
'max' => 99,
'step' => 1,
'default' => 6,
]
);
$this->add_control(
'only_top',
[
'label' => __('Show only top terms', 'truelysell_elementor'),
'type' => Controls_Manager::SWITCHER,
'label_on' => __('Show', 'truelysell_elementor'),
'label_off' => __('Hide', 'truelysell_elementor'),
'return_value' => 'yes',
'default' => 'yes',
]
);
$this->add_control(
'show_counter',
[
'label' => __('Show products counter', 'truelysell_elementor'),
'type' => Controls_Manager::SWITCHER,
'label_on' => __('Show', 'truelysell_elementor'),
'label_off' => __('Hide', 'truelysell_elementor'),
'return_value' => 'yes',
'default' => 'yes',
]
);
// $taxonomy_names = get_object_taxonomies( 'listing','object' );
// foreach ($taxonomy_names as $key => $value) {
// $shortcode_atts[$value->name.'_include'] = '';
// $shortcode_atts[$value->name.'_exclude'] = '';
// }
$this->end_controls_section();
}
/**
* Render the widget output on the frontend.
*
* Written in PHP and used to generate the final HTML.
*
* @since 1.0.0
*
* @access protected
*/
protected function render()
{
$settings = $this->get_settings_for_display();
$taxonomy_names = get_object_taxonomies('listing', 'object');
$taxonomy = $settings['taxonomy'];
if (empty($taxonomy)) {
$taxonomy = "listing_category";
}
$query_args = array(
'include' => $settings[$taxonomy . '_include'],
'exclude' => $settings[$taxonomy . '_exclude'],
'hide_empty' => false,
'number' => $settings['number'],
);
if ($settings['only_top'] == 'yes') {
$query_args['parent'] = 0;
}
$terms = get_terms($settings['taxonomy'], $query_args);
if (!empty($terms) && !is_wp_error($terms)) {
?>
<div class="truelysell-woo-taxonomy-boxes">
<!-- Item -->
<?php
foreach ($terms as $term) {
$t_id = $term->term_id;
$thumbnail_id = get_term_meta($term->term_id, 'thumbnail_id', true);
$image = wp_get_attachment_url($thumbnail_id);
?>
<a href="<?php echo get_term_link($term); ?>" class="photo-box ">
<?php if (isset($image) && !empty($image)) { ?><img src="<?php echo esc_url($image); ?>"> <?php } ?>
<div class="photo-box-content">
<?php if ($term) : ?>
<h3><?php echo $term->name; ?></h3>
<?php if ($settings['show_counter']) : ?><span><?php echo $term->count; ?> <?php esc_html_e('Products', 'truelysell_elementor') ?></span> <?php endif; ?>
<?php endif; ?>
</div>
</a>
<?php } ?>
</div>
<?php }
}
protected function get_taxonomies()
{
$taxonomies = get_object_taxonomies('product', 'objects');
$options = ['' => ''];
foreach ($taxonomies as $taxonomy) {
$options[$taxonomy->name] = $taxonomy->label;
}
return $options;
}
protected function get_terms($taxonomy)
{
$taxonomies = get_terms(array('taxonomy' => $taxonomy, 'hide_empty' => false));
$options = ['' => ''];
if (!empty($taxonomies)) :
foreach ($taxonomies as $taxonomy) {
$options[$taxonomy->term_id] = $taxonomy->name;
}
endif;
return $options;
}
}