/wpsc-widgets/category_widget.php

https://github.com/Matmon/WP-e-Commerce · PHP · 180 lines · 113 code · 35 blank · 32 comment · 15 complexity · bf7162fb1f09c833973cfcd940dc902e MD5 · raw file

  1. <?php
  2. /**
  3. * Product Categories widget class
  4. *
  5. * Takes the settings, works out if there is anything to display, if so, displays it.
  6. *
  7. * @since 3.7.1
  8. */
  9. class WP_Widget_Product_Categories extends WP_Widget {
  10. /**
  11. * Widget Constuctor
  12. */
  13. function WP_Widget_Product_Categories() {
  14. $widget_ops = array(
  15. 'classname' => 'widget_wpsc_categorisation',
  16. 'description' => __( 'Product Grouping Widget', 'wpsc' )
  17. );
  18. $this->WP_Widget( 'wpsc_categorisation', __( 'Product Categories', 'wpsc' ), $widget_ops );
  19. }
  20. /**
  21. * Widget Output
  22. *
  23. * @param $args (array)
  24. * @param $instance (array) Widget values.
  25. */
  26. function widget( $args, $instance ) {
  27. global $wpdb;
  28. extract( $args );
  29. $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Product Categories', 'wpsc' ) : $instance['title'] );
  30. echo $before_widget;
  31. if ( $title )
  32. echo $before_title . $title . $after_title;
  33. $show_thumbnails = $instance['image'];
  34. if ( isset($instance['grid'] ) )
  35. $grid = (bool)$instance['grid'];
  36. if ( isset($instance['width'] ) )
  37. $width = $instance['width'];
  38. if ( isset( $instance['height'] ) )
  39. $height = $instance['height'];
  40. if ( !isset( $instance['categories'] ) ){
  41. $instance_categories = get_terms( 'wpsc_product_category', 'hide_empty=0&parent=0');
  42. if(!empty($instance_categories)){
  43. foreach($instance_categories as $categories){
  44. $instance['categories'][$categories->term_id] = 'on';
  45. }
  46. }
  47. }
  48. foreach ( array_keys( (array)$instance['categories'] ) as $category_id ) {
  49. if (!get_term($category_id, "wpsc_product_category"))
  50. continue;
  51. if ( file_exists( wpsc_get_template_file_path( 'wpsc-category_widget.php' ) ) ) {
  52. include( wpsc_get_template_file_path( 'wpsc-category_widget.php' ) );
  53. } else {
  54. include( wpsc_get_template_file_path( 'category_widget.php' ) );
  55. }
  56. }
  57. if ( isset( $grid ) && $grid )
  58. echo "<div class='clear_category_group'></div>";
  59. echo $after_widget;
  60. }
  61. /**
  62. * Update Widget
  63. *
  64. * @param $new_instance (array) New widget values.
  65. * @param $old_instance (array) Old widget values.
  66. *
  67. * @return (array) New values.
  68. */
  69. function update( $new_instance, $old_instance ) {
  70. $instance = $old_instance;
  71. $instance['title'] = strip_tags( $new_instance['title'] );
  72. $instance['image'] = $new_instance['image'] ? 1 : 0;
  73. $instance['categories'] = $new_instance['categories'];
  74. $instance['grid'] = $new_instance['grid'] ? 1 : 0;
  75. $instance['height'] = (int)$new_instance['height'];
  76. $instance['width'] = (int)$new_instance['width'];
  77. $instance['show_name'] = (bool)$new_instance['show_name'];
  78. return $instance;
  79. }
  80. /**
  81. * Widget Options Form
  82. *
  83. * @param $instance (array) Widget values.
  84. */
  85. function form( $instance ) {
  86. global $wpdb;
  87. // Defaults
  88. $instance = wp_parse_args((array) $instance, array(
  89. 'title' => '',
  90. 'width' => 45,
  91. 'height' => 45,
  92. 'image' => false,
  93. 'grid' => false,
  94. 'show_name' => false,
  95. ));
  96. // Values
  97. $title = esc_attr( $instance['title'] );
  98. $image = (bool) $instance['image'];
  99. $width = (int) $instance['width'];
  100. $height = (int) $instance['height'];
  101. $grid = (bool) $instance['grid'];
  102. $show_name= (bool) $instance['show_name'];
  103. ?>
  104. <p>
  105. <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e( 'Title:', 'wpsc' ); ?></label>
  106. <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $title; ?>" />
  107. </p>
  108. <p>
  109. <?php _e('Show Categories','wpsc'); ?>:<br />
  110. <?php wpsc_list_categories('wpsc_category_widget_admin_category_list', array("id"=>$this->get_field_id('categories'),"name"=>$this->get_field_name('categories'),"instance"=>$instance), 0); ?>
  111. <?php _e('(leave all unchecked if you want to display all)','wpsc'); ?>
  112. </p>
  113. <p>
  114. <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('grid'); ?>" name="<?php echo $this->get_field_name('grid'); ?>"<?php checked( $grid ); ?> />
  115. <label for="<?php echo $this->get_field_id('grid'); ?>"><?php _e('Use Category Grid View', 'wpsc'); ?></label><br />
  116. <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('image'); ?>" name="<?php echo $this->get_field_name('image'); ?>"<?php checked( $image ); ?> onclick="jQuery('.wpsc_category_image').toggle()" />
  117. <label for="<?php echo $this->get_field_id('image'); ?>"><?php _e('Show Thumbnails', 'wpsc'); ?></label>
  118. </p>
  119. <div class="wpsc_category_image"<?php if( !checked( $image ) ) { echo ' style="display:none;"'; } ?>>
  120. <p>
  121. <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('show_name'); ?>" name="<?php echo $this->get_field_name('show_name'); ?>"<?php checked( $show_name ); ?> /><label for="<?php echo $this->get_field_id('show_name'); ?>"><?php _e(' Show N/A when No Image Available', 'wpsc'); ?></label>
  122. </p>
  123. <p>
  124. <label for="<?php echo $this->get_field_id('width'); ?>"><?php _e('Width:', 'wpsc'); ?></label>
  125. <input type="text" id="<?php echo $this->get_field_id('width'); ?>" name="<?php echo $this->get_field_name('width'); ?>" value="<?php echo $width ; ?>" size="3" />
  126. <label for="<?php echo $this->get_field_id('height'); ?>"><?php _e('Height:', 'wpsc'); ?></label>
  127. <input type="text" id="<?php echo $this->get_field_id('height'); ?>" name="<?php echo $this->get_field_name('height'); ?>" value="<?php echo $height ; ?>" size="3" />
  128. </p>
  129. </div>
  130. <?php
  131. }
  132. }
  133. add_action( 'widgets_init', create_function( '', 'return register_widget("WP_Widget_Product_Categories");' ) );
  134. function wpsc_category_widget_admin_category_list( $category, $level, $fieldconfig ) {
  135. // Only let the user choose top-level categories
  136. if ( $level )
  137. return;
  138. if ( !empty( $fieldconfig['instance']['categories'] ) && array_key_exists( $category->term_id, $fieldconfig['instance']['categories'] ) )
  139. $checked = 'checked';
  140. else
  141. $checked = ''; ?>
  142. <input type="checkbox" class="checkbox" id="<?php echo $fieldconfig['id']; ?>-<?php echo $category->term_id; ?>" name="<?php echo $fieldconfig['name']; ?>[<?php echo $category->term_id; ?>]" <?php echo $checked; ?>></input> <label for="<?php echo $fieldconfig['id']; ?>-<?php echo $category->term_id; ?>"><?php echo htmlentities($category->name, ENT_QUOTES, 'UTF-8' ); ?></label><br />
  143. <?php
  144. }
  145. ?>