PluginProbe ʕ •ᴥ•ʔ
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 3.0.0-beta2
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v3.0.0-beta2
4.7.0 4.6.6 4.6.5 4.6.4 4.6.3 4.6.2 4.6.1 4.6.0 4.5.1 4.5.0 4.4.2 4.4.1 4.4.0 4.3.3 4.3.2 4.3.1 4.3.0 4.2.3 4.2.2 4.2.1 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.11.0 1.11.1 1.11.2 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.7.0 1.7.1 1.7.2 1.8.0 1.8.1 1.8.2 1.8.3 1.8.4 1.8.5 1.9.0 1.9.1 1.9.2 1.9.3 1.9.4 1.9.5 2.0.0 2.0.1 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.10.0 2.10.1 2.11.0 2.11.1 2.11.2 2.11.3 2.11.4 2.12.0 2.13.0 2.14.0 2.14.1 2.15.0 2.15.1 2.16.0 2.16.1 2.16.2 2.16.3 2.17.0 2.17.1 2.17.2 2.18.0 2.19.0 2.19.2 2.19.3 2.19.4 2.2.0 2.2.1 2.20.0 2.20.1 2.20.2 2.20.3 2.20.4 2.20.5 2.20.6 2.21.0 2.22.0 2.22.1 2.23.0 2.24.0 2.25.0 2.25.1 2.25.2 2.26.0 2.27.0 2.27.1 2.28.0 2.29.0 2.29.1 2.29.2 2.29.3 2.29.4 2.3.0 2.3.1 2.30.0 2.31.0 2.31.1 2.31.2 2.31.3 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.40.0 2.40.1 2.5.0 2.5.1 2.5.2 2.6.0 2.6.1 2.6.2 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.8.2 2.8.3 2.8.4 2.9.0 3.0.0 3.0.0-RC1 3.0.0-RC2 3.0.0-beta1 3.0.0-beta2 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.10.0 3.10.1 3.11.0 3.12.0 3.13.0 3.13.1 3.13.2 3.13.3 3.13.4 3.14.0 3.15.0 3.15.1 3.15.2 3.15.3 3.15.4 3.15.5 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.16.6 3.16.7 3.16.8 3.17.0 3.17.1 3.17.2 3.17.3 3.17.4 3.17.5 3.17.6 3.18.0 3.19.0 3.19.1 3.19.2 3.2.0 3.2.1 3.2.2 3.20.0 3.20.1 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.4.3 3.5.0 3.5.1 3.5.2 3.5.3 3.6.0 3.6.1 3.6.2 3.7.0 3.7.1 3.7.2 3.7.3 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.9.0 4.0.0 4.0.1 4.0.2 4.0.3 trunk 4.1.0 0.2.19.1 4.1.1 1.0.0 4.2.0 1.0.1 1.0.2
surecart / app / src / Cart / CartService.php
surecart / app / src / Cart Last commit date
CartService.php 1 year ago CartServiceProvider.php 3 years ago
CartService.php
275 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 // TODO: Load Only the base css styles.
25 \SureCart::assets()->enqueueComponents();
26
27 // Enqueue the cart drawer script modules.
28 wp_enqueue_script_module( '@surecart/cart' );
29 wp_enqueue_script_module( '@surecart/checkout' );
30 }
31 );
32
33 add_action( 'wp_footer', [ $this, 'renderCartComponent' ] );
34 }
35 }
36
37 /**
38 * Get the icon name saved in the settings
39 *
40 * @return string
41 */
42 public function getIconNameFromSettings() {
43 return get_option( 'surecart_cart_icon', 'shopping-bag' );
44 }
45
46 /**
47 * Get the icon.
48 *
49 * @param 'menu'|'floating' $type Menu type.
50 *
51 * @return string
52 */
53 public function getIcon( $type ) {
54 $icon = $this->getIconNameFromSettings();
55
56 /**
57 * Allow filtering of the cart menu icon.
58 *
59 * @param string $icon The icon.
60 * @param string $mode The icon position.
61 */
62 return apply_filters( 'sc_cart_menu_icon', $icon, $type );
63 }
64
65
66 /**
67 * Get selected ids.
68 *
69 * @return array|false
70 */
71 public function getSelectedIds() {
72 return get_option( 'surecart_cart_menu_selected_ids', false );
73 }
74
75 /**
76 * Get icon type.
77 *
78 * @return array|false
79 */
80 public function getIconType() {
81 return get_option( 'surecart_cart_icon_type', 'floating_icon' );
82 }
83
84 /**
85 * Check if cart menu is always shown.
86 *
87 * @return boolean
88 */
89 public function isAlwaysShown() {
90 return (bool) get_option( 'surecart_cart_menu_always_shown', true );
91 }
92
93 /**
94 * Is the cart enabled?
95 */
96 public function isCartEnabled() {
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 public function menuItemTemplate() {
151 $cart_menu_icon_attributes = [
152 'cart_menu_always_shown' => $this->isAlwaysShown(),
153 'cart_icon' => $this->getIcon( 'menu' ) ?? 'shopping-bag',
154 ];
155 $cart_menu_icon_block_content = '<!-- wp:surecart/cart-menu-icon-button ' . wp_json_encode( $cart_menu_icon_attributes ) . ' /-->';
156
157 ob_start(); ?>
158 <li class='menu-item'>
159 <?php echo do_blocks( $cart_menu_icon_block_content ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
160 </li>
161 <?php
162 return ob_get_clean();
163 }
164
165 /**
166 * Get the cart template.
167 *
168 * @return string
169 */
170 public function cartTemplate() {
171 $form = $this->getForm();
172 if ( empty( $form->ID ) ) {
173 return '';
174 }
175
176 // get cart block.
177 $template = get_block_template( 'surecart/surecart//cart', 'wp_template_part' );
178 if ( ! $template || empty( $template->content ) ) {
179 return;
180 }
181
182 $cart_icon_block_content = '<!-- wp:surecart/cart-icon /-->';
183
184 ob_start();
185 ?>
186
187 <!-- Render the cart. -->
188 <?php echo do_blocks( $template->content ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
189
190 <!-- Render floating cart icon -->
191 <?php if ( $this->isFloatingIconEnabled() ) : ?>
192 <?php echo do_blocks( $cart_icon_block_content ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
193 <?php endif; ?>
194
195 <?php
196 return trim( preg_replace( '/\s+/', ' ', ob_get_clean() ) );
197 }
198
199 /**
200 * Render the cart components.
201 *
202 * @return void
203 */
204 public function renderCartComponent() {
205 $form = $this->getForm();
206 if ( empty( $form->ID ) ) {
207 return;
208 }
209
210 echo $this->cartTemplate();
211 }
212
213 /**
214 * Get the form post.
215 *
216 * @return \WP_Post The default form post.
217 */
218 public function getForm() {
219 return \SureCart::forms()->getDefault();
220 }
221
222 /**
223 * Check if floating cart icon is enabled
224 *
225 * @return string
226 */
227 public function isFloatingIconEnabled() {
228 // If we have a checkout form block or shortcode, don't render the cart.
229 $object = get_queried_object();
230
231 if ( is_a( $object, \WP_Post::class ) ) {
232 $block = wp_get_first_block( parse_blocks( $object->post_content ), 'surecart/checkout-form' ) || has_shortcode( $object->post_content, 'sc_form' );
233 if ( ! empty( $block ) ) {
234 return false;
235 }
236 }
237
238 $cart_icon_type = (string) get_option( 'surecart_cart_icon_type', 'floating_icon' );
239 return in_array( $cart_icon_type, array( 'floating_icon', 'both' ) );
240 }
241
242 /**
243 * Check if menu cart icon is enabled
244 *
245 * @param integer $term_id Term ID.
246 * @return bool
247 */
248 public function isMenuIconEnabled( $term_id ) {
249 $cart_menu_ids = (array) $this->getSelectedIds();
250 $cart_icon_type = (string) $this->getIconType();
251 if ( ! in_array( $cart_icon_type, array( 'menu_icon', 'both' ) ) ) {
252 return;
253 }
254 return in_array( $term_id, $cart_menu_ids );
255 }
256
257 /**
258 * Remove deprecated cart content.
259 *
260 * @param string $content Cart content.
261 *
262 * @return string
263 */
264 public static function removeDeprecatedCartContent( $content ) {
265 $review_cart_present = strpos( $content, 'wp:surecart/slide-out-cart-header {"text":"Review Your Cart"' );
266 $my_cart_present = strpos( $content, '<sc-cart-header><span>My Cart</span></sc-cart-header>' );
267
268 if ( false !== $review_cart_present && false !== $my_cart_present ) {
269 $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 );
270 }
271
272 return $content;
273 }
274 }
275