CartService.php
292 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Cart; |
| 4 | |
| 5 | use SureCart\Models\Form; |
| 6 | |
| 7 | /** |
| 8 | * The cart service. |
| 9 | */ |
| 10 | class CartService { |
| 11 | /** |
| 12 | * Bootstrap the cart. |
| 13 | * |
| 14 | * @return void |
| 15 | */ |
| 16 | public function bootstrap() { |
| 17 | add_filter( 'wp_nav_menu_items', array( $this, 'addCartMenu' ), 10, 2 ); |
| 18 | |
| 19 | // only load scripts if cart is enabled. |
| 20 | if ( $this->isCartEnabled() ) { |
| 21 | add_action( |
| 22 | 'wp_enqueue_scripts', |
| 23 | function () { |
| 24 | // Enqueue the cart drawer script modules. |
| 25 | wp_enqueue_script_module( '@surecart/cart' ); |
| 26 | wp_enqueue_script_module( '@surecart/checkout' ); |
| 27 | } |
| 28 | ); |
| 29 | |
| 30 | add_action( 'template_include', array( $this, 'includeCartTemplate' ) ); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Get the icon name saved in the settings |
| 36 | * |
| 37 | * @return string |
| 38 | */ |
| 39 | public function getIconNameFromSettings() { |
| 40 | return get_option( 'surecart_cart_icon', 'shopping-bag' ); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Get the icon. |
| 45 | * |
| 46 | * @param 'menu'|'floating' $type Menu type. |
| 47 | * |
| 48 | * @return string |
| 49 | */ |
| 50 | public function getIcon( $type ) { |
| 51 | $icon = $this->getIconNameFromSettings(); |
| 52 | |
| 53 | /** |
| 54 | * Allow filtering of the cart menu icon. |
| 55 | * |
| 56 | * @param string $icon The icon. |
| 57 | * @param string $mode The icon position. |
| 58 | */ |
| 59 | return apply_filters( 'sc_cart_menu_icon', $icon, $type ); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Get selected ids. |
| 64 | * |
| 65 | * @return array|false |
| 66 | */ |
| 67 | public function getSelectedIds() { |
| 68 | return get_option( 'surecart_cart_menu_selected_ids', false ); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Get icon type. |
| 73 | * |
| 74 | * @return array|false |
| 75 | */ |
| 76 | public function getIconType() { |
| 77 | return get_option( 'surecart_cart_icon_type', 'floating_icon' ); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Check if cart menu is always shown. |
| 82 | * |
| 83 | * @return boolean |
| 84 | */ |
| 85 | public function isAlwaysShown() { |
| 86 | return (bool) get_option( 'surecart_cart_menu_always_shown', true ); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Is the cart enabled? |
| 91 | */ |
| 92 | public function isCartEnabled() { |
| 93 | if ( apply_filters( 'sc_cart_disabled', false ) ) { |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | return ! (bool) get_option( 'sc_slide_out_cart_disabled', false ); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Get cart menu alignment. |
| 102 | * |
| 103 | * @return 'left'|'right |
| 104 | */ |
| 105 | public function getAlignment() { |
| 106 | return (string) get_option( 'surecart_cart_menu_alignment', 'right' ); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Get mode. |
| 111 | * |
| 112 | * @return string |
| 113 | */ |
| 114 | public function getMode() { |
| 115 | $form = $this->getForm(); |
| 116 | if ( empty( $form->ID ) ) { |
| 117 | return ''; |
| 118 | } |
| 119 | |
| 120 | return Form::getMode( $form->ID ); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Add cart to menu. |
| 125 | * |
| 126 | * @param array $items Menu items. |
| 127 | * @param object $args Menu args. |
| 128 | * |
| 129 | * @return array |
| 130 | */ |
| 131 | public function addCartMenu( $items, $args ) { |
| 132 | $menu = wp_get_nav_menu_object( $args->menu ); |
| 133 | $id = $menu ? $menu->term_id : false; |
| 134 | |
| 135 | // if there is no id, or the menu icon is not enabled, or the cart is disabled, return. |
| 136 | if ( ! $id || ! $this->isMenuIconEnabled( $id ) || ! $this->isCartEnabled() ) { |
| 137 | return $items; |
| 138 | } |
| 139 | |
| 140 | $cart_menu_alignment = $this->getAlignment(); |
| 141 | |
| 142 | $menu = $this->menuItemTemplate(); |
| 143 | |
| 144 | // left or right. |
| 145 | $items = 'right' === $cart_menu_alignment ? $items . $menu : $menu . $items; |
| 146 | |
| 147 | return $items; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Get the menu item template. |
| 152 | * |
| 153 | * @return string |
| 154 | */ |
| 155 | public function menuItemTemplate() { |
| 156 | $cart_menu_icon_attributes = [ |
| 157 | 'cart_menu_always_shown' => $this->isAlwaysShown(), |
| 158 | 'cart_icon' => $this->getIcon( 'menu' ) ?? 'shopping-bag', |
| 159 | ]; |
| 160 | $cart_menu_icon_block_content = '<!-- wp:surecart/cart-menu-icon-button ' . wp_json_encode( $cart_menu_icon_attributes ) . ' /-->'; |
| 161 | |
| 162 | ob_start(); ?> |
| 163 | <li class='menu-item'> |
| 164 | <?php echo do_blocks( $cart_menu_icon_block_content ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> |
| 165 | </li> |
| 166 | <?php |
| 167 | return ob_get_clean(); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Get the cart template. |
| 172 | * |
| 173 | * @return string |
| 174 | */ |
| 175 | public function cartTemplate() { |
| 176 | $form = $this->getForm(); |
| 177 | if ( empty( $form->ID ) ) { |
| 178 | return ''; |
| 179 | } |
| 180 | |
| 181 | // get cart block. |
| 182 | $template = get_block_template( 'surecart/surecart//cart', 'wp_template_part' ); |
| 183 | if ( ! $template || empty( $template->content ) ) { |
| 184 | return; |
| 185 | } |
| 186 | |
| 187 | $cart_icon_block_content = '<!-- wp:surecart/cart-icon /-->'; |
| 188 | |
| 189 | ob_start(); |
| 190 | ?> |
| 191 | |
| 192 | <!-- Render the cart. --> |
| 193 | <?php echo do_blocks( $template->content ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> |
| 194 | |
| 195 | <!-- Render floating cart icon --> |
| 196 | <?php if ( $this->isFloatingIconEnabled() ) : ?> |
| 197 | <?php echo do_blocks( $cart_icon_block_content ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> |
| 198 | <?php endif; ?> |
| 199 | |
| 200 | <?php |
| 201 | return trim( preg_replace( '/\s+/', ' ', ob_get_clean() ) ); |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Get the form post. |
| 206 | * |
| 207 | * @return \WP_Post The default form post. |
| 208 | */ |
| 209 | public function getForm() { |
| 210 | return \SureCart::forms()->getDefault(); |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Check if floating cart icon is enabled |
| 215 | * |
| 216 | * @return string |
| 217 | */ |
| 218 | public function isFloatingIconEnabled() { |
| 219 | // If we have a checkout form block or shortcode, don't render the cart. |
| 220 | $object = get_queried_object(); |
| 221 | |
| 222 | if ( is_a( $object, \WP_Post::class ) ) { |
| 223 | $block = wp_get_first_block( parse_blocks( $object->post_content ), 'surecart/checkout-form' ) || has_shortcode( $object->post_content, 'sc_form' ); |
| 224 | if ( ! empty( $block ) ) { |
| 225 | return false; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | $cart_icon_type = (string) get_option( 'surecart_cart_icon_type', 'floating_icon' ); |
| 230 | return in_array( $cart_icon_type, array( 'floating_icon', 'both' ) ); |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Check if menu cart icon is enabled |
| 235 | * |
| 236 | * @param integer $term_id Term ID. |
| 237 | * @return bool |
| 238 | */ |
| 239 | public function isMenuIconEnabled( $term_id ) { |
| 240 | $cart_menu_ids = (array) $this->getSelectedIds(); |
| 241 | $cart_icon_type = (string) $this->getIconType(); |
| 242 | if ( ! in_array( $cart_icon_type, array( 'menu_icon', 'both' ) ) ) { |
| 243 | return; |
| 244 | } |
| 245 | return in_array( $term_id, $cart_menu_ids ); |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Remove deprecated cart content. |
| 250 | * |
| 251 | * @param string $content Cart content. |
| 252 | * |
| 253 | * @return string |
| 254 | */ |
| 255 | public static function removeDeprecatedCartContent( $content ) { |
| 256 | $review_cart_present = strpos( $content, 'wp:surecart/slide-out-cart-header {"text":"Review Your Cart"' ); |
| 257 | $my_cart_present = strpos( $content, '<sc-cart-header><span>My Cart</span></sc-cart-header>' ); |
| 258 | |
| 259 | if ( false !== $review_cart_present && false !== $my_cart_present ) { |
| 260 | $content = str_replace( '<sc-cart-header><span>My Cart</span></sc-cart-header>', '<sc-cart-header><span>Review Your Cart</span></sc-cart-header>', $content ); |
| 261 | } |
| 262 | |
| 263 | return $content; |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Include cart template. |
| 268 | * This needs to run before <head> so that blocks can add scripts and styles in wp_head(). |
| 269 | * |
| 270 | * @param string $template The template path. |
| 271 | * @return string |
| 272 | */ |
| 273 | public function includeCartTemplate( $template ) { |
| 274 | $form = $this->getForm(); |
| 275 | if ( empty( $form->ID ) ) { |
| 276 | return $template; |
| 277 | } |
| 278 | |
| 279 | $cart_template = $this->cartTemplate(); |
| 280 | |
| 281 | // add cart template to footer. |
| 282 | add_action( |
| 283 | 'wp_footer', |
| 284 | function () use ( $cart_template ) { |
| 285 | echo $cart_template; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 286 | } |
| 287 | ); |
| 288 | |
| 289 | return $template; |
| 290 | } |
| 291 | } |
| 292 |