BeRocket_AAPF

Class BeRocket_AAPF.

Defined (1)

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

/woocommerce-filters.php  
  1. class BeRocket_AAPF { 
  2.  
  3. public static $defaults = array( 
  4. "no_products_message" => "There are no products meeting your criteria",  
  5. "no_products_class" => "",  
  6. "control_sorting" => "0",  
  7. "products_holder_id" => "ul.products",  
  8. "filters_turn_off" => "0",  
  9. "seo_friendly_urls" => "0" 
  10. ); 
  11.  
  12. function __construct() { 
  13. register_activation_hook(__FILE__, array( __CLASS__, 'br_add_defaults' ) ); 
  14. register_uninstall_hook(__FILE__, array( __CLASS__, 'br_delete_plugin_options' ) ); 
  15.  
  16. if ( ( is_plugin_active( 'woocommerce/woocommerce.php' ) || is_plugin_active_for_network( 'woocommerce/woocommerce.php' ) ) && br_get_woocommerce_version() >= 2.1 ) { 
  17. if ( defined('DOING_AJAX') && DOING_AJAX ) { 
  18. add_action( 'setup_theme', array( __CLASS__, 'WPML_fix' ) ); 
  19. add_action( 'admin_menu', array( __CLASS__, 'br_add_options_page' ) ); 
  20. add_action( 'admin_init', array( __CLASS__, 'register_br_options' ) ); 
  21. add_action( 'init', array( __CLASS__, 'init' ) ); 
  22.  
  23. add_shortcode( 'br_filters', array( __CLASS__, 'shortcode' ) ); 
  24.  
  25. if( @ $_GET['filters'] and ! @ defined( 'DOING_AJAX' ) ) { 
  26. add_filter( 'pre_get_posts', array( __CLASS__, 'apply_user_price' ) ); 
  27. add_filter( 'pre_get_posts', array( __CLASS__, 'apply_user_filters' ), 99999 ); 
  28.  
  29. if( @ $_GET['explode'] == 'explode' ) { 
  30. add_action( 'woocommerce_before_template_part', array( 'BeRocket_AAPF_Widget', 'pre_get_posts'), 999999 ); 
  31. add_action( 'wp_footer', array( 'BeRocket_AAPF_Widget', 'end_clean'), 999999 ); 
  32. add_action( 'init', array( 'BeRocket_AAPF_Widget', 'start_clean'), 1 ); 
  33. } else { 
  34. if( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) { 
  35. add_action( 'admin_notices', array( __CLASS__, 'update_woocommerce' ) ); 
  36. } else { 
  37. add_action( 'admin_notices', array( __CLASS__, 'no_woocommerce' ) ); 
  38.  
  39. public static function init() { 
  40.  
  41. wp_register_style( 'berocket_aapf_widget-style', plugins_url( 'css/widget.css', __FILE__ ), array(), BeRocket_AJAX_filters_version ); 
  42. wp_enqueue_style( 'berocket_aapf_widget-style' ); 
  43.  
  44. /** custom scrollbar */ 
  45. wp_enqueue_script( 'berocket_aapf_widget-scroll-script', plugins_url( 'js/scrollbar/Scrollbar.concat.min.js', __FILE__ ), array( 'jquery' ), BeRocket_AJAX_filters_version ); 
  46. wp_register_style( 'berocket_aapf_widget-scroll-style', plugins_url( 'css/scrollbar/Scrollbar.min.css', __FILE__ ), array(), BeRocket_AJAX_filters_version ); 
  47. wp_enqueue_style( 'berocket_aapf_widget-scroll-style' ); 
  48.  
  49. public static function no_woocommerce() { 
  50. ?> 
  51. <div class="error"> 
  52. <p><?php _e( 'Activate WooCommerce plugin before', 'BeRocket_AJAX_domain' ) ?></p> 
  53. </div> 
  54. <?php 
  55.  
  56. public static function update_woocommerce() { 
  57. ?> 
  58. <div class="error"> 
  59. <p><?php _e( 'Update WooCommerce plugin', 'BeRocket_AJAX_domain' ) ?></p> 
  60. </div> 
  61. <?php 
  62.  
  63. public static function br_add_options_page() { 
  64. add_submenu_page( 'woocommerce', __( 'Product Filters Settings', 'BeRocket_AJAX_domain' ), __( 'Product Filters', 'BeRocket_AJAX_domain' ), 'manage_options', 'br-product-filters', array( __CLASS__, 'br_render_form' ) ); 
  65.  
  66. public static function shortcode( $atts = array() ) { 
  67. $a = shortcode_atts( 
  68. array( 
  69. 'widget_type' => 'filter',  
  70. 'attribute' => '',  
  71. 'type' => 'checkbox',  
  72. 'filter_type' => 'attribute',  
  73. 'operator' => 'OR',  
  74. 'title' => '',  
  75. 'product_cat' => null,  
  76. 'cat_propagation' => '',  
  77. 'height' => 'auto',  
  78. 'scroll_theme' => 'dark',  
  79. ), $atts 
  80. ); 
  81.  
  82. if ( isset( $a['product_cat'] ) ) { 
  83. $a['product_cat'] = json_encode( explode( "|", $a['product_cat'] ) ); 
  84.  
  85. if ( ! $a['attribute'] || ! $a['type'] ) return false; 
  86.  
  87. the_widget( 'BeRocket_AAPF_widget', $a); 
  88.  
  89. public static function br_render_form() { 
  90. wp_enqueue_script( 'berocket_aapf_widget-colorpicker', plugins_url( 'js/colpick.js', __FILE__ ), array( 'jquery' ), BeRocket_AJAX_filters_version ); 
  91. wp_enqueue_script( 'berocket_aapf_widget-admin', plugins_url( 'js/admin.js', __FILE__ ), array( 'jquery' ), BeRocket_AJAX_filters_version ); 
  92.  
  93. wp_register_style( 'berocket_aapf_widget-colorpicker-style', plugins_url( 'css/colpick.css', __FILE__ ), array(), BeRocket_AJAX_filters_version ); 
  94. wp_register_style( 'berocket_aapf_widget-admin-style', plugins_url( 'css/admin.css', __FILE__ ), array(), BeRocket_AJAX_filters_version ); 
  95. wp_enqueue_style( 'berocket_aapf_widget-colorpicker-style' ); 
  96. wp_enqueue_style( 'berocket_aapf_widget-admin-style' ); 
  97.  
  98. include AAPF_TEMPLATE_PATH . "admin-settings.php"; 
  99.  
  100. public static function apply_user_price( $query, $is_shortcode = FALSE ) { 
  101. $option_permalink = get_option( 'berocket_permalink_option' ); 
  102. if ( ( ( ! is_admin() && $query->is_main_query() ) || $is_shortcode ) && ( @ $_GET['filters'] || $query->get( $option_permalink['variable'], '' ) ) ) { 
  103. br_aapf_args_converter( $query ); 
  104.  
  105. if ( @ $_POST['price'] ) { 
  106. list( $_GET['min_price'], $_GET['max_price'] ) = $_POST['price']; 
  107. add_filter( 'loop_shop_post_in', array( __CLASS__, 'price_filter' ) ); 
  108.  
  109. public static function apply_user_filters( $query ) { 
  110. if( $query->is_main_query() and 
  111. ( is_shop() or is_product_category() or is_product_taxonomy() or is_product_tag() ) 
  112. ) { 
  113. br_aapf_args_converter(); 
  114. $args = br_aapf_args_parser(); 
  115.  
  116. if ( @ $_POST['price'] ) { 
  117. list( $_GET['min_price'], $_GET['max_price'] ) = $_POST['price']; 
  118. add_filter( 'loop_shop_post_in', array( __CLASS__, 'price_filter' ) ); 
  119.  
  120. if ( @ $_POST['limits'] ) { 
  121. add_filter( 'loop_shop_post_in', array( __CLASS__, 'limits_filter' ) ); 
  122.  
  123. $args_fields = array( 'meta_key', 'tax_query', 'fields', 'where', 'join', 'meta_query' ); 
  124. foreach ( $args_fields as $args_field ) { 
  125. if ( @ $args[$args_field] ) { 
  126. $query->set( $args_field, $args[$args_field] ); 
  127.  
  128. return $query; 
  129.  
  130. public static function remove_out_of_stock( $filtered_posts ) { 
  131. global $wpdb; 
  132. $matched_products_query = $wpdb->get_results( " 
  133. SELECT DISTINCT ID, post_parent, post_type FROM $wpdb->posts 
  134. INNER JOIN $wpdb->postmeta as meta ON ID = meta.post_id 
  135. WHERE post_type IN ( 'product', 'product_variation' ) AND post_status = 'publish' 
  136. AND meta_key = '_stock_status' AND meta_value != 'outofstock'", OBJECT_K ); 
  137. $matched_products = array( 0 ); 
  138.  
  139. foreach ( $matched_products_query as $product ) { 
  140. if ( $product->post_type == 'product' ) 
  141. $matched_products[] = $product->ID; 
  142. if ( $product->post_parent > 0 && ! in_array( $product->post_parent, $matched_products ) ) 
  143. $matched_products[] = $product->post_parent; 
  144.  
  145. $matched_products = @ array_unique( $matched_products ); 
  146.  
  147. if ( sizeof( $filtered_posts ) == 0) { 
  148. $filtered_posts = $matched_products; 
  149. } else { 
  150. $filtered_posts = array_intersect( $filtered_posts, $matched_products ); 
  151.  
  152. return (array) $filtered_posts; 
  153.  
  154. public static function remove_hidden( $filtered_posts ) { 
  155. global $wpdb; 
  156. $matched_products_query = $wpdb->get_results( " 
  157. SELECT DISTINCT ID, post_parent, post_type FROM $wpdb->posts 
  158. INNER JOIN $wpdb->postmeta as meta ON ID = meta.post_id 
  159. WHERE post_type IN ( 'product', 'product_variation' ) AND post_status = 'publish' 
  160. AND meta_key = '_visibility' AND meta_value NOT IN ('hidden', 'search')", OBJECT_K ); 
  161. $matched_products = array( 0 ); 
  162.  
  163. foreach ( $matched_products_query as $product ) { 
  164. if ( $product->post_type == 'product' ) 
  165. $matched_products[] = $product->ID; 
  166. if ( $product->post_parent > 0 && ! in_array( $product->post_parent, $matched_products ) ) 
  167. $matched_products[] = $product->post_parent; 
  168.  
  169. $matched_products = @ array_unique( $matched_products ); 
  170.  
  171. if ( sizeof( $filtered_posts ) == 0) { 
  172. $filtered_posts = $matched_products; 
  173. } else { 
  174. $filtered_posts = array_intersect( $filtered_posts, $matched_products ); 
  175.  
  176. return (array) $filtered_posts; 
  177.  
  178. public static function limits_filter( $filtered_posts ) { 
  179. global $wpdb; 
  180.  
  181. if ( @ $_POST['limits'] ) { 
  182. $matched_products = false; 
  183.  
  184. foreach ( $_POST['limits'] as $v ) { 
  185. $matched_products_query = $wpdb->get_results( $wpdb->prepare(" 
  186. SELECT DISTINCT ID, post_parent, post_type FROM $wpdb->posts 
  187. INNER JOIN $wpdb->term_relationships as tr ON ID = tr.object_id 
  188. INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_taxonomy_id = tr.term_taxonomy_id 
  189. INNER JOIN $wpdb->terms as t ON t.term_id = tt.term_id 
  190. WHERE post_type IN ( 'product', 'product_variation' ) AND post_status = 'publish' 
  191. AND tt.taxonomy = %s AND t.slug BETWEEN %d AND %d 
  192. ", $v[0], $v[1], $v[2] ), OBJECT_K ); 
  193.  
  194. if ( $matched_products_query ) { 
  195. if ( $matched_products === false ) { 
  196. $matched_products = array( 0 ); 
  197. foreach ( $matched_products_query as $product ) { 
  198. if ( $product->post_type == 'product' ) 
  199. $matched_products[] = $product->ID; 
  200. if ( $product->post_parent > 0 && ! in_array( $product->post_parent, $matched_products ) ) 
  201. $matched_products[] = $product->post_parent; 
  202. } else { 
  203. $new_products = array( 0 ); 
  204. foreach ( $matched_products_query as $product ) { 
  205. if ( $product->post_type == 'product' && in_array($product->ID, $matched_products)) 
  206. $new_products[] = $product->ID; 
  207. if ( $product->post_parent > 0 && ! in_array( $product->post_parent, $matched_products ) && in_array($product->post_parent, $matched_products)) 
  208. $new_products[] = $product->post_parent; 
  209. $matched_products = $new_products; 
  210.  
  211. if ( $matched_products === false ) { 
  212. $matched_products = array( 0 ); 
  213.  
  214. $matched_products = @ array_unique( $matched_products ); 
  215.  
  216. // Filter the id's 
  217. if ( sizeof( $filtered_posts ) == 0 ) { 
  218. $filtered_posts = $matched_products; 
  219. } else { 
  220. $filtered_posts = array_intersect( $filtered_posts, $matched_products ); 
  221.  
  222. return (array) $filtered_posts; 
  223.  
  224. public static function price_filter( $filtered_posts ) { 
  225. global $wpdb; 
  226.  
  227. if ( @ $_POST['price'] ) { 
  228. $matched_products = array( 0 ); 
  229. $min = floatval( $_POST['price'][0] ); 
  230. $max = floatval( $_POST['price'][1] ); 
  231.  
  232. $matched_products_query = apply_filters( 'woocommerce_price_filter_results', $wpdb->get_results( $wpdb->prepare(" 
  233. SELECT DISTINCT ID, post_parent, post_type FROM $wpdb->posts 
  234. INNER JOIN $wpdb->postmeta ON ID = post_id 
  235. WHERE post_type IN ( 'product', 'product_variation' ) AND post_status = 'publish' AND meta_key = %s AND meta_value BETWEEN %d AND %d 
  236. ", '_price', $min, $max ), OBJECT_K ), $min, $max ); 
  237.  
  238. if ( $matched_products_query ) { 
  239. foreach ( $matched_products_query as $product ) { 
  240. if ( $product->post_type == 'product' ) 
  241. $matched_products[] = $product->ID; 
  242. if ( $product->post_parent > 0 && ! in_array( $product->post_parent, $matched_products ) ) 
  243. $matched_products[] = $product->post_parent; 
  244.  
  245. // Filter the id's 
  246. if ( sizeof( $filtered_posts ) == 0) { 
  247. $filtered_posts = $matched_products; 
  248. } else { 
  249. $filtered_posts = array_intersect( $filtered_posts, $matched_products ); 
  250.  
  251.  
  252. return (array) $filtered_posts; 
  253.  
  254. /** 
  255. * Get template part (for templates like the slider). 
  256. * @access public 
  257. * @param string $name (default: '') 
  258. * @return void 
  259. */ 
  260. public static function br_get_template_part( $name = '' ) { 
  261. $template = ''; 
  262.  
  263. // Look in your_child_theme/woocommerce-filters/name.php 
  264. if ( $name ) { 
  265. $template = locate_template( "woocommerce-filters/{$name}.php" ); 
  266.  
  267. // Get default slug-name.php 
  268. if ( ! $template && $name && file_exists( AAPF_TEMPLATE_PATH . "{$name}.php" ) ) { 
  269. $template = AAPF_TEMPLATE_PATH . "{$name}.php"; 
  270.  
  271. // Allow 3rd party plugin filter template file from their plugin 
  272. $template = apply_filters( 'br_get_template_part', $template, $name ); 
  273.  
  274.  
  275. if ( $template ) { 
  276. load_template( $template, false ); 
  277.  
  278. public static function register_br_options() { 
  279. register_setting( 'br_filters_plugin_options', 'br_filters_options' ); 
  280.  
  281. public static function br_add_defaults() { 
  282. $tmp = get_option( 'br_filters_options' ); 
  283. if ( @ $tmp['chk_default_options_db'] == '1' or ! @ is_array( $tmp ) ) { 
  284. delete_option( 'br_filters_options' ); 
  285. update_option( 'br_filters_options', BeRocket_AAPF::$defaults ); 
  286.  
  287. public static function br_delete_plugin_options() { 
  288. delete_option( 'br_filters_options' ); 
  289.  
  290. public static function WPML_fix() { 
  291. global $sitepress; 
  292. if ( method_exists( $sitepress, 'switch_lang' ) && isset( $_POST['current_language'] ) && $_POST['current_language'] !== $sitepress->get_default_language() ) { 
  293. $sitepress->switch_lang( $_POST['current_language'], true ); 
  294.  
  295. public static function order_by_popularity_post_clauses( $args ) { 
  296. global $wpdb; 
  297. $args['orderby'] = "$wpdb->postmeta.meta_value+0 DESC, $wpdb->posts.post_date DESC"; 
  298. return $args; 
  299.  
  300. public static function order_by_rating_post_clauses( $args ) { 
  301. global $wpdb; 
  302. $args['fields'] .= ", AVG( $wpdb->commentmeta.meta_value ) as average_rating "; 
  303. $args['where'] .= " AND ( $wpdb->commentmeta.meta_key = 'rating' OR $wpdb->commentmeta.meta_key IS null ) "; 
  304. $args['join'] .= " 
  305. LEFT OUTER JOIN $wpdb->comments ON($wpdb->posts.ID = $wpdb->comments.comment_post_ID) 
  306. LEFT JOIN $wpdb->commentmeta ON($wpdb->comments.comment_ID = $wpdb->commentmeta.comment_id) 
  307. "; 
  308. $args['orderby'] = "average_rating DESC, $wpdb->posts.post_date DESC"; 
  309. $args['groupby'] = "$wpdb->posts.ID"; 
  310. return $args;