Skip to content

Commit

Permalink
Code consistency changes for includes/widgets/class-wc-widget-layered…
Browse files Browse the repository at this point in the history
…-nav.php
  • Loading branch information
mattyza committed Jul 11, 2016
1 parent a9b2c7e commit 389c1d3
Showing 1 changed file with 33 additions and 32 deletions.
65 changes: 33 additions & 32 deletions includes/widgets/class-wc-widget-layered-nav.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public function __construct() {
*
* @see WP_Widget->update
*
* @param array $new_instance
* @param array $old_instance
* @param array $new_instance
* @param array $old_instance
*
* @return array
*/
Expand Down Expand Up @@ -69,12 +69,12 @@ public function init_settings() {
}

$this->settings = array(
'title' => array(
'title' => array(
'type' => 'text',
'std' => __( 'Filter by', 'woocommerce' ),
'label' => __( 'Title', 'woocommerce' )
),
'attribute' => array(
'attribute' => array(
'type' => 'select',
'std' => '',
'label' => __( 'Attribute', 'woocommerce' ),
Expand All @@ -89,7 +89,7 @@ public function init_settings() {
'dropdown' => __( 'Dropdown', 'woocommerce' )
)
),
'query_type' => array(
'query_type' => array(
'type' => 'select',
'std' => 'and',
'label' => __( 'Query type', 'woocommerce' ),
Expand Down Expand Up @@ -207,10 +207,10 @@ protected function get_current_term_slug() {

/**
* Show dropdown layered nav.
* @param array $terms
* @param array $terms
* @param string $taxonomy
* @param string $query_type
* @return bool Will nav display?
* @return bool Will nav display?
*/
protected function layered_nav_dropdown( $terms, $taxonomy, $query_type ) {
$found = false;
Expand All @@ -231,9 +231,9 @@ protected function layered_nav_dropdown( $terms, $taxonomy, $query_type ) {
}

// Get count based on current view
$current_values = isset( $_chosen_attributes[ $taxonomy ]['terms'] ) ? $_chosen_attributes[ $taxonomy ]['terms'] : array();
$option_is_set = in_array( $term->slug, $current_values );
$count = isset( $term_counts[ $term->term_id ] ) ? $term_counts[ $term->term_id ] : 0;
$current_values = isset( $_chosen_attributes[ $taxonomy ]['terms'] ) ? $_chosen_attributes[ $taxonomy ]['terms'] : array();
$option_is_set = in_array( $term->slug, $current_values );
$count = isset( $term_counts[ $term->term_id ] ) ? $term_counts[ $term->term_id ] : 0;

// Only show options with count > 0
if ( 0 < $count ) {
Expand All @@ -248,9 +248,9 @@ protected function layered_nav_dropdown( $terms, $taxonomy, $query_type ) {
echo '</select>';

wc_enqueue_js( "
jQuery( '.dropdown_layered_nav_". esc_js( $taxonomy_filter_name ) . "' ).change( function() {
jQuery( '.dropdown_layered_nav_" . esc_js( $taxonomy_filter_name ) . "' ).change( function() {
var slug = jQuery( this ).val();
location.href = '" . preg_replace( '%\/page\/[0-9]+%', '', str_replace( array( '&amp;', '%2C' ), array( '&', ',' ), esc_js( add_query_arg( 'filtering', '1', remove_query_arg( array( 'page', 'filter_' . $taxonomy_filter_name ) ) ) ) ) ) . "&filter_". esc_js( $taxonomy_filter_name ) . "=' + slug;
location.href = '" . preg_replace( '%\/page\/[0-9]+%', '', str_replace( array( '&amp;', '%2C' ), array( '&', ',' ), esc_js( add_query_arg( 'filtering', '1', remove_query_arg( array( 'page', 'filter_' . $taxonomy_filter_name ) ) ) ) ) ) . '&filter_' . esc_js( $taxonomy_filter_name ) . "=' + slug;
});
" );
}
Expand Down Expand Up @@ -328,7 +328,7 @@ protected function get_page_base_url( $taxonomy ) {

/**
* Count products within certain terms, taking the main WP query into consideration.
* @param array $term_ids
* @param array $term_ids
* @param string $taxonomy
* @param string $query_type
* @return array
Expand All @@ -347,10 +347,10 @@ protected function get_filtered_term_product_counts( $term_ids, $taxonomy, $quer
}
}

$meta_query = new WP_Meta_Query( $meta_query );
$tax_query = new WP_Tax_Query( $tax_query );
$meta_query_sql = $meta_query->get_sql( 'post', $wpdb->posts, 'ID' );
$tax_query_sql = $tax_query->get_sql( $wpdb->posts, 'ID' );
$meta_query = new WP_Meta_Query( $meta_query );
$tax_query = new WP_Tax_Query( $tax_query );
$meta_query_sql = $meta_query->get_sql( 'post', $wpdb->posts, 'ID' );
$tax_query_sql = $tax_query->get_sql( $wpdb->posts, 'ID' );

// Generate query
$query = array();
Expand All @@ -361,24 +361,24 @@ protected function get_filtered_term_product_counts( $term_ids, $taxonomy, $quer
INNER JOIN {$wpdb->term_taxonomy} AS term_taxonomy USING( term_taxonomy_id )
INNER JOIN {$wpdb->terms} AS terms USING( term_id )
" . $tax_query_sql['join'] . $meta_query_sql['join'];
$query['where'] = "
$query['where'] = "
WHERE {$wpdb->posts}.post_type IN ( 'product' )
AND {$wpdb->posts}.post_status = 'publish'
" . $tax_query_sql['where'] . $meta_query_sql['where'] . "
AND terms.term_id IN (" . implode( ',', array_map( 'absint', $term_ids ) ) . ")
";
" . $tax_query_sql['where'] . $meta_query_sql['where'] . '
AND terms.term_id IN (' . implode( ',', array_map( 'absint', $term_ids ) ) . ')
';
if ( is_search() ) {
$search_terms = $wp_query->query_vars['search_terms'];
$count = 0;
$query['search'] = " AND (";
$search_terms = $wp_query->query_vars['search_terms'];
$count = 0;
$query['search'] = ' AND (';
foreach ( $search_terms as $search_term ) {
$query['search'] .= $count > 0 ? " AND " : "";
$query['search'] .= $count > 0 ? ' AND ' : '';
$query['search'] .= "((wp_posts.post_title LIKE '%" . $search_term . "%') OR (wp_posts.post_excerpt LIKE '%" . $search_term . "%') OR (wp_posts.post_content LIKE '%" . $search_term . "%'))";
$count++;
}
$query['search'] .= ")";
$query['search'] .= ')';
}
$query['group_by'] = "GROUP BY terms.term_id";
$query['group_by'] = 'GROUP BY terms.term_id';
$query = apply_filters( 'woocommerce_get_filtered_term_product_counts_query', $query );
$query = implode( ' ', $query );
$results = $wpdb->get_results( $query );
Expand All @@ -388,10 +388,10 @@ protected function get_filtered_term_product_counts( $term_ids, $taxonomy, $quer

/**
* Show list based layered nav.
* @param array $terms
* @param array $terms
* @param string $taxonomy
* @param string $query_type
* @return bool Will nav display?
* @return bool Will nav display?
*/
protected function layered_nav_list( $terms, $taxonomy, $query_type ) {
// List display
Expand All @@ -402,9 +402,9 @@ protected function layered_nav_list( $terms, $taxonomy, $query_type ) {
$found = false;

foreach ( $terms as $term ) {
$current_values = isset( $_chosen_attributes[ $taxonomy ]['terms'] ) ? $_chosen_attributes[ $taxonomy ]['terms'] : array();
$option_is_set = in_array( $term->slug, $current_values );
$count = isset( $term_counts[ $term->term_id ] ) ? $term_counts[ $term->term_id ] : 0;
$current_values = isset( $_chosen_attributes[ $taxonomy ]['terms'] ) ? $_chosen_attributes[ $taxonomy ]['terms'] : array();
$option_is_set = in_array( $term->slug, $current_values );
$count = isset( $term_counts[ $term->term_id ] ) ? $term_counts[ $term->term_id ] : 0;

// skip the term for the current archive
if ( $this->get_current_term_id() === $term->term_id ) {
Expand Down Expand Up @@ -467,4 +467,5 @@ protected function layered_nav_list( $terms, $taxonomy, $query_type ) {

return $found;
}

}

0 comments on commit 389c1d3

Please sign in to comment.