PluginProbe ʕ •ᴥ•ʔ
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 3.0.0-beta1
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v3.0.0-beta1
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 2 years ago CartServiceProvider.php 3 years ago
CartService.php
278 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 // TODO: Remove this once we can add the module via interactivity dependency.
32 wp_enqueue_script( 'wp-a11y' );
33 }
34 );
35
36 add_action( 'wp_footer', [ $this, 'renderCartComponent' ] );
37 }
38 }
39
40 /**
41 * Get the icon name saved in the settings
42 *
43 * @return string
44 */
45 public function getIconNameFromSettings() {
46 return get_option( 'surecart_cart_icon', 'shopping-bag' );
47 }
48
49 /**
50 * Get the icon.
51 *
52 * @param 'menu'|'floating' $type Menu type.
53 *
54 * @return string
55 */
56 public function getIcon( $type ) {
57 $icon = $this->getIconNameFromSettings();
58
59 /**
60 * Allow filtering of the cart menu icon.
61 *
62 * @param string $icon The icon.
63 * @param string $mode The icon position.
64 */
65 return apply_filters( 'sc_cart_menu_icon', $icon, $type );
66 }
67
68
69 /**
70 * Get selected ids.
71 *
72 * @return array|false
73 */
74 public function getSelectedIds() {
75 return get_option( 'surecart_cart_menu_selected_ids', false );
76 }
77
78 /**
79 * Get icon type.
80 *
81 * @return array|false
82 */
83 public function getIconType() {
84 return get_option( 'surecart_cart_icon_type', 'floating_icon' );
85 }
86
87 /**
88 * Check if cart menu is always shown.
89 *
90 * @return boolean
91 */
92 public function isAlwaysShown() {
93 return (bool) get_option( 'surecart_cart_menu_always_shown', true );
94 }
95
96 /**
97 * Is the cart enabled?
98 */
99 public function isCartEnabled() {
100 return ! (bool) get_option( 'sc_slide_out_cart_disabled', false );
101 }
102
103 /**
104 * Get cart menu alignment.
105 *
106 * @return 'left'|'right
107 */
108 public function getAlignment() {
109 return (string) get_option( 'surecart_cart_menu_alignment', 'right' );
110 }
111
112 /**
113 * Get mode.
114 *
115 * @return string
116 */
117 public function getMode() {
118 $form = $this->getForm();
119 if ( empty( $form->ID ) ) {
120 return '';
121 }
122
123 return Form::getMode( $form->ID );
124 }
125
126 /**
127 * Add cart to menu.
128 *
129 * @param array $items Menu items.
130 * @param object $args Menu args.
131 *
132 * @return array
133 */
134 public function addCartMenu( $items, $args ) {
135 $menu = wp_get_nav_menu_object( $args->menu );
136 $id = $menu ? $menu->term_id : false;
137
138 // if there is no id, or the menu icon is not enabled, or the cart is disabled, return.
139 if ( ! $id || ! $this->isMenuIconEnabled( $id ) || ! $this->isCartEnabled() ) {
140 return $items;
141 }
142
143 $cart_menu_alignment = $this->getAlignment();
144
145 $menu = $this->menuItemTemplate();
146
147 // left or right.
148 $items = 'right' === $cart_menu_alignment ? $items . $menu : $menu . $items;
149
150 return $items;
151 }
152
153 public function menuItemTemplate() {
154 $cart_menu_icon_attributes = [
155 'cart_menu_always_shown' => $this->isAlwaysShown(),
156 'cart_icon' => $this->getIcon( 'menu' ) ?? 'shopping-bag',
157 ];
158 $cart_menu_icon_block_content = '<!-- wp:surecart/cart-menu-icon-button ' . wp_json_encode( $cart_menu_icon_attributes ) . ' /-->';
159
160 ob_start(); ?>
161 <li class='menu-item'>
162 <?php echo do_blocks( $cart_menu_icon_block_content ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
163 </li>
164 <?php
165 return ob_get_clean();
166 }
167
168 /**
169 * Get the cart template.
170 *
171 * @return string
172 */
173 public function cartTemplate() {
174 $form = $this->getForm();
175 if ( empty( $form->ID ) ) {
176 return '';
177 }
178
179 // get cart block.
180 $template = get_block_template( 'surecart/surecart//cart', 'wp_template_part' );
181 if ( ! $template || empty( $template->content ) ) {
182 return;
183 }
184
185 $cart_icon_block_content = '<!-- wp:surecart/cart-icon /-->';
186
187 ob_start();
188 ?>
189
190 <!-- Render the cart. -->
191 <?php echo do_blocks( $template->content ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
192
193 <!-- Render floating cart icon -->
194 <?php if ( $this->isFloatingIconEnabled() ) : ?>
195 <?php echo do_blocks( $cart_icon_block_content ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
196 <?php endif; ?>
197
198 <?php
199 return trim( preg_replace( '/\s+/', ' ', ob_get_clean() ) );
200 }
201
202 /**
203 * Render the cart components.
204 *
205 * @return void
206 */
207 public function renderCartComponent() {
208 $form = $this->getForm();
209 if ( empty( $form->ID ) ) {
210 return;
211 }
212
213 echo $this->cartTemplate();
214 }
215
216 /**
217 * Get the form
218 *
219 * @return \WP_Post The default form post.
220 */
221 public function getForm() {
222 return \SureCart::forms()->getDefault();
223 }
224
225 /**
226 * Check if floating cart icon is enabled
227 *
228 * @return string
229 */
230 public function isFloatingIconEnabled() {
231 // If we have a checkout form block or shortcode, don't render the cart.
232 $object = get_queried_object();
233
234 if ( is_a( $object, \WP_Post::class ) ) {
235 $block = wp_get_first_block( parse_blocks( $object->post_content ), 'surecart/checkout-form' ) || has_shortcode( $object->post_content, 'sc_form' );
236 if ( ! empty( $block ) ) {
237 return false;
238 }
239 }
240
241 $cart_icon_type = (string) get_option( 'surecart_cart_icon_type', 'floating_icon' );
242 return in_array( $cart_icon_type, array( 'floating_icon', 'both' ) );
243 }
244
245 /**
246 * Check if menu cart icon is enabled
247 *
248 * @param integer $term_id Term ID.
249 * @return bool
250 */
251 public function isMenuIconEnabled( $term_id ) {
252 $cart_menu_ids = (array) $this->getSelectedIds();
253 $cart_icon_type = (string) $this->getIconType();
254 if ( ! in_array( $cart_icon_type, array( 'menu_icon', 'both' ) ) ) {
255 return;
256 }
257 return in_array( $term_id, $cart_menu_ids );
258 }
259
260 /**
261 * Remove deprecated cart content.
262 *
263 * @param string $content Cart content.
264 *
265 * @return string
266 */
267 public static function removeDeprecatedCartContent( $content ) {
268 $review_cart_present = strpos( $content, 'wp:surecart/slide-out-cart-header {"text":"Review Your Cart"' );
269 $my_cart_present = strpos( $content, '<sc-cart-header><span>My Cart</span></sc-cart-header>' );
270
271 if ( false !== $review_cart_present && false !== $my_cart_present ) {
272 $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 );
273 }
274
275 return $content;
276 }
277 }
278