クラス
Redirect
ソース ソース
ファイル: src/Routes/Redirect.php
class Redirect
{
const REDIRECT_KEY_ADD_TO_WISHLIST = 'wcexwlAddToWishlist';
const REDIRECT_KEY_ADD_TO_WISHLIST_BY_SERIAL = 'wcexwlAddToWishlistBySerial';
/**
* Master object
*
* @var Master
*/
private $master;
/**
* Injects `Master` object
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @param Master $master
* @return void
*/
public function __construct(Master $master) {
$this->master = $master;
}
/**
* Registers hooks for redirection
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return void
*/
public function init() {
add_action('usces_action_after_login', [$this, 'handleAddToWishlistRedirectOnLogin'], 1, 1);
}
/**
* If the user attempted to add an item to their wishlist and was redirected
* to the login page, add that item after a successful login and redirect to
* the wishlist page.
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @global \usc_e_shop $usces
* @return void
*/
public function handleAddToWishlistRedirectOnLogin() {
global $usces;
// we have to do this because the hook is called before cookie is set. {@see usceshop.class.php LINE 3797}
$usces->set_cookie($usces->get_cookie());
if (!empty($_POST[self::REDIRECT_KEY_ADD_TO_WISHLIST])) {
$paramstring = urldecode((string)$_POST[self::REDIRECT_KEY_ADD_TO_WISHLIST]);
parse_str($paramstring, $item);
$backuppost = $_POST;
foreach ($item as $key => $val) {
$_POST[$key] = $val;
}
$added = (new API\Item($this->master))->addToWishlist(
$item['postId'],
$item['sku'],
isset($item['itemOption']) ? $item['itemOption'] : [],
isset($item['advance']) ? $item['advance'] : ''
);
foreach ($item as $key => $val) {
if (isset($backuppost[$key])) {
$_POST[$key] = $backuppost[$key];
} else {
unset($_POST[$key]);
}
}
if ($added !== true) {
return;
}
wp_safe_redirect(WCEXWL_PAGE_URL);
exit;
} elseif (!empty($_POST[self::REDIRECT_KEY_ADD_TO_WISHLIST_BY_SERIAL])) {
$cart = isset($_SESSION['usces_cart']) ? $_SESSION['usces_cart'] : [];
$serials = (array)$_POST[self::REDIRECT_KEY_ADD_TO_WISHLIST_BY_SERIAL];
$cartapi = new API\Cart($this->master);
$erroroccured = false;
foreach ($serials as $encodedserial) {
$serial = urldecode($encodedserial);
$advance = isset($cart[$serial]['advance']) ? $cart[$serial]['advance'] : '';
$added = $cartapi->addToWishlistBySerial($serial, $advance);
if ($added !== true) {
$erroroccured = true;
}
}
if ($erroroccured === true) {
return;
}
wp_safe_redirect(WCEXWL_PAGE_URL);
exit;
}
}
}
- __construct — Injects `Master` object
- handleAddToWishlistRedirectOnLogin — If the user attempted to add an item to their wishlist and was redirected to the login page, add that item after a successful login and redirect to the wishlist page.
- init — Registers hooks for redirection