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