| 1 |
<?php |
| 2 |
/** |
| 3 |
* Caddy Cart Block Render Template |
| 4 |
* |
| 5 |
* Renders the interactive cart block using the WordPress Interactivity API. |
| 6 |
* |
| 7 |
* @package Caddy |
| 8 |
* @since 2.1.3 |
| 9 |
*/ |
| 10 |
|
| 11 |
if (!defined('ABSPATH')) { |
| 12 |
exit; // Exit if accessed directly |
| 13 |
} |
| 14 |
|
| 15 |
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Template partial; these variables are scoped to the including method, not the global namespace. |
| 16 |
|
| 17 |
// Get block attributes with defaults |
| 18 |
$auto_open = $attributes['autoOpen'] ?? true; |
| 19 |
|
| 20 |
// Only render the free version when the premium plugin is not active |
| 21 |
if (!class_exists('Caddy_Premium')) { |
| 22 |
// Note: we deliberately do NOT force a WooCommerce session cookie here. |
| 23 |
// Setting a cookie on every guest pageview makes the response uncacheable |
| 24 |
// (WP Rocket and host page caches skip responses with Set-Cookie), which |
| 25 |
// disabled page caching site-wide for first-time visitors. Sessionless |
| 26 |
// guests render an empty drawer; the Store API creates the session on |
| 27 |
// their first cart mutation. |
| 28 |
if (function_exists('WC') && WC()->session) { |
| 29 |
// Ensure cart is loaded |
| 30 |
if (WC()->cart) { |
| 31 |
$cart_count = WC()->cart->get_cart_contents_count(); |
| 32 |
if ( $cart_count > 0 ) { |
| 33 |
WC()->cart->calculate_totals(); |
| 34 |
} |
| 35 |
} else { |
| 36 |
$cart_count = 0; |
| 37 |
} |
| 38 |
} else { |
| 39 |
$cart_count = 0; |
| 40 |
} |
| 41 |
?> |
| 42 |
<div data-wp-interactive="caddy/cart" data-wp-context='{"autoOpen": <?php echo json_encode($auto_open); ?>}'> |
| 43 |
<!-- Floating cart launcher --> |
| 44 |
<div class="cc-compass" role="button" tabindex="0" aria-label="<?php esc_attr_e( 'Cart', 'caddy' ); ?>" aria-expanded="false" data-wp-bind--aria-expanded="state.isOpen" data-wp-on--click="actions.toggleCart" data-wp-on--keydown="actions.handleCompassKeydown" data-wp-class--cc-compass-open="state.isOpen"> |
| 45 |
<span class="cc-compass-cart-icon" data-wp-class--cc-hidden="state.isOpen"> |
| 46 |
<?php |
| 47 |
$allowed_svg = array( |
| 48 |
'svg' => array( 'xmlns' => true, 'viewbox' => true, 'width' => true, 'height' => true, 'fill' => true, 'stroke' => true, 'stroke-linecap' => true, 'stroke-linejoin' => true, 'style' => true, 'class' => true, 'aria-hidden' => true ), |
| 49 |
'path' => array( 'd' => true, 'stroke-width' => true, 'fill' => true, 'stroke' => true ), |
| 50 |
'circle' => array( 'cx' => true, 'cy' => true, 'r' => true, 'fill' => true, 'stroke' => true, 'stroke-width' => true ), |
| 51 |
'rect' => array( 'x' => true, 'y' => true, 'width' => true, 'height' => true, 'rx' => true, 'ry' => true, 'fill' => true, 'stroke' => true, 'stroke-width' => true ), |
| 52 |
'line' => array( 'x1' => true, 'y1' => true, 'x2' => true, 'y2' => true, 'stroke' => true, 'stroke-width' => true ), |
| 53 |
'polyline' => array( 'points' => true, 'fill' => true, 'stroke' => true, 'stroke-width' => true ), |
| 54 |
'polygon' => array( 'points' => true, 'fill' => true, 'stroke' => true, 'stroke-width' => true ), |
| 55 |
'g' => array( 'fill' => true, 'stroke' => true, 'transform' => true ), |
| 56 |
); |
| 57 |
echo wp_kses( apply_filters('caddy_cart_bubble_icon', '<svg xmlns="http://www.w3.org/2000/svg" viewBox="-0.5 -0.5 24 24" width="24" height="24" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" style="display:inline-block;vertical-align:middle;"><path d="M5.75 1.9166666666666667 2.875 5.75v13.416666666666668a1.9166666666666667 1.9166666666666667 0 0 0 1.9166666666666667 1.9166666666666667h13.416666666666668a1.9166666666666667 1.9166666666666667 0 0 0 1.9166666666666667 -1.9166666666666667V5.75l-2.875 -3.8333333333333335z" stroke-width="1.2"></path><path d="m2.875 5.75 17.25 0" stroke-width="1.2"></path><path d="M15.333333333333334 9.583333333333334a3.8333333333333335 3.8333333333333335 0 0 1 -7.666666666666667 0" stroke-width="1.2"></path></svg>'), $allowed_svg ); |
| 58 |
?> |
| 59 |
</span> |
| 60 |
<span class="cc-compass-close-icon cc-hidden" data-wp-class--cc-hidden="!state.isOpen"> |
| 61 |
<i class="ccicon-close"></i> |
| 62 |
</span> |
| 63 |
<div class="cc-loader" style="display: none;"></div> |
| 64 |
<span class="cc-compass-count" |
| 65 |
data-wp-class--cc-cart-zero="state.cartCount === 0" |
| 66 |
data-wp-text="state.cartCount"> |
| 67 |
<?php echo esc_html($cart_count); ?> |
| 68 |
</span> |
| 69 |
</div> |
| 70 |
|
| 71 |
<!-- Cart window --> |
| 72 |
<div class="cc-window disable-scrollbars" role="dialog" aria-modal="true" aria-label="<?php esc_attr_e( 'Shopping cart', 'caddy' ); ?>" data-wp-class--cc-show="state.isOpen"> |
| 73 |
<div class="cc-window-wrapper"> |
| 74 |
<?php |
| 75 |
include(plugin_dir_path(__FILE__) . 'public/partials/caddy-public-window.php'); |
| 76 |
?> |
| 77 |
</div> |
| 78 |
<!-- Screen-reader live region announcing cart state changes --> |
| 79 |
<div class="cc-sr-announce" aria-live="polite" aria-atomic="true"></div> |
| 80 |
</div> |
| 81 |
|
| 82 |
<!-- Overlay --> |
| 83 |
<div class="cc-overlay" data-wp-class--cc-show="state.isOpen" data-wp-on--click="actions.closeCart"></div> |
| 84 |
</div> |
| 85 |
<?php |
| 86 |
} |
| 87 |
?> |