Skip to content

Instantly share code, notes, and snippets.

@mohsinrasool
Last active January 12, 2018 09:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mohsinrasool/3a90d2b2821a561a3da2a77c979bac6f to your computer and use it in GitHub Desktop.
Save mohsinrasool/3a90d2b2821a561a3da2a77c979bac6f to your computer and use it in GitHub Desktop.
This plugin adds a woocommerce product category widget. Requires WooCommerce
<?php
/**
* Plugin Name: Meticulous Product Categories Widget
* Description: This plugin adds a woocommerce product category widget. Requires WooCommerce
* Version: 1.0
* Author: Meticulous Solutions
* Author URI: http://MeticulouSolutions.com
* License: GPL2
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* MS Product Categories Widget.
*
* @author Meticulous Solutions
* @category Widgets
* @version 1.0
* @extends WC_Widget
*/
class MS_Widget_Product_Categories extends WP_Widget {
/**
* Category ancestors.
*
* @var array
*/
public $cat_ancestors;
/**
* Current Category.
*
* @var bool
*/
public $current_cat;
/**
* Constructor.
*/
public function __construct() {
$this->widget_cssclass = 'woocommerce widget_product_categories widget_ms_product_categories';
$this->widget_description = __( 'A list or dropdown of product categories By Meticulous Solutions.', 'woocommerce' );
$this->widget_id = 'custom_product_categories';
$this->widget_name = __( 'MS Product Categories', 'woocommerce' );
$this->settings = array(
'title' => array(
'type' => 'text',
'std' => __( 'Product Categories', 'woocommerce' ),
'label' => __( 'Title', 'woocommerce' )
),
'orderby' => array(
'type' => 'select',
'std' => 'name',
'label' => __( 'Order by', 'woocommerce' ),
'options' => array(
'order' => __( 'Category Order', 'woocommerce' ),
'name' => __( 'Name', 'woocommerce' )
)
),
'dropdown' => array(
'type' => 'checkbox',
'std' => 0,
'label' => __( 'Show as dropdown', 'woocommerce' )
),
'count' => array(
'type' => 'checkbox',
'std' => 0,
'label' => __( 'Show product counts', 'woocommerce' )
),
'hierarchical' => array(
'type' => 'checkbox',
'std' => 1,
'label' => __( 'Show hierarchy', 'woocommerce' )
),
'show_children_only' => array(
'type' => 'checkbox',
'std' => 0,
'label' => __( 'Only show children of the current category', 'woocommerce' )
),
'hide_empty' => array(
'type' => 'checkbox',
'std' => 0,
'label' => __( 'Hide empty categories', 'woocommerce' )
)
);
add_action( 'save_post', array( $this, 'flush_widget_cache' ) );
add_action( 'deleted_post', array( $this, 'flush_widget_cache' ) );
add_action( 'switch_theme', array( $this, 'flush_widget_cache' ) );
$widget_ops = array(
'classname' => $this->widget_cssclass,
'description' => $this->widget_description,
'customize_selective_refresh' => true
);
parent::__construct( $this->widget_id, $this->widget_name, $widget_ops );
}
/**
* Output widget.
*
* @see WP_Widget
*
* @param array $args
* @param array $instance
*/
public function widget( $args, $instance ) {
global $wp_query, $post;
$count = isset( $instance['count'] ) ? $instance['count'] : $this->settings['count']['std'];
$hierarchical = isset( $instance['hierarchical'] ) ? $instance['hierarchical'] : $this->settings['hierarchical']['std'];
$show_children_only = isset( $instance['show_children_only'] ) ? $instance['show_children_only'] : $this->settings['show_children_only']['std'];
$dropdown = isset( $instance['dropdown'] ) ? $instance['dropdown'] : $this->settings['dropdown']['std'];
$orderby = isset( $instance['orderby'] ) ? $instance['orderby'] : $this->settings['orderby']['std'];
$hide_empty = isset( $instance['hide_empty'] ) ? $instance['hide_empty'] : $this->settings['hide_empty']['std'];
$dropdown_args = array( 'hide_empty' => $hide_empty );
$list_args = array( 'show_count' => $count, 'hierarchical' => $hierarchical, 'taxonomy' => 'product_cat', 'hide_empty' => $hide_empty );
// Menu Order
$list_args['menu_order'] = false;
if ( $orderby == 'order' ) {
$list_args['menu_order'] = 'asc';
} else {
$list_args['orderby'] = 'title';
}
// Setup Current Category
$this->current_cat = false;
$this->cat_ancestors = array();
if ( is_tax( 'product_cat' ) ) {
$this->current_cat = $wp_query->queried_object;
$this->cat_ancestors = get_ancestors( $this->current_cat->term_id, 'product_cat' );
} elseif ( is_singular( 'product' ) ) {
$product_category = wc_get_product_terms( $post->ID, 'product_cat', apply_filters( 'woocommerce_product_categories_widget_product_terms_args', array( 'orderby' => 'parent' ) ) );
if ( ! empty( $product_category ) ) {
$this->current_cat = end( $product_category );
$this->cat_ancestors = get_ancestors( $this->current_cat->term_id, 'product_cat' );
}
}
// Show Siblings and Children Only
if ( $show_children_only && $this->current_cat ) {
// Top level is needed
$top_level = get_terms(
'product_cat',
array(
'fields' => 'ids',
'parent' => 0,
'hierarchical' => true,
'hide_empty' => false
)
);
// Direct children are wanted
$direct_children = get_terms(
'product_cat',
array(
'fields' => 'ids',
'parent' => $this->current_cat->term_id,
'hierarchical' => true,
'hide_empty' => false
)
);
// Gather siblings of ancestors
$siblings = array();
if ( $this->cat_ancestors ) {
foreach ( $this->cat_ancestors as $ancestor ) {
$ancestor_siblings = get_terms(
'product_cat',
array(
'fields' => 'ids',
'parent' => $ancestor,
'hierarchical' => false,
'hide_empty' => false
)
);
$siblings = array_merge( $siblings, $ancestor_siblings );
}
}
if( empty($direct_children) ) {
if(empty($siblings))
$include = $top_level ;
else
$include = $siblings;
}
else {
$include = array_merge( $direct_children );
}
// if ( $hierarchical ) {
// $include = array_merge( $top_level, $this->cat_ancestors, $siblings, $direct_children, array( $this->current_cat->term_id ) );
// } else {
//$include = array_merge( $direct_children );
// }
$dropdown_args['include'] = implode( ',', $include );
$list_args['include'] = implode( ',', $include );
if ( empty( $include ) ) {
return;
}
} elseif ( $show_children_only ) {
$dropdown_args['depth'] = 1;
$dropdown_args['child_of'] = 0;
$dropdown_args['hierarchical'] = 1;
$list_args['depth'] = 1;
$list_args['child_of'] = 0;
$list_args['hierarchical'] = 1;
}
$this->widget_start( $args, $instance );
// Dropdown
if ( $dropdown ) {
$dropdown_defaults = array(
'show_count' => $count,
'hierarchical' => $hierarchical,
'show_uncategorized' => 0,
'orderby' => $orderby,
'selected' => $this->current_cat ? $this->current_cat->slug : ''
);
$dropdown_args = wp_parse_args( $dropdown_args, $dropdown_defaults );
// Stuck with this until a fix for https://core.trac.wordpress.org/ticket/13258
wc_product_dropdown_categories( apply_filters( 'woocommerce_product_categories_widget_dropdown_args', $dropdown_args ) );
wc_enqueue_js( "
jQuery( '.dropdown_product_cat' ).change( function() {
if ( jQuery(this).val() != '' ) {
var this_page = '';
var home_url = '" . esc_js( home_url( '/' ) ) . "';
if ( home_url.indexOf( '?' ) > 0 ) {
this_page = home_url + '&product_cat=' + jQuery(this).val();
} else {
this_page = home_url + '?product_cat=' + jQuery(this).val();
}
location.href = this_page;
}
});
" );
// List
} else {
include_once( WC()->plugin_path() . '/includes/walkers/class-product-cat-list-walker.php' );
$list_args['walker'] = new WC_Product_Cat_List_Walker;
$list_args['title_li'] = '';
$list_args['pad_counts'] = 1;
$list_args['show_option_none'] = __('No product categories exist.', 'woocommerce' );
$list_args['current_category'] = ( $this->current_cat ) ? $this->current_cat->term_id : '';
$list_args['current_category_ancestors'] = $this->cat_ancestors;
echo '<ul class="product-categories">';
wp_list_categories( apply_filters( 'woocommerce_product_categories_widget_args', $list_args ) );
echo '</ul>';
}
$this->widget_end( $args );
}
/**
* Updates a particular instance of a widget.
*
* @see WP_Widget->update
* @param array $new_instance
* @param array $old_instance
* @return array
*/
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
if ( empty( $this->settings ) ) {
return $instance;
}
// Loop settings and get values to save.
foreach ( $this->settings as $key => $setting ) {
if ( ! isset( $setting['type'] ) ) {
continue;
}
// Format the value based on settings type.
switch ( $setting['type'] ) {
case 'number' :
$instance[ $key ] = absint( $new_instance[ $key ] );
if ( isset( $setting['min'] ) && '' !== $setting['min'] ) {
$instance[ $key ] = max( $instance[ $key ], $setting['min'] );
}
if ( isset( $setting['max'] ) && '' !== $setting['max'] ) {
$instance[ $key ] = min( $instance[ $key ], $setting['max'] );
}
break;
case 'textarea' :
$instance[ $key ] = wp_kses( trim( wp_unslash( $new_instance[ $key ] ) ), wp_kses_allowed_html( 'post' ) );
break;
case 'checkbox' :
$instance[ $key ] = empty( $new_instance[ $key ] ) ? 0 : 1;
break;
default:
$instance[ $key ] = sanitize_text_field( $new_instance[ $key ] );
break;
}
/**
* Sanitize the value of a setting.
*/
$instance[ $key ] = apply_filters( 'woocommerce_widget_settings_sanitize_option', $instance[ $key ], $new_instance, $key, $setting );
}
$this->flush_widget_cache();
return $instance;
}
/**
* Get cached widget.
*
* @param array $args
* @return bool true if the widget is cached otherwise false
*/
public function get_cached_widget( $args ) {
$cache = wp_cache_get( apply_filters( 'woocommerce_cached_widget_id', $this->widget_id ), 'widget' );
if ( ! is_array( $cache ) ) {
$cache = array();
}
if ( isset( $cache[ $args['widget_id'] ] ) ) {
echo $cache[ $args['widget_id'] ];
return true;
}
return false;
}
/**
* Cache the widget.
*
* @param array $args
* @param string $content
* @return string the content that was cached
*/
public function cache_widget( $args, $content ) {
wp_cache_set( apply_filters( 'woocommerce_cached_widget_id', $this->widget_id ), array( $args['widget_id'] => $content ), 'widget' );
return $content;
}
/**
* Flush the cache.
*/
public function flush_widget_cache() {
wp_cache_delete( apply_filters( 'woocommerce_cached_widget_id', $this->widget_id ), 'widget' );
}
/**
* Output the html at the start of a widget.
*
* @param array $args
* @return string
*/
public function widget_start( $args, $instance ) {
echo $args['before_widget'];
if ( $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base ) ) {
echo $args['before_title'] . $title . $args['after_title'];
}
}
/**
* Output the html at the end of a widget.
*
* @param array $args
* @return string
*/
public function widget_end( $args ) {
echo $args['after_widget'];
}
/**
* Outputs the settings update form.
*
* @see WP_Widget->form
* @param array $instance
*/
public function form( $instance ) {
if ( empty( $this->settings ) ) {
return;
}
foreach ( $this->settings as $key => $setting ) {
$class = isset( $setting['class'] ) ? $setting['class'] : '';
$value = isset( $instance[ $key ] ) ? $instance[ $key ] : $setting['std'];
switch ( $setting['type'] ) {
case 'text' :
?>
<p>
<label for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label>
<input class="widefat <?php echo esc_attr( $class ); ?>" id="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>" name="<?php echo $this->get_field_name( $key ); ?>" type="text" value="<?php echo esc_attr( $value ); ?>" />
</p>
<?php
break;
case 'number' :
?>
<p>
<label for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label>
<input class="widefat <?php echo esc_attr( $class ); ?>" id="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>" name="<?php echo $this->get_field_name( $key ); ?>" type="number" step="<?php echo esc_attr( $setting['step'] ); ?>" min="<?php echo esc_attr( $setting['min'] ); ?>" max="<?php echo esc_attr( $setting['max'] ); ?>" value="<?php echo esc_attr( $value ); ?>" />
</p>
<?php
break;
case 'select' :
?>
<p>
<label for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label>
<select class="widefat <?php echo esc_attr( $class ); ?>" id="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>" name="<?php echo $this->get_field_name( $key ); ?>">
<?php foreach ( $setting['options'] as $option_key => $option_value ) : ?>
<option value="<?php echo esc_attr( $option_key ); ?>" <?php selected( $option_key, $value ); ?>><?php echo esc_html( $option_value ); ?></option>
<?php endforeach; ?>
</select>
</p>
<?php
break;
case 'textarea' :
?>
<p>
<label for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label>
<textarea class="widefat <?php echo esc_attr( $class ); ?>" id="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>" name="<?php echo $this->get_field_name( $key ); ?>" cols="20" rows="3"><?php echo esc_textarea( $value ); ?></textarea>
<?php if ( isset( $setting['desc'] ) ) : ?>
<small><?php echo esc_html( $setting['desc'] ); ?></small>
<?php endif; ?>
</p>
<?php
break;
case 'checkbox' :
?>
<p>
<input class="checkbox <?php echo esc_attr( $class ); ?>" id="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( $key ) ); ?>" type="checkbox" value="1" <?php checked( $value, 1 ); ?> />
<label for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label>
</p>
<?php
break;
// Default: run an action
default :
do_action( 'woocommerce_widget_field_' . $setting['type'], $key, $value, $setting, $instance );
break;
}
}
}
}
function ms_register_widgets() {
register_widget( 'MS_Widget_Product_Categories' );
}
add_action( 'widgets_init', 'ms_register_widgets' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment