Merge pull request #17822 from woocommerce/fix/17733

Use WC_Query->get_query_vars() instead of WC_Query->query_vars
This commit is contained in:
Mike Jolley 2017-11-21 14:00:53 +00:00 committed by GitHub
commit 78ddd97472
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 284 additions and 334 deletions

View File

@ -3,7 +3,7 @@
* Contains the query functions for WooCommerce which alter the front-end post queries and loops
*
* @version 3.2.0
* @package WooCommerce/Classes
* @package WooCommerce\Classes
* @author Automattic
*/
@ -50,7 +50,9 @@ class WC_Query {
* Get any errors from querystring.
*/
public function get_errors() {
if ( ! empty( $_GET['wc_error'] ) && ( $error = sanitize_text_field( $_GET['wc_error'] ) ) && ! wc_has_notice( $error, 'error' ) ) {
$error = ! empty( $_GET['wc_error'] ) ? sanitize_text_field( wp_unslash( $_GET['wc_error'] ) ) : ''; // WPCS: input var ok, CSRF ok.
if ( $error && ! wc_has_notice( $error, 'error' ) ) {
wc_add_notice( $error, 'error' );
}
}
@ -81,53 +83,54 @@ class WC_Query {
/**
* Get page title for an endpoint.
* @param string
*
* @param string $endpoint Endpoint key.
* @return string
*/
public function get_endpoint_title( $endpoint ) {
global $wp;
switch ( $endpoint ) {
case 'order-pay' :
case 'order-pay':
$title = __( 'Pay for order', 'woocommerce' );
break;
case 'order-received' :
break;
case 'order-received':
$title = __( 'Order received', 'woocommerce' );
break;
case 'orders' :
break;
case 'orders':
if ( ! empty( $wp->query_vars['orders'] ) ) {
/* translators: %s: page */
$title = sprintf( __( 'Orders (page %d)', 'woocommerce' ), intval( $wp->query_vars['orders'] ) );
} else {
$title = __( 'Orders', 'woocommerce' );
}
break;
case 'view-order' :
break;
case 'view-order':
$order = wc_get_order( $wp->query_vars['view-order'] );
/* translators: %s: order number */
$title = ( $order ) ? sprintf( __( 'Order #%s', 'woocommerce' ), $order->get_order_number() ) : '';
break;
case 'downloads' :
break;
case 'downloads':
$title = __( 'Downloads', 'woocommerce' );
break;
case 'edit-account' :
break;
case 'edit-account':
$title = __( 'Account details', 'woocommerce' );
break;
case 'edit-address' :
break;
case 'edit-address':
$title = __( 'Addresses', 'woocommerce' );
break;
case 'payment-methods' :
break;
case 'payment-methods':
$title = __( 'Payment methods', 'woocommerce' );
break;
case 'add-payment-method' :
break;
case 'add-payment-method':
$title = __( 'Add payment method', 'woocommerce' );
break;
case 'lost-password' :
break;
case 'lost-password':
$title = __( 'Lost password', 'woocommerce' );
break;
default :
break;
default:
$title = '';
break;
break;
}
return apply_filters( 'woocommerce_endpoint_' . $endpoint . '_title', $title, $endpoint );
@ -145,7 +148,7 @@ class WC_Query {
$myaccount_page_id = get_option( 'woocommerce_myaccount_page_id' );
$checkout_page_id = get_option( 'woocommerce_checkout_page_id' );
if ( in_array( $page_on_front, array( $myaccount_page_id, $checkout_page_id ) ) ) {
if ( in_array( $page_on_front, array( $myaccount_page_id, $checkout_page_id ), true ) ) {
return EP_ROOT | EP_PAGES;
}
}
@ -159,7 +162,7 @@ class WC_Query {
public function add_endpoints() {
$mask = $this->get_endpoints_mask();
foreach ( $this->query_vars as $key => $var ) {
foreach ( $this->get_query_vars() as $key => $var ) {
if ( ! empty( $var ) ) {
add_rewrite_endpoint( $var, $mask );
}
@ -170,7 +173,8 @@ class WC_Query {
* Add query vars.
*
* @access public
* @param array $vars
*
* @param array $vars Query vars.
* @return array
*/
public function add_query_vars( $vars ) {
@ -210,10 +214,10 @@ class WC_Query {
public function parse_request() {
global $wp;
// Map query vars to their keys, or get them if endpoints are not supported
// Map query vars to their keys, or get them if endpoints are not supported.
foreach ( $this->get_query_vars() as $key => $var ) {
if ( isset( $_GET[ $var ] ) ) {
$wp->query_vars[ $key ] = $_GET[ $var ];
if ( isset( $_GET[ $var ] ) ) { // WPCS: input var ok, CSRF ok.
$wp->query_vars[ $key ] = sanitize_text_field( wp_unslash( $_GET[ $var ] ) ); // WPCS: input var ok, CSRF ok.
} elseif ( isset( $wp->query_vars[ $var ] ) ) {
$wp->query_vars[ $key ] = $wp->query_vars[ $var ];
}
@ -223,8 +227,7 @@ class WC_Query {
/**
* Are we currently on the front page?
*
* @param object $q
*
* @param WP_Query $q Query instance.
* @return bool
*/
private function is_showing_page_on_front( $q ) {
@ -234,8 +237,7 @@ class WC_Query {
/**
* Is the front page a page we define?
*
* @param int $page_id
*
* @param int $page_id Page ID.
* @return bool
*/
private function page_on_front_is( $page_id ) {
@ -245,18 +247,18 @@ class WC_Query {
/**
* Hook into pre_get_posts to do the main product query.
*
* @param object $q query object
* @param WP_Query $q Query instance.
*/
public function pre_get_posts( $q ) {
// We only want to affect the main query
// We only want to affect the main query.
if ( ! $q->is_main_query() ) {
return;
}
// Fix for endpoints on the homepage
// Fix for endpoints on the homepage.
if ( $this->is_showing_page_on_front( $q ) && ! $this->page_on_front_is( $q->get( 'page_id' ) ) ) {
$_query = wp_parse_args( $q->query );
if ( ! empty( $_query ) && array_intersect( array_keys( $_query ), array_keys( $this->query_vars ) ) ) {
if ( ! empty( $_query ) && array_intersect( array_keys( $_query ), array_keys( $this->get_query_vars() ) ) ) {
$q->is_page = true;
$q->is_home = false;
$q->is_singular = true;
@ -276,14 +278,14 @@ class WC_Query {
}
}
// Fix product feeds
// Fix product feeds.
if ( $q->is_feed() && $q->is_post_type_archive( 'product' ) ) {
$q->is_comment_feed = false;
}
// Special check for shops with the product archive on front
// Special check for shops with the product archive on front.
if ( $q->is_page() && 'page' === get_option( 'show_on_front' ) && absint( $q->get( 'page_id' ) ) === wc_get_page_id( 'shop' ) ) {
// This is a front-page shop
// This is a front-page shop.
$q->set( 'post_type', 'product' );
$q->set( 'page_id', '' );
@ -291,54 +293,55 @@ class WC_Query {
$q->set( 'paged', $q->query['paged'] );
}
// Define a variable so we know this is the front page shop later on
// Define a variable so we know this is the front page shop later on.
wc_maybe_define_constant( 'SHOP_IS_ON_FRONT', true );
// Get the actual WP page to avoid errors and let us use is_front_page()
// This is hacky but works. Awaiting https://core.trac.wordpress.org/ticket/21096
// Get the actual WP page to avoid errors and let us use is_front_page().
// This is hacky but works. Awaiting https://core.trac.wordpress.org/ticket/21096.
global $wp_post_types;
$shop_page = get_post( wc_get_page_id( 'shop' ) );
$shop_page = get_post( wc_get_page_id( 'shop' ) );
$wp_post_types['product']->ID = $shop_page->ID;
$wp_post_types['product']->post_title = $shop_page->post_title;
$wp_post_types['product']->post_name = $shop_page->post_name;
$wp_post_types['product']->post_type = $shop_page->post_type;
$wp_post_types['product']->ancestors = get_ancestors( $shop_page->ID, $shop_page->post_type );
$wp_post_types['product']->ID = $shop_page->ID;
$wp_post_types['product']->post_title = $shop_page->post_title;
$wp_post_types['product']->post_name = $shop_page->post_name;
$wp_post_types['product']->post_type = $shop_page->post_type;
$wp_post_types['product']->ancestors = get_ancestors( $shop_page->ID, $shop_page->post_type );
// Fix conditional Functions like is_front_page
// Fix conditional Functions like is_front_page.
$q->is_singular = false;
$q->is_post_type_archive = true;
$q->is_archive = true;
$q->is_page = true;
// Remove post type archive name from front page title tag
// Remove post type archive name from front page title tag.
add_filter( 'post_type_archive_title', '__return_empty_string', 5 );
// Fix WP SEO
// Fix WP SEO.
if ( class_exists( 'WPSEO_Meta' ) ) {
add_filter( 'wpseo_metadesc', array( $this, 'wpseo_metadesc' ) );
add_filter( 'wpseo_metakey', array( $this, 'wpseo_metakey' ) );
}
// Only apply to product categories, the product post archive, the shop page, product tags, and product attribute taxonomies
} elseif ( ! $q->is_post_type_archive( 'product' ) && ! $q->is_tax( get_object_taxonomies( 'product' ) ) ) {
// Only apply to product categories, the product post archive, the shop page, product tags, and product attribute taxonomies.
return;
}
$this->product_query( $q );
// And remove the pre_get_posts hook
// And remove the pre_get_posts hook.
$this->remove_product_query();
}
/**
* Search post excerpt.
*
* @deprecated 3.2.0 - Not needed anymore since WordPress 4.5.
* @param string $deprecated Deprecated.
*/
public function search_post_excerpt( $where = '' ) {
public function search_post_excerpt( $deprecated = '' ) {
wc_deprecated_function( 'WC_Query::search_post_excerpt', '3.2.0', 'Excerpt added to search query by default since WordPress 4.5.' );
return $where;
return $deprecated;
}
/**
@ -346,7 +349,6 @@ class WC_Query {
*
* Hooked into wpseo_ hook already, so no need for function_exist.
*
* @access public
* @return string
*/
public function wpseo_metadesc() {
@ -366,9 +368,10 @@ class WC_Query {
}
/**
* Query the products, applying sorting/ordering etc. This applies to the main wordpress loop.
* Query the products, applying sorting/ordering etc.
* This applies to the main WordPress loop.
*
* @param mixed $q
* @param WP_Query $q Query instance.
*/
public function product_query( $q ) {
if ( ! is_feed() ) {
@ -381,7 +384,7 @@ class WC_Query {
}
}
// Query vars that affect posts shown
// Query vars that affect posts shown.
$q->set( 'meta_query', $this->get_meta_query( $q->get( 'meta_query' ), true ) );
$q->set( 'tax_query', $this->get_tax_query( $q->get( 'tax_query' ), true ) );
$q->set( 'posts_per_page', $q->get( 'posts_per_page' ) ? $q->get( 'posts_per_page' ) : apply_filters( 'loop_shop_per_page', get_option( 'posts_per_page' ) ) );
@ -391,7 +394,6 @@ class WC_Query {
do_action( 'woocommerce_product_query', $q, $this );
}
/**
* Remove the query.
*/
@ -411,6 +413,7 @@ class WC_Query {
/**
* Remove the posts_where filter.
*
* @deprecated 3.2.0 - Nothing to remove anymore because search_post_excerpt() is deprecated.
*/
public function remove_posts_where() {
@ -420,19 +423,17 @@ class WC_Query {
/**
* Returns an array of arguments for ordering products based on the selected values.
*
* @access public
*
* @param string $orderby
* @param string $order
* @param string $orderby Order by.
* @param string $order Sorting order.
*
* @return array
*/
public function get_catalog_ordering_args( $orderby = '', $order = '' ) {
// Get ordering from query string unless defined
// Get ordering from query string unless defined.
if ( ! $orderby ) {
$orderby_value = isset( $_GET['orderby'] ) ? wc_clean( (string) $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
$orderby_value = isset( $_GET['orderby'] ) ? wc_clean( wp_unslash( (string) $_GET['orderby'] ) ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) ); // WPCS: input var ok, CSRF ok.
// Get order + orderby args from string
// Get order + orderby args from string.
$orderby_value = explode( '-', $orderby_value );
$orderby = esc_attr( $orderby_value[0] );
$order = ! empty( $orderby_value[1] ) ? $orderby_value[1] : $order;
@ -443,49 +444,49 @@ class WC_Query {
$args = array(
'orderby' => 'relevance',
'order' => 'DESC',
'meta_key' => '',
'meta_key' => '', // WPCS: slow query ok.
);
// Set to default. Menu order for non-searches, relevance for searches.
if ( ! is_search() ) {
$args['orderby'] = 'menu_order title';
$args['order'] = ( 'DESC' === $order ) ? 'DESC' : 'ASC';
$args['meta_key'] = '';
$args['meta_key'] = ''; // WPCS: slow query ok.
}
switch ( $orderby ) {
case 'rand' :
case 'rand':
$args['orderby'] = 'rand';
break;
case 'date' :
case 'date':
$args['orderby'] = 'date ID';
$args['order'] = ( 'ASC' === $order ) ? 'ASC' : 'DESC';
break;
case 'price' :
case 'price':
if ( 'DESC' === $order ) {
add_filter( 'posts_clauses', array( $this, 'order_by_price_desc_post_clauses' ) );
} else {
add_filter( 'posts_clauses', array( $this, 'order_by_price_asc_post_clauses' ) );
}
break;
case 'popularity' :
$args['meta_key'] = 'total_sales';
case 'popularity':
$args['meta_key'] = 'total_sales'; // WPCS: slow query ok.
// Sorting handled later though a hook
// Sorting handled later though a hook.
add_filter( 'posts_clauses', array( $this, 'order_by_popularity_post_clauses' ) );
break;
case 'rating' :
$args['meta_key'] = '_wc_average_rating';
case 'rating':
$args['meta_key'] = '_wc_average_rating'; // WPCS: slow query ok.
$args['orderby'] = array(
'meta_value_num' => 'DESC',
'ID' => 'ASC',
);
break;
case 'title' :
case 'title':
$args['orderby'] = 'title';
$args['order'] = ( 'DESC' === $order ) ? 'DESC' : 'ASC';
break;
case 'relevance' :
case 'relevance':
$args['orderby'] = 'relevance';
$args['order'] = 'DESC';
break;
@ -497,8 +498,7 @@ class WC_Query {
/**
* Handle numeric price sorting.
*
* @access public
* @param array $args
* @param array $args Query args.
* @return array
*/
public function order_by_price_asc_post_clauses( $args ) {
@ -527,8 +527,7 @@ class WC_Query {
/**
* Handle numeric price sorting.
*
* @access public
* @param array $args
* @param array $args Query args.
* @return array
*/
public function order_by_price_desc_post_clauses( $args ) {
@ -560,8 +559,7 @@ class WC_Query {
*
* This lets us sort by meta value desc, and have a second orderby param.
*
* @access public
* @param array $args
* @param array $args Query args.
* @return array
*/
public function order_by_popularity_post_clauses( $args ) {
@ -574,7 +572,7 @@ class WC_Query {
* Order by rating post clauses.
*
* @deprecated 3.0.0
* @param array $args
* @param array $args Query args.
* @return array
*/
public function order_by_rating_post_clauses( $args ) {
@ -597,8 +595,8 @@ class WC_Query {
/**
* Appends meta queries to an array.
*
* @param array $meta_query
* @param bool $main_query
* @param array $meta_query Meta query.
* @param bool $main_query If is main query.
* @return array
*/
public function get_meta_query( $meta_query = array(), $main_query = false ) {
@ -611,18 +609,21 @@ class WC_Query {
/**
* Appends tax queries to an array.
* @param array $tax_query
* @param bool $main_query
*
* @param array $tax_query Tax query.
* @param bool $main_query If is main query.
* @return array
*/
public function get_tax_query( $tax_query = array(), $main_query = false ) {
if ( ! is_array( $tax_query ) ) {
$tax_query = array( 'relation' => 'AND' );
$tax_query = array(
'relation' => 'AND',
);
}
// Layered nav filters on terms.
if ( $main_query && ( $_chosen_attributes = $this->get_layered_nav_chosen_attributes() ) ) {
foreach ( $_chosen_attributes as $taxonomy => $data ) {
if ( $main_query ) {
foreach ( $this->get_layered_nav_chosen_attributes() as $taxonomy => $data ) {
$tax_query[] = array(
'taxonomy' => $taxonomy,
'field' => 'slug',
@ -642,11 +643,11 @@ class WC_Query {
}
// Filter by rating.
if ( isset( $_GET['rating_filter'] ) ) {
$rating_filter = array_filter( array_map( 'absint', explode( ',', $_GET['rating_filter'] ) ) );
if ( isset( $_GET['rating_filter'] ) ) { // WPCS: input var ok, CSRF ok.
$rating_filter = array_filter( array_map( 'absint', explode( ',', $_GET['rating_filter'] ) ) ); // WPCS: input var ok, CSRF ok, Sanitization ok.
$rating_terms = array();
for ( $i = 1; $i <= 5; $i ++ ) {
if ( in_array( $i, $rating_filter ) && isset( $product_visibility_terms[ 'rated-' . $i ] ) ) {
if ( in_array( $i, $rating_filter, true ) && isset( $product_visibility_terms[ 'rated-' . $i ] ) ) {
$rating_terms[] = $product_visibility_terms[ 'rated-' . $i ];
}
}
@ -675,11 +676,12 @@ class WC_Query {
/**
* Return a meta query for filtering by price.
*
* @return array
*/
private function price_filter_meta_query() {
if ( isset( $_GET['max_price'] ) || isset( $_GET['min_price'] ) ) {
$meta_query = wc_get_min_max_price_meta_query( $_GET );
if ( isset( $_GET['max_price'] ) || isset( $_GET['min_price'] ) ) { // WPCS: input var ok, CSRF ok.
$meta_query = wc_get_min_max_price_meta_query( $_GET ); // WPCS: input var ok, CSRF ok.
$meta_query['price_filter'] = true;
return $meta_query;
@ -702,7 +704,7 @@ class WC_Query {
* Returns a meta query to handle product visibility.
*
* @deprecated 3.0.0 Replaced with taxonomy.
* @param string $compare (default: 'IN')
* @param string $compare Compare type.
* @return array
*/
public function visibility_meta_query( $compare = 'IN' ) {
@ -713,7 +715,7 @@ class WC_Query {
* Returns a meta query to handle product stock status.
*
* @deprecated 3.0.0 Replaced with taxonomy.
* @param string $status (default: 'instock')
* @param string $status Status.
* @return array
*/
public function stock_status_meta_query( $status = 'instock' ) {
@ -722,6 +724,7 @@ class WC_Query {
/**
* Get the tax query which was used by the main query.
*
* @return array
*/
public static function get_main_tax_query() {
@ -734,6 +737,7 @@ class WC_Query {
/**
* Get the meta query which was used by the main query.
*
* @return array
*/
public static function get_main_meta_query() {
@ -769,7 +773,7 @@ class WC_Query {
}
$like = '%' . $wpdb->esc_like( $term ) . '%';
$sql[] = $wpdb->prepare( "(($wpdb->posts.post_title $like_op %s) $andor_op ($wpdb->posts.post_excerpt $like_op %s) $andor_op ($wpdb->posts.post_content $like_op %s))", $like, $like, $like );
$sql[] = $wpdb->prepare( "(($wpdb->posts.post_title $like_op %s) $andor_op ($wpdb->posts.post_excerpt $like_op %s) $andor_op ($wpdb->posts.post_content $like_op %s))", $like, $like, $like ); // WPCS: db call ok, cache ok, unprepared SQL ok.
}
if ( ! empty( $sql ) && ! is_user_logged_in() ) {
@ -781,23 +785,26 @@ class WC_Query {
/**
* Layered Nav Init.
*
* @return array
*/
public static function get_layered_nav_chosen_attributes() {
if ( ! is_array( self::$_chosen_attributes ) ) {
self::$_chosen_attributes = array();
if ( $attribute_taxonomies = wc_get_attribute_taxonomies() ) {
$attribute_taxonomies = wc_get_attribute_taxonomies();
if ( $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();
$filter_terms = ! empty( $_GET[ 'filter_' . $attribute ] ) ? explode( ',', wc_clean( wp_unslash( $_GET[ 'filter_' . $attribute ] ) ) ) : array(); // WPCS: input var ok, CSRF ok.
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
$query_type = ! empty( $_GET[ 'query_type_' . $attribute ] ) && in_array( wp_unslash( $_GET[ 'query_type_' . $attribute ] ), array( 'and', 'or' ), true ) ? wc_clean( wp_unslash( $_GET[ 'query_type_' . $attribute ] ) ) : ''; // WPCS: input var ok, CSRF ok.
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' );
}
}
@ -806,6 +813,8 @@ class WC_Query {
}
/**
* Layered nav init.
*
* @deprecated 2.6.0
*/
public function layered_nav_init() {
@ -814,6 +823,7 @@ class WC_Query {
/**
* Get an unpaginated list all product IDs (both filtered and unfiltered). Makes use of transients.
*
* @deprecated 2.6.0 due to performance concerns
*/
public function get_products_in_view() {
@ -822,11 +832,12 @@ class WC_Query {
/**
* Layered Nav post filter.
*
* @deprecated 2.6.0 due to performance concerns
*
* @param $filtered_posts
* @param mixed $deprecated Deprecated.
*/
public function layered_nav_query( $filtered_posts ) {
public function layered_nav_query( $deprecated ) {
wc_deprecated_function( 'layered_nav_query', '2.6' );
}
}

View File

@ -4,30 +4,29 @@
*
* Functions related to pages and menus.
*
* @author WooThemes
* @author Automattic
* @category Core
* @package WooCommerce/Functions
* @package WooCommerce\Functions
* @version 2.6.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
exit; // Exit if accessed directly.
}
/**
* Replace a page title with the endpoint title.
* @param string $title
*
* @param string $title Post title.
* @return string
*/
function wc_page_endpoint_title( $title ) {
global $wp_query;
if ( ! is_null( $wp_query ) && ! is_admin() && is_main_query() && in_the_loop() && is_page() && is_wc_endpoint_url() ) {
$endpoint = WC()->query->get_current_endpoint();
if ( $endpoint_title = WC()->query->get_endpoint_title( $endpoint ) ) {
$title = $endpoint_title;
}
$endpoint = WC()->query->get_current_endpoint();
$endpoint_title = WC()->query->get_endpoint_title( $endpoint );
$title = $endpoint_title ? $endpoint_title : $title;
remove_filter( 'the_title', 'wc_page_endpoint_title' );
}
@ -40,12 +39,11 @@ add_filter( 'the_title', 'wc_page_endpoint_title' );
/**
* Retrieve page ids - used for myaccount, edit_address, shop, cart, checkout, pay, view_order, terms. returns -1 if no page is found.
*
* @param string $page
* @param string $page Page slug.
* @return int
*/
function wc_get_page_id( $page ) {
if ( 'pay' == $page || 'thanks' == $page ) {
if ( 'pay' === $page || 'thanks' === $page ) {
wc_deprecated_argument( __FUNCTION__, '2.1', 'The "pay" and "thanks" pages are no-longer used - an endpoint is added to the checkout instead. To get a valid link use the WC_Order::get_checkout_payment_url() or WC_Order::get_checkout_order_received_url() methods instead.' );
$page = 'checkout';
@ -64,7 +62,7 @@ function wc_get_page_id( $page ) {
/**
* Retrieve page permalink.
*
* @param string $page
* @param string $page page slug.
* @return string
*/
function wc_get_page_permalink( $page ) {
@ -78,9 +76,9 @@ function wc_get_page_permalink( $page ) {
*
* Gets the URL for an endpoint, which varies depending on permalink settings.
*
* @param string $endpoint
* @param string $value
* @param string $permalink
* @param string $endpoint Endpoint slug.
* @param string $value Query param value.
* @param string $permalink Permalink.
*
* @return string
*/
@ -89,13 +87,14 @@ function wc_get_endpoint_url( $endpoint, $value = '', $permalink = '' ) {
$permalink = get_permalink();
}
// Map endpoint to options
$endpoint = ! empty( WC()->query->query_vars[ $endpoint ] ) ? WC()->query->query_vars[ $endpoint ] : $endpoint;
$value = ( get_option( 'woocommerce_myaccount_edit_address_endpoint', 'edit-address' ) === $endpoint ) ? wc_edit_address_i18n( $value ) : $value;
// Map endpoint to options.
$query_vars = WC()->query->get_query_vars();
$endpoint = ! empty( $query_vars[ $endpoint ] ) ? $query_vars[ $endpoint ] : $endpoint;
$value = ( get_option( 'woocommerce_myaccount_edit_address_endpoint', 'edit-address' ) === $endpoint ) ? wc_edit_address_i18n( $value ) : $value;
if ( get_option( 'permalink_structure' ) ) {
if ( strstr( $permalink, '?' ) ) {
$query_string = '?' . parse_url( $permalink, PHP_URL_QUERY );
$query_string = '?' . wp_parse_url( $permalink, PHP_URL_QUERY );
$permalink = current( explode( '?', $permalink ) );
} else {
$query_string = '';
@ -123,8 +122,8 @@ function wc_nav_menu_items( $items ) {
if ( empty( $item->url ) ) {
continue;
}
$path = parse_url( $item->url, PHP_URL_PATH );
$query = parse_url( $item->url, PHP_URL_QUERY );
$path = wp_parse_url( $item->url, PHP_URL_PATH );
$query = wp_parse_url( $item->url, PHP_URL_QUERY );
if ( strstr( $path, $customer_logout ) || strstr( $query, $customer_logout ) ) {
unset( $items[ $key ] );
@ -141,43 +140,41 @@ add_filter( 'wp_nav_menu_objects', 'wc_nav_menu_items', 10 );
/**
* Fix active class in nav for shop page.
*
* @param array $menu_items
* @param array $menu_items Menu items.
* @return array
*/
function wc_nav_menu_item_classes( $menu_items ) {
if ( ! is_woocommerce() ) {
return $menu_items;
}
$shop_page = (int) wc_get_page_id( 'shop' );
$shop_page = (int) wc_get_page_id( 'shop' );
$page_for_posts = (int) get_option( 'page_for_posts' );
if ( ! empty( $menu_items ) && is_array( $menu_items ) ) {
foreach ( $menu_items as $key => $menu_item ) {
$classes = (array) $menu_item->classes;
$menu_id = (int) $menu_item->object_id;
// Unset active class for blog page
if ( $page_for_posts == $menu_item->object_id ) {
// Unset active class for blog page.
if ( $page_for_posts === $menu_id ) {
$menu_items[ $key ]->current = false;
if ( in_array( 'current_page_parent', $classes ) ) {
unset( $classes[ array_search( 'current_page_parent', $classes ) ] );
if ( in_array( 'current_page_parent', $classes, true ) ) {
unset( $classes[ array_search( 'current_page_parent', $classes, true ) ] );
}
if ( in_array( 'current-menu-item', $classes ) ) {
unset( $classes[ array_search( 'current-menu-item', $classes ) ] );
if ( in_array( 'current-menu-item', $classes, true ) ) {
unset( $classes[ array_search( 'current-menu-item', $classes, true ) ] );
}
// Set active state if this is the shop page link
} elseif ( is_shop() && $shop_page == $menu_item->object_id && 'page' === $menu_item->object ) {
} elseif ( is_shop() && $shop_page === $menu_id && 'page' === $menu_item->object ) {
// Set active state if this is the shop page link.
$menu_items[ $key ]->current = true;
$classes[] = 'current-menu-item';
$classes[] = 'current_page_item';
// Set parent state if this is a product page
} elseif ( is_singular( 'product' ) && $shop_page == $menu_item->object_id ) {
} elseif ( is_singular( 'product' ) && $shop_page === $menu_id ) {
// Set parent state if this is a product page.
$classes[] = 'current_page_parent';
}
@ -193,10 +190,9 @@ add_filter( 'wp_nav_menu_objects', 'wc_nav_menu_item_classes', 2 );
/**
* Fix active class in wp_list_pages for shop page.
*
* https://github.com/woocommerce/woocommerce/issues/177.
* See details in https://github.com/woocommerce/woocommerce/issues/177.
*
* @author Jessor, Peter Sterling
* @param string $pages
* @param string $pages Pages list.
* @return string
*/
function wc_list_pages( $pages ) {

View File

@ -4,9 +4,9 @@
*
* Functions for the templating system.
*
* @author WooThemes
* @author Automattic
* @category Core
* @package WooCommerce/Functions
* @package WooCommerce\Functions
* @version 2.5.0
*/
@ -20,7 +20,7 @@ if ( ! defined( 'ABSPATH' ) ) {
function wc_template_redirect() {
global $wp_query, $wp;
if ( ! empty( $_GET['page_id'] ) && '' === get_option( 'permalink_structure' ) && wc_get_page_id( 'shop' ) == $_GET['page_id'] ) {
if ( ! empty( $_GET['page_id'] ) && '' === get_option( 'permalink_structure' ) && wc_get_page_id( 'shop' ) === absint( $_GET['page_id'] ) ) { // WPCS: input var ok, CSRF ok.
// When default permalinks are enabled, redirect shop page to post type archive url.
wp_safe_redirect( get_post_type_archive_link( 'product' ) );
@ -33,11 +33,7 @@ function wc_template_redirect() {
wp_redirect( wc_get_page_permalink( 'cart' ) );
exit;
} elseif (
isset( $wp->query_vars['customer-logout'] ) &&
! empty( $_REQUEST['_wpnonce'] ) &&
wp_verify_nonce( sanitize_key( $_REQUEST['_wpnonce'] ), 'customer-logout' )
) {
} elseif ( isset( $wp->query_vars['customer-logout'] ) && ! empty( $_REQUEST['_wpnonce'] ) && wp_verify_nonce( sanitize_key( $_REQUEST['_wpnonce'] ), 'customer-logout' ) ) { // WPCS: input var ok, CSRF ok.
// Logout.
wp_redirect( str_replace( '&amp;', '&', wp_logout_url( wc_get_page_permalink( 'myaccount' ) ) ) );
@ -97,8 +93,8 @@ add_action( 'template_redirect', 'wc_send_frame_options_header' );
* @since 2.5.3
*/
function wc_prevent_endpoint_indexing() {
if ( is_wc_endpoint_url() || isset( $_GET['download_file'] ) ) {
@header( 'X-Robots-Tag: noindex' );
if ( is_wc_endpoint_url() || isset( $_GET['download_file'] ) ) { // WPCS: input var ok, CSRF ok.
@header( 'X-Robots-Tag: noindex' ); // @codingStandardsIgnoreLine
}
}
add_action( 'template_redirect', 'wc_prevent_endpoint_indexing' );
@ -142,7 +138,7 @@ function wc_setup_product_data( $post ) {
$the_post = $post;
}
if ( empty( $the_post->post_type ) || ! in_array( $the_post->post_type, array( 'product', 'product_variation' ) ) ) {
if ( empty( $the_post->post_type ) || ! in_array( $the_post->post_type, array( 'product', 'product_variation' ), true ) ) {
return;
}
@ -156,8 +152,6 @@ if ( ! function_exists( 'woocommerce_reset_loop' ) ) {
/**
* Reset the loop's index and columns when we're done outputting a product loop.
*
* @subpackage Loop
*/
function woocommerce_reset_loop() {
$GLOBALS['woocommerce_loop'] = array(
@ -189,6 +183,7 @@ function wc_products_rss_feed() {
if ( $term ) {
$feed = add_query_arg( 'product_cat', $term->slug, get_post_type_archive_feed_link( 'product' ) );
/* translators: %s: category name */
echo '<link rel="alternate" type="application/rss+xml" title="' . esc_attr( sprintf( __( 'New products added to %s', 'woocommerce' ), $term->name ) ) . '" href="' . esc_url( $feed ) . '" />';
}
} elseif ( is_tax( 'product_tag' ) ) {
@ -197,7 +192,8 @@ function wc_products_rss_feed() {
if ( $term ) {
$feed = add_query_arg( 'product_tag', $term->slug, get_post_type_archive_feed_link( 'product' ) );
echo '<link rel="alternate" type="application/rss+xml" title="' . sprintf( esc_attr__( 'New products tagged %s', 'woocommerce' ), urlencode( $term->name ) ) . '" href="' . esc_url( $feed ) . '" />';
/* translators: %s: tag name */
echo '<link rel="alternate" type="application/rss+xml" title="' . sprintf( esc_attr__( 'New products tagged %s', 'woocommerce' ), rawurlencode( $term->name ) ) . '" href="' . esc_url( $feed ) . '" />';
}
}
}
@ -259,7 +255,7 @@ function wc_body_class( $classes ) {
$classes[] = 'woocommerce-demo-store';
}
foreach ( WC()->query->query_vars as $key => $value ) {
foreach ( WC()->query->get_query_vars() as $key => $value ) {
if ( is_wc_endpoint_url( $key ) ) {
$classes[] = 'woocommerce-' . sanitize_html_class( $key );
}
@ -289,7 +285,7 @@ function wc_product_cat_class( $class = '', $category = null ) {
function wc_get_loop_class() {
global $woocommerce_loop;
$woocommerce_loop['loop'] = ! empty( $woocommerce_loop['loop'] ) ? $woocommerce_loop['loop'] + 1 : 1;
$woocommerce_loop['loop'] = ! empty( $woocommerce_loop['loop'] ) ? $woocommerce_loop['loop'] + 1 : 1;
$woocommerce_loop['columns'] = max( 1, ! empty( $woocommerce_loop['columns'] ) ? $woocommerce_loop['columns'] : apply_filters( 'loop_shop_columns', 4 ) );
if ( 0 === ( $woocommerce_loop['loop'] - 1 ) % $woocommerce_loop['columns'] || 1 === $woocommerce_loop['columns'] ) {
@ -331,7 +327,7 @@ function wc_get_product_cat_class( $class = '', $category = null ) {
* @return array
*/
function wc_product_post_class( $classes, $class = '', $post_id = '' ) {
if ( ! $post_id || ! in_array( get_post_type( $post_id ), array( 'product', 'product_variation' ) ) ) {
if ( ! $post_id || ! in_array( get_post_type( $post_id ), array( 'product', 'product_variation' ), true ) ) {
return $classes;
}
@ -379,7 +375,8 @@ function wc_product_post_class( $classes, $class = '', $post_id = '' ) {
}
}
if ( false !== ( $key = array_search( 'hentry', $classes ) ) ) {
$key = array_search( 'hentry', $classes, true );
if ( false !== $key ) {
unset( $classes[ $key ] );
}
@ -398,7 +395,7 @@ function wc_product_post_class( $classes, $class = '', $post_id = '' ) {
*/
function wc_query_string_form_fields( $values = null, $exclude = array(), $current_key = '', $return = false ) {
if ( is_null( $values ) ) {
$values = $_GET;
$values = $_GET; // WPCS: input var ok, CSRF ok.
}
$html = '';
@ -440,13 +437,13 @@ if ( ! function_exists( 'woocommerce_content' ) ) {
if ( is_singular( 'product' ) ) {
while ( have_posts() ) : the_post();
while ( have_posts() ) :
the_post();
wc_get_template_part( 'content', 'single-product' );
endwhile;
} else { ?>
} else {
?>
<?php if ( apply_filters( 'woocommerce_show_page_title', true ) ) : ?>
@ -464,7 +461,8 @@ if ( ! function_exists( 'woocommerce_content' ) ) {
<?php woocommerce_product_subcategories(); ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php while ( have_posts() ) : ?>
<?php the_post(); ?>
<?php wc_get_template_part( 'content', 'product' ); ?>
@ -474,12 +472,17 @@ if ( ! function_exists( 'woocommerce_content' ) ) {
<?php do_action( 'woocommerce_after_shop_loop' ); ?>
<?php elseif ( ! woocommerce_product_subcategories( array( 'before' => woocommerce_product_loop_start( false ), 'after' => woocommerce_product_loop_end( false ) ) ) ) : ?>
<?php
elseif ( ! woocommerce_product_subcategories( array(
'before' => woocommerce_product_loop_start( false ),
'after' => woocommerce_product_loop_end( false ),
) ) ) :
?>
<?php do_action( 'woocommerce_no_products_found' ); ?>
<?php endif;
<?php
endif;
}
}
}
@ -552,9 +555,11 @@ if ( ! function_exists( 'woocommerce_page_title' ) ) {
function woocommerce_page_title( $echo = true ) {
if ( is_search() ) {
/* translators: %s: search query */
$page_title = sprintf( __( 'Search results: &ldquo;%s&rdquo;', 'woocommerce' ), get_search_query() );
if ( get_query_var( 'paged' ) ) {
/* translators: %s: page number */
$page_title .= sprintf( __( '&nbsp;&ndash; Page %s', 'woocommerce' ), get_query_var( 'paged' ) );
}
} elseif ( is_tax() ) {
@ -679,8 +684,6 @@ if ( ! function_exists( 'woocommerce_taxonomy_archive_description' ) ) {
/**
* Show an archive description on taxonomy archives.
*
* @subpackage Archives
*/
function woocommerce_taxonomy_archive_description() {
if ( is_product_taxonomy() && 0 === absint( get_query_var( 'paged' ) ) ) {
@ -720,8 +723,6 @@ if ( ! function_exists( 'woocommerce_template_loop_add_to_cart' ) ) {
/**
* Get the add to cart template for the loop.
*
* @subpackage Loop
*
* @param array $args Arguments.
*/
function woocommerce_template_loop_add_to_cart( $args = array() ) {
@ -731,10 +732,10 @@ if ( ! function_exists( 'woocommerce_template_loop_add_to_cart' ) ) {
$defaults = array(
'quantity' => 1,
'class' => implode( ' ', array_filter( array(
'button',
'product_type_' . $product->get_type(),
$product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
$product->supports( 'ajax_add_to_cart' ) ? 'ajax_add_to_cart' : '',
'button',
'product_type_' . $product->get_type(),
$product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
$product->supports( 'ajax_add_to_cart' ) ? 'ajax_add_to_cart' : '',
) ) ),
);
@ -748,8 +749,6 @@ if ( ! function_exists( 'woocommerce_template_loop_product_thumbnail' ) ) {
/**
* Get the product thumbnail for the loop.
*
* @subpackage Loop
*/
function woocommerce_template_loop_product_thumbnail() {
echo woocommerce_get_product_thumbnail(); // WPCS: XSS ok.
@ -759,8 +758,6 @@ if ( ! function_exists( 'woocommerce_template_loop_price' ) ) {
/**
* Get the product price for the loop.
*
* @subpackage Loop
*/
function woocommerce_template_loop_price() {
wc_get_template( 'loop/price.php' );
@ -770,8 +767,6 @@ if ( ! function_exists( 'woocommerce_template_loop_rating' ) ) {
/**
* Display the average rating in the loop.
*
* @subpackage Loop
*/
function woocommerce_template_loop_rating() {
wc_get_template( 'loop/rating.php' );
@ -781,8 +776,6 @@ if ( ! function_exists( 'woocommerce_show_product_loop_sale_flash' ) ) {
/**
* Get the sale flash for the loop.
*
* @subpackage Loop
*/
function woocommerce_show_product_loop_sale_flash() {
wc_get_template( 'loop/sale-flash.php' );
@ -794,7 +787,6 @@ if ( ! function_exists( 'woocommerce_get_product_thumbnail' ) ) {
/**
* Get the product thumbnail, or the placeholder if not set.
*
* @subpackage Loop
* @param string $size (default: 'woocommerce_thumbnail').
* @param int $deprecated1 Deprecated since WooCommerce 2.0 (default: 0).
* @param int $deprecated2 Deprecated since WooCommerce 2.0 (default: 0).
@ -813,8 +805,6 @@ if ( ! function_exists( 'woocommerce_result_count' ) ) {
/**
* Output the result count text (Showing x - x of x results).
*
* @subpackage Loop
*/
function woocommerce_result_count() {
wc_get_template( 'loop/result-count.php' );
@ -825,8 +815,6 @@ if ( ! function_exists( 'woocommerce_catalog_ordering' ) ) {
/**
* Output the product sorting options.
*
* @subpackage Loop
*/
function woocommerce_catalog_ordering() {
global $wp_query;
@ -835,7 +823,7 @@ if ( ! function_exists( 'woocommerce_catalog_ordering' ) ) {
return;
}
$orderby = isset( $_GET['orderby'] ) ? wc_clean( wp_unslash( $_GET['orderby'] ) ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
$orderby = isset( $_GET['orderby'] ) ? wc_clean( wp_unslash( $_GET['orderby'] ) ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) ); // WPCS: input var ok, CSRF ok.
$show_default_orderby = 'menu_order' === apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
$catalog_orderby_options = apply_filters( 'woocommerce_catalog_orderby', array(
'menu_order' => __( 'Default sorting', 'woocommerce' ),
@ -847,7 +835,9 @@ if ( ! function_exists( 'woocommerce_catalog_ordering' ) ) {
) );
if ( $wp_query->is_search() ) {
$catalog_orderby_options = array_merge( array( 'relevance' => __( 'Relevance', 'woocommerce' ) ), $catalog_orderby_options );
$catalog_orderby_options = array_merge( array(
'relevance' => __( 'Relevance', 'woocommerce' ),
), $catalog_orderby_options );
unset( $catalog_orderby_options['menu_order'] );
if ( 'menu_order' === $orderby ) {
$orderby = 'relevance';
@ -862,7 +852,11 @@ if ( ! function_exists( 'woocommerce_catalog_ordering' ) ) {
unset( $catalog_orderby_options['rating'] );
}
wc_get_template( 'loop/orderby.php', array( 'catalog_orderby_options' => $catalog_orderby_options, 'orderby' => $orderby, 'show_default_orderby' => $show_default_orderby ) );
wc_get_template( 'loop/orderby.php', array(
'catalog_orderby_options' => $catalog_orderby_options,
'orderby' => $orderby,
'show_default_orderby' => $show_default_orderby,
) );
}
}
@ -870,8 +864,6 @@ if ( ! function_exists( 'woocommerce_pagination' ) ) {
/**
* Output the pagination.
*
* @subpackage Loop
*/
function woocommerce_pagination() {
wc_get_template( 'loop/pagination.php' );
@ -886,8 +878,6 @@ if ( ! function_exists( 'woocommerce_show_product_images' ) ) {
/**
* Output the product image before the single product summary.
*
* @subpackage Product
*/
function woocommerce_show_product_images() {
wc_get_template( 'single-product/product-image.php' );
@ -897,8 +887,6 @@ if ( ! function_exists( 'woocommerce_show_product_thumbnails' ) ) {
/**
* Output the product thumbnails.
*
* @subpackage Product
*/
function woocommerce_show_product_thumbnails() {
wc_get_template( 'single-product/product-thumbnails.php' );
@ -909,8 +897,6 @@ if ( ! function_exists( 'woocommerce_output_product_data_tabs' ) ) {
/**
* Output the product tabs.
*
* @subpackage Product/Tabs
*/
function woocommerce_output_product_data_tabs() {
wc_get_template( 'single-product/tabs/tabs.php' );
@ -920,8 +906,6 @@ if ( ! function_exists( 'woocommerce_template_single_title' ) ) {
/**
* Output the product title.
*
* @subpackage Product
*/
function woocommerce_template_single_title() {
wc_get_template( 'single-product/title.php' );
@ -931,8 +915,6 @@ if ( ! function_exists( 'woocommerce_template_single_rating' ) ) {
/**
* Output the product rating.
*
* @subpackage Product
*/
function woocommerce_template_single_rating() {
if ( post_type_supports( 'product', 'comments' ) ) {
@ -944,8 +926,6 @@ if ( ! function_exists( 'woocommerce_template_single_price' ) ) {
/**
* Output the product price.
*
* @subpackage Product
*/
function woocommerce_template_single_price() {
wc_get_template( 'single-product/price.php' );
@ -955,8 +935,6 @@ if ( ! function_exists( 'woocommerce_template_single_excerpt' ) ) {
/**
* Output the product short description (excerpt).
*
* @subpackage Product
*/
function woocommerce_template_single_excerpt() {
wc_get_template( 'single-product/short-description.php' );
@ -966,8 +944,6 @@ if ( ! function_exists( 'woocommerce_template_single_meta' ) ) {
/**
* Output the product meta.
*
* @subpackage Product
*/
function woocommerce_template_single_meta() {
wc_get_template( 'single-product/meta.php' );
@ -977,8 +953,6 @@ if ( ! function_exists( 'woocommerce_template_single_sharing' ) ) {
/**
* Output the product sharing.
*
* @subpackage Product
*/
function woocommerce_template_single_sharing() {
wc_get_template( 'single-product/share.php' );
@ -988,8 +962,6 @@ if ( ! function_exists( 'woocommerce_show_product_sale_flash' ) ) {
/**
* Output the product sale flash.
*
* @subpackage Product
*/
function woocommerce_show_product_sale_flash() {
wc_get_template( 'single-product/sale-flash.php' );
@ -1000,8 +972,6 @@ if ( ! function_exists( 'woocommerce_template_single_add_to_cart' ) ) {
/**
* Trigger the single product add to cart action.
*
* @subpackage Product
*/
function woocommerce_template_single_add_to_cart() {
global $product;
@ -1012,8 +982,6 @@ if ( ! function_exists( 'woocommerce_simple_add_to_cart' ) ) {
/**
* Output the simple product add to cart area.
*
* @subpackage Product
*/
function woocommerce_simple_add_to_cart() {
wc_get_template( 'single-product/add-to-cart/simple.php' );
@ -1023,8 +991,6 @@ if ( ! function_exists( 'woocommerce_grouped_add_to_cart' ) ) {
/**
* Output the grouped product add to cart area.
*
* @subpackage Product
*/
function woocommerce_grouped_add_to_cart() {
global $product;
@ -1044,8 +1010,6 @@ if ( ! function_exists( 'woocommerce_variable_add_to_cart' ) ) {
/**
* Output the variable product add to cart area.
*
* @subpackage Product
*/
function woocommerce_variable_add_to_cart() {
global $product;
@ -1068,8 +1032,6 @@ if ( ! function_exists( 'woocommerce_external_add_to_cart' ) ) {
/**
* Output the external product add to cart area.
*
* @subpackage Product
*/
function woocommerce_external_add_to_cart() {
global $product;
@ -1139,8 +1101,6 @@ if ( ! function_exists( 'woocommerce_product_description_tab' ) ) {
/**
* Output the description tab content.
*
* @subpackage Product/Tabs
*/
function woocommerce_product_description_tab() {
wc_get_template( 'single-product/tabs/description.php' );
@ -1150,8 +1110,6 @@ if ( ! function_exists( 'woocommerce_product_additional_information_tab' ) ) {
/**
* Output the attributes tab content.
*
* @subpackage Product/Tabs
*/
function woocommerce_product_additional_information_tab() {
wc_get_template( 'single-product/tabs/additional-information.php' );
@ -1162,8 +1120,7 @@ if ( ! function_exists( 'woocommerce_product_reviews_tab' ) ) {
/**
* Output the reviews tab content.
*
* @deprecated 2.4.0 Unused
* @subpackage Product/Tabs
* @deprecated 2.4.0 Unused.
*/
function woocommerce_product_reviews_tab() {
wc_deprecated_function( 'woocommerce_product_reviews_tab', '2.4' );
@ -1202,6 +1159,7 @@ if ( ! function_exists( 'woocommerce_default_product_tabs' ) ) {
// Reviews tab - shows comments.
if ( comments_open() ) {
$tabs['reviews'] = array(
/* translators: %s: reviews count */
'title' => sprintf( __( 'Reviews (%d)', 'woocommerce' ), $product->get_review_count() ),
'priority' => 30,
'callback' => 'comments_template',
@ -1224,7 +1182,7 @@ if ( ! function_exists( 'woocommerce_sort_product_tabs' ) ) {
// Make sure the $tabs parameter is an array.
if ( ! is_array( $tabs ) ) {
trigger_error( 'Function woocommerce_sort_product_tabs() expects an array as the first parameter. Defaulting to empty array.' );
trigger_error( 'Function woocommerce_sort_product_tabs() expects an array as the first parameter. Defaulting to empty array.' ); // @codingStandardsIgnoreLine
$tabs = array();
}
@ -1256,14 +1214,17 @@ if ( ! function_exists( 'woocommerce_comments' ) ) {
/**
* Output the Review comments template.
*
* @subpackage Product
* @param WP_Comment $comment Comment object.
* @param array $args Arguments.
* @param int $depth Depth.
*/
function woocommerce_comments( $comment, $args, $depth ) {
$GLOBALS['comment'] = $comment; // WPCS: override ok.
wc_get_template( 'single-product/review.php', array( 'comment' => $comment, 'args' => $args, 'depth' => $depth ) );
wc_get_template( 'single-product/review.php', array(
'comment' => $comment,
'args' => $args,
'depth' => $depth,
) );
}
}
@ -1319,15 +1280,13 @@ if ( ! function_exists( 'woocommerce_output_related_products' ) ) {
/**
* Output the related products.
*
* @subpackage Product
*/
function woocommerce_output_related_products() {
$args = array(
'posts_per_page' => 4,
'columns' => 4,
'orderby' => 'rand',
'posts_per_page' => 4,
'columns' => 4,
'orderby' => 'rand',
);
woocommerce_related_products( apply_filters( 'woocommerce_output_related_products_args', $args ) );
@ -1420,8 +1379,6 @@ if ( ! function_exists( 'woocommerce_shipping_calculator' ) ) {
/**
* Output the cart shipping calculator.
*
* @subpackage Cart
*/
function woocommerce_shipping_calculator() {
wc_get_template( 'cart/shipping-calculator.php' );
@ -1432,8 +1389,6 @@ if ( ! function_exists( 'woocommerce_cart_totals' ) ) {
/**
* Output the cart totals.
*
* @subpackage Cart
*/
function woocommerce_cart_totals() {
if ( is_checkout() ) {
@ -1486,8 +1441,6 @@ if ( ! function_exists( 'woocommerce_button_proceed_to_checkout' ) ) {
/**
* Output the proceed to checkout button.
*
* @subpackage Cart
*/
function woocommerce_button_proceed_to_checkout() {
wc_get_template( 'cart/proceed-to-checkout-button.php' );
@ -1498,8 +1451,6 @@ if ( ! function_exists( 'woocommerce_widget_shopping_cart_button_view_cart' ) )
/**
* Output the view cart button.
*
* @subpackage Cart
*/
function woocommerce_widget_shopping_cart_button_view_cart() {
echo '<a href="' . esc_url( wc_get_cart_url() ) . '" class="button wc-forward">' . esc_html__( 'View cart', 'woocommerce' ) . '</a>';
@ -1510,8 +1461,6 @@ if ( ! function_exists( 'woocommerce_widget_shopping_cart_proceed_to_checkout' )
/**
* Output the proceed to checkout button.
*
* @subpackage Cart
*/
function woocommerce_widget_shopping_cart_proceed_to_checkout() {
echo '<a href="' . esc_url( wc_get_checkout_url() ) . '" class="button checkout wc-forward">' . esc_html__( 'Checkout', 'woocommerce' ) . '</a>';
@ -1546,7 +1495,6 @@ if ( ! function_exists( 'woocommerce_login_form' ) ) {
/**
* Output the WooCommerce Login Form.
*
* @subpackage Forms
* @param array $args Arguments.
*/
function woocommerce_login_form( $args = array() ) {
@ -1567,11 +1515,11 @@ if ( ! function_exists( 'woocommerce_checkout_login_form' ) ) {
/**
* Output the WooCommerce Checkout Login Form.
*
* @subpackage Checkout
*/
function woocommerce_checkout_login_form() {
wc_get_template( 'checkout/form-login.php', array( 'checkout' => WC()->checkout() ) );
wc_get_template( 'checkout/form-login.php', array(
'checkout' => WC()->checkout(),
) );
}
}
@ -1616,12 +1564,12 @@ if ( ! function_exists( 'woocommerce_order_review' ) ) {
/**
* Output the Order review table for the checkout.
*
* @subpackage Checkout
*
* @param bool $deprecated Deprecated param.
*/
function woocommerce_order_review( $deprecated = false ) {
wc_get_template( 'checkout/review-order.php', array( 'checkout' => WC()->checkout() ) );
wc_get_template( 'checkout/review-order.php', array(
'checkout' => WC()->checkout(),
) );
}
}
@ -1629,8 +1577,6 @@ if ( ! function_exists( 'woocommerce_checkout_payment' ) ) {
/**
* Output the Payment Methods on the checkout.
*
* @subpackage Checkout
*/
function woocommerce_checkout_payment() {
if ( WC()->cart->needs_payment() ) {
@ -1652,11 +1598,11 @@ if ( ! function_exists( 'woocommerce_checkout_coupon_form' ) ) {
/**
* Output the Coupon form for the checkout.
*
* @subpackage Checkout
*/
function woocommerce_checkout_coupon_form() {
wc_get_template( 'checkout/form-coupon.php', array( 'checkout' => WC()->checkout() ) );
wc_get_template( 'checkout/form-coupon.php', array(
'checkout' => WC()->checkout(),
) );
}
}
@ -1665,7 +1611,6 @@ if ( ! function_exists( 'woocommerce_products_will_display' ) ) {
/**
* Check if we will be showing products or not (and not sub-categories only).
*
* @subpackage Loop
* @return bool
*/
function woocommerce_products_will_display() {
@ -1687,19 +1632,18 @@ if ( ! function_exists( 'woocommerce_products_will_display' ) ) {
if ( is_product_category() ) {
switch ( get_woocommerce_term_meta( $term->term_id, 'display_type', true ) ) {
case 'subcategories' :
case 'subcategories':
// Nothing - we want to continue to see if there are products/subcats.
break;
case 'products' :
case 'both' :
break;
case 'products':
case 'both':
return true;
break;
default :
default:
// Default - no setting.
if ( get_option( 'woocommerce_category_archive_display' ) != 'subcategories' ) {
if ( get_option( 'woocommerce_category_archive_display' ) !== 'subcategories' ) {
return true;
}
break;
break;
}
}
@ -1708,10 +1652,11 @@ if ( ! function_exists( 'woocommerce_products_will_display' ) ) {
return true;
}
$transient_name = 'wc_products_will_display_' . $term->term_id . '_' . WC_Cache_Helper::get_transient_version( 'product_query' );
$transient_name = 'wc_products_will_display_' . $term->term_id . '_' . WC_Cache_Helper::get_transient_version( 'product_query' );
$products_will_display = get_transient( $transient_name );
if ( false === ( $products_will_display = get_transient( $transient_name ) ) ) {
$has_children = $wpdb->get_col( $wpdb->prepare( "SELECT term_id FROM {$wpdb->term_taxonomy} WHERE parent = %d AND taxonomy = %s", $term->term_id, $term->taxonomy ) ); // WPCS: cache ok.
if ( false === $products_will_display ) {
$has_children = $wpdb->get_col( $wpdb->prepare( "SELECT term_id FROM {$wpdb->term_taxonomy} WHERE parent = %d AND taxonomy = %s", $term->term_id, $term->taxonomy ) ); // WPCS: db call ok, cache ok.
if ( $has_children ) {
// Check terms have products inside - parents first. If products are found inside, subcats will be shown instead of products so we can return false.
@ -1744,7 +1689,6 @@ if ( ! function_exists( 'woocommerce_product_subcategories' ) ) {
/**
* Display product sub categories as thumbnails.
*
* @subpackage Loop
* @param array $args Arguments.
* @return null|boolean
*/
@ -1777,21 +1721,20 @@ if ( ! function_exists( 'woocommerce_product_subcategories' ) ) {
}
// Find the category + category parent, if applicable.
$term = get_queried_object();
$parent_id = empty( $term->term_id ) ? 0 : $term->term_id;
$term = get_queried_object();
$parent_id = empty( $term->term_id ) ? 0 : $term->term_id;
if ( is_product_category() ) {
$display_type = get_woocommerce_term_meta( $term->term_id, 'display_type', true );
switch ( $display_type ) {
case 'products' :
case 'products':
return;
break;
case '' :
case '':
if ( '' === get_option( 'woocommerce_category_archive_display' ) ) {
return;
}
break;
break;
}
}
@ -1806,7 +1749,9 @@ if ( ! function_exists( 'woocommerce_product_subcategories' ) ) {
) ) );
if ( apply_filters( 'woocommerce_product_subcategories_hide_empty', true ) ) {
$product_categories = wp_list_filter( $product_categories, array( 'count' => 0 ), 'NOT' );
$product_categories = wp_list_filter( $product_categories, array(
'count' => 0,
), 'NOT' );
}
if ( $product_categories ) {
@ -1823,16 +1768,16 @@ if ( ! function_exists( 'woocommerce_product_subcategories' ) ) {
$display_type = get_woocommerce_term_meta( $term->term_id, 'display_type', true );
switch ( $display_type ) {
case 'subcategories' :
case 'subcategories':
$wp_query->post_count = 0;
$wp_query->max_num_pages = 0;
break;
case '' :
break;
case '':
if ( 'subcategories' === get_option( 'woocommerce_category_archive_display' ) ) {
$wp_query->post_count = 0;
$wp_query->max_num_pages = 0;
}
break;
break;
}
}
@ -1854,12 +1799,11 @@ if ( ! function_exists( 'woocommerce_subcategory_thumbnail' ) ) {
* Show subcategory thumbnails.
*
* @param mixed $category Category.
* @subpackage Loop
*/
function woocommerce_subcategory_thumbnail( $category ) {
$small_thumbnail_size = apply_filters( 'subcategory_archive_thumbnail_size', 'woocommerce_thumbnail' );
$dimensions = wc_get_image_size( $small_thumbnail_size );
$thumbnail_id = get_woocommerce_term_meta( $category->term_id, 'thumbnail_id', true );
$small_thumbnail_size = apply_filters( 'subcategory_archive_thumbnail_size', 'woocommerce_thumbnail' );
$dimensions = wc_get_image_size( $small_thumbnail_size );
$thumbnail_id = get_woocommerce_term_meta( $category->term_id, 'thumbnail_id', true );
if ( $thumbnail_id ) {
$image = wp_get_attachment_image_src( $thumbnail_id, $small_thumbnail_size );
@ -1868,7 +1812,8 @@ if ( ! function_exists( 'woocommerce_subcategory_thumbnail' ) ) {
$image_sizes = function_exists( 'wp_get_attachment_image_sizes' ) ? wp_get_attachment_image_sizes( $thumbnail_id, $small_thumbnail_size ) : false;
} else {
$image = wc_placeholder_img_src();
$image_srcset = $image_sizes = false;
$image_srcset = false;
$image_sizes = false;
}
if ( $image ) {
@ -1892,7 +1837,6 @@ if ( ! function_exists( 'woocommerce_order_details_table' ) ) {
* Displays order details in a table.
*
* @param mixed $order_id Order ID.
* @subpackage Orders
*/
function woocommerce_order_details_table( $order_id ) {
if ( ! $order_id ) {
@ -1929,7 +1873,6 @@ if ( ! function_exists( 'woocommerce_order_again_button' ) ) {
* Display an 'order again' button on the view order page.
*
* @param object $order Order.
* @subpackage Orders
*/
function woocommerce_order_again_button( $order ) {
if ( ! $order || ! $order->has_status( apply_filters( 'woocommerce_valid_order_statuses_for_order_again', array( 'completed' ) ) ) || ! is_user_logged_in() ) {
@ -1949,12 +1892,9 @@ if ( ! function_exists( 'woocommerce_form_field' ) ) {
/**
* Outputs a checkout/address form field.
*
* @subpackage Forms
*
* @param string $key Key.
* @param mixed $args Arguments.
* @param string $value (default: null).
*
* @return string
*/
function woocommerce_form_field( $key, $args, $value = null ) {
@ -2031,8 +1971,7 @@ if ( ! function_exists( 'woocommerce_form_field' ) ) {
$field_container = '<p class="form-row %1$s" id="%2$s" data-priority="' . esc_attr( $sort ) . '">%3$s</p>';
switch ( $args['type'] ) {
case 'country' :
case 'country':
$countries = 'shipping_country' === $key ? WC()->countries->get_shipping_countries() : WC()->countries->get_allowed_countries();
if ( 1 === count( $countries ) ) {
@ -2056,7 +1995,7 @@ if ( ! function_exists( 'woocommerce_form_field' ) ) {
}
break;
case 'state' :
case 'state':
/* Get country this state field is representing */
$for_country = isset( $args['country'] ) ? $args['country'] : WC()->checkout->get_value( 'billing_state' === $key ? 'billing_country' : 'shipping_country' );
$states = WC()->countries->get_states( $for_country );
@ -2085,30 +2024,27 @@ if ( ! function_exists( 'woocommerce_form_field' ) ) {
}
break;
case 'textarea' :
case 'textarea':
$field .= '<textarea name="' . esc_attr( $key ) . '" class="input-text ' . esc_attr( implode( ' ', $args['input_class'] ) ) . '" id="' . esc_attr( $args['id'] ) . '" placeholder="' . esc_attr( $args['placeholder'] ) . '" ' . ( empty( $args['custom_attributes']['rows'] ) ? ' rows="2"' : '' ) . ( empty( $args['custom_attributes']['cols'] ) ? ' cols="5"' : '' ) . implode( ' ', $custom_attributes ) . '>' . esc_textarea( $value ) . '</textarea>';
break;
case 'checkbox' :
case 'checkbox':
$field = '<label class="checkbox ' . implode( ' ', $args['label_class'] ) . '" ' . implode( ' ', $custom_attributes ) . '>
<input type="' . esc_attr( $args['type'] ) . '" class="input-checkbox ' . esc_attr( implode( ' ', $args['input_class'] ) ) . '" name="' . esc_attr( $key ) . '" id="' . esc_attr( $args['id'] ) . '" value="1" ' . checked( $value, 1, false ) . ' /> '
. $args['label'] . $required . '</label>';
break;
case 'password' :
case 'text' :
case 'email' :
case 'tel' :
case 'number' :
case 'password':
case 'text':
case 'email':
case 'tel':
case 'number':
$field .= '<input type="' . esc_attr( $args['type'] ) . '" class="input-text ' . esc_attr( implode( ' ', $args['input_class'] ) ) . '" name="' . esc_attr( $key ) . '" id="' . esc_attr( $args['id'] ) . '" placeholder="' . esc_attr( $args['placeholder'] ) . '" value="' . esc_attr( $value ) . '" ' . implode( ' ', $custom_attributes ) . ' />';
break;
case 'select' :
$options = $field = '';
case 'select':
$field = '';
$options = '';
if ( ! empty( $args['options'] ) ) {
foreach ( $args['options'] as $option_key => $option_text ) {
@ -2128,8 +2064,7 @@ if ( ! function_exists( 'woocommerce_form_field' ) ) {
}
break;
case 'radio' :
case 'radio':
$label_id = current( array_keys( $args['options'] ) );
if ( ! empty( $args['options'] ) ) {
@ -2145,7 +2080,7 @@ if ( ! function_exists( 'woocommerce_form_field' ) ) {
if ( ! empty( $field ) ) {
$field_html = '';
if ( $args['label'] && 'checkbox' != $args['type'] ) {
if ( $args['label'] && 'checkbox' !== $args['type'] ) {
$field_html .= '<label for="' . esc_attr( $label_id ) . '" class="' . esc_attr( implode( ' ', $args['label_class'] ) ) . '">' . $args['label'] . $required . '</label>';
}
@ -2181,11 +2116,10 @@ if ( ! function_exists( 'get_product_search_form' ) ) {
*
* The default searchform uses html5.
*
* @subpackage Forms
* @param bool $echo (default: true).
* @return string
*/
function get_product_search_form( $echo = true ) {
function get_product_search_form( $echo = true ) {
global $product_search_form_index;
ob_start();
@ -2263,7 +2197,7 @@ if ( ! function_exists( 'wc_dropdown_variation_attribute_options' ) ) {
'options' => false,
'attribute' => false,
'product' => false,
'selected' => false,
'selected' => false,
'name' => '',
'id' => '',
'class' => '',
@ -2290,10 +2224,12 @@ if ( ! function_exists( 'wc_dropdown_variation_attribute_options' ) ) {
if ( ! empty( $options ) ) {
if ( $product && taxonomy_exists( $attribute ) ) {
// Get terms if this is a taxonomy - ordered. We need the names too.
$terms = wc_get_product_terms( $product->get_id(), $attribute, array( 'fields' => 'all' ) );
$terms = wc_get_product_terms( $product->get_id(), $attribute, array(
'fields' => 'all',
) );
foreach ( $terms as $term ) {
if ( in_array( $term->slug, $options ) ) {
if ( in_array( $term->slug, $options, true ) ) {
$html .= '<option value="' . esc_attr( $term->slug ) . '" ' . selected( sanitize_title( $args['selected'] ), $term->slug, false ) . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $term->name ) ) . '</option>';
}
}
@ -2360,7 +2296,11 @@ if ( ! function_exists( 'woocommerce_account_orders' ) ) {
*/
function woocommerce_account_orders( $current_page ) {
$current_page = empty( $current_page ) ? 1 : absint( $current_page );
$customer_orders = wc_get_orders( apply_filters( 'woocommerce_my_account_my_orders_query', array( 'customer' => get_current_user_id(), 'page' => $current_page, 'paginate' => true ) ) );
$customer_orders = wc_get_orders( apply_filters( 'woocommerce_my_account_my_orders_query', array(
'customer' => get_current_user_id(),
'page' => $current_page,
'paginate' => true,
) ) );
wc_get_template(
'myaccount/orders.php',
@ -2504,10 +2444,10 @@ if ( ! function_exists( 'wc_display_item_meta' ) ) {
$html = '';
$args = wp_parse_args( $args, array(
'before' => '<ul class="wc-item-meta"><li>',
'after' => '</li></ul>',
'separator' => '</li><li>',
'echo' => true,
'autop' => false,
'after' => '</li></ul>',
'separator' => '</li><li>',
'echo' => true,
'autop' => false,
) );
foreach ( $item->get_formatted_meta_data() as $meta_id => $meta ) {
@ -2549,7 +2489,9 @@ if ( ! function_exists( 'wc_display_item_downloads' ) ) {
'show_url' => false,
) );
if ( is_object( $item ) && $item->is_type( 'line_item' ) && ( $downloads = $item->get_item_downloads() ) ) {
$downloads = is_object( $item ) && $item->is_type( 'line_item' ) ? $item->get_item_downloads() : array();
if ( $downloads ) {
$i = 0;
foreach ( $downloads as $file ) {
$i ++;
@ -2557,6 +2499,7 @@ if ( ! function_exists( 'wc_display_item_downloads' ) ) {
if ( $args['show_url'] ) {
$strings[] = '<strong class="wc-item-download-label">' . esc_html( $file['name'] ) . ':</strong> ' . esc_html( $file['download_url'] );
} else {
/* translators: %d: downloads count */
$prefix = count( $downloads ) > 1 ? sprintf( __( 'Download %d', 'woocommerce' ), $i ) : __( 'Download', 'woocommerce' );
$strings[] = '<strong class="wc-item-download-label">' . $prefix . ':</strong> <a href="' . esc_url( $file['download_url'] ) . '" target="_blank">' . esc_html( $file['name'] ) . '</a>';
}