WC_PHC

The WooCommerce Product Hover Carousel WC PHC class.

Defined (1)

The class is defined in the following location(s).

/hover-carousel.php  
  1. class WC_PHC { 
  2.  
  3. public static function getInstance() { 
  4. static $_instance; 
  5. if (!$_instance) { 
  6. $_instance = new WC_PHC(); 
  7. return $_instance; 
  8.  
  9. public function __construct() { 
  10. add_action('init', array(&$this, 'init')); 
  11. add_action('plugins_loaded', array(&$this, 'load_localisation')); 
  12. //Register image sizes 
  13. add_action('after_setup_theme', array($this, 'phc_register_image_sizes')); 
  14.  
  15. /** 
  16. * Initialize the plugin. 
  17. */ 
  18. function init() { 
  19. // Settings 
  20. add_filter('woocommerce_get_sections_products', __CLASS__ . '::add_products_section'); 
  21. add_filter('woocommerce_get_settings_products', __CLASS__ . '::all_settings', 10, 2); 
  22. // Enqueue the styles 
  23. add_action('wp_enqueue_scripts', array($this, 'phc_scripts')); 
  24. add_action('woocommerce_after_shop_loop_item', array($this, 'woocommerce_template_loop_previewslider'), 1); 
  25. // Add custom css within head section using wp_head action.  
  26. add_action('wp_head', array($this, 'phc_hook_css')); 
  27.  
  28. /** ----------------------------------------------------------------------------------- */ 
  29. /** Class Functions */ 
  30. /** ----------------------------------------------------------------------------------- */ 
  31.  
  32. /** 
  33. * Create the section beneath the products tab 
  34. * */ 
  35. public static function add_products_section($sections) { 
  36. $sections['phc'] = __('Hover Carousel', 'phc'); 
  37. return $sections; 
  38.  
  39. /** 
  40. * Add settings to the section 
  41. */ 
  42. public static function all_settings($settings, $current_section) { 
  43.  
  44. /** 
  45. * Check the current section is what we want 
  46. * */ 
  47. if ($current_section == 'phc') { 
  48.  
  49. $settings_phc = array(); 
  50.  
  51. // Add Title to the Settings 
  52. $settings_phc[] = array( 
  53. 'name' => __('Hover Carousel', 'phc'),  
  54. 'type' => 'title',  
  55. 'desc' => __('The following options are used to configure Hover Carousel', 'phc'),  
  56. 'id' => 'phc' 
  57. ); 
  58.  
  59. $settings_phc[] = array( 
  60. 'name' => __('Carousel Width', 'phc'),  
  61. 'desc_tip' => __('The carousel width in pixels.', 'phc'),  
  62. 'id' => 'phc_carousel_width',  
  63. 'type' => 'number',  
  64. 'default' => 92 
  65. ); 
  66.  
  67. $settings_phc[] = array( 
  68. 'name' => __('Carousel Height', 'phc'),  
  69. 'desc_tip' => __('The carousel height in pixels.', 'phc'),  
  70. 'id' => 'phc_carousel_height',  
  71. 'type' => 'number',  
  72. 'default' => 256 
  73. ); 
  74.  
  75. $settings_phc[] = array( 
  76. 'name' => __('Carousel Items Width', 'phc'),  
  77. 'desc_tip' => __('The carousel thumbnails width in pixels.', 'phc'),  
  78. 'id' => 'phc_thumb_width',  
  79. 'type' => 'number',  
  80. 'default' => 80 
  81. ); 
  82.  
  83. $settings_phc[] = array( 
  84. 'name' => __('Carousel Items Height', 'phc'),  
  85. 'desc_tip' => __('The carousel thumbnails height in pixels.', 'phc'),  
  86. 'id' => 'phc_thumb_height',  
  87. 'type' => 'number',  
  88. 'default' => 80 
  89. ); 
  90.  
  91. $settings_phc[] = array( 
  92. 'name' => __('Crop Mode', 'text-domain'),  
  93. 'desc_tip' => __('Soft Mode resizing the image proportionally and Hard Mode cropping the image.<br>if you want to rebuild thumbnails please install AJAX Thumbnail Rebuild.', 'phc'),  
  94. 'id' => 'phc_crop_mode',  
  95. 'type' => 'select',  
  96. 'default' => true,  
  97. 'options' => array( 
  98. false => 'Soft',  
  99. true => 'Hard' 
  100. ); 
  101. $settings_phc[] = array( 
  102. 'title' => __('Custom CSS', 'phc'),  
  103. 'type' => 'textarea',  
  104. 'id' => 'phc_custom_css',  
  105. 'css' => 'width:50%; height: 75px;',  
  106. 'desc_tip' => __('Apply your own custom CSS. CSS is automatically wrapped with style tags', 'phc') 
  107. ); 
  108. $settings_phc[] = array( 
  109. 'type' => 'sectionend',  
  110. 'id' => 'phc_end' 
  111. ); 
  112. $settings_phc[] = array('type' => 'sectionend', 'id' => 'phc'); 
  113.  
  114. return $settings_phc; 
  115. /** 
  116. * If not, return the standard settings 
  117. * */ 
  118. } else { 
  119. return $settings; 
  120.  
  121. // Register_image_sizes() 
  122. public static function phc_register_image_sizes() { 
  123. $thumb_width = get_option('phc_thumb_width'); 
  124. $thumb_height = get_option('phc_thumb_height'); 
  125. $crop_mode = get_option('phc_crop_mode'); 
  126.  
  127. add_image_size('phc-thumbnail', $thumb_width, $thumb_height, $crop_mode); 
  128.  
  129. /** 
  130. * load_localisation function. 
  131. * @access public 
  132. * @since 0.2.0 
  133. * @return void 
  134. */ 
  135. public function load_localisation() { 
  136. load_plugin_textdomain('phc', false, dirname(plugin_basename(__FILE__)) . '/languages'); 
  137.  
  138. // Setup styles 
  139. public function phc_scripts() { 
  140. if (is_shop() || is_product_category() || is_product_tag() || is_product_taxonomy() || is_product()) { 
  141. wp_enqueue_style('phc-styles', plugins_url('/assets/css/style.css', __FILE__)); 
  142. wp_enqueue_script('phc-script', plugins_url('/assets/js/thumbelina.js', __FILE__)); 
  143. wp_enqueue_script('phc-init', plugins_url('/assets/js/init.js', __FILE__), array('jquery')); 
  144.  
  145. // Add custom css within head section using wp_head action.  
  146. public function phc_hook_css() { 
  147. ?> 
  148. <style> 
  149. .previewslider { 
  150. width:<?php echo get_option('phc_carousel_width'); ?>px!important; 
  151. height:<?php echo get_option('phc_carousel_height'); ?>px!important; 
  152. }  
  153. <?php echo get_option('phc_custom_css'); ?> 
  154. </style> 
  155.  
  156. <?php 
  157.  
  158. /** 
  159. * Display the carousel on shop and archive pages 
  160. *  
  161. * @access public 
  162. * @return void 
  163. * @since 0.1.0 
  164. */ 
  165. public function woocommerce_template_loop_previewslider() { 
  166.  
  167. global $post, $product, $woocommerce; 
  168.  
  169. $attachment_ids = $product->get_gallery_attachment_ids(); 
  170. if (!empty($attachment_ids)) 
  171. array_unshift($attachment_ids, get_post_thumbnail_id()); 
  172. $md5 = substr(md5(rand()), 0, 7); 
  173.  
  174. if ($attachment_ids) { 
  175. ?> 
  176. <div class="previewslider" id="previewslider-<?php echo $md5; ?>"> 
  177. <div class="thumbelina-but vert top">˄</div> 
  178. <ul> 
  179. <?php 
  180. $loop = 0; 
  181. $columns = apply_filters('woocommerce_product_thumbnails_columns', 3); 
  182.  
  183. foreach ($attachment_ids as $attachment_id) { 
  184.  
  185. $classes = array('zoom'); 
  186.  
  187. if ($loop == 0 || $loop % $columns == 0) 
  188. $classes[] = 'first'; 
  189.  
  190. if (( $loop + 1 ) % $columns == 0) 
  191. $classes[] = 'last'; 
  192.  
  193. list($image_link) = wp_get_attachment_image_src($attachment_id, 'shop_catalog'); 
  194.  
  195. if (!$image_link) 
  196. continue; 
  197.  
  198. $image = wp_get_attachment_image($attachment_id, 'phc-thumbnail'); 
  199. $image_class = esc_attr(implode(' ', $classes)); 
  200. $image_title = esc_attr(get_the_title($attachment_id)); 
  201.  
  202. echo apply_filters('woocommerce_single_product_image_thumbnail_html', sprintf('<li><a data-href="%s" class="%s" title="%s">%s</a></li>', $image_link, $image_class, $image_title, $image), $attachment_id, $post->ID, $image_class); 
  203.  
  204. $loop++; 
  205. ?> 
  206. </ul> 
  207. <div class="thumbelina-but vert bottom">˅</div> 
  208.  
  209. </div> 
  210. <script> 
  211. jQuery('#previewslider-<?php echo $md5; ?>').Thumbelina({ 
  212. orientation: 'vertical', // Use vertical mode (default horizontal). 
  213. $bwdBut: jQuery('#previewslider-<?php echo $md5; ?> .top'), // Selector to top button. 
  214. $fwdBut: jQuery('#previewslider-<?php echo $md5; ?> .bottom') // Selector to bottom button. 
  215. }); 
  216. </script> 
  217.  
  218. <?php 
  219.