PageRenderTime 61ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/widgets/product-categories.php

https://github.com/ThemesWpFr/jigoshop
PHP | 220 lines | 102 code | 33 blank | 85 comment | 5 complexity | f7673daa68e2478f121f97be4e0919e7 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /**
  3. * Product Categories Widget
  4. *
  5. * DISCLAIMER
  6. *
  7. * Do not edit or add directly to this file if you wish to upgrade Jigoshop to newer
  8. * versions in the future. If you wish to customise Jigoshop core for your needs,
  9. * please use our GitHub repository to publish essential changes for consideration.
  10. *
  11. * @package Jigoshop
  12. * @category Widgets
  13. * @author Jigowatt
  14. * @copyright Copyright Š 2011-2012 Jigowatt Ltd.
  15. * @license http://jigoshop.com/license/commercial-edition
  16. */
  17. class Jigoshop_Widget_Product_Categories extends WP_Widget {
  18. /**
  19. * Constructor
  20. *
  21. * Setup the widget with the available options
  22. * Add actions to clear the cache whenever a post is saved|deleted or a theme is switched
  23. */
  24. public function __construct() {
  25. $options = array(
  26. 'classname' => 'jigoshop_product_categories',
  27. 'description' => __( 'A list or dropdown of product categories', 'jigoshop' ),
  28. );
  29. // Create the widget
  30. parent::__construct( 'jigoshop_product_categories', __( 'Jigoshop: Product Categories', 'jigoshop' ), $options );
  31. // Flush cache after every save
  32. add_action( 'save_post', array( &$this, 'flush_widget_cache' ) );
  33. add_action( 'deleted_post', array( &$this, 'flush_widget_cache' ) );
  34. add_action( 'switch_theme', array( &$this, 'flush_widget_cache' ) );
  35. }
  36. /**
  37. * Widget
  38. *
  39. * Display the widget in the sidebar
  40. * Save output to the cache if empty
  41. *
  42. * @param array sidebar arguments
  43. * @param array instance
  44. */
  45. public function widget( $args, $instance ) {
  46. // Get the widget cache from the transient
  47. // NOTE: disabled for Jigoshop 1.3 and WPML -- see rhr #381 -- (JAP)
  48. // $cache = get_transient( 'jigoshop_widget_cache' );
  49. // If this category widget instance is cached, get from the cache
  50. // if ( isset( $cache[$this->id] ) ) {
  51. // echo $cache[$this->id];
  52. // return false;
  53. // }
  54. // Otherwise Start buffering and output the Widget
  55. ob_start();
  56. extract( $args );
  57. // Set the widget title
  58. $title = apply_filters(
  59. 'widget_title',
  60. ( $instance['title'] ) ? $instance['title'] : __( 'Product Categories', 'jigoshop' ),
  61. $instance,
  62. $this->id_base
  63. );
  64. // Get options
  65. $count = (bool) isset( $instance['count'] ) ? $instance['count'] : false;
  66. $is_hierarchial = (bool) isset( $instance['hierarchical'] ) ? $instance['hierarchical'] : false;
  67. $is_dropdown = (bool) isset( $instance['dropdown'] ) ? $instance['dropdown'] : false;
  68. // Print the widget wrapper & title
  69. echo $before_widget;
  70. echo $before_title . $title . $after_title;
  71. // Define options for the list
  72. $args = array(
  73. 'orderby' => 'name',
  74. 'show_count' => $count,
  75. 'hierarchical' => $is_hierarchial,
  76. 'taxonomy' => 'product_cat',
  77. 'title_li' => null,
  78. );
  79. if ( is_product() ) {
  80. global $post;
  81. $categories = get_the_terms( $post->ID, 'product_cat' );
  82. if ( ! empty( $categories ) ) foreach( $categories as $id => $cat ) {
  83. $args['current_category'] = apply_filters( 'jigoshop_product_cat_widget_terms', $cat->term_id, $categories);
  84. break; // we can only take the first one
  85. }
  86. }
  87. // Output as dropdown or unordered list
  88. if( $is_dropdown ) {
  89. // Set up arguments
  90. $args['name'] = 'dropdown_product_cat';
  91. // Print dropdown
  92. // wp_dropdown_categories($args); Commented out due to wordpress bug 13258 not supporting custom taxonomies
  93. // See: http://core.trac.wordpress.org/ticket/13258
  94. jigoshop_product_dropdown_categories( $args['show_count'], $args['hierarchical'] );
  95. // TODO: Move this javascript to its own file (plugins.js?)
  96. ?>
  97. <script type='text/javascript'>
  98. /* <![CDATA[ */
  99. var dropdown = document.getElementById("dropdown_product_cat");
  100. function onCatChange() {
  101. if ( dropdown.options[dropdown.selectedIndex].value !=='' ) {
  102. location.href = "<?php echo home_url(); ?>/?product_cat="+dropdown.options[dropdown.selectedIndex].value;
  103. }
  104. }
  105. dropdown.onchange = onCatChange;
  106. /* ]]> */
  107. </script>
  108. <?php
  109. } else {
  110. // Print list of categories
  111. echo '<ul>';
  112. wp_list_categories(apply_filters('widget_product_categories_args', $args));
  113. echo '</ul>';
  114. }
  115. // Print closing widget wrapper
  116. echo $after_widget;
  117. // Flush output buffer and save to transient cache
  118. $result = ob_get_flush();
  119. $cache[$this->id] = $result;
  120. set_transient( 'jigoshop_widget_cache', $cache, 3600*3 ); // 3 hours ahead
  121. }
  122. /**
  123. * Update
  124. *
  125. * Handles the processing of information entered in the wordpress admin
  126. * Flushes the cache & removes entry from options array
  127. *
  128. * @param array new instance
  129. * @param array old instance
  130. * @return array instance
  131. */
  132. function update( $new_instance, $old_instance ) {
  133. $instance = $old_instance;
  134. // Save the new values
  135. $instance['title'] = strip_tags( $new_instance['title'] );
  136. $instance['count'] = (bool) isset( $new_instance['count'] ) ? $new_instance['count'] : false;
  137. $instance['hierarchical'] = (bool) isset( $new_instance['hierarchical'] ) ? $new_instance['hierarchical'] : false;
  138. $instance['dropdown'] = (bool) isset( $new_instance['dropdown'] ) ? $new_instance['dropdown'] : false;
  139. // Flush the cache
  140. $this->flush_widget_cache();
  141. return $instance;
  142. }
  143. /**
  144. * Flush Widget Cache
  145. *
  146. * Flushes the cached output
  147. */
  148. public function flush_widget_cache() {
  149. delete_transient( 'jigoshop_widget_cache' );
  150. }
  151. /**
  152. * Form
  153. *
  154. * Displays the form for the wordpress admin
  155. *
  156. * @param array instance
  157. */
  158. function form( $instance ) {
  159. // Get values from instance
  160. $title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : null;
  161. $dropdown = (bool) isset($instance['dropdown']) ? $instance['dropdown'] : false;
  162. $count = (bool) isset($instance['count']) ? $instance['count'] : false;
  163. $hierarchical = (bool) isset($instance['hierarchical']) ? $instance['hierarchical'] : false;
  164. // Widget Title
  165. echo "
  166. <p>
  167. <label for='{$this->get_field_id( 'title' )}'>" . __( 'Title:', 'jigoshop' ) . "</label>
  168. <input class='widefat' id='{$this->get_field_id( 'title' )}' name='{$this->get_field_name( 'title' )}' type='text' value='{$title}' />
  169. </p>";
  170. // As a dropdown?
  171. echo "
  172. <p>
  173. <input type='checkbox' class='checkbox' id='{$this->get_field_id('dropdown')}' name='{$this->get_field_name('dropdown')}'" . ( $dropdown ? 'checked' : null ) . " />
  174. <label for='{$this->get_field_id( 'dropdown' )}'>" . __( 'Show as dropdown', 'jigoshop' ) . "</label>
  175. <br/>";
  176. // Show product count?
  177. echo "
  178. <input type='checkbox' class='checkbox' id='{$this->get_field_id('count')}' name='{$this->get_field_name('count')}'" . ( $count ? 'checked' : null ) . " />
  179. <label for='{$this->get_field_id( 'count' )}'>" . __( 'Show product counts', 'jigoshop' ) . "</label>
  180. <br/>";
  181. // Is hierarchical?
  182. echo "
  183. <input type='checkbox' class='checkbox' id='{$this->get_field_id('hierarchical')}' name='{$this->get_field_name('hierarchical')}'" . ( $hierarchical ? 'checked' : null ) . " />
  184. <label for='{$this->get_field_id( 'hierarchical' )}'>" . __( 'Show hierarchy', 'jigoshop' ) . "</label>
  185. </p>";
  186. }
  187. } // class Jigoshop_Widget_Product_Categories