/includes/functions.php

  1. <?php 
  2.  
  3. if( ! function_exists( 'br_get_woocommerce_version' ) ) { 
  4. /** 
  5. * Public function to get WooCommerce version 
  6. * 
  7. * @return float|NULL 
  8. */ 
  9. function br_get_woocommerce_version() { 
  10. if ( ! function_exists( 'get_plugins' ) ) 
  11. require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); 
  12.  
  13. $plugin_folder = get_plugins( '/' . 'woocommerce' ); 
  14. $plugin_file = 'woocommerce.php'; 
  15.  
  16. if ( isset( $plugin_folder[$plugin_file]['Version'] ) ) { 
  17. return $plugin_folder[$plugin_file]['Version']; 
  18. } else { 
  19. return NULL; 
  20.  
  21. if( ! function_exists( 'br_get_template_part' ) ) { 
  22. /** 
  23. * Public function to get plugin's template 
  24. * 
  25. * @param string $name Template name to search for 
  26. * 
  27. * @return void 
  28. */ 
  29. function br_get_template_part( $name = '' ) { 
  30. BeRocket_AAPF::br_get_template_part( $name ); 
  31.  
  32. if( ! function_exists( 'br_aapf_get_attributes' ) ) { 
  33. /** 
  34. * Get all possible woocommerce attribute taxonomies 
  35. * 
  36. * @return mixed|void 
  37. */ 
  38. function br_aapf_get_attributes() { 
  39. $attribute_taxonomies = wc_get_attribute_taxonomies(); 
  40. $attributes = array(); 
  41.  
  42. if ( $attribute_taxonomies ) { 
  43. foreach ( $attribute_taxonomies as $tax ) { 
  44. $attributes[ wc_attribute_taxonomy_name( $tax->attribute_name ) ] = $tax->attribute_label; 
  45.  
  46. return apply_filters( 'berocket_aapf_get_attributes', $attributes ); 
  47.  
  48. if( ! function_exists( 'br_parse_order_by' ) ) { 
  49. /** 
  50. * br_aapf_parse_order_by - parsing order by data and saving to $args array that was passed into 
  51. * 
  52. * @param $args 
  53. */ 
  54. function br_aapf_parse_order_by( &$args ) { 
  55. $orderby = $_GET['orderby'] = $_POST['orderby']; 
  56. $order = "ASK"; 
  57. if( @ preg_match( "/-/", $orderby ) ) { 
  58. list( $orderby, $order ) = explode( "-", $orderby ); 
  59.  
  60. // needed for woocommerce sorting funtionality 
  61. if( @ $orderby and @ $order ) { 
  62.  
  63. // Get ordering from query string unless defined 
  64. $orderby = strtolower( $orderby ); 
  65. $order = strtoupper( $order ); 
  66.  
  67. // default - menu_order 
  68. $args['orderby'] = 'menu_order title'; 
  69. $args['order'] = $order == 'DESC' ? 'DESC' : 'ASC'; 
  70.  
  71. switch ( $orderby ) { 
  72. case 'rand' : 
  73. $args['orderby'] = 'rand'; 
  74. break; 
  75. case 'date' : 
  76. $args['orderby'] = 'date'; 
  77. $args['order'] = $order == 'ASC' ? 'ASC' : 'DESC'; 
  78. break; 
  79. case 'price' : 
  80. $args['orderby'] = 'meta_value_num'; 
  81. $args['order'] = $order == 'DESC' ? 'DESC' : 'ASC'; 
  82. $args['meta_key'] = '_price'; 
  83. break; 
  84. case 'popularity' : 
  85. $args['meta_key'] = 'total_sales'; 
  86.  
  87. // Sorting handled later though a hook 
  88. add_filter( 'posts_clauses', array( 'BeRocket_AAPF', 'order_by_popularity_post_clauses' ) ); 
  89. break; 
  90. case 'rating' : 
  91. // Sorting handled later though a hook 
  92. add_filter( 'posts_clauses', array( 'BeRocket_AAPF', 'order_by_rating_post_clauses' ) ); 
  93. break; 
  94. case 'title' : 
  95. $args['orderby'] = 'title'; 
  96. $args['order'] = $order == 'DESC' ? 'DESC' : 'ASC'; 
  97. break; 
  98.  
  99. if( ! function_exists( 'br_aapf_args_parser' ) ) { 
  100. /** 
  101. * br_aapf_args_parser - extend $args based on passed filters 
  102. * 
  103. * @param array $args 
  104. * 
  105. * @return array 
  106. */ 
  107. function br_aapf_args_parser( $args = array() ) { 
  108. $attributes_terms = $tax_query = array(); 
  109. $attributes = apply_filters( 'berocket_aapf_listener_get_attributes', br_aapf_get_attributes() ); 
  110. $taxonomies = array(); 
  111.  
  112. if ( @$attributes ) { 
  113. foreach ( $attributes as $k => $v ) { 
  114. $terms = get_terms( array( $k ), $args = array( 'orderby' => 'name', 'order' => 'ASC', 'fields' => 'id=>slug' ) ); 
  115. if ( $terms ) { 
  116. foreach ( $terms as $term_id => $term_slug ) { 
  117. $attributes_terms[ $k ][ $term_id ] = $term_slug; 
  118. unset( $terms ); 
  119. unset( $attributes ); 
  120.  
  121. if ( @$_POST['terms'] ) { 
  122. foreach ( $_POST['terms'] as $t ) { 
  123. if( !isset($taxonomies[ $t[0] ]) ) { 
  124. $taxonomies[ $t[0] ] = array(); 
  125. $taxonomies[ $t[0] ][] = @ $attributes_terms[ $t[0] ][ $t[1] ]; 
  126. $taxonomies_operator[ $t[0] ] = $t[2]; 
  127. unset( $attributes_terms ); 
  128.  
  129. $taxonomies = apply_filters( 'berocket_aapf_listener_taxonomies', @$taxonomies ); 
  130. $taxonomies_operator = apply_filters( 'berocket_aapf_listener_taxonomies_operator', @$taxonomies_operator ); 
  131.  
  132. if ( @$taxonomies ) { 
  133. $tax_query['relation'] = 'AND'; 
  134. if ( $taxonomies ) { 
  135. foreach ( $taxonomies as $k => $v ) { 
  136. if ( $taxonomies_operator[ $k ] == 'AND' ) { 
  137. $op = 'AND'; 
  138. } else { 
  139. $op = 'IN'; 
  140.  
  141. $tax_query[] = array( 
  142. 'taxonomy' => $k,  
  143. 'field' => 'slug',  
  144. 'terms' => $v,  
  145. 'operator' => $op 
  146. ); 
  147.  
  148. if ( @$_POST['product_cat'] and $_POST['product_cat'] != '-1' ) { 
  149. $tax_query[] = array( 
  150. 'taxonomy' => 'product_cat',  
  151. 'field' => 'slug',  
  152. 'terms' => strip_tags( $_POST['product_cat'] ),  
  153. 'operator' => 'IN' 
  154. ); 
  155.  
  156. $args['tax_query'] = $tax_query; 
  157. $args['post_type'] = 'product'; 
  158.  
  159. if ( @ $_POST['orderby'] ) { 
  160. br_aapf_parse_order_by( $args ); 
  161.  
  162. return $args; 
  163.  
  164. if( ! function_exists( 'br_aapf_args_converter' ) ) { 
  165. /** 
  166. * convert args-url to normal filters 
  167. */ 
  168. function br_aapf_args_converter() { 
  169. if ( preg_match( "~\|~", $_GET['filters'] ) ) { 
  170. $filters = explode( "|", $_GET['filters'] ); 
  171. } else { 
  172. $filters[0] = $_GET['filters']; 
  173.  
  174. foreach ( $filters as $filter ) { 
  175.  
  176. if ( preg_match( "~\[~", $filter ) ) { 
  177. list( $attribute, $value ) = explode( "[", trim( preg_replace( "~\]~", "", $filter) ), 2 ); 
  178. if ( preg_match( "~\-~", $value ) ) { 
  179. $value = explode( "-", $value ); 
  180. $operator = 'OR'; 
  181. } elseif ( preg_match( "~\_~", $value ) ) { 
  182. list( $min, $max ) = explode( "_", $value ); 
  183. $operator = ''; 
  184. } else { 
  185. $value = explode( " ", $value ); 
  186. $operator = 'AND'; 
  187. }else{ 
  188. list( $attribute, $value ) = explode( "-", $filter, 2 ); 
  189.  
  190. if ( $attribute == 'price' ) { 
  191. $_POST['price'] = array( $min, $max ); 
  192. } elseif ( $attribute == 'order' ) { 
  193. $_GET['orderby'] = $value; 
  194. } else { 
  195. if ( $operator ) { 
  196. foreach ( $value as $v ) { 
  197. $_POST['terms'][] = array( "pa_" . $attribute, $v, $operator ); 
  198. } else { 
  199. $_POST['limits'][] = array( "pa_" . $attribute, $min, $max ); 
.