Fixed coding standards

This commit is contained in:
Claudio Sanches 2017-08-25 19:25:19 -03:00
parent 32ea76f09f
commit b032a82381
2 changed files with 70 additions and 133 deletions

View File

@ -1,17 +1,19 @@
<?php <?php
/**
* Shortcodes
*
* @author Automattic
* @category Class
* @package WooCommerce/Classes
* @version 3.2.0
*/
if ( ! defined( 'ABSPATH' ) ) { if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly exit;
} }
/** /**
* WC_Shortcodes class * WooCommerce Shortcodes class.
*
* @class WC_Shortcodes
* @version 2.1.0
* @package WooCommerce/Classes
* @category Class
* @author WooThemes
*/ */
class WC_Shortcodes { class WC_Shortcodes {
@ -45,22 +47,22 @@ class WC_Shortcodes {
add_shortcode( apply_filters( "{$shortcode}_shortcode_tag", $shortcode ), $function ); add_shortcode( apply_filters( "{$shortcode}_shortcode_tag", $shortcode ), $function );
} }
// Alias for pre 2.1 compatibility // Alias for pre 2.1 compatibility.
add_shortcode( 'woocommerce_messages', __CLASS__ . '::shop_messages' ); add_shortcode( 'woocommerce_messages', __CLASS__ . '::shop_messages' );
} }
/** /**
* Shortcode Wrapper. * Shortcode Wrapper.
* *
* @param string[] $function * @param string[] $function Callback function.
* @param array $atts (default: array()) * @param array $atts Attributes. Default to empty array.
* @param array $wrapper * @param array $wrapper Customer wrapper data.
* *
* @return string * @return string
*/ */
public static function shortcode_wrapper( public static function shortcode_wrapper(
$function, $function,
$atts = array(), $atts = array(),
$wrapper = array( $wrapper = array(
'class' => 'woocommerce', 'class' => 'woocommerce',
'before' => null, 'before' => null,
@ -69,68 +71,15 @@ class WC_Shortcodes {
) { ) {
ob_start(); ob_start();
// @codingStandardsIgnoreStart
echo empty( $wrapper['before'] ) ? '<div class="' . esc_attr( $wrapper['class'] ) . '">' : $wrapper['before']; echo empty( $wrapper['before'] ) ? '<div class="' . esc_attr( $wrapper['class'] ) . '">' : $wrapper['before'];
call_user_func( $function, $atts ); call_user_func( $function, $atts );
echo empty( $wrapper['after'] ) ? '</div>' : $wrapper['after']; echo empty( $wrapper['after'] ) ? '</div>' : $wrapper['after'];
// @codingStandardsIgnoreEnd
return ob_get_clean(); return ob_get_clean();
} }
/**
* Loop over found products.
* @param array $query_args
* @param array $atts
* @param string $loop_name
* @return string
*/
private static function product_loop( $query_args, $atts, $loop_name ) {
global $woocommerce_loop;
$columns = absint( $atts['columns'] );
$woocommerce_loop['columns'] = $columns;
$woocommerce_loop['name'] = $loop_name;
$query_args = apply_filters( 'woocommerce_shortcode_products_query', $query_args, $atts, $loop_name );
$transient_name = 'wc_loop' . substr( md5( json_encode( $query_args ) . $loop_name ), 28 ) . WC_Cache_Helper::get_transient_version( 'product_query' );
$products = get_transient( $transient_name );
if ( false === $products || ! is_a( $products, 'WP_Query' ) ) {
$products = new WP_Query( $query_args );
set_transient( $transient_name, $products, DAY_IN_SECONDS * 30 );
}
ob_start();
if ( $products->have_posts() ) {
// Prime caches before grabbing objects.
update_post_caches( $products->posts, array( 'product', 'product_variation' ) );
?>
<?php do_action( "woocommerce_shortcode_before_{$loop_name}_loop", $atts ); ?>
<?php woocommerce_product_loop_start(); ?>
<?php while ( $products->have_posts() ) : $products->the_post(); ?>
<?php wc_get_template_part( 'content', 'product' ); ?>
<?php endwhile; // end of the loop. ?>
<?php woocommerce_product_loop_end(); ?>
<?php do_action( "woocommerce_shortcode_after_{$loop_name}_loop", $atts ); ?>
<?php
} else {
do_action( "woocommerce_shortcode_{$loop_name}_loop_no_results", $atts );
}
woocommerce_reset_loop();
wp_reset_postdata();
return '<div class="woocommerce columns-' . $columns . '">' . ob_get_clean() . '</div>';
}
/** /**
* Cart page shortcode. * Cart page shortcode.
* *
@ -143,7 +92,7 @@ class WC_Shortcodes {
/** /**
* Checkout page shortcode. * Checkout page shortcode.
* *
* @param mixed $atts * @param array $atts Attributes.
* @return string * @return string
*/ */
public static function checkout( $atts ) { public static function checkout( $atts ) {
@ -153,7 +102,7 @@ class WC_Shortcodes {
/** /**
* Order tracking page shortcode. * Order tracking page shortcode.
* *
* @param mixed $atts * @param array $atts Attributes.
* @return string * @return string
*/ */
public static function order_tracking( $atts ) { public static function order_tracking( $atts ) {
@ -163,7 +112,7 @@ class WC_Shortcodes {
/** /**
* My account page shortcode. * My account page shortcode.
* *
* @param mixed $atts * @param array $atts Attributes.
* @return string * @return string
*/ */
public static function my_account( $atts ) { public static function my_account( $atts ) {
@ -173,7 +122,7 @@ class WC_Shortcodes {
/** /**
* List products in a category shortcode. * List products in a category shortcode.
* *
* @param array $atts * @param array $atts Attributes.
* @return string * @return string
*/ */
public static function product_category( $atts ) { public static function product_category( $atts ) {
@ -198,7 +147,7 @@ class WC_Shortcodes {
/** /**
* List all (or limited) product categories. * List all (or limited) product categories.
* *
* @param array $atts * @param array $atts Attributes.
* @return string * @return string
*/ */
public static function product_categories( $atts ) { public static function product_categories( $atts ) {
@ -217,7 +166,7 @@ class WC_Shortcodes {
$ids = array_filter( array_map( 'trim', explode( ',', $atts['ids'] ) ) ); $ids = array_filter( array_map( 'trim', explode( ',', $atts['ids'] ) ) );
$hide_empty = ( true === $atts['hide_empty'] || 'true' === $atts['hide_empty'] || 1 === $atts['hide_empty'] || '1' === $atts['hide_empty'] ) ? 1 : 0; $hide_empty = ( true === $atts['hide_empty'] || 'true' === $atts['hide_empty'] || 1 === $atts['hide_empty'] || '1' === $atts['hide_empty'] ) ? 1 : 0;
// get terms and workaround WP bug with parents/pad counts // Get terms and workaround WP bug with parents/pad counts.
$args = array( $args = array(
'orderby' => $atts['orderby'], 'orderby' => $atts['orderby'],
'order' => $atts['order'], 'order' => $atts['order'],
@ -230,12 +179,14 @@ class WC_Shortcodes {
$product_categories = get_terms( 'product_cat', $args ); $product_categories = get_terms( 'product_cat', $args );
if ( '' !== $atts['parent'] ) { if ( '' !== $atts['parent'] ) {
$product_categories = wp_list_filter( $product_categories, array( 'parent' => $atts['parent'] ) ); $product_categories = wp_list_filter( $product_categories, array(
'parent' => $atts['parent'],
) );
} }
if ( $hide_empty ) { if ( $hide_empty ) {
foreach ( $product_categories as $key => $category ) { foreach ( $product_categories as $key => $category ) {
if ( 0 == $category->count ) { if ( 0 === $category->count ) {
unset( $product_categories[ $key ] ); unset( $product_categories[ $key ] );
} }
} }
@ -270,7 +221,7 @@ class WC_Shortcodes {
/** /**
* Recent Products shortcode. * Recent Products shortcode.
* *
* @param array $atts * @param array $atts Attributes.
* @return string * @return string
*/ */
public static function recent_products( $atts ) { public static function recent_products( $atts ) {
@ -291,7 +242,7 @@ class WC_Shortcodes {
/** /**
* List multiple products shortcode. * List multiple products shortcode.
* *
* @param array $atts * @param array $atts Attributes.
* @return string * @return string
*/ */
public static function products( $atts ) { public static function products( $atts ) {
@ -303,7 +254,7 @@ class WC_Shortcodes {
/** /**
* Display a single product. * Display a single product.
* *
* @param array $atts * @param array $atts Attributes.
* @return string * @return string
*/ */
public static function product( $atts ) { public static function product( $atts ) {
@ -322,7 +273,7 @@ class WC_Shortcodes {
/** /**
* Display a single product price + cart button. * Display a single product price + cart button.
* *
* @param array $atts * @param array $atts Attributes.
* @return string * @return string
*/ */
public static function product_add_to_cart( $atts ) { public static function product_add_to_cart( $atts ) {
@ -350,27 +301,29 @@ class WC_Shortcodes {
return ''; return '';
} }
$product = is_object( $product_data ) && in_array( $product_data->post_type, array( 'product', 'product_variation' ) ) ? wc_setup_product_data( $product_data ) : false; $product = is_object( $product_data ) && in_array( $product_data->post_type, array( 'product', 'product_variation' ), true ) ? wc_setup_product_data( $product_data ) : false;
if ( ! $product ) { if ( ! $product ) {
return ''; return '';
} }
$styles = empty( $atts['style'] ) ? '' : ' style="' . esc_attr( $atts['style'] ) . '"';
ob_start(); ob_start();
?>
<p class="product woocommerce add_to_cart_inline <?php echo esc_attr( $atts['class'] ); ?>"<?php echo $styles; ?>>
<?php if ( 'true' == $atts['show_price'] ) : ?> echo '<p class="product woocommerce add_to_cart_inline ' . esc_attr( $atts['class'] ) . '" style="' . empty( $atts['style'] ) ? '' : esc_attr( $atts['style'] ) . '">';
<?php echo $product->get_price_html(); ?>
<?php endif; ?>
<?php woocommerce_template_loop_add_to_cart( array( 'quantity' => $atts['quantity'] ) ); ?> if ( wc_string_to_bool( $atts['show_price'] ) ) {
// @codingStandardsIgnoreStart
echo $product->get_price_html();
// @codingStandardsIgnoreEnd
}
</p><?php woocommerce_template_loop_add_to_cart( array(
'quantity' => $atts['quantity'],
) );
// Restore Product global in case this is shown inside a product post echo '</p>';
// Restore Product global in case this is shown inside a product post.
wc_setup_product_data( $post ); wc_setup_product_data( $post );
return ob_get_clean(); return ob_get_clean();
@ -379,7 +332,7 @@ class WC_Shortcodes {
/** /**
* Get the add to cart URL for a product. * Get the add to cart URL for a product.
* *
* @param array $atts * @param array $atts Attributes.
* @return string * @return string
*/ */
public static function product_add_to_cart_url( $atts ) { public static function product_add_to_cart_url( $atts ) {
@ -396,7 +349,7 @@ class WC_Shortcodes {
return ''; return '';
} }
$product = is_object( $product_data ) && in_array( $product_data->post_type, array( 'product', 'product_variation' ) ) ? wc_setup_product_data( $product_data ) : false; $product = is_object( $product_data ) && in_array( $product_data->post_type, array( 'product', 'product_variation' ), true ) ? wc_setup_product_data( $product_data ) : false;
if ( ! $product ) { if ( ! $product ) {
return ''; return '';
@ -410,7 +363,7 @@ class WC_Shortcodes {
/** /**
* List all products on sale. * List all products on sale.
* *
* @param array $atts * @param array $atts Attributes.
* @return string * @return string
*/ */
public static function sale_products( $atts ) { public static function sale_products( $atts ) {
@ -431,7 +384,7 @@ class WC_Shortcodes {
/** /**
* List best selling products on sale. * List best selling products on sale.
* *
* @param array $atts * @param array $atts Attributes.
* @return string * @return string
*/ */
public static function best_selling_products( $atts ) { public static function best_selling_products( $atts ) {
@ -450,7 +403,7 @@ class WC_Shortcodes {
/** /**
* List top rated products on sale. * List top rated products on sale.
* *
* @param array $atts * @param array $atts Attributes.
* @return string * @return string
*/ */
public static function top_rated_products( $atts ) { public static function top_rated_products( $atts ) {
@ -471,7 +424,7 @@ class WC_Shortcodes {
/** /**
* Output featured products. * Output featured products.
* *
* @param array $atts * @param array $atts Attributes.
* @return string * @return string
*/ */
public static function featured_products( $atts ) { public static function featured_products( $atts ) {
@ -492,7 +445,7 @@ class WC_Shortcodes {
/** /**
* Show a single product page. * Show a single product page.
* *
* @param array $atts * @param array $atts Attributes.
* @return string * @return string
*/ */
public static function product_page( $atts ) { public static function product_page( $atts ) {
@ -530,16 +483,16 @@ class WC_Shortcodes {
$preselected_id = '0'; $preselected_id = '0';
// check if sku is a variation // Check if sku is a variation.
if ( isset( $atts['sku'] ) && $single_product->have_posts() && 'product_variation' === $single_product->post->post_type ) { if ( isset( $atts['sku'] ) && $single_product->have_posts() && 'product_variation' === $single_product->post->post_type ) {
$variation = new WC_Product_Variation( $single_product->post->ID ); $variation = new WC_Product_Variation( $single_product->post->ID );
$attributes = $variation->get_attributes(); $attributes = $variation->get_attributes();
// set preselected id to be used by JS to provide context // Set preselected id to be used by JS to provide context.
$preselected_id = $single_product->post->ID; $preselected_id = $single_product->post->ID;
// get the parent product object // Get the parent product object.
$args = array( $args = array(
'posts_per_page' => 1, 'posts_per_page' => 1,
'post_type' => 'product', 'post_type' => 'product',
@ -572,7 +525,9 @@ class WC_Shortcodes {
// Backup query object so following loops think this is a product page. // Backup query object so following loops think this is a product page.
$previous_wp_query = $wp_query; $previous_wp_query = $wp_query;
// @codingStandardsIgnoreStart
$wp_query = $single_product; $wp_query = $single_product;
// @codingStandardsIgnoreEnd
wp_enqueue_script( 'wc-single-product' ); wp_enqueue_script( 'wc-single-product' );
@ -585,8 +540,10 @@ class WC_Shortcodes {
<?php <?php
} }
// restore $previous_wp_query and reset post data. // Restore $previous_wp_query and reset post data.
// @codingStandardsIgnoreStart
$wp_query = $previous_wp_query; $wp_query = $previous_wp_query;
// @codingStandardsIgnoreEnd
wp_reset_postdata(); wp_reset_postdata();
return '<div class="woocommerce">' . ob_get_clean() . '</div>'; return '<div class="woocommerce">' . ob_get_clean() . '</div>';
@ -616,9 +573,9 @@ class WC_Shortcodes {
/** /**
* List products with an attribute shortcode. * List products with an attribute shortcode.
* Example [product_attribute attribute='color' filter='black']. * Example [product_attribute attribute="color" filter="black"].
* *
* @param array $atts * @param array $atts Attributes.
* @return string * @return string
*/ */
public static function product_attribute( $atts ) { public static function product_attribute( $atts ) {
@ -642,50 +599,26 @@ class WC_Shortcodes {
/** /**
* List related products. * List related products.
* @param array $atts *
* @param array $atts Attributes.
* @return string * @return string
*/ */
public static function related_products( $atts ) { public static function related_products( $atts ) {
// @codingStandardsIgnoreStart
$atts = shortcode_atts( array( $atts = shortcode_atts( array(
'per_page' => '4', 'per_page' => '4',
'columns' => '4', 'columns' => '4',
'orderby' => 'rand', 'orderby' => 'rand',
), $atts, 'related_products' ); ), $atts, 'related_products' );
// @codingStandardsIgnoreEnd
ob_start(); ob_start();
// Rename arg // Rename arg.
$atts['posts_per_page'] = absint( $atts['per_page'] ); $atts['posts_per_page'] = absint( $atts['per_page'] );
woocommerce_related_products( $atts ); woocommerce_related_products( $atts );
return ob_get_clean(); return ob_get_clean();
} }
/**
* Adds a tax_query index to the query to filter by category.
*
* @param array $args
* @param string $category
* @param string $operator
* @return array;
* @access private
*/
private static function _maybe_add_category_args( $args, $category, $operator ) {
if ( ! empty( $category ) ) {
if ( empty( $args['tax_query'] ) ) {
$args['tax_query'] = array();
}
$args['tax_query'][] = array(
array(
'taxonomy' => 'product_cat',
'terms' => array_map( 'sanitize_title', explode( ',', $category ) ),
'field' => 'slug',
'operator' => $operator,
),
);
}
return $args;
}
} }

View File

@ -166,7 +166,9 @@ class WC_Shortcode_Products {
// Best selling. // Best selling.
if ( 'best_selling_products' === $this->type ) { if ( 'best_selling_products' === $this->type ) {
// @codingStandardsIgnoreStart
$query_args['meta_key'] = 'total_sales'; $query_args['meta_key'] = 'total_sales';
// @codingStandardsIgnoreEnd
$query_args['order'] = 'DESC'; $query_args['order'] = 'DESC';
$query_args['orderby'] = 'meta_value_num'; $query_args['orderby'] = 'meta_value_num';
} }
@ -197,7 +199,9 @@ class WC_Shortcode_Products {
$query_args['order'] = $ordering_args['order']; $query_args['order'] = $ordering_args['order'];
if ( isset( $ordering_args['meta_key'] ) ) { if ( isset( $ordering_args['meta_key'] ) ) {
// @codingStandardsIgnoreStart
$query_args['meta_key'] = $ordering_args['meta_key']; $query_args['meta_key'] = $ordering_args['meta_key'];
// @codingStandardsIgnoreEnd
} }
$query_args['tax_query'][] = array( $query_args['tax_query'][] = array(