Advertisement
borkolivic

gallery

Apr 7th, 2016
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.08 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Plugin Name: MX WooCommerce Product Gallery Images Widget
  4.  * Plugin URI: http://media-x.hr
  5.  * Description: Display product gallery images in a widget (instead of on a page)
  6.  * Author: Media X
  7.  * Author URI: http://media-x.hr
  8.  * Version: 1.0.0
  9.  * Text Domain: mx-wc-product-images-widget
  10.  * License: GNU General Public License v3.0
  11.  * License URI: http://www.gnu.org/licenses/gpl-3.0.html
  12.  *
  13.  */
  14.  
  15.  
  16. if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
  17.  
  18.  
  19. /**
  20.  * A widget for displaying product gallery images in a widget instead of on the product page
  21.  *
  22.  * @since 1.0.0
  23.  * @extends \WP_Widget
  24.  */
  25.  
  26. class WC_Product_Images_Widget extends WP_Widget {
  27.  
  28.     /**
  29.      * Setup the widget options
  30.      *
  31.      * @since 1.0.0
  32.      */
  33.     public function __construct() {
  34.    
  35.         // remove product gallery images from the page when this widget is used
  36.         add_action( 'wp', array( $this, 'remove_product_images' ) );
  37.         add_action( 'wp_print_footer_scripts', array( $this, 'modify_product_meta_styles' ) );
  38.        
  39.         // set widget options
  40.         $options = array(
  41.             'classname'   => 'widget_wc_product_images',
  42.             'description' => __( 'Displays product gallery images on product pages.', 'mx-wc-product-images-widget' ),
  43.         );
  44.        
  45.         // instantiate the widget
  46.         parent::__construct( 'WC_Product_Images_Widget', __( 'WooCommerce Product Gallery Images', 'mx-wc-product-images-widget' ), $options );
  47.     }
  48.    
  49.    
  50.     /**
  51.      * Render the widget
  52.      *
  53.      * @since 1.0
  54.      * @see WP_Widget::widget()
  55.      * @param array $args
  56.      * @param array $instance
  57.      */
  58.     public function widget( $args, $instance ) {
  59.        
  60.         global $post, $woocommerce, $product;
  61.  
  62.         if ( has_post_thumbnail() ) {
  63.              $image_caption = get_post( get_post_thumbnail_id() )->post_excerpt;
  64.              $image_link = wp_get_attachment_url( get_post_thumbnail_id() );
  65.              $image = get_the_post_thumbnail( $post->ID, apply_filters( 'single_product_large_thumbnail_size', 'shop_single' ), array(
  66.                'title'  => get_the_title( get_post_thumbnail_id() )
  67.             ) );
  68.         $attachment_count = count( $product->get_gallery_attachment_ids() );
  69.         }
  70.  
  71.         // Only show this if we're looking at a product page
  72.         if ( ! is_singular( 'product' )  &&  empty ($gallery) ) {
  73.             return;
  74.         }
  75.        
  76.         // get the widget configuration
  77.         $title = $instance['title'];
  78.        
  79.         echo $args['before_widget'];
  80.        
  81.         if ( $title ) {
  82.             echo $args['before_title'] . wp_kses_post( $title ) . $args['after_title'];
  83.         }
  84.        
  85.         // Show the product gallery images
  86.         woocommerce_show_product_thumbnails();
  87.        
  88.         echo $args['after_widget'];
  89.     }
  90.    
  91.  
  92.     /**
  93.      * Update the widget title & selected product
  94.      *
  95.      * @since 1.0
  96.      * @see WP_Widget::update()
  97.      * @param array $new_instance new widget settings
  98.      * @param array $old_instance old widget settings
  99.      * @return array updated widget settings
  100.      */
  101.     public function update( $new_instance, $old_instance ) {
  102.         $instance['title'] = strip_tags( $new_instance['title'] );
  103.         return $instance;
  104.     }
  105.    
  106.  
  107.     /**
  108.      * Render the admin form for the widget
  109.      *
  110.      * @since 1.0.0
  111.      * @see WP_Widget::form()
  112.      * @param array $instance the widget settings
  113.      * @return string|void
  114.      */
  115.     public function form( $instance ) {
  116.         ?>
  117.         <p>
  118.             <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title', 'mx-wc-product-images-widget' ) ?>:</label>
  119.             <input type="text" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" value="<?php echo esc_attr( isset( $instance['title'] ) ? $instance['title'] : '' ); ?>" />
  120.         </p>
  121.         <?php
  122.     }
  123.  
  124.  
  125.     /**
  126.      * Removes the product gallery images from its current location on the product page
  127.      *
  128.      * @since 1.0.0
  129.      */
  130.     public function remove_product_images() {
  131.         if ( is_singular( 'product' ) && is_active_widget( false, false, $this->id_base ) ) {
  132.             remove_action( 'woocommerce_product_thumbnails', 'woocommerce_show_product_thumbnails', 20 );
  133.            
  134.         }
  135.     }
  136.    
  137.    
  138.     /**
  139.      * Add some specific CSS for the widget
  140.      *
  141.      * @since 1.0.0
  142.      */
  143.     public function modify_product_meta_styles() {
  144.         if ( is_singular( 'product' ) && is_active_widget( false, false, $this->id_base ) ) {
  145.             echo '<style>
  146.                 .woocommerce .widget_wc_product_images .thumbnails {
  147.                     padding: .53em 0;
  148.                     box-sizing: border-box;
  149.                 }
  150.                
  151.                 .woocommerce .widget_wc_product_images .thumbnails img {
  152.                   width: 47%;
  153.                   float: left;
  154.                   padding: 3px;
  155.                }
  156.                
  157.                 .woocommerce .widget_wc_product_images .thumbnails:after {
  158.                  visibility: hidden;
  159.                  display: block;
  160.                  content: " ";
  161.                  clear: both;
  162.                }
  163.                
  164.             </style>';
  165.         }
  166.     }
  167.    
  168. } // end \WC_Product_Images_Widget class
  169.  
  170.  
  171. /**
  172.  * Registers the new widget to add it to the available widgets
  173.  *
  174.  * @since 1.0.0
  175.  */
  176. function mx_wc_product_images_register_widget() {
  177.     register_widget( 'WC_Product_Images_Widget' );
  178. }
  179. add_action( 'widgets_init', 'mx_wc_product_images_register_widget' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement