クラス
CartPage
ソース ソース
ファイル: src/Views/CartPage/CartPage.php
class CartPage
{
const SCRIPT_HANDLE = 'wcexwl-cart';
/**
* Master object
*
* @var Master
*/
protected $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
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return void
*/
public function init() {
add_filter('usces_filter_cart_upbutton', [$this, 'cartBatchDropdown'], 10, 1);
add_filter('usces_theme_filter_upbutton', [$this, 'cartBatchDropdown'], 10, 1);
add_filter('usces_filter_cart_row', [$this, 'filterCartRow'], 11, 3);
add_action('usces_action_cart_page_header', [$this, 'startMobileBuffer'], 1);
add_action('usces_action_cart_page_inform', [$this, 'endMobileBuffer'], 1);
// for deprecated direct edit of theme file wc_cart_page.php
add_filter('asumil_filter_cart_batch_dropdown', [$this, 'cartBatchDropdown'], 10, 1);
}
/**
* Dropdown list of batch actions. Displayed on the cart page
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @param string $html
* @return string
*/
public function cartBatchDropdown($html) {
/**
* Return `false` with this hook to prevent cart page CSS from being loaded
*
* @important
* @param bool $flag Default: `true`
*/
if (apply_filters('wcexwl_filter_cart_page_load_css', true)) {
// load semantic-ui and common css
Utils::loadCommonCss();
Semantic::loadLoaderCss();
// load css for page
wp_enqueue_style(
'wcexwl-cart-page',
WCEXWL_PLUGIN_URL . '/src/Styles/cart.css',
[],
WCEXWL_VERSION
);
}
// load JS client SDK
$this->master->loadClientSdk();
/**
* Fires after all cart page common/default JS/CSS has been enqueued
*
* Use this hook to enqueue your own JS/CSS on the cart page if necessary.
*
* @important
*/
do_action('wcexwl_cart_page_on_load_assets');
return $this->loadImplementation($html);
}
/**
* Dropdown list of batch actions. Displayed on the cart page
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @param string $html
* @return string
*/
protected function loadImplementation($html) {
wp_enqueue_script(
self::SCRIPT_HANDLE,
WCEXWL_PLUGIN_URL . '/src/Views/CartPage/cart.js',
[],
WCEXWL_VERSION,
true
);
wp_localize_script(self::SCRIPT_HANDLE, 'wcexwl', $this->master->getScriptInjectionVariables());
Snackbar::load([self::SCRIPT_HANDLE]);
$html = self::getBatchActionsHtml();
return $html;
}
/**
* Returns batch actions section HTML
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return string
*/
public static function getBatchActionsHtml() {
$display = Master::getFilterableValues();
ob_start();
?>
<div class="wcexwl flex column-nowrap mb-1rem cart-page-batch-actions">
<div class="upquant-inform"><?php _e('Press the `update` button when you change the amount of items.', 'usces'); ?></div>
<div class="batch-select-container wcexwl flex row-nowrap">
<div class="wcexwl flex row-nowrap">
<select id="wcexwl_batch_dropdown" class="wcexwl batch-select" value="none">
<option value="none"><?php echo $display['selectText']; ?></option>
<option value="addToWishlist"><?php echo $display['toWishlistSelectText']; ?></option>
<option value="batchDelete"><?php echo $display['deleteSelectText']; ?></option>
</select>
<input
id="wcexwl_batch_button"
class="wcexwl <?php echo $display['btnprimary']; ?> batch-button"
type="submit"
value="<?php echo $display['cartBatchButtonText']; ?>"
onclick="return wcexwl.cart.handleBatchAction(event);"
/>
</div>
<div class="wcexwl flex p-0 m-0 ml-auto upbutton cart-quant-update-btn">
<input
type="submit"
name="upButton"
class="wcexwl m-0"
value="<?php _e('Quantity renewal', 'usces'); ?>"
onclick="return uscesCart.upCart();"
/>
</div>
</div>
</div>
<?php
$html = (string)ob_get_clean();
/**
* Filters cart page batch dropdown HTML section
*
* @important
* @param string $html
*/
$html = apply_filters('wcexwl_filter_cart_batch_dropdown_html', $html);
return $html;
}
/**
* `preg_replace`s cart number td in cart table with a checkbox for batch operations
*
* Since WCEX Mobile does not specify `class="num"` on the number td, we just assume
* that the first td in the row is the one we want to replace.
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @param string $row
* @param array $cart
* @param array $args
* @return string
*/
public function filterCartRow($row, $cart, $args) {
$pattern = '/(<td\s+class="num">).*(<\/td>)/';
if (self::isMobileTheme()) {
$pattern = '/(<td.*>).*(<\/td>)/';
}
$replacement = preg_replace(
$pattern,
'${1}' . self::getCartRowCheckbox($args['cart_row']['serial']) . '${2}',
$row,
1
);
return $replacement === null ? $row : $replacement;
}
/**
* Returns checkox HTML for cart row td
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @param string $serial
* @return string
*/
public static function getCartRowCheckbox($serial) {
ob_start();
?>
<div class="wcexwl flex ai-center jc-center">
<input class="wcexwl-batch-action-checkbox" value="<?php echo urlencode($serial); ?>" type="checkbox" />
</div>
<?php
$checboxtd = (string)ob_get_clean();
return $checboxtd;
}
/**
* There is no filter for modifying the quantity update button for WCEX Mobile themes
* so we are forced to `preg_replace` buffered output of the cart page
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return void
*/
public function startMobileBuffer() {
if (!self::isMobileTheme()) {
return;
}
ob_start();
}
/**
* `preg_replace` quantity update button with our batch action HTML
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return void
*/
public function endMobileBuffer() {
if (!self::isMobileTheme()) {
return;
}
$html = (string)ob_get_clean();
$dropdown = $this->cartBatchDropdown('');
$replacement = preg_replace('/<div\s+class="upbutton">.*<\/div>/', $dropdown, $html, 1);
echo $replacement === null ? $html : $replacement;
}
/**
* Returns `true` if WCEX Mobile is enabled and a mobile theme is being used, `false` otherwise
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return bool
*/
public static function isMobileTheme() {
if (!defined('WCEX_MOBILE')) {
return false;
}
global $wcmb;
$d = $wcmb['device_div'];
if (SMARTPHONE === $d || DOCOMO === $d || SOFTBANK === $d || KDDI === $d) {
return true;
}
return false;
}
}
- __construct — Injects `Master` object
- cartBatchDropdown — Dropdown list of batch actions. Displayed on the cart page
- endMobileBuffer — `preg_replace` quantity update button with our batch action HTML
- filterCartRow — `preg_replace`s cart number td in cart table with a checkbox for batch operations
- getBatchActionsHtml — Returns batch actions section HTML
- getCartRowCheckbox — Returns checkox HTML for cart row td
- init — Registers hooks
- isMobileTheme — Returns `true` if WCEX Mobile is enabled and a mobile theme is being used, `false` otherwise
- loadImplementation — Dropdown list of batch actions. Displayed on the cart page
- startMobileBuffer — There is no filter for modifying the quantity update button for WCEX Mobile themes so we are forced to `preg_replace` buffered output of the cart page