Merge pull request #19315 from woocommerce/update/order-functions-phpcs

Fixed includes/wc-order-functions.php PHPCS violations
This commit is contained in:
Rodrigo Primo 2018-03-08 16:39:08 -03:00 committed by GitHub
commit 71c7e85c2b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 122 additions and 103 deletions

View File

@ -4,13 +4,11 @@
*
* Functions for order specific things.
*
* @author WooThemes
* @category Core
* @package WooCommerce/Functions
* @package WooCommerce/Functions
* @version 3.4.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
defined( 'ABSPATH' ) || exit;
/**
* Standard way of retrieving orders based on certain parameters.
@ -21,7 +19,7 @@ if ( ! defined( 'ABSPATH' ) ) {
* Args and usage: https://github.com/woocommerce/woocommerce/wiki/wc_get_orders-and-WC_Order_Query
*
* @since 2.6.0
* @param array $args Array of args (above)
* @param array $args Array of args (above).
* @return array|stdClass Number of pages and an array of order objects if
* paginate is true, or just an array of values.
*/
@ -107,7 +105,8 @@ function wc_get_order_statuses() {
/**
* See if a string is an order status.
* @param string $maybe_status Status, including any wc- prefix
*
* @param string $maybe_status Status, including any wc- prefix.
* @return bool
*/
function wc_is_order_status( $maybe_status ) {
@ -117,6 +116,7 @@ function wc_is_order_status( $maybe_status ) {
/**
* Get list of statuses which are consider 'paid'.
*
* @since 3.0.0
* @return array
*/
@ -128,7 +128,7 @@ function wc_get_is_paid_statuses() {
* Get the nice name for an order status.
*
* @since 2.2
* @param string $status
* @param string $status Status.
* @return string
*/
function wc_get_order_status_name( $status ) {
@ -141,8 +141,8 @@ function wc_get_order_status_name( $status ) {
/**
* Finds an Order ID based on an order key.
*
* @param string $order_key An order key has generated by
* @return int The ID of an order, or 0 if the order could not be found
* @param string $order_key An order key has generated by.
* @return int The ID of an order, or 0 if the order could not be found.
*/
function wc_get_order_id_by_order_key( $order_key ) {
$data_store = WC_Data_Store::load( 'order' );
@ -152,12 +152,10 @@ function wc_get_order_id_by_order_key( $order_key ) {
/**
* Get all registered order types.
*
* $for optionally define what you are getting order types for so only relevant types are returned.
*
* e.g. for 'order-meta-boxes', 'order-count'
*
* @since 2.2
* @param string $for
* @param string $for Optionally define what you are getting order types for so
* only relevant types are returned.
* e.g. for 'order-meta-boxes', 'order-count'.
* @return array
*/
function wc_get_order_types( $for = '' ) {
@ -170,51 +168,51 @@ function wc_get_order_types( $for = '' ) {
$order_types = array();
switch ( $for ) {
case 'order-count' :
case 'order-count':
foreach ( $wc_order_types as $type => $args ) {
if ( ! $args['exclude_from_order_count'] ) {
$order_types[] = $type;
}
}
break;
case 'order-meta-boxes' :
break;
case 'order-meta-boxes':
foreach ( $wc_order_types as $type => $args ) {
if ( $args['add_order_meta_boxes'] ) {
$order_types[] = $type;
}
}
break;
case 'view-orders' :
break;
case 'view-orders':
foreach ( $wc_order_types as $type => $args ) {
if ( ! $args['exclude_from_order_views'] ) {
$order_types[] = $type;
}
}
break;
case 'reports' :
break;
case 'reports':
foreach ( $wc_order_types as $type => $args ) {
if ( ! $args['exclude_from_order_reports'] ) {
$order_types[] = $type;
}
}
break;
case 'sales-reports' :
break;
case 'sales-reports':
foreach ( $wc_order_types as $type => $args ) {
if ( ! $args['exclude_from_order_sales_reports'] ) {
$order_types[] = $type;
}
}
break;
case 'order-webhooks' :
break;
case 'order-webhooks':
foreach ( $wc_order_types as $type => $args ) {
if ( ! $args['exclude_from_order_webhooks'] ) {
$order_types[] = $type;
}
}
break;
default :
break;
default:
$order_types = array_keys( $wc_order_types );
break;
break;
}
return apply_filters( 'wc_order_types', $order_types, $for );
@ -222,8 +220,9 @@ function wc_get_order_types( $for = '' ) {
/**
* Get an order type by post type name.
* @param string post type name
* @return bool|array of details about the order type
*
* @param string $type Post type name.
* @return bool|array Details about the order type.
*/
function wc_get_order_type( $type ) {
global $wc_order_types;
@ -253,8 +252,8 @@ function wc_get_order_type( $type ) {
*
* @since 2.2
* @see register_post_type for $args used in that function
* @param string $type Post type. (max. 20 characters, can not contain capital letters or spaces)
* @param array $args An array of arguments.
* @param string $type Post type. (max. 20 characters, can not contain capital letters or spaces).
* @param array $args An array of arguments.
* @return bool Success or failure
*/
function wc_register_order_type( $type, $args = array() ) {
@ -268,12 +267,12 @@ function wc_register_order_type( $type, $args = array() ) {
$wc_order_types = array();
}
// Register as a post type
// Register as a post type.
if ( is_wp_error( register_post_type( $type, $args ) ) ) {
return false;
}
// Register for WC usage
// Register for WC usage.
$order_type_args = array(
'exclude_from_orders_screen' => false,
'add_order_meta_boxes' => true,
@ -305,7 +304,7 @@ function wc_processing_order_count() {
/**
* Return the orders count of a specific order status.
*
* @param string $status
* @param string $status Status.
* @return int
*/
function wc_orders_count( $status ) {
@ -313,11 +312,11 @@ function wc_orders_count( $status ) {
$status = 'wc-' . $status;
$order_statuses = array_keys( wc_get_order_statuses() );
if ( ! in_array( $status, $order_statuses ) ) {
if ( ! in_array( $status, $order_statuses, true ) ) {
return 0;
}
$cache_key = WC_Cache_Helper::get_cache_prefix( 'orders' ) . $status;
$cache_key = WC_Cache_Helper::get_cache_prefix( 'orders' ) . $status;
$cached_count = wp_cache_get( $cache_key, 'counts' );
if ( false !== $cached_count ) {
@ -339,11 +338,11 @@ function wc_orders_count( $status ) {
/**
* Grant downloadable product access to the file identified by $download_id.
*
* @param string $download_id file identifier
* @param int|WC_Product $product
* @param WC_Order $order the order
* @param int $qty purchased
* @return int|bool insert id or false on failure
* @param string $download_id File identifier.
* @param int|WC_Product $product Product instance or ID.
* @param WC_Order $order Order data.
* @param int $qty Quantity purchased.
* @return int|bool insert id or false on failure.
*/
function wc_downloadable_file_permission( $download_id, $product, $order, $qty = 1 ) {
if ( is_numeric( $product ) ) {
@ -373,8 +372,8 @@ function wc_downloadable_file_permission( $download_id, $product, $order, $qty =
/**
* Order Status completed - give downloadable product access to customer.
*
* @param int $order_id
* @param bool $force
* @param int $order_id Order ID.
* @param bool $force Force downloadable permissions.
*/
function wc_downloadable_product_permissions( $order_id, $force = false ) {
$order = wc_get_order( $order_id );
@ -387,7 +386,7 @@ function wc_downloadable_product_permissions( $order_id, $force = false ) {
return;
}
if ( sizeof( $order->get_items() ) > 0 ) {
if ( count( $order->get_items() ) > 0 ) {
foreach ( $order->get_items() as $item ) {
$product = $item->get_product();
@ -410,7 +409,7 @@ add_action( 'woocommerce_order_status_processing', 'wc_downloadable_product_perm
/**
* Clear all transients cache for order data.
*
* @param int|WC_Order $order
* @param int|WC_Order $order Order instance or ID.
*/
function wc_delete_shop_order_transients( $order = 0 ) {
if ( is_numeric( $order ) ) {
@ -418,7 +417,7 @@ function wc_delete_shop_order_transients( $order = 0 ) {
}
$reports = WC_Admin_Reports::get_reports();
$transients_to_clear = array(
'wc_admin_report'
'wc_admin_report',
);
foreach ( $reports as $report_group ) {
@ -431,7 +430,7 @@ function wc_delete_shop_order_transients( $order = 0 ) {
delete_transient( $transient );
}
// Clear money spent for user associated with order
// Clear money spent for user associated with order.
if ( is_a( $order, 'WC_Order' ) ) {
$order_id = $order->get_id();
delete_user_meta( $order->get_customer_id(), '_money_spent' );
@ -440,10 +439,10 @@ function wc_delete_shop_order_transients( $order = 0 ) {
$order_id = 0;
}
// Increments the transient version to invalidate cache
// Increments the transient version to invalidate cache.
WC_Cache_Helper::get_transient_version( 'orders', true );
// Do the same for regular cache
// Do the same for regular cache.
WC_Cache_Helper::incr_cache_prefix( 'orders' );
do_action( 'woocommerce_delete_shop_order_transients', $order_id );
@ -451,6 +450,7 @@ function wc_delete_shop_order_transients( $order = 0 ) {
/**
* See if we only ship to billing addresses.
*
* @return bool
*/
function wc_ship_to_billing_address_only() {
@ -463,7 +463,8 @@ function wc_ship_to_billing_address_only() {
* Returns a new refund object on success which can then be used to add additional data.
*
* @since 2.2
* @param array $args
* @throws Exception Throws exceptions when fail to create, but returns WP_Error instead.
* @param array $args New refund arguments.
* @return WC_Order_Refund|WP_Error
*/
function wc_create_refund( $args = array() ) {
@ -478,9 +479,10 @@ function wc_create_refund( $args = array() ) {
);
try {
$args = wp_parse_args( $args, $default_args );
$args = wp_parse_args( $args, $default_args );
$order = wc_get_order( $args['order_id'] );
if ( ! $order = wc_get_order( $args['order_id'] ) ) {
if ( ! $order ) {
throw new Exception( __( 'Invalid order ID.', 'woocommerce' ) );
}
@ -502,8 +504,8 @@ function wc_create_refund( $args = array() ) {
$refund->set_reason( $args['reason'] );
}
// Negative line items
if ( sizeof( $args['line_items'] ) > 0 ) {
// Negative line items.
if ( count( $args['line_items'] ) > 0 ) {
$items = $order->get_items( array( 'line_item', 'fee', 'shipping' ) );
foreach ( $items as $item_id => $item ) {
@ -524,7 +526,12 @@ function wc_create_refund( $args = array() ) {
$refunded_item->set_id( 0 );
$refunded_item->add_meta_data( '_refunded_item_id', $item_id, true );
$refunded_item->set_total( wc_format_refund_total( $refund_total ) );
$refunded_item->set_taxes( array( 'total' => array_map( 'wc_format_refund_total', $refund_tax ), 'subtotal' => array_map( 'wc_format_refund_total', $refund_tax ) ) );
$refunded_item->set_taxes(
array(
'total' => array_map( 'wc_format_refund_total', $refund_tax ),
'subtotal' => array_map( 'wc_format_refund_total', $refund_tax ),
)
);
if ( is_callable( array( $refunded_item, 'set_subtotal' ) ) ) {
$refunded_item->set_subtotal( wc_format_refund_total( $refund_total ) );
@ -544,13 +551,14 @@ function wc_create_refund( $args = array() ) {
$refund->set_total( $args['amount'] * -1 );
// this should remain after update_taxes(), as this will save the order, and write the current date to the db
// so we must wait until the order is persisted to set the date
// so we must wait until the order is persisted to set the date.
if ( isset( $args['date_created'] ) ) {
$refund->set_date_created( $args['date_created'] );
}
/**
* Action hook to adjust refund before save.
*
* @since 3.0.0
*/
do_action( 'woocommerce_create_refund', $refund, $args );
@ -569,7 +577,7 @@ function wc_create_refund( $args = array() ) {
wc_restock_refunded_items( $order, $args['line_items'] );
}
// Trigger notification emails
// Trigger notification emails.
if ( ( $remaining_refund_amount - $args['amount'] ) > 0 || ( $order->has_free_item() && ( $remaining_refund_items - $refund_item_count ) > 0 ) ) {
do_action( 'woocommerce_order_partially_refunded', $order->get_id(), $refund->get_id() );
} else {
@ -600,9 +608,10 @@ function wc_create_refund( $args = array() ) {
* Try to refund the payment for an order via the gateway.
*
* @since 3.0.0
* @param WC_Order $order
* @param string $amount
* @param string $reason
* @throws Exception Throws exceptions when fail to refund, but returns WP_Error instead.
* @param WC_Order $order Order instance.
* @param string $amount Amount to refund.
* @param string $reason Refund reason.
* @return bool|WP_Error
*/
function wc_refund_payment( $order, $amount, $reason = '' ) {
@ -644,9 +653,9 @@ function wc_refund_payment( $order, $amount, $reason = '' ) {
/**
* Restock items during refund.
*
* @since 3.0.0
* @param WC_Order $order
* @param array $refunded_line_items
* @since 3.0.0
* @param WC_Order $order Order instance.
* @param array $refunded_line_items Refunded items list.
*/
function wc_restock_refunded_items( $order, $refunded_line_items ) {
$line_items = $order->get_items();
@ -661,6 +670,7 @@ function wc_restock_refunded_items( $order, $refunded_line_items ) {
$old_stock = $product->get_stock_quantity();
$new_stock = wc_update_product_stock( $product, $refunded_line_items[ $item_id ]['qty'], 'increase' );
/* translators: 1: product ID 2: old stock level 3: new stock level */
$order->add_order_note( sprintf( __( 'Item #%1$s stock increased from %2$s to %3$s.', 'woocommerce' ), $product->get_id(), $old_stock, $new_stock ) );
do_action( 'woocommerce_restock_refunded_item', $product->get_id(), $old_stock, $new_stock, $order, $product );
@ -672,7 +682,7 @@ function wc_restock_refunded_items( $order, $refunded_line_items ) {
* Get tax class by tax id.
*
* @since 2.2
* @param int $tax_id
* @param int $tax_id Tax ID.
* @return string
*/
function wc_get_tax_class_by_tax_id( $tax_id ) {
@ -684,7 +694,7 @@ function wc_get_tax_class_by_tax_id( $tax_id ) {
* Get payment gateway class by order data.
*
* @since 2.2
* @param int|WC_Order $order
* @param int|WC_Order $order Order instance.
* @return WC_Payment_Gateway|bool
*/
function wc_get_payment_gateway_by_order( $order ) {
@ -708,23 +718,25 @@ function wc_get_payment_gateway_by_order( $order ) {
* This is manual; no gateway refund will be performed.
*
* @since 2.4
* @param int $order_id
* @param int $order_id Order ID.
*/
function wc_order_fully_refunded( $order_id ) {
$order = wc_get_order( $order_id );
$max_refund = wc_format_decimal( $order->get_total() - $order->get_total_refunded() );
$order = wc_get_order( $order_id );
$max_refund = wc_format_decimal( $order->get_total() - $order->get_total_refunded() );
if ( ! $max_refund ) {
return;
}
// Create the refund object
wc_create_refund( array(
'amount' => $max_refund,
'reason' => __( 'Order fully refunded', 'woocommerce' ),
'order_id' => $order_id,
'line_items' => array(),
) );
// Create the refund object.
wc_create_refund(
array(
'amount' => $max_refund,
'reason' => __( 'Order fully refunded', 'woocommerce' ),
'order_id' => $order_id,
'line_items' => array(),
)
);
}
add_action( 'woocommerce_order_status_refunded', 'wc_order_fully_refunded' );
@ -744,7 +756,7 @@ function wc_order_search( $term ) {
* Update total sales amount for each product within a paid order.
*
* @since 3.0.0
* @param int $order_id
* @param int $order_id Order ID.
*/
function wc_update_total_sales_counts( $order_id ) {
$order = wc_get_order( $order_id );
@ -753,9 +765,11 @@ function wc_update_total_sales_counts( $order_id ) {
return;
}
if ( sizeof( $order->get_items() ) > 0 ) {
if ( count( $order->get_items() ) > 0 ) {
foreach ( $order->get_items() as $item ) {
if ( $product_id = $item->get_product_id() ) {
$product_id = $item->get_product_id();
if ( $product_id ) {
$data_store = WC_Data_Store::load( 'product' );
$data_store->update_product_sales( $product_id, absint( $item['qty'] ), 'increase' );
}
@ -779,10 +793,12 @@ add_action( 'woocommerce_order_status_on-hold', 'wc_update_total_sales_counts' )
* Update used coupon amount for each coupon within an order.
*
* @since 3.0.0
* @param int $order_id
* @param int $order_id Order ID.
*/
function wc_update_coupon_usage_counts( $order_id ) {
if ( ! $order = wc_get_order( $order_id ) ) {
$order = wc_get_order( $order_id );
if ( ! $order ) {
return;
}
@ -798,25 +814,26 @@ function wc_update_coupon_usage_counts( $order_id ) {
return;
}
if ( sizeof( $order->get_used_coupons() ) > 0 ) {
if ( count( $order->get_used_coupons() ) > 0 ) {
foreach ( $order->get_used_coupons() as $code ) {
if ( ! $code ) {
continue;
}
$coupon = new WC_Coupon( $code );
$coupon = new WC_Coupon( $code );
$used_by = $order->get_user_id();
if ( ! $used_by = $order->get_user_id() ) {
if ( ! $used_by ) {
$used_by = $order->get_billing_email();
}
switch ( $action ) {
case 'reduce' :
case 'reduce':
$coupon->decrease_usage_count( $used_by );
break;
case 'increase' :
break;
case 'increase':
$coupon->increase_usage_count( $used_by );
break;
break;
}
}
}
@ -861,7 +878,7 @@ add_action( 'woocommerce_cancel_unpaid_orders', 'wc_cancel_unpaid_orders' );
* This function will fix this.
*
* @since 3.1.0
* @param int $order_id
* @param int $order_id Order ID.
*/
function wc_sanitize_order_id( $order_id ) {
return filter_var( $order_id, FILTER_SANITIZE_NUMBER_INT );
@ -884,13 +901,15 @@ function wc_get_order_note( $data ) {
return null;
}
return (object) apply_filters( 'woocommerce_get_order_note', array(
'id' => (int) $data->comment_ID,
'date_created' => wc_string_to_datetime( $data->comment_date ),
'content' => $data->comment_content,
'customer_note' => (bool) get_comment_meta( $data->comment_ID, 'is_customer_note', true ),
'added_by' => __( 'WooCommerce', 'woocommerce' ) === $data->comment_author ? 'system' : $data->comment_author,
), $data );
return (object) apply_filters(
'woocommerce_get_order_note', array(
'id' => (int) $data->comment_ID,
'date_created' => wc_string_to_datetime( $data->comment_date ),
'content' => $data->comment_content,
'customer_note' => (bool) get_comment_meta( $data->comment_ID, 'is_customer_note', true ),
'added_by' => __( 'WooCommerce', 'woocommerce' ) === $data->comment_author ? 'system' : $data->comment_author,
), $data
);
}
/**
@ -946,7 +965,7 @@ function wc_get_order_notes( $args ) {
// Set WooCommerce order type.
if ( isset( $args['type'] ) && 'customer' === $args['type'] ) {
$args['meta_query'] = array(
$args['meta_query'] = array( // WPCS: slow query ok.
array(
'key' => 'is_customer_note',
'value' => 1,
@ -954,7 +973,7 @@ function wc_get_order_notes( $args ) {
),
);
} elseif ( isset( $args['type'] ) && 'internal' === $args['type'] ) {
$args['meta_query'] = array(
$args['meta_query'] = array( // WPCS: slow query ok.
array(
'key' => 'is_customer_note',
'compare' => 'NOT EXISTS',
@ -968,7 +987,7 @@ function wc_get_order_notes( $args ) {
// Always approved.
$args['status'] = 'approve';
// Does not support 'count' or 'fields';
// Does not support 'count' or 'fields'.
unset( $args['count'], $args['fields'] );
remove_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_order_comments' ), 10, 1 );
@ -986,8 +1005,8 @@ function wc_get_order_notes( $args ) {
* @since 3.2.0
* @param int $order_id Order ID.
* @param string $note Note to add.
* @param bool $is_customer_note Is this a note for the customer?
* @param bool $added_by_user Was the note added by a user?
* @param bool $is_customer_note If is a costumer note.
* @param bool $added_by_user If note is create by an user.
* @return int|WP_Error Integer when created or WP_Error when found an error.
*/
function wc_create_order_note( $order_id, $note, $is_customer_note = false, $added_by_user = false ) {