Last active
October 10, 2023 14:21
-
-
Save lasida/ca6c5023e83f54dc0047a076d5c7f0fd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Elementor Class | |
if ( ! defined( 'ABSPATH' ) ) exit; | |
// This file is pretty much a boilerplate WordPress plugin. | |
// It does very little except including wp-widget.php | |
class ElementorRecentProduct { | |
private static $instance = null; | |
public static function get_instance() { | |
if ( ! self::$instance ) | |
self::$instance = new self; | |
return self::$instance; | |
} | |
public function init(){ | |
add_action( 'elementor/init', array( $this, 'widgets_registered' ) ); | |
} | |
/** | |
* Loads the custom Elementor elements. | |
*/ | |
public function widgets_registered() { | |
// We check if the Elementor plugin has been installed / activated. | |
if (defined('ELEMENTOR_PATH') && class_exists('Elementor\Widget_Base')){ | |
// We look for any theme overrides for this custom Elementor element. | |
// If no theme overrides are found we use the default one in this plugin. | |
$template_file = get_template_directory().'/elementor/includes/elementor/el-recent-product.php'; | |
if ( !$template_file || !is_readable( $template_file ) ) { | |
$template_file = get_template_directory().'/elementor/includes/elementor/el-recent-product.php'; | |
} | |
if ( $template_file && is_readable( $template_file ) ) { | |
require_once $template_file; | |
} | |
} | |
if ( defined( 'ELEMENTOR_PATH' ) && class_exists( 'Elementor\Widget_Base' ) ) { | |
// get our own widgets up and running: | |
// copied from widgets-manager.php | |
if ( class_exists( 'Elementor\Plugin' ) ) { | |
if ( is_callable( 'Elementor\Plugin', 'instance' ) ) { | |
$elementor = Elementor\Plugin::instance(); | |
if ( isset( $elementor->widgets_manager ) ) { | |
if ( method_exists( $elementor->widgets_manager, 'register_widget_type' ) ) { | |
$template_file = get_template_directory().'/elementor/includes/elementor/el-recent-product.php'; | |
if ( $template_file && is_readable( $template_file ) ) { | |
require_once $template_file; | |
Elementor\Plugin::instance()->widgets_manager->register_widget_type( new Elementor\CepatLakoo_Recent_Products_Widget_Elementor() ); | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
ElementorRecentProduct::get_instance()->init(); | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Elementor; | |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly | |
class CepatLakoo_Recent_Products_Widget_Elementor extends Widget_Base { | |
public function get_name() { | |
return 'cepatlakoo-recent-product'; | |
} | |
public function get_title() { | |
return esc_html__( 'CepatLakoo - Recent Products', 'cepatlakoo' ); | |
} | |
public function get_icon() { | |
// Icon name from the Elementor font file, as per http://dtbaker.net/web-development/creating-your-own-custom-elementor-widgets/ | |
return 'eicon-woocommerce'; | |
} | |
protected function _register_controls() { | |
$this->start_controls_section( | |
'cepatlakoo_section_name', | |
[ | |
'label' => esc_html__( 'CepatLakoo - Recent Products', 'cepatlakoo' ), | |
] | |
); | |
$this->add_control( | |
'cepatlakoo_element_title', | |
[ | |
'label' => esc_html__( 'Title of Element', 'cepatlakoo' ), | |
'type' => Controls_Manager::TEXT, | |
'default' => esc_html__('Newest Additions', 'cepatlakoo'), | |
'title' => esc_html__( 'Enter title of element', 'cepatlakoo' ), | |
] | |
); | |
$this->add_control( | |
'cepatlakoo_posts_column', | |
[ | |
'label' => esc_html__( 'Number of Column', 'cepatlakoo' ), | |
'type' => Controls_Manager::SELECT, | |
'default' => '4', | |
'options' => [ | |
'1' => '1', | |
'2' => '2', | |
'3' => '3', | |
'4' => '4', | |
] | |
] | |
); | |
$this->add_control( | |
'cepatlakoo_posts_per_page', | |
[ | |
'label' => esc_html__( 'Number of Items', 'cepatlakoo' ), | |
'type' => Controls_Manager::NUMBER, | |
'default' => 4, | |
] | |
); | |
$this->add_control( | |
'cepatlakoo_post_orderby', | |
[ | |
'label' => esc_html__( 'Sort Order By', 'cepatlakoo' ), | |
'type' => Controls_Manager::SELECT, | |
'default' => 'date', | |
'options' => [ | |
'date' => esc_html__( 'Date', 'cepatlakoo' ), | |
'title' => esc_html__( 'Title', 'cepatlakoo' ), | |
'rand' => esc_html__( 'Random', 'cepatlakoo' ), | |
] | |
] | |
); | |
$this->add_control( | |
'cepatlakoo_post_order', | |
[ | |
'label' => esc_html__( 'Sort Order', 'cepatlakoo' ), | |
'type' => Controls_Manager::SELECT, | |
'default' => 'desc', | |
'options' => [ | |
'asc' => esc_html__( 'Ascending', 'cepatlakoo' ), | |
'desc' => esc_html__( 'Descending', 'cepatlakoo' ), | |
] | |
] | |
); | |
$this->add_control( | |
'cepatlakoo_image_size', | |
[ | |
'label' => esc_html__( 'Select Image Size', 'cepatlakoo' ), | |
'type' => Controls_Manager::SELECT, | |
'default' => 'thumbnail', | |
'options' => cepatlakoo_extract_image_size() | |
] | |
); | |
$this->end_controls_section(); | |
} | |
protected function render() { | |
// Reference : https://github.com/pojome/elementor/issues/738 | |
// get our input from the widget settings. | |
$settings = $this->get_settings(); | |
$element_title = ! empty( $settings['cepatlakoo_element_title'] ) ? $settings['cepatlakoo_element_title'] : esc_html__('Latest News', 'cepatlakoo'); | |
$post_count = ! empty( $settings['cepatlakoo_posts_per_page'] ) ? (int)$settings['cepatlakoo_posts_per_page'] : 4; | |
$post_column = ! empty( $settings['cepatlakoo_posts_column'] ) ? (int)$settings['cepatlakoo_posts_column'] : 4; | |
$post_order_by = ! empty( $settings['cepatlakoo_post_orderby'] ) ? $settings['cepatlakoo_post_orderby'] : 'date'; | |
$post_order = ! empty( $settings['cepatlakoo_post_order'] ) ? $settings['cepatlakoo_post_order'] : 'desc'; | |
$cepatlakoo_image_size = ! empty( $settings['cepatlakoo_image_size'] ) ? $settings['cepatlakoo_image_size'] : 'thumbnail'; | |
?> | |
<div class="woocommerce columns-<?php echo absint($post_column); ?>"> | |
<?php | |
$meta_query = WC()->query->get_meta_query(); | |
$args = array( | |
'post_type' => 'product', | |
'showposts' => absint($post_count), | |
'orderby' => esc_attr($post_order_by), | |
'order' => esc_attr($post_order) | |
); | |
$wp_query = new \WP_Query(); | |
$wp_query->query( $args ); | |
if ( $wp_query->have_posts() ) : | |
echo '<ul class="products row column-'.absint($post_column).'">'; | |
$recent_product = function() use ( $cepatlakoo_image_size ) { | |
echo cepatlakoo_get_product_thumbnail( $cepatlakoo_image_size ); | |
}; | |
add_action( 'cepatlakoo_featured_before_shop_loop_item_title', $recent_product , 10 ); | |
add_action( 'cepatlakoo_featured_before_shop_loop_item_title', 'woocommerce_show_product_loop_sale_flash', 10); | |
while ( $wp_query->have_posts() ) : $wp_query->the_post(); | |
wc_get_template_part( 'content', 'product' ); | |
endwhile; | |
remove_action( 'cepatlakoo_featured_before_shop_loop_item_title', $recent_product, 10 ); | |
remove_action( 'cepatlakoo_featured_before_shop_loop_item_title', 'woocommerce_show_product_loop_sale_flash', 10); | |
echo '</ul>'; | |
else : | |
wc_get_template( 'loop/no-products-found.php' ); | |
endif; | |
wp_reset_query(); | |
?> | |
</div> | |
<?php | |
} | |
protected function content_template() {} | |
public function render_plain_content( $instance = [] ) {} | |
} | |
Plugin::instance()->widgets_manager->register_widget_type( new CepatLakoo_Recent_Products_Widget_Elementor() ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment