Remove use of global

This commit is contained in:
Mike Jolley 2016-02-10 10:19:32 +00:00
parent 50407eb478
commit 0dba78acc0
6 changed files with 43 additions and 42 deletions

View File

@ -23,6 +23,12 @@ class WC_Query {
/** @public array Query vars to add to wp */
public $query_vars = array();
/**
* Stores chosen attributes
* @var array
*/
private static $_chosen_attributes;
/**
* Constructor for the query class. Hooks in methods.
*
@ -571,14 +577,12 @@ class WC_Query {
* @return array
*/
public function get_tax_query( $tax_query = array() ) {
global $_chosen_attributes;
if ( ! is_array( $tax_query ) ) {
$tax_query = array();
}
// Layered nav filters on terms
if ( $_chosen_attributes ) {
if ( $_chosen_attributes = $this->get_layered_nav_chosen_attributes() ) {
foreach ( $_chosen_attributes as $taxonomy => $data ) {
$tax_query[] = array(
'taxonomy' => $taxonomy,
@ -630,28 +634,34 @@ class WC_Query {
/**
* Layered Nav Init.
*/
public function layered_nav_init( ) {
global $_chosen_attributes;
public static function get_layered_nav_chosen_attributes() {
if ( ! is_array( self::$_chosen_attributes ) ) {
self::$_chosen_attributes = array();
$_chosen_attributes = array();
if ( $attribute_taxonomies = wc_get_attribute_taxonomies() ) {
foreach ( $attribute_taxonomies as $tax ) {
$attribute = wc_sanitize_taxonomy_name( $tax->attribute_name );
$taxonomy = wc_attribute_taxonomy_name( $attribute );
$filter_terms = ! empty( $_GET[ 'filter_' . $attribute ] ) ? explode( ',', wc_clean( $_GET[ 'filter_' . $attribute ] ) ) : array();
if ( $attribute_taxonomies = wc_get_attribute_taxonomies() ) {
foreach ( $attribute_taxonomies as $tax ) {
$attribute = wc_sanitize_taxonomy_name( $tax->attribute_name );
$taxonomy = wc_attribute_taxonomy_name( $attribute );
$filter_terms = ! empty( $_GET[ 'filter_' . $attribute ] ) ? explode( ',', wc_clean( $_GET[ 'filter_' . $attribute ] ) ) : array();
if ( empty( $filter_terms ) || ! taxonomy_exists( $taxonomy ) ) {
continue;
}
if ( empty( $filter_terms ) || ! taxonomy_exists( $taxonomy ) ) {
continue;
$query_type = ! empty( $_GET[ 'query_type_' . $attribute ] ) && in_array( $_GET[ 'query_type_' . $attribute ], array( 'and', 'or' ) ) ? wc_clean( $_GET[ 'query_type_' . $attribute ] ) : '';
self::$_chosen_attributes[ $taxonomy ]['terms'] = array_map( 'sanitize_title', $filter_terms ); // Ensures correct encoding
self::$_chosen_attributes[ $taxonomy ]['query_type'] = $query_type ? $query_type : apply_filters( 'woocommerce_layered_nav_default_query_type', 'and' );
}
$query_type = ! empty( $_GET[ 'query_type_' . $attribute ] ) && in_array( $_GET[ 'query_type_' . $attribute ], array( 'and', 'or' ) ) ? wc_clean( $_GET[ 'query_type_' . $attribute ] ) : '';
$_chosen_attributes[ $taxonomy ]['terms'] = array_map( 'sanitize_title', $filter_terms ); // Ensures correct encoding
$_chosen_attributes[ $taxonomy ]['query_type'] = $query_type ? $query_type : apply_filters( 'woocommerce_layered_nav_default_query_type', 'and' );
}
}
return self::$_chosen_attributes;
}
/**
* @deprecated 2.6.0
*/
public function layered_nav_init() {}
/**
* Get an unpaginated list all product ID's (both filtered and unfiltered). Makes use of transients.
* @deprecated 2.6.0 due to performance concerns

View File

@ -254,9 +254,7 @@ if ( ! function_exists( 'is_filtered' ) ) {
* @return bool
*/
function is_filtered() {
global $_chosen_attributes;
return apply_filters( 'woocommerce_is_filtered', ( sizeof( $_chosen_attributes ) > 0 || isset( $_GET['max_price'] ) || isset( $_GET['min_price'] ) ) );
return apply_filters( 'woocommerce_is_filtered', ( sizeof( WC_Query::get_layered_nav_chosen_attributes() ) > 0 || isset( $_GET['max_price'] ) || isset( $_GET['min_price'] ) || isset( $_GET['min_rating'] ) ) );
}
}

View File

@ -43,18 +43,14 @@ class WC_Widget_Layered_Nav_Filters extends WC_Widget {
* @param array $instance
*/
public function widget( $args, $instance ) {
global $_chosen_attributes;
if ( ! is_post_type_archive( 'product' ) && ! is_tax( get_object_taxonomies( 'product' ) ) ) {
return;
}
// Price
$min_price = isset( $_GET['min_price'] ) ? wc_clean( $_GET['min_price'] ) : 0;
$max_price = isset( $_GET['max_price'] ) ? wc_clean( $_GET['max_price'] ) : 0;
// Rating
$min_rating = isset( $_GET['min_rating'] ) ? wc_clean( $_GET['min_rating'] ) : 0;
$_chosen_attributes = WC_Query::get_layered_nav_chosen_attributes();
$min_price = isset( $_GET['min_price'] ) ? wc_clean( $_GET['min_price'] ) : 0;
$max_price = isset( $_GET['max_price'] ) ? wc_clean( $_GET['max_price'] ) : 0;
$min_rating = isset( $_GET['min_rating'] ) ? wc_clean( $_GET['min_rating'] ) : 0;
if ( 0 < count( $_chosen_attributes ) || 0 < $min_price || 0 < $max_price || 0 < $min_rating ) {
@ -63,7 +59,7 @@ class WC_Widget_Layered_Nav_Filters extends WC_Widget {
echo '<ul>';
// Attributes
if ( ! is_null( $_chosen_attributes ) ) {
if ( $_chosen_attributes ) {
foreach ( $_chosen_attributes as $taxonomy => $data ) {
foreach ( $data['terms'] as $term_slug ) {
if ( ! $term = get_term_by( 'slug', $term_slug, $taxonomy ) ) {

View File

@ -110,15 +110,14 @@ class WC_Widget_Layered_Nav extends WC_Widget {
* @param array $instance
*/
public function widget( $args, $instance ) {
global $_chosen_attributes;
if ( ! is_post_type_archive( 'product' ) && ! is_tax( get_object_taxonomies( 'product' ) ) ) {
return;
}
$taxonomy = isset( $instance['attribute'] ) ? wc_attribute_taxonomy_name( $instance['attribute'] ) : $this->settings['attribute']['std'];
$query_type = isset( $instance['query_type'] ) ? $instance['query_type'] : $this->settings['query_type']['std'];
$display_type = isset( $instance['display_type'] ) ? $instance['display_type'] : $this->settings['display_type']['std'];
$_chosen_attributes = WC_Query::get_layered_nav_chosen_attributes();
$taxonomy = isset( $instance['attribute'] ) ? wc_attribute_taxonomy_name( $instance['attribute'] ) : $this->settings['attribute']['std'];
$query_type = isset( $instance['query_type'] ) ? $instance['query_type'] : $this->settings['query_type']['std'];
$display_type = isset( $instance['display_type'] ) ? $instance['display_type'] : $this->settings['display_type']['std'];
if ( ! taxonomy_exists( $taxonomy ) ) {
return;
@ -205,11 +204,10 @@ class WC_Widget_Layered_Nav extends WC_Widget {
* @return bool Will nav display?
*/
protected function layered_nav_dropdown( $terms, $taxonomy, $query_type ) {
global $_chosen_attributes;
$found = false;
if ( $taxonomy !== $this->get_current_taxonomy() ) {
$_chosen_attributes = WC_Query::get_layered_nav_chosen_attributes();
$taxonomy_filter_name = str_replace( 'pa_', '', $taxonomy );
echo '<select class="dropdown_layered_nav_' . esc_attr( $taxonomy_filter_name ) . '">';
@ -345,12 +343,11 @@ class WC_Widget_Layered_Nav extends WC_Widget {
* @return bool Will nav display?
*/
protected function layered_nav_list( $terms, $taxonomy, $query_type ) {
global $_chosen_attributes;
// List display
echo '<ul>';
$found = false;
$_chosen_attributes = WC_Query::get_layered_nav_chosen_attributes();
$found = false;
foreach ( $terms as $term ) {
// Get count based on current view - uses transients

View File

@ -53,7 +53,7 @@ class WC_Widget_Price_Filter extends WC_Widget {
* @param array $instance
*/
public function widget( $args, $instance ) {
global $_chosen_attributes, $wpdb, $wp, $wp_the_query;
global $wp, $wp_the_query;
if ( ! is_post_type_archive( 'product' ) && ! is_tax( get_object_taxonomies( 'product' ) ) ) {
return;
@ -95,7 +95,7 @@ class WC_Widget_Price_Filter extends WC_Widget {
$fields .= '<input type="hidden" name="min_rating" value="' . esc_attr( $_GET['min_rating'] ) . '" />';
}
if ( $_chosen_attributes ) {
if ( $_chosen_attributes = WC_Query::get_layered_nav_chosen_attributes() ) {
foreach ( $_chosen_attributes as $attribute => $data ) {
$taxonomy_filter = 'filter_' . str_replace( 'pa_', '', $attribute );

View File

@ -124,7 +124,7 @@ class WC_Widget_Rating_Filter extends WC_Widget {
* @param array $instance
*/
public function widget( $args, $instance ) {
global $_chosen_attributes, $wpdb, $wp, $wp_the_query;
global $wp_the_query;
if ( ! is_post_type_archive( 'product' ) && ! is_tax( get_object_taxonomies( 'product' ) ) ) {
return;