Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Plugin Name: MX WooCommerce Product Gallery Images Widget
- * Plugin URI: http://media-x.hr
- * Description: Display product gallery images in a widget (instead of on a page)
- * Author: Media X
- * Author URI: http://media-x.hr
- * Version: 1.0.0
- * Text Domain: mx-wc-product-images-widget
- * License: GNU General Public License v3.0
- * License URI: http://www.gnu.org/licenses/gpl-3.0.html
- *
- */
- if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
- /**
- * A widget for displaying product gallery images in a widget instead of on the product page
- *
- * @since 1.0.0
- * @extends \WP_Widget
- */
- class WC_Product_Images_Widget extends WP_Widget {
- /**
- * Setup the widget options
- *
- * @since 1.0.0
- */
- public function __construct() {
- // remove product gallery images from the page when this widget is used
- add_action( 'wp', array( $this, 'remove_product_images' ) );
- add_action( 'wp_print_footer_scripts', array( $this, 'modify_product_meta_styles' ) );
- // set widget options
- $options = array(
- 'classname' => 'widget_wc_product_images',
- 'description' => __( 'Displays product gallery images on product pages.', 'mx-wc-product-images-widget' ),
- );
- // instantiate the widget
- parent::__construct( 'WC_Product_Images_Widget', __( 'WooCommerce Product Gallery Images', 'mx-wc-product-images-widget' ), $options );
- }
- /**
- * Render the widget
- *
- * @since 1.0
- * @see WP_Widget::widget()
- * @param array $args
- * @param array $instance
- */
- public function widget( $args, $instance ) {
- global $post, $woocommerce, $product;
- if ( has_post_thumbnail() ) {
- $image_caption = get_post( get_post_thumbnail_id() )->post_excerpt;
- $image_link = wp_get_attachment_url( get_post_thumbnail_id() );
- $image = get_the_post_thumbnail( $post->ID, apply_filters( 'single_product_large_thumbnail_size', 'shop_single' ), array(
- 'title' => get_the_title( get_post_thumbnail_id() )
- ) );
- $attachment_count = count( $product->get_gallery_attachment_ids() );
- }
- // Only show this if we're looking at a product page
- if ( ! is_singular( 'product' ) && empty ($gallery) ) {
- return;
- }
- // get the widget configuration
- $title = $instance['title'];
- echo $args['before_widget'];
- if ( $title ) {
- echo $args['before_title'] . wp_kses_post( $title ) . $args['after_title'];
- }
- // Show the product gallery images
- woocommerce_show_product_thumbnails();
- echo $args['after_widget'];
- }
- /**
- * Update the widget title & selected product
- *
- * @since 1.0
- * @see WP_Widget::update()
- * @param array $new_instance new widget settings
- * @param array $old_instance old widget settings
- * @return array updated widget settings
- */
- public function update( $new_instance, $old_instance ) {
- $instance['title'] = strip_tags( $new_instance['title'] );
- return $instance;
- }
- /**
- * Render the admin form for the widget
- *
- * @since 1.0.0
- * @see WP_Widget::form()
- * @param array $instance the widget settings
- * @return string|void
- */
- public function form( $instance ) {
- ?>
- <p>
- <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title', 'mx-wc-product-images-widget' ) ?>:</label>
- <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'] : '' ); ?>" />
- </p>
- <?php
- }
- /**
- * Removes the product gallery images from its current location on the product page
- *
- * @since 1.0.0
- */
- public function remove_product_images() {
- if ( is_singular( 'product' ) && is_active_widget( false, false, $this->id_base ) ) {
- remove_action( 'woocommerce_product_thumbnails', 'woocommerce_show_product_thumbnails', 20 );
- }
- }
- /**
- * Add some specific CSS for the widget
- *
- * @since 1.0.0
- */
- public function modify_product_meta_styles() {
- if ( is_singular( 'product' ) && is_active_widget( false, false, $this->id_base ) ) {
- echo '<style>
- .woocommerce .widget_wc_product_images .thumbnails {
- padding: .53em 0;
- box-sizing: border-box;
- }
- .woocommerce .widget_wc_product_images .thumbnails img {
- width: 47%;
- float: left;
- padding: 3px;
- }
- .woocommerce .widget_wc_product_images .thumbnails:after {
- visibility: hidden;
- display: block;
- content: " ";
- clear: both;
- }
- </style>';
- }
- }
- } // end \WC_Product_Images_Widget class
- /**
- * Registers the new widget to add it to the available widgets
- *
- * @since 1.0.0
- */
- function mx_wc_product_images_register_widget() {
- register_widget( 'WC_Product_Images_Widget' );
- }
- add_action( 'widgets_init', 'mx_wc_product_images_register_widget' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement