eGift and the output into the cart and checkout totals.
*/
public static function init() {
add_action( 'egift_settings_sections', array( __CLASS__, 'render_setting' ), 20 );
add_action( 'admin_post_' . self::SAVE_ACTION, array( __CLASS__, 'handle_save' ) );
// Rows of the cart totals and of the checkout's order review, so WooCommerce refreshes them
// together with the totals.
add_action( 'woocommerce_cart_totals_after_order_total', array( __CLASS__, 'render' ) );
add_action( 'woocommerce_review_order_after_order_total', array( __CLASS__, 'render' ) );
}
/**
* Whether the Merchant switched the mode on.
*
* @return bool
*/
public static function enabled() {
return ! empty( Egift_Settings::get( 'diagnostics' ) );
}
/**
* Records a calculation of the bons; the last one in a request is shown.
*
* @param int $total The cart total before the bons, in cents.
* @param int $eligible What gift vouchers may pay of it after the egift_eligible_amount filter, in cents.
* @param array[] $bons The bons in the order entered, each with its code, credit and amount.
*/
public static function record( $total, $eligible, array $bons ) {
self::$calculation = array(
'total' => $total,
'eligible' => $eligible,
'bons' => $bons,
);
}
/**
* Saves the checkbox.
*/
public static function handle_save() {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'Sorry, you are not allowed to change these settings.', 'egift' ), 403 );
}
check_admin_referer( self::SAVE_ACTION );
$on = ! empty( $_POST['egift_diagnostics'] );
Egift_Settings::update( array( 'diagnostics' => $on ? true : null ) );
Egift_Settings::redirect_with_notice( 'egift_diagnostics_notice', $on ? 'on' : 'off' );
}
/**
* The diagnostics section of Settings > eGift.
*/
public static function render_setting() {
// Display only, nothing is changed here; the save itself checked the nonce.
$notice = isset( $_GET['egift_diagnostics_notice'] ) ? sanitize_key( wp_unslash( $_GET['egift_diagnostics_notice'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
?>
'success',
'additional_classes' => array( 'inline', 'egift-diagnostics-notice' ),
)
);
}
?>
cart;
}
/**
* The calculation and the active plugins, as one row below the order total.
*/
public static function render() {
if ( ! self::visible() ) {
return;
}
?>
|
eGift.', 'egift' ); ?>
|
cart;
$rows = array(
self::row( 'subtotal', __( 'Subtotal (excl. VAT)', 'egift' ), $cart->get_subtotal() ),
self::row( 'discounts', __( 'Discounts (excl. VAT)', 'egift' ), $cart->get_discount_total(), true ),
self::row( 'shipping', __( 'Shipping (excl. VAT)', 'egift' ), $cart->get_shipping_total() ),
self::row( 'fees', __( 'Fees (excl. VAT)', 'egift' ), $cart->get_fee_total() ),
self::row( 'vat', __( 'VAT', 'egift' ), $cart->get_total_tax() ),
);
$final_total = Egift_Money::to_cents( $cart->get_total( 'edit' ) );
if ( ! Egift_Checkout::shop_accepts_bons() ) {
$rows[] = array( 'note' => __( 'This shop cannot accept gift vouchers now: no API-sleutel is saved, or the shop currency is not euro.', 'egift' ) );
$rows[] = self::row_cents( 'final-total', __( 'Cart total', 'egift' ), $final_total );
return $rows;
}
if ( null === self::$calculation ) {
$rows[] = self::row_cents( 'final-total', __( 'Cart total', 'egift' ), $final_total );
$rows[] = array( 'note' => __( 'No gift voucher is applied to this cart.', 'egift' ) );
return $rows;
}
$calculation = self::$calculation;
$rows[] = self::row_cents( 'total', __( 'Cart total', 'egift' ), $calculation['total'] );
$rows[] = self::row_cents( 'eligible', __( 'Gift vouchers may pay (filter egift_eligible_amount)', 'egift' ), $calculation['eligible'] );
$to_pay = $calculation['total'];
foreach ( $calculation['bons'] as $bon ) {
$amount = Egift_Money::to_cents( $bon['amount'] );
$to_pay -= $amount;
$rows[] = self::row_cents(
'bon',
/* translators: 1: masked gift voucher code, 2: the credit on the gift voucher, for example €50,00 */
sprintf( __( 'Gift voucher %1$s (credit %2$s)', 'egift' ), Egift_Order_Bons::mask( $bon['code'] ), Egift_Money::plain_price( Egift_Money::to_cents( $bon['credit'] ) ) ),
$amount,
true
);
}
$rows[] = self::row_cents( 'to-pay', __( 'To pay, as calculated by eGift', 'egift' ), $to_pay );
$rows[] = self::row_cents( 'final-total', __( 'To pay, after all plugins', 'egift' ), $final_total );
if ( $final_total !== $to_pay ) {
$rows[] = array( 'note' => __( 'Another plugin changed the total after eGift calculated it.', 'egift' ) );
}
return $rows;
}
/**
* A step of the calculation with an amount in euro.
*
* @param string $step A stable name for the step.
* @param string $label Plain text.
* @param float|string $amount An amount in euro.
* @param bool $subtract Whether it is taken off.
* @return array
*/
private static function row( $step, $label, $amount, $subtract = false ) {
return self::row_cents( $step, $label, Egift_Money::to_cents( $amount ), $subtract );
}
/**
* A step of the calculation with an amount in cents.
*
* @param string $step A stable name for the step.
* @param string $label Plain text.
* @param int $cents An amount in cents.
* @param bool $subtract Whether it is taken off.
* @return array
*/
private static function row_cents( $step, $label, $cents, $subtract = false ) {
return array(
'step' => $step,
'label' => $label,
'cents' => $cents,
'subtract' => $subtract,
);
}
/**
* One step of the calculation.
*
* @param string $step A stable name for the step.
* @param string $label Plain text.
* @param string $value Formatted price HTML.
*/
private static function step( $step, $label, $value ) {
?>
|
|
' . esc_html( $text ) . ' | ';
}
/**
* A price in cents as the shop shows it.
*
* @param int $cents An amount in cents.
* @param bool $subtract Whether it is taken off.
* @return string
*/
private static function price_cents( $cents, $subtract = false ) {
return ( $subtract ? Egift_Money::MINUS : '' ) . wc_price( Egift_Money::from_cents( $cents ) );
}
/**
* The active plugins with their versions: those of this site, the network's and the must-use ones.
*
* @return string[]
*/
public static function active_plugins() {
if ( ! function_exists( 'get_plugins' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
$installed = get_plugins();
$files = (array) get_option( 'active_plugins', array() );
if ( is_multisite() ) {
$files = array_merge( $files, array_keys( (array) get_site_option( 'active_sitewide_plugins', array() ) ) );
}
$plugins = array();
foreach ( array_unique( $files ) as $file ) {
$plugins[] = isset( $installed[ $file ] ) ? self::plugin_line( $installed[ $file ] ) : (string) $file;
}
foreach ( get_mu_plugins() as $data ) {
/* translators: %s: a must-use plugin's name and version */
$plugins[] = sprintf( __( '%s (must-use)', 'egift' ), self::plugin_line( $data ) );
}
return $plugins;
}
/**
* A plugin's name and version.
*
* @param array $data The plugin's header data.
* @return string
*/
private static function plugin_line( array $data ) {
return trim( $data['Name'] . ' ' . $data['Version'] );
}
}