This commit is contained in:
Mike Jolley 2018-02-22 14:30:13 +00:00
parent 0f85e00af1
commit 9571a90c9f
2 changed files with 53 additions and 40 deletions

View File

@ -1,18 +1,15 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* WooCommerce WC_AJAX.
*
* AJAX Event Handler.
* WooCommerce WC_AJAX. AJAX Event Handlers.
*
* @class WC_AJAX
* @package WooCommerce/Classes
* @category Class
* @author WooThemes
*/
defined( 'ABSPATH' ) || exit;
/**
* WC_Ajax class.
*/
class WC_AJAX {
@ -28,7 +25,7 @@ class WC_AJAX {
/**
* Get WC Ajax Endpoint.
*
* @param string $request Optional
* @param string $request Optional.
* @return string
*/
public static function get_endpoint( $request = '' ) {
@ -43,7 +40,7 @@ class WC_AJAX {
wc_maybe_define_constant( 'DOING_AJAX', true );
wc_maybe_define_constant( 'WC_DOING_AJAX', true );
if ( ! WP_DEBUG || ( WP_DEBUG && ! WP_DEBUG_DISPLAY ) ) {
@ini_set( 'display_errors', 0 ); // Turn off display_errors during AJAX events to prevent malformed JSON
@ini_set( 'display_errors', 0 ); // Turn off display_errors during AJAX events to prevent malformed JSON.
}
$GLOBALS['wpdb']->hide_errors();
}

View File

@ -4,8 +4,6 @@
*
* Functions for product specific things.
*
* @author WooThemes
* @category Core
* @package WooCommerce/Functions
* @version 3.0.0
*/
@ -393,7 +391,9 @@ function wc_scheduled_sales() {
if ( $product_ids ) {
do_action( 'wc_before_products_starting_sales', $product_ids );
foreach ( $product_ids as $product_id ) {
if ( $product = wc_get_product( $product_id ) ) {
$product = wc_get_product( $product_id );
if ( $product ) {
$sale_price = $product->get_sale_price();
if ( $sale_price ) {
@ -417,7 +417,9 @@ function wc_scheduled_sales() {
if ( $product_ids ) {
do_action( 'wc_before_products_ending_sales', $product_ids );
foreach ( $product_ids as $product_id ) {
if ( $product = wc_get_product( $product_id ) ) {
$product = wc_get_product( $product_id );
if ( $product ) {
$regular_price = $product->get_regular_price();
$product->set_price( $regular_price );
$product->set_sale_price( '' );
@ -482,14 +484,15 @@ function wc_track_product_view() {
global $post;
if ( empty( $_COOKIE['woocommerce_recently_viewed'] ) ) {
if ( empty( $_COOKIE['woocommerce_recently_viewed'] ) ) { // @codingStandardsIgnoreLine.
$viewed_products = array();
} else {
$viewed_products = (array) explode( '|', $_COOKIE['woocommerce_recently_viewed'] );
$viewed_products = wp_parse_id_list( (array) explode( '|', wp_unslash( $_COOKIE['woocommerce_recently_viewed'] ) ) ); // @codingStandardsIgnoreLine.
}
// Unset if already in viewed products list.
$keys = array_flip( $viewed_products );
if ( isset( $keys[ $post->ID ] ) ) {
unset( $viewed_products[ $keys[ $post->ID ] ] );
}
@ -547,9 +550,10 @@ function wc_product_has_unique_sku( $product_id, $sku ) {
* @param integer $product_id Product ID.
*/
function wc_product_force_unique_sku( $product_id ) {
$product = wc_get_product( $product_id );
$product = wc_get_product( $product_id );
$current_sku = $product ? $product->get_sku( 'edit' ) : '';
if ( $product && ( $current_sku = $product->get_sku( 'edit' ) ) ) {
if ( $current_sku ) {
try {
$new_sku = wc_product_generate_unique_sku( $product_id, $current_sku );
@ -557,7 +561,7 @@ function wc_product_force_unique_sku( $product_id ) {
$product->set_sku( $new_sku );
$product->save();
}
} catch ( Exception $e ) {}
} catch ( Exception $e ) {} // @codingStandardsIgnoreLine.
}
}
@ -621,7 +625,7 @@ function wc_get_product_variation_attributes( $variation_id ) {
// Get the variation attributes from meta.
foreach ( $all_meta as $name => $value ) {
// Only look at valid attribute meta, and also compare variation level attributes and remove any which do not exist at parent level.
if ( 0 !== strpos( $name, 'attribute_' ) || ! in_array( $name, $found_parent_attributes ) ) {
if ( 0 !== strpos( $name, 'attribute_' ) || ! in_array( $name, $found_parent_attributes, true ) ) {
unset( $variation_attributes[ $name ] );
continue;
}
@ -679,7 +683,7 @@ function wc_get_product_cat_ids( $product_id ) {
* @return array
*/
function wc_get_product_attachment_props( $attachment_id = null, $product = false ) {
$props = array(
$props = array(
'title' => '',
'caption' => '',
'url' => '',
@ -688,7 +692,9 @@ function wc_get_product_attachment_props( $attachment_id = null, $product = fals
'srcset' => false,
'sizes' => false,
);
if ( $attachment = get_post( $attachment_id ) ) {
$attachment = get_post( $attachment_id );
if ( $attachment ) {
$props['title'] = trim( strip_tags( $attachment->post_title ) );
$props['caption'] = trim( strip_tags( $attachment->post_excerpt ) );
$props['url'] = wp_get_attachment_url( $attachment_id );
@ -701,7 +707,7 @@ function wc_get_product_attachment_props( $attachment_id = null, $product = fals
$props['full_src_h'] = $src[2];
// Thumbnail version.
$src = wp_get_attachment_image_src( $attachment_id, 'woocommerce_thumbnail' );
$src = wp_get_attachment_image_src( $attachment_id, 'woocommerce_thumbnail' );
$props['thumb_src'] = $src[0];
$props['thumb_src_w'] = $src[1];
$props['thumb_src_h'] = $src[2];
@ -715,8 +721,8 @@ function wc_get_product_attachment_props( $attachment_id = null, $product = fals
$props['sizes'] = function_exists( 'wp_get_attachment_image_sizes' ) ? wp_get_attachment_image_sizes( $attachment_id, 'woocommerce_single' ) : false;
// Alt text fallbacks.
$props['alt'] = empty( $props['alt'] ) ? $props['caption'] : $props['alt'];
$props['alt'] = empty( $props['alt'] ) ? trim( strip_tags( $attachment->post_title ) ) : $props['alt'];
$props['alt'] = empty( $props['alt'] ) ? $props['caption'] : $props['alt'];
$props['alt'] = empty( $props['alt'] ) ? trim( strip_tags( $attachment->post_title ) ) : $props['alt'];
$props['alt'] = empty( $props['alt'] ) && $product ? trim( strip_tags( get_the_title( $product->ID ) ) ) : $props['alt'];
}
return $props;
@ -758,7 +764,9 @@ function wc_get_min_max_price_meta_query( $args ) {
$class_max = $max;
foreach ( $tax_classes as $tax_class ) {
if ( $tax_rates = WC_Tax::get_rates( $tax_class ) ) {
$tax_rates = WC_Tax::get_rates( $tax_class );
if ( $tax_rates ) {
$class_min = $min + WC_Tax::get_tax_total( WC_Tax::calc_exclusive_tax( $min, $tax_rates ) );
$class_max = $max - WC_Tax::get_tax_total( WC_Tax::calc_exclusive_tax( $max, $tax_rates ) );
}
@ -850,7 +858,7 @@ function wc_get_related_products( $product_id, $limit = 5, $exclude_ids = array(
if ( false === $related_posts || count( $related_posts ) < $limit ) {
$cats_array = apply_filters( 'woocommerce_product_related_posts_relate_by_category', true, $product_id ) ? apply_filters( 'woocommerce_get_related_product_cat_terms', wc_get_product_term_ids( $product_id, 'product_cat' ), $product_id ) : array();
$tags_array = apply_filters( 'woocommerce_product_related_posts_relate_by_tag', true, $product_id ) ? apply_filters( 'woocommerce_get_related_product_tag_terms', wc_get_product_term_ids( $product_id, 'product_tag' ), $product_id ) : array();
$tags_array = apply_filters( 'woocommerce_product_related_posts_relate_by_tag', true, $product_id ) ? apply_filters( 'woocommerce_get_related_product_tag_terms', wc_get_product_term_ids( $product_id, 'product_tag' ), $product_id ) : array();
// Don't bother if none are set, unless woocommerce_product_related_posts_force_display is set to true in which case all products are related.
if ( empty( $cats_array ) && empty( $tags_array ) && ! apply_filters( 'woocommerce_product_related_posts_force_display', false, $product_id ) ) {
@ -976,9 +984,9 @@ function wc_get_price_excluding_tax( $product, $args = array() ) {
}
if ( $product->is_taxable() && wc_prices_include_tax() ) {
$tax_rates = WC_Tax::get_base_tax_rates( $product->get_tax_class( 'unfiltered' ) );
$taxes = WC_Tax::calc_tax( $price * $qty, $tax_rates, true );
$price = WC_Tax::round( $price * $qty - wc_round_tax_total( array_sum( $taxes ) ) );
$tax_rates = WC_Tax::get_base_tax_rates( $product->get_tax_class( 'unfiltered' ) );
$taxes = WC_Tax::calc_tax( $price * $qty, $tax_rates, true );
$price = WC_Tax::round( $price * $qty - wc_round_tax_total( array_sum( $taxes ) ) );
} else {
$price = $price * $qty;
}
@ -1003,7 +1011,15 @@ function wc_get_price_to_display( $product, $args = array() ) {
$price = $args['price'];
$qty = $args['qty'];
return 'incl' === get_option( 'woocommerce_tax_display_shop' ) ? wc_get_price_including_tax( $product, array( 'qty' => $qty, 'price' => $price ) ) : wc_get_price_excluding_tax( $product, array( 'qty' => $qty, 'price' => $price ) );
return 'incl' === get_option( 'woocommerce_tax_display_shop' ) ?
wc_get_price_including_tax( $product, array(
'qty' => $qty,
'price' => $price,
) ) :
wc_get_price_excluding_tax( $product, array(
'qty' => $qty,
'price' => $price,
) );
}
/**
@ -1091,15 +1107,15 @@ function wc_products_array_orderby( $products, $orderby = 'date', $order = 'desc
$orderby = strtolower( $orderby );
$order = strtolower( $order );
switch ( $orderby ) {
case 'title' :
case 'id' :
case 'date' :
case 'modified' :
case 'menu_order' :
case 'price' :
case 'title':
case 'id':
case 'date':
case 'modified':
case 'menu_order':
case 'price':
usort( $products, 'wc_products_array_orderby_' . $orderby );
break;
default :
default:
shuffle( $products );
break;
}