Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/woocomme.../src/Blocks/Domain/Services
File: CheckoutLink.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* Functionality that takes a static URL, constructs a cart, and redirects to the checkout with a cart session.
[2] Fix | Delete
*/
[3] Fix | Delete
[4] Fix | Delete
declare(strict_types=1);
[5] Fix | Delete
[6] Fix | Delete
namespace Automattic\WooCommerce\Blocks\Domain\Services;
[7] Fix | Delete
[8] Fix | Delete
use Automattic\WooCommerce\Blocks\Utils\CartCheckoutUtils;
[9] Fix | Delete
use Automattic\WooCommerce\StoreApi\Utilities\CartTokenUtils;
[10] Fix | Delete
use Automattic\WooCommerce\StoreApi\Utilities\CartController;
[11] Fix | Delete
[12] Fix | Delete
defined( 'ABSPATH' ) || exit;
[13] Fix | Delete
[14] Fix | Delete
/**
[15] Fix | Delete
* Checkout Link class.
[16] Fix | Delete
*/
[17] Fix | Delete
class CheckoutLink {
[18] Fix | Delete
/**
[19] Fix | Delete
* Initialize the checkout link service.
[20] Fix | Delete
*/
[21] Fix | Delete
public function init() {
[22] Fix | Delete
add_action( 'init', array( $this, 'add_checkout_link_endpoint' ) );
[23] Fix | Delete
add_filter( 'query_vars', array( $this, 'add_query_vars' ), 0 );
[24] Fix | Delete
add_action( 'template_redirect', array( $this, 'handle_checkout_link_endpoint' ) );
[25] Fix | Delete
}
[26] Fix | Delete
[27] Fix | Delete
/**
[28] Fix | Delete
* Add the checkout link endpoint.
[29] Fix | Delete
*/
[30] Fix | Delete
public function add_checkout_link_endpoint() {
[31] Fix | Delete
// get registered rewrite rules.
[32] Fix | Delete
$rules = get_option( 'rewrite_rules', array() );
[33] Fix | Delete
$regex = '^checkout-link$';
[34] Fix | Delete
[35] Fix | Delete
add_rewrite_rule( $regex, 'index.php?checkout-link=true', 'top' );
[36] Fix | Delete
[37] Fix | Delete
// maybe flush rewrite rules if it was not previously in the option.
[38] Fix | Delete
if ( ! isset( $rules[ $regex ] ) ) {
[39] Fix | Delete
flush_rewrite_rules();
[40] Fix | Delete
}
[41] Fix | Delete
}
[42] Fix | Delete
[43] Fix | Delete
/**
[44] Fix | Delete
* Add the checkout link query var.
[45] Fix | Delete
*
[46] Fix | Delete
* @param array $vars The query vars.
[47] Fix | Delete
* @return array The query vars.
[48] Fix | Delete
*/
[49] Fix | Delete
public function add_query_vars( $vars ) {
[50] Fix | Delete
$vars[] = 'checkout-link';
[51] Fix | Delete
return $vars;
[52] Fix | Delete
}
[53] Fix | Delete
[54] Fix | Delete
/**
[55] Fix | Delete
* Handle the checkout link endpoint.
[56] Fix | Delete
*
[57] Fix | Delete
* @return void
[58] Fix | Delete
*/
[59] Fix | Delete
public function handle_checkout_link_endpoint() {
[60] Fix | Delete
if ( ! get_query_var( 'checkout-link' ) ) {
[61] Fix | Delete
return;
[62] Fix | Delete
}
[63] Fix | Delete
[64] Fix | Delete
if ( ! $this->validate_checkout_link() ) {
[65] Fix | Delete
$redirect = add_query_arg( 'wc_error', rawurlencode( __( 'The provided checkout link was out of date or invalid. No products were added to the cart.', 'woocommerce' ) ), wc_get_cart_url() );
[66] Fix | Delete
} else {
[67] Fix | Delete
wc()->cart->empty_cart();
[68] Fix | Delete
$redirect = $this->get_checkout_link();
[69] Fix | Delete
}
[70] Fix | Delete
[71] Fix | Delete
wp_safe_redirect( $redirect );
[72] Fix | Delete
exit;
[73] Fix | Delete
}
[74] Fix | Delete
[75] Fix | Delete
/**
[76] Fix | Delete
* Validate the checkout link.
[77] Fix | Delete
*
[78] Fix | Delete
* @return bool True if the checkout link is valid, false otherwise.
[79] Fix | Delete
*/
[80] Fix | Delete
protected function validate_checkout_link() {
[81] Fix | Delete
$products = $this->get_products_from_checkout_link();
[82] Fix | Delete
[83] Fix | Delete
return ! empty( $products );
[84] Fix | Delete
}
[85] Fix | Delete
[86] Fix | Delete
/**
[87] Fix | Delete
* Get the products from the checkout link.
[88] Fix | Delete
*
[89] Fix | Delete
* @return array The products (keys) and their quantities (values).
[90] Fix | Delete
*/
[91] Fix | Delete
protected function get_products_from_checkout_link() {
[92] Fix | Delete
$raw_products = array_filter( explode( ',', wc_clean( wp_unslash( $_GET['products'] ?? '' ) ) ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
[93] Fix | Delete
$products = [];
[94] Fix | Delete
[95] Fix | Delete
foreach ( $raw_products as $product_id_qty ) {
[96] Fix | Delete
if ( strpos( $product_id_qty, ':' ) !== false ) {
[97] Fix | Delete
list( $product_id, $qty ) = explode( ':', $product_id_qty );
[98] Fix | Delete
} else {
[99] Fix | Delete
$product_id = $product_id_qty;
[100] Fix | Delete
$qty = 1;
[101] Fix | Delete
}
[102] Fix | Delete
$product_id = absint( $product_id );
[103] Fix | Delete
$qty = absint( $qty );
[104] Fix | Delete
[105] Fix | Delete
if ( ! $product_id || ! $qty ) {
[106] Fix | Delete
continue;
[107] Fix | Delete
}
[108] Fix | Delete
[109] Fix | Delete
$products[ $product_id ] = $qty;
[110] Fix | Delete
}
[111] Fix | Delete
[112] Fix | Delete
return $products;
[113] Fix | Delete
}
[114] Fix | Delete
[115] Fix | Delete
/**
[116] Fix | Delete
* Add error notices to the cart.
[117] Fix | Delete
*
[118] Fix | Delete
* @param \WP_Error $errors The errors.
[119] Fix | Delete
* @return void
[120] Fix | Delete
*/
[121] Fix | Delete
protected function add_error_notices( \WP_Error $errors ) {
[122] Fix | Delete
foreach ( $errors->get_error_messages() as $message ) {
[123] Fix | Delete
wc_add_notice( $message, 'error' );
[124] Fix | Delete
}
[125] Fix | Delete
}
[126] Fix | Delete
[127] Fix | Delete
/**
[128] Fix | Delete
* Process the query params and return the checkout link to redirect to complete with session token.
[129] Fix | Delete
*
[130] Fix | Delete
* @return string The checkout link.
[131] Fix | Delete
*/
[132] Fix | Delete
protected function get_checkout_link() {
[133] Fix | Delete
$controller = new CartController();
[134] Fix | Delete
$products = $this->get_products_from_checkout_link();
[135] Fix | Delete
$errors = new \WP_Error();
[136] Fix | Delete
[137] Fix | Delete
foreach ( $products as $product_id => $qty ) {
[138] Fix | Delete
try {
[139] Fix | Delete
$controller->add_to_cart(
[140] Fix | Delete
[
[141] Fix | Delete
'id' => $product_id,
[142] Fix | Delete
'quantity' => $qty,
[143] Fix | Delete
]
[144] Fix | Delete
);
[145] Fix | Delete
} catch ( \Exception $e ) {
[146] Fix | Delete
$errors->add( 'error', $e->getMessage() );
[147] Fix | Delete
}
[148] Fix | Delete
}
[149] Fix | Delete
[150] Fix | Delete
// Nothing was added to the cart. We need to redirect to the cart page with an error notice. Since guests may not
[151] Fix | Delete
// have a session, add the notice in the query string.
[152] Fix | Delete
if ( wc()->cart->is_empty() ) {
[153] Fix | Delete
$errors->add( 'error', __( 'The provided checkout link was out of date or invalid. No products were added to the cart.', 'woocommerce' ) );
[154] Fix | Delete
[155] Fix | Delete
if ( ! wc()->session->has_session() ) {
[156] Fix | Delete
return add_query_arg( 'wc_error', rawurlencode( $errors->get_error_message() ), wc_get_cart_url() );
[157] Fix | Delete
} else {
[158] Fix | Delete
$this->add_error_notices( $errors );
[159] Fix | Delete
}
[160] Fix | Delete
[161] Fix | Delete
return wc_get_cart_url();
[162] Fix | Delete
}
[163] Fix | Delete
[164] Fix | Delete
// Apply coupon if provided.
[165] Fix | Delete
$coupon = wc_format_coupon_code( wp_unslash( $_GET['coupon'] ?? '' ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
[166] Fix | Delete
[167] Fix | Delete
if ( wc_coupons_enabled() && ! empty( $coupon ) ) {
[168] Fix | Delete
try {
[169] Fix | Delete
$controller->apply_coupon( $coupon );
[170] Fix | Delete
} catch ( \Exception $e ) {
[171] Fix | Delete
$errors->add( 'error', $e->getMessage() );
[172] Fix | Delete
}
[173] Fix | Delete
}
[174] Fix | Delete
[175] Fix | Delete
// Add error notices to the cart. This requires a session otherwise the notices will not be displayed.
[176] Fix | Delete
$this->add_error_notices( $errors );
[177] Fix | Delete
[178] Fix | Delete
$redirect_url = wc_get_checkout_url();
[179] Fix | Delete
[180] Fix | Delete
// Preserve the query string--pass it to the checkout page.
[181] Fix | Delete
if ( ! empty( $_SERVER['QUERY_STRING'] ) ) {
[182] Fix | Delete
$redirect_url = remove_query_arg(
[183] Fix | Delete
[
[184] Fix | Delete
'products',
[185] Fix | Delete
'coupon',
[186] Fix | Delete
'checkout-link',
[187] Fix | Delete
],
[188] Fix | Delete
add_query_arg( wp_unslash( $_SERVER['QUERY_STRING'] ), '', $redirect_url ) // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
[189] Fix | Delete
);
[190] Fix | Delete
}
[191] Fix | Delete
[192] Fix | Delete
// If the user is logged in, the session is tied to the user ID. Do not use a cart token.
[193] Fix | Delete
if ( ! is_user_logged_in() ) {
[194] Fix | Delete
$session_token = CartTokenUtils::get_cart_token( (string) wc()->session->get_customer_id() );
[195] Fix | Delete
$redirect_url = add_query_arg( 'session', $session_token, $redirect_url );
[196] Fix | Delete
}
[197] Fix | Delete
[198] Fix | Delete
return $redirect_url;
[199] Fix | Delete
}
[200] Fix | Delete
}
[201] Fix | Delete
[202] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function