Skip to content

Commit

Permalink
v3.3.3
Browse files Browse the repository at this point in the history
  • Loading branch information
pablo-sg-pacheco committed Apr 3, 2024
1 parent 5967e2b commit 61db0dc
Show file tree
Hide file tree
Showing 12 changed files with 420 additions and 281 deletions.
4 changes: 2 additions & 2 deletions cost-of-goods-for-woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
Plugin Name: Cost of Goods for WooCommerce
Plugin URI: https://wpfactory.com/item/cost-of-goods-for-woocommerce/
Description: Save product purchase costs (cost of goods) in WooCommerce. Beautifully.
Version: 3.3.2
Version: 3.3.3
Author: WPFactory
Author URI: https://wpfactory.com
Text Domain: cost-of-goods-for-woocommerce
Domain Path: /langs
Copyright: © 2024 WPFactory
WC tested up to: 8.6
WC tested up to: 8.7
License: GNU General Public License v3.0
License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
Expand Down
85 changes: 75 additions & 10 deletions includes/alg-wc-cog-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Cost of Goods for WooCommerce - Functions.
*
* @version 3.2.1
* @version 3.3.3
* @since 3.2.1
* @author WPFactory
*/
Expand Down Expand Up @@ -224,28 +224,93 @@ function alg_wc_cog_get_blocked_options_message() {
/**
* alg_wc_cog_sanitize_number.
*
* @version 2.9.5
* @version 3.3.3
* @since 2.9.5
*
* @param $args
*
* @return numeric
* @return float
*/
function alg_wc_cog_sanitize_number( $args = null ) {
$args = wp_parse_args( $args, array(
'number' => 0,
$args = wp_parse_args( $args, array(
'value' => 0,
'dots_and_commas_operation' => 'comma-to-dot', // comma-to-dot | dot-to-comma | none
'typecasting' => 'smart', // float | int | smart | none
) );
$args = apply_filters( 'alg_wc_cog_sanitize_number_args', $args );
$number = $args['number'];
$args = apply_filters( 'alg_wc_cog_sanitize_number_args', $args );
$value = sanitize_text_field( wc_clean( $args['value'] ) );
$typecasting = $args['typecasting'];
$dots_and_commas_operation = $args['dots_and_commas_operation'];
if ( 'comma-to-dot' === $dots_and_commas_operation ) {
$number = str_replace( ',', '.', $number );
$value = str_replace( ',', '.', $value );
} elseif ( 'dot-to-comma' === $dots_and_commas_operation ) {
$number = str_replace( '.', ',', (string) $number );
$value = str_replace( '.', ',', (string) $value );
}

switch ( $typecasting ) {
case 'smart':
if ( strpos( $value, ',' ) === false ) {
$value = (float) $value;
}
break;
case 'float':
$value = (float) $value;
break;
case 'int':
$value = (int) $value;
break;

}

return $number;
return $value;
}
}

if ( ! function_exists( 'alg_wc_cog_sanitize_cost' ) ) {
/**
* alg_wc_cog_sanitize_cost.
*
* @version 3.3.3
* @since 3.3.3
*
* @param $args
*
* @return float
*/
function alg_wc_cog_sanitize_cost( $args = null ) {
$args = wp_parse_args( $args, array(
'value' => 0,
'dots_and_commas_operation' => alg_wc_cog_need_to_replace_cog_comma_by_dots() ? 'comma-to-dot' : 'none'
) );
return alg_wc_cog_sanitize_number( $args );
}
}

if ( ! function_exists( 'alg_wc_cog_need_to_replace_cog_comma_by_dots' ) ) {
/**
* alg_wc_cog_replace_cog_comma_by_dots.
*
* @version 3.3.3
* @since 3.3.3
*
* @return bool
*/
function alg_wc_cog_need_to_replace_cog_comma_by_dots() {
return 'yes' === get_option( 'alg_wc_cog_replace_cog_comma_by_dots', alg_wc_cog_need_to_replace_cog_comma_by_dots_default() );
}
}

if ( ! function_exists( 'alg_wc_cog_need_to_replace_cog_comma_by_dots_default' ) ) {
/**
* alg_wc_cog_replace_cog_comma_by_dots_default.
*
* @version 3.3.3
* @since 3.3.3
*
* @return string
*/
function alg_wc_cog_need_to_replace_cog_comma_by_dots_default() {
return get_option( 'alg_wc_cog_products_sanitize_cog_meta', 'yes' );
}
}

Expand Down
22 changes: 17 additions & 5 deletions includes/class-alg-wc-cog-cost-inputs.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Cost of Goods for WooCommerce - Costs input.
*
* @version 3.1.9
* @version 3.3.3
* @since 2.6.4
* @author WPFactory
*/
Expand Down Expand Up @@ -165,25 +165,37 @@ function add_cost_input_variation( $loop, $variation_data, $variation ) {
/**
* save_cost_input.
*
* @version 1.7.0
* @version 3.3.3
* @since 1.0.0
* @todo [next] maybe pre-calculate and save `_alg_wc_cog_profit` (same in `save_cost_input_variation()`)
*/
function save_cost_input( $product_id, $__post ) {
if ( isset( $_POST['_alg_wc_cog_cost'] ) ) {
update_post_meta( $product_id, '_alg_wc_cog_cost', wc_clean( $_POST['_alg_wc_cog_cost'] ) );
$new_cost = alg_wc_cog_sanitize_cost( array(
'value' => $_POST['_alg_wc_cog_cost']
)
);
if ( $new_cost !== (float) alg_wc_cog()->core->products->get_product_cost( $product_id ) ) {
update_post_meta( $product_id, '_alg_wc_cog_cost', $new_cost );
}
}
}

/**
* save_cost_input_variation.
*
* @version 1.1.0
* @version 3.3.3
* @since 1.0.0
*/
function save_cost_input_variation( $variation_id, $i ) {
if ( isset( $_POST['variable_alg_wc_cog_cost'][ $i ] ) ) {
update_post_meta( $variation_id, '_alg_wc_cog_cost', wc_clean( $_POST['variable_alg_wc_cog_cost'][ $i ] ) );
$new_cost = alg_wc_cog_sanitize_cost( array(
'value' => $_POST['variable_alg_wc_cog_cost'][ $i ]
)
);
if ( $new_cost !== (float) alg_wc_cog()->core->products->get_product_cost( $variation_id ) ) {
update_post_meta( $variation_id, '_alg_wc_cog_cost', $new_cost );
}
}
}

Expand Down
8 changes: 4 additions & 4 deletions includes/class-alg-wc-cog-orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Cost of Goods for WooCommerce - Orders Class.
*
* @version 3.3.0
* @version 3.3.3
* @since 2.1.0
* @author WPFactory
*/
Expand Down Expand Up @@ -1292,7 +1292,7 @@ function get_shipping_total_for_percentage_fees( $order ) {
/**
* update_order_items_costs.
*
* @version 3.1.9
* @version 3.3.3
* @since 1.1.0
* @todo [maybe] filters: add more?
* @todo [maybe] `$total_price`: customizable calculation method (e.g. `$order->get_subtotal()`) (this will affect `_alg_wc_cog_order_profit_margin`)
Expand Down Expand Up @@ -1425,7 +1425,7 @@ function update_order_items_costs( $args ) {
$quantity = $calculate_qty_excluding_refunds ? $item->get_quantity() + $order->get_qty_refunded_for_item( $item_id ) : $item->get_quantity();
if ( '' !== $cost || '' !== $handling_fee ) {
$cost = alg_wc_cog_sanitize_number( array(
'number' => $cost,
'value' => $cost,
'dots_and_commas_operation' => 'comma-to-dot'
) );
$cost = (float) $cost;
Expand All @@ -1440,7 +1440,7 @@ function update_order_items_costs( $args ) {
$total_price += $line_total;
// handling fee.
$handling_fee = alg_wc_cog_sanitize_number( array(
'number' => $handling_fee,
'value' => $handling_fee,
'dots_and_commas_operation' => 'comma-to-dot'
) );
$handling_fee = (float) $handling_fee;
Expand Down
20 changes: 10 additions & 10 deletions includes/class-alg-wc-cog-products-add-stock.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Cost of Goods for WooCommerce - Products - Add Stock.
*
* @version 3.2.5
* @version 3.3.3
* @since 2.8.2
* @author WPFactory
*/
Expand Down Expand Up @@ -81,7 +81,7 @@ function add_product_add_stock_meta_box( $post_type, $post ) {
/**
* save_product_add_stock.
*
* @version 3.1.7
* @version 3.3.3
* @since 1.7.0
* @todo [next] handle variable products (also unset `$_POST['variable_stock']`)
* @todo [maybe] remove `$this->is_add_stock`
Expand Down Expand Up @@ -118,8 +118,8 @@ function save_product_add_stock( $product_id, $post ) {
'product_id' => (int) $product_id,
'stock' => floatval( $_POST['alg_wc_cog_add_stock'] ),
'stock_prev' => isset( $_POST['_stock'] ) ? (int) $_POST['_stock'] : '',
'cost_prev' => isset( $_POST['_alg_wc_cog_cost'] ) ? (int) $_POST['_alg_wc_cog_cost'] : '',
'cost' => floatval( $_POST['alg_wc_cog_add_stock_cost'] ),
'cost_prev' => isset( $_POST['_alg_wc_cog_cost'] ) ? alg_wc_cog_sanitize_cost( array( 'number' => $_POST['_alg_wc_cog_cost'] ) ) : '',
'cost' => alg_wc_cog_sanitize_cost( array( 'value' => $_POST['alg_wc_cog_add_stock_cost'] ) ),
) );
if ( isset( $_POST['_stock'] ) ) {
unset( $_POST['_stock'] );
Expand Down Expand Up @@ -155,7 +155,7 @@ function get_variation_stock_prev_from_post( $variation_id ) {
/**
* get_variation_cost_prev_from_post.
*
* @version 3.1.7
* @version 3.3.3
* @since 3.1.7
*
* @param $variation_id
Expand All @@ -172,7 +172,7 @@ function get_variation_cost_prev_from_post( $variation_id ) {
$variation_cost_prev = $_POST['variable_alg_wc_cog_cost'][ $variation_key ];
}

return $variation_cost_prev;
return alg_wc_cog_sanitize_cost( array( 'value' => $variation_cost_prev ) );
}

/**
Expand Down Expand Up @@ -573,7 +573,7 @@ function get_variations_to_update_html( $parent_product ) {
/**
* calculate_add_stock_cost.
*
* @version 2.4.2
* @version 3.3.3
* @since 2.4.2
*
* @param null $args
Expand All @@ -596,14 +596,14 @@ function calculate_add_stock_cost( $args = null ) {
$cost_calculation_template = $args['calculation_template'];
$cost_calculation_template = $this->sanitize_math_expression( str_replace( array_keys( $template_variables ), $template_variables, $cost_calculation_template ) );
include_once WC()->plugin_path() . '/includes/libraries/class-wc-eval-math.php';
$cost_now = WC_Eval_Math::evaluate( $cost_calculation_template );
$cost_now = (float) WC_Eval_Math::evaluate( $cost_calculation_template );
return $cost_now;
}

/**
* product_add_stock.
*
* @version 3.1.7
* @version 3.3.3
* @since 1.7.0
* @todo [next] maybe use `$product = wc_get_product( $product_id )`, i.e. `$product->get_stock_quantity()`, `$product->set_stock_quantity( $stock_now )` and `$product->save()`?
* @todo [maybe] `$cost_now`: round?
Expand Down Expand Up @@ -636,7 +636,7 @@ function product_add_stock( $args = null ) {
if ( ! $cost_prev ) {
$cost_prev = 0;
}
$cost_now = @$this->calculate_add_stock_cost( array(
$cost_now = (float) @$this->calculate_add_stock_cost( array(
'product_id' => $product_id,
'template_variables' => array(
'%stock_prev%' => $stock_prev,
Expand Down
8 changes: 4 additions & 4 deletions includes/class-alg-wc-cog-products-cost-archive.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Cost of Goods for WooCommerce - Products - Cost archive.
*
* @version 3.1.7
* @version 3.3.3
* @since 2.8.2
* @author WPFactory
*/
Expand Down Expand Up @@ -161,7 +161,7 @@ function get_cost_archive_table_ajax() {
/**
* get_product_cost_archive_table.
*
* @version 3.1.7
* @version 3.3.3
* @since 3.1.7
*
* @param $product
Expand All @@ -187,11 +187,11 @@ function get_product_cost_archive_table( $product ) {
$dots_and_commas_operation = 'comma-to-dot';
foreach ( $product_cost_archive as $cost_info ) {
$prev_cost = alg_wc_cog_sanitize_number( array(
'number' => $cost_info['prev_cost_value'],
'value' => $cost_info['prev_cost_value'],
'dots_and_commas_operation' => $dots_and_commas_operation
) );
$new_cost_value = alg_wc_cog_sanitize_number( array(
'number' => $cost_info['new_cost_value'],
'value' => $cost_info['new_cost_value'],
'dots_and_commas_operation' => $dots_and_commas_operation
) );
$table_rows[] = array(
Expand Down
20 changes: 8 additions & 12 deletions includes/class-alg-wc-cog-products.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Cost of Goods for WooCommerce - Products Class.
*
* @version 3.3.0
* @version 3.3.3
* @since 2.1.0
* @author WPFactory
*/
Expand Down Expand Up @@ -236,22 +236,18 @@ function handle_product_columns_style() {
*
* @see https://stackoverflow.com/a/4325608/1193038
*
* @version 2.9.5
* @version 3.3.3
* @since 2.3.5
*
* @param $value
*
* @return mixed
*/
function sanitize_cog_meta( $value ) {
if ( 'yes' === get_option( 'alg_wc_cog_products_sanitize_cog_meta', 'no' ) ) {
$value = alg_wc_cog_sanitize_number( array(
'number' => $value,
'dots_and_commas_operation' => 'comma-to-dot'
) );
}

return $value;
remove_filter( 'sanitize_post_meta_' . '_alg_wc_cog_cost', array( $this, 'sanitize_cog_meta' ) );
return alg_wc_cog_sanitize_cost( array(
'value' => $value,
) );
}

/**
Expand Down Expand Up @@ -406,7 +402,7 @@ function render_product_columns( $column, $product_id ) {
/**
* get_product_cost.
*
* @version 2.9.5
* @version 3.3.3
* @since 1.0.0
*/
function get_product_cost( $product_id, $args = null ) {
Expand All @@ -427,7 +423,7 @@ function get_product_cost( $product_id, $args = null ) {
$cost = get_post_meta( $parent_id, '_alg_wc_cog_cost', true );
}
$cost = alg_wc_cog_sanitize_number( array(
'number' => $cost,
'value' => $cost,
'dots_and_commas_operation' => $dots_and_commas_operation
) );
$cost = $convert_to_number ? (float) $cost : $cost;
Expand Down
2 changes: 1 addition & 1 deletion includes/class-alg-wc-cog.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ final class Alg_WC_Cost_of_Goods {
* @since 1.0.0
* @var string
*/
public $version = '3.3.2';
public $version = '3.3.3';

/**
* @since 1.0.0
Expand Down
Loading

0 comments on commit 61db0dc

Please sign in to comment.