PluginProbe ʕ •ᴥ•ʔ
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / trunk
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments vtrunk
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 9 months ago CartServiceProvider.php 3 years ago
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