AutoPurge.php
4 months ago
BeaverBuilder.php
4 months ago
CPTOptimization.php
1 month ago
CacheWarmup.php
1 month ago
CartCache.php
4 months ago
Components.php
1 month ago
EditorClearCache.php
1 month ago
GeneratePreview.php
7 months ago
HTMLCompression.php
3 months ago
Logger.php
4 months ago
OptimizationLevel.php
3 days ago
Optimizations.php
1 month ago
PurgeCache.php
3 days ago
Shortcodes.php
4 months ago
StockRefresh.php
2 months ago
Subscription.php
4 months ago
SystemReport.php
4 months ago
TestMode.php
1 month ago
CartCache.php
137 lines
| 1 | <?php |
| 2 | |
| 3 | namespace NitroPack\WordPress\Settings; |
| 4 | use Nitropack\WordPress\NitroPack; |
| 5 | |
| 6 | class CartCache { |
| 7 | |
| 8 | public function __construct() { |
| 9 | add_action( 'wp_ajax_nitropack_set_cart_cache_ajax', [ $this, 'nitropack_set_cart_cache_ajax' ] ); |
| 10 | } |
| 11 | |
| 12 | /** |
| 13 | * AJAX handler when toggling the setting in the Dashboard |
| 14 | * @return void |
| 15 | */ |
| 16 | public function nitropack_set_cart_cache_ajax() { |
| 17 | nitropack_verify_ajax_nonce( $_REQUEST ); |
| 18 | if ( get_nitropack()->isConnected() && class_exists( 'WooCommerce' ) ) { |
| 19 | $cartCacheStatus = (int) ( ! empty( $_POST["cartCacheStatus"] ) ); |
| 20 | |
| 21 | if ( $cartCacheStatus == 1 ) { |
| 22 | $this->enable_cart_cache(); |
| 23 | } else { |
| 24 | $this->disable_cart_cache(); |
| 25 | } |
| 26 | } |
| 27 | nitropack_json_and_exit( array( |
| 28 | "type" => "error", |
| 29 | "message" => nitropack_admin_toast_msgs( 'error' ) |
| 30 | ) ); |
| 31 | } |
| 32 | public function enable_cart_cache() { |
| 33 | if ( null !== $nitro = get_nitropack_sdk() ) { |
| 34 | try { |
| 35 | $nitro->enableCartCache(); |
| 36 | NitroPack::getInstance()->getLogger()->notice( 'Cart cache is enabled' ); |
| 37 | nitropack_json_and_exit( array( |
| 38 | "type" => "success", |
| 39 | "message" => nitropack_admin_toast_msgs( 'success' ) |
| 40 | ) ); |
| 41 | } catch (\Exception $e) { |
| 42 | NitroPack::getInstance()->getLogger()->error( 'Cart cache cannot be enabled. Error: ' . $e ); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | nitropack_json_and_exit( array( |
| 47 | "type" => "error", |
| 48 | "message" => nitropack_admin_toast_msgs( 'error' ) |
| 49 | ) ); |
| 50 | } |
| 51 | |
| 52 | public function disable_cart_cache() { |
| 53 | if ( null !== $nitro = get_nitropack_sdk() ) { |
| 54 | try { |
| 55 | $nitro->disableCartCache(); |
| 56 | NitroPack::getInstance()->getLogger()->notice( 'Cart cache is disabled' ); |
| 57 | nitropack_json_and_exit( array( |
| 58 | "type" => "success", |
| 59 | "message" => nitropack_admin_toast_msgs( 'success' ) |
| 60 | ) ); |
| 61 | } catch (\Exception $e) { |
| 62 | NitroPack::getInstance()->getLogger()->error( 'Cart cache cannot be disabled. Error: ' . $e ); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | nitropack_json_and_exit( array( |
| 67 | "type" => "error", |
| 68 | "message" => nitropack_admin_toast_msgs( 'error' ) |
| 69 | ) ); |
| 70 | } |
| 71 | public function nitropack_is_cart_cache_active() { |
| 72 | $nitro = get_nitropack()->getSdk(); |
| 73 | if ( $nitro ) { |
| 74 | $config = $nitro->getConfig(); |
| 75 | if ( ! empty( $config->StatefulCache->Status ) && ! empty( $config->StatefulCache->CartCache ) ) { |
| 76 | return $this->nitropack_is_cart_cache_available(); |
| 77 | } |
| 78 | } |
| 79 | return false; |
| 80 | } |
| 81 | public function nitropack_is_cart_cache_available() { |
| 82 | $nitro = get_nitropack()->getSdk(); |
| 83 | if ( $nitro ) { |
| 84 | $config = $nitro->getConfig(); |
| 85 | if ( ! empty( $config->StatefulCache->isCartCacheAvailable ) ) { |
| 86 | return true; |
| 87 | } |
| 88 | } |
| 89 | return false; |
| 90 | } |
| 91 | /** |
| 92 | * Renders the Cart Cache option in the Dashboard |
| 93 | * @return void |
| 94 | */ |
| 95 | public function render() { |
| 96 | if ( class_exists( 'WooCommerce' ) ) { ?> |
| 97 | <div class="nitro-option" id="cart-cache-widget"> |
| 98 | <div class="nitro-option-main"> |
| 99 | <div class="text-box"> |
| 100 | <h6><?php esc_html_e( 'Cart cache', 'nitropack' ); ?></h6> |
| 101 | <p> |
| 102 | <?php esc_html_e( 'Your visitors will enjoy full site speed while browsing with items in cart. Fully optimized page cache will be served.', 'nitropack' ); ?> |
| 103 | </p> |
| 104 | </div> |
| 105 | <?php $components = new Components(); |
| 106 | $components->render_toggle( 'cart-cache-status', $this->nitropack_is_cart_cache_active(), [ 'disabled' => ! $this->nitropack_is_cart_cache_available() ] ); ?> |
| 107 | </div> |
| 108 | <?php if ( ! $this->nitropack_is_cart_cache_available() ) : ?> |
| 109 | <div class="msg-container bg-success paid-msg"> |
| 110 | <p><svg width="20" height="20" viewBox="0 0 20 20" fill="none" xmlns="http://www.w3.org/2000/svg" |
| 111 | class="text-success"> |
| 112 | <g clip-path="url(#clip0_1244_36215)"> |
| 113 | <path |
| 114 | d="M10.0001 18.3333C14.6025 18.3333 18.3334 14.6023 18.3334 9.99996C18.3334 5.39759 14.6025 1.66663 10.0001 1.66663C5.39771 1.66663 1.66675 5.39759 1.66675 9.99996C1.66675 14.6023 5.39771 18.3333 10.0001 18.3333Z" |
| 115 | stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"></path> |
| 116 | <path d="M13.3334 9.99996L10.0001 6.66663L6.66675 9.99996" stroke="currentColor" stroke-width="1.5" |
| 117 | stroke-linecap="round" stroke-linejoin="round"></path> |
| 118 | <path d="M10 13.3333V6.66663" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" |
| 119 | stroke-linejoin="round"></path> |
| 120 | </g> |
| 121 | <defs> |
| 122 | <clipPath id="clip0_1244_36215"> |
| 123 | <rect width="20" height="20" fill="white"></rect> |
| 124 | </clipPath> |
| 125 | </defs> |
| 126 | </svg> |
| 127 | <?php esc_html_e( 'This feature is available on Plus plan and above.', 'nitropack' ); ?> |
| 128 | <a href="https://app.nitropack.io/subscription/buy" class="text-primary" |
| 129 | target="_blank"><b><?php esc_html_e( 'Upgrade here', 'nitropack' ); ?></b></a> |
| 130 | </p> |
| 131 | </div> |
| 132 | <?php endif; ?> |
| 133 | </div> |
| 134 | <?php |
| 135 | } |
| 136 | } |
| 137 | } |