PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/wordpress/wp-content/plugins/thecartpress/widgets/TaxonomyTreesPostTypeWidget.class.php

http://ownerpress.googlecode.com/
PHP | 192 lines | 150 code | 9 blank | 33 comment | 11 complexity | 171181bbd490628c66e9ab88c4b773ff MD5 | raw file
Possible License(s): Apache-2.0, AGPL-1.0, GPL-2.0, GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * This file is part of TheCartPress.
  4. *
  5. * TheCartPress is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * TheCartPress is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with TheCartPress. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. class TaxonomyTreesPostTypeWidget extends WP_Widget {
  19. function TaxonomyTreesPostTypeWidget() {
  20. $widget = array(
  21. 'classname' => 'taxonomytreesposttype',
  22. 'description' => __('Use this widget to add trees of different taxonomis', 'tcp'),
  23. );
  24. $control = array(
  25. 'width' => 400,
  26. 'id_base' => 'taxonomytreesposttype-widget',
  27. );
  28. $this->WP_Widget('taxonomytreesposttype-widget', 'TCP Taxonomy trees', $widget, $control);
  29. //add_filter( 'get_terms', array( $this, 'get_terms' ), 9, 3 );
  30. }
  31. //function get_terms( $terms, $taxonomies, $args ) {
  32. // if ( in_array( 'tcp_product_category', $taxonomies ) )
  33. // var_dump( $terms );
  34. // return $terms;
  35. //}
  36. function widget( $args, $instance ) {
  37. extract( $args );
  38. $title = apply_filters( 'widget_title', $instance['title'] );
  39. echo $before_widget;
  40. if ( $title ) echo $before_title, $title, $after_title;
  41. $args = array(
  42. //'show_option_all' => ,
  43. //'orderby' => 'name',
  44. //'order' => 'ASC',
  45. 'show_last_update' => 0,
  46. 'style' => 'list',
  47. 'show_count' => isset( $instance['see_number_products'] ) ? $instance['see_number_products'] : false,
  48. 'hide_empty' => isset( $instance['hide_empty_taxonomies'] ) ? $instance['hide_empty_taxonomies'] : false,
  49. 'use_desc_for_title' => 1,
  50. 'child_of' => 0,
  51. //'feed' => ,
  52. //'feed_type' => ,
  53. //'feed_image' => ,
  54. //'exclude' => ,
  55. //'exclude_tree' => ,
  56. //'include' => ,
  57. 'current_category' => 0,
  58. 'hierarchical' => true,
  59. 'title_li' => '', //$options['txt_title_li'],
  60. 'number' => NULL,
  61. 'echo' => 0,
  62. 'depth' => 0,
  63. 'taxonomy' => isset( $instance['taxonomy'] ) ? $instance['taxonomy'] : 'tcp_product_category',
  64. );
  65. $excluded_taxonomies = isset( $instance['excluded_taxonomies'] ) ? $instance['excluded_taxonomies'] : false;
  66. if ( is_array( $excluded_taxonomies ) ) $args['exclude'] = implode( ",", $excluded_taxonomies );
  67. $included_taxonomies = isset( $instance['included_taxonomies'] ) ? $instance['included_taxonomies'] : false;
  68. if ( is_array( $included_taxonomies ) ) $args['include'] = implode( ",", $included_taxonomies );
  69. $order_included = isset( $instance['order_included'] ) ? $instance['order_included'] : false;
  70. if ( $order_included ) {
  71. $this->orderIncluded = explode( '#', $order_included );
  72. add_filter( 'get_terms', array( $this, 'orderTaxonomies' ) );
  73. }
  74. echo '<ul>' . wp_list_categories( $args ) . '</ul>';
  75. if ( strlen( $order_included ) > 0 )
  76. remove_filter( 'get_terms', array( $this, 'orderTaxonomies' ) );
  77. echo $after_widget;
  78. }
  79. //for order taxonomies list
  80. function orderTaxonomies( $terms ) {
  81. usort( $terms, array( $this, 'compare' ) );
  82. return $terms;
  83. }
  84. //for order taxonomies list
  85. function compare( $a, $b ) {
  86. if ( $a == $b ) return 0;
  87. foreach( $this->orderIncluded as $id )
  88. if ( $id == $a->term_id ) return -1;
  89. elseif ( $id == $b->term_id ) return 1;
  90. return 0;
  91. }
  92. function update( $new_instance, $old_instance ) {
  93. $instance = $old_instance;
  94. $instance['title'] = strip_tags( $new_instance['title'] );
  95. $instance['post_type'] = $new_instance['post_type'];
  96. $instance['taxonomy'] = $new_instance['taxonomy'];
  97. $instance['see_number_products'] = $new_instance['see_number_products'];
  98. $instance['hide_empty_taxonomies'] = isset( $new_instance['hide_empty_taxonomies'] ) ? true : false;
  99. $instance['included_taxonomies'] = isset( $new_instance['included_taxonomies'] ) ? true : false;
  100. $instance['order_included'] = $new_instance['order_included'];
  101. $instance['excluded_taxonomies'] = $new_instance['excluded_taxonomies'];
  102. return $instance;
  103. }
  104. function form( $instance ) {
  105. $defaults = array(
  106. 'title' => 'Taxonomy trees post type',
  107. 'post_type' => 'tcp_product',
  108. 'taxonomy' => 'tcp_product_category',
  109. 'see_number_products' => true,
  110. 'hide_empty_taxonomies' => false,
  111. 'order_included' => '',
  112. );
  113. $instance = wp_parse_args( (array) $instance, $defaults );
  114. $see_number_products = isset( $instance['see_number_products'] ) ? (bool)$instance['see_number_products'] : false;
  115. $hide_empty_taxonomies = isset( $instance['hide_empty_taxonomies'] ) ? (bool)$instance['hide_empty_taxonomies'] : false;
  116. $included_taxonomies = isset( $instance['included_taxonomies'] ) ? (array)$instance['included_taxonomies'] : array();
  117. $excluded_taxonomies = isset( $instance['excluded_taxonomies'] ) ? (array)$instance['excluded_taxonomies'] : array();
  118. ?>
  119. <p>
  120. <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title', 'tcp' )?>:</label>
  121. <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $instance['title'] ); ?>" />
  122. </p><p>
  123. <label for="<?php echo $this->get_field_id( 'post_type' ); ?>"><?php _e( 'Post type', 'tcp' )?>:</label>
  124. <select name="<?php echo $this->get_field_name( 'post_type' ); ?>" id="<?php echo $this->get_field_id( 'post_type' ); ?>" class="widefat">
  125. <?php foreach( get_post_types() as $post_type ): ?>
  126. <option value="<?php echo $post_type;?>"<?php selected( $instance['post_type'], $post_type ); ?>><?php echo $post_type;?></option>
  127. <?php endforeach; ?>
  128. </select>
  129. <span class="description"><?php _e( 'press save to load the taxonomies list.', 'tcp' );?></span>
  130. </p><p>
  131. <label for="<?php echo $this->get_field_id( 'taxonomy' ); ?>"><?php _e( 'Taxonomy', 'tcp' )?>:</label>
  132. <select name="<?php echo $this->get_field_name( 'taxonomy' ); ?>" id="<?php echo $this->get_field_id( 'taxonomy' ); ?>" class="widefat">
  133. <?php foreach(get_object_taxonomies( $instance['post_type'] ) as $taxonomy): $tax = get_taxonomy( $taxonomy ); ?>
  134. <option value="<?php echo esc_attr( $taxonomy );?>"<?php selected( $instance['taxonomy'], $taxonomy ); ?>><?php echo $tax->labels->name;?></option>
  135. <?php endforeach; ?>
  136. </select>
  137. <span class="description"><?php _e( 'Press save to load the next lists', 'tcp' );?></span>
  138. </p><p>
  139. <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'see_number_products' ); ?>" name="<?php echo $this->get_field_name( 'see_number_products' ); ?>" <?php checked( $see_number_products, true ); ?> />
  140. <label for="<?php echo $this->get_field_id( 'see_number_products' ); ?>"><?php _e( 'See children number', 'tcp' ); ?></label>
  141. <br />
  142. <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'hide_empty_taxonomies' ); ?>" name="<?php echo $this->get_field_name( 'hide_empty_taxonomies' ); ?>" <?php checked( $hide_empty_taxonomies, true ); ?> />
  143. <label for="<?php echo $this->get_field_id( 'hide_empty_taxonomies' ); ?>"><?php _e( 'Hide empty terms', 'tcp' ); ?></label>
  144. </p><p>
  145. <label for="<?php echo $this->get_field_id( 'included_taxonomies' ); ?>"><?php _e( 'Included and sorted', 'tcp' )?>:</label>
  146. <select name="<?php echo $this->get_field_name( 'included_taxonomies' ); ?>[]" id="<?= $this->get_field_id( 'included_taxonomies' ); ?>" class="widefat" multiple="true" size="8" style="height: auto">
  147. <option value="0"<?php $this->selected_multiple( $included_taxonomies, 0 ); ?>><?php _e( 'All', 'tcp' );?></option>
  148. <?php $args = array (
  149. 'taxonomy' => $instance['taxonomy'],
  150. 'hide_empty' => false,
  151. );
  152. $categories = get_categories( $args );
  153. $this->orderIncluded = explode( '#', $instance['order_included'] );
  154. usort( $categories, array( $this, 'compare' ) );
  155. foreach( $categories as $cat ): ?>
  156. <option value="<?php echo esc_attr( $cat->term_id );?>"<?php $this->selected_multiple( $included_taxonomies, $cat->term_id ); ?>><?php echo $cat->cat_name;?></option>
  157. <?php endforeach; ?>
  158. </select>
  159. <input type="button" onclick="tcp_select_up('<?php echo $this->get_field_id( 'included_taxonomies' ); ?>', '<?php echo $this->get_field_id( 'order_included' ); ?>');" id="tcp_up" value="<?php _e( 'up', 'tcp' );?>" class="button-secondary"/>
  160. <input type="button" onclick="tcp_select_down('<?php echo $this->get_field_id( 'included_taxonomies' ); ?>', '<?php echo $this->get_field_id( 'order_included' ); ?>');" id="tcp_down" value="<?php _e( 'down', 'tcp' );?>" class="button-secondary"/>
  161. <span class="description"><?php _e( 'Use those actions to sort the list.', 'tcp' );?></span>
  162. <input type="hidden" id="<?php echo $this->get_field_id( 'order_included' ); ?>" name="<?php echo $this->get_field_name( 'order_included' ); ?>" value="<?php echo $instance['order_included'];?>"/>
  163. </p><p>
  164. <label for="<?php echo $this->get_field_id( 'excluded_taxonomies' ); ?>"><?php _e( 'Excluded', 'tcp' )?>:</label>
  165. <select name="<?php echo $this->get_field_name( 'excluded_taxonomies' ); ?>[]" id="<?php echo $this->get_field_id( 'excluded_taxonomies' ); ?>" class="widefat" multiple="true" size="6" style="height: auto">
  166. <option value="0"<?php $this->selected_multiple( $excluded_taxonomies, 0 ); ?>><?php _e('No one', 'tcp');?></option>
  167. <?php $args = array (
  168. 'taxonomy' => $instance['taxonomy'],
  169. 'hide_empty' => false,
  170. );
  171. foreach( get_categories( $args ) as $cat ): ?>
  172. <option value="<?php echo esc_attr( $cat->term_id);?>"<?php $this->selected_multiple($excluded_taxonomies, $cat->term_id ); ?>><?php echo $cat->cat_name;?></option>
  173. <?php endforeach; ?>
  174. </select>
  175. </p>
  176. <?php
  177. }
  178. function selected_multiple( $values, $value ) {
  179. if ( in_array( $value, $values ) )
  180. echo ' selected';
  181. }
  182. }
  183. ?>