PluginProbe ʕ •ᴥ•ʔ
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 4.5.0
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v4.5.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 / WordPress / Shortcodes / ShortcodesServiceProvider.php
surecart / app / src / WordPress / Shortcodes Last commit date
ProductContextShortcodeWrapper.php 1 month ago ShortcodesBlockConversionService.php 2 years ago ShortcodesService.php 1 month ago ShortcodesServiceProvider.php 1 month ago
ShortcodesServiceProvider.php
523 lines
1 <?php
2
3 namespace SureCart\WordPress\Shortcodes;
4
5 use SureCartBlocks\Blocks\BuyButton\Block as BuyButtonBlock;
6 use SureCartCore\ServiceProviders\ServiceProviderInterface;
7
8 /**
9 * Register shortcodes.
10 */
11 class ShortcodesServiceProvider implements ServiceProviderInterface {
12 /**
13 * The service container.
14 *
15 * @var \Pimple\Container $container Service Container.
16 */
17 protected $container;
18
19 /**
20 * Register all dependencies in the IoC container.
21 *
22 * @param \Pimple\Container $container Service container.
23 * @return void
24 */
25 public function register( $container ) {
26 $container['surecart.shortcodes'] = function () {
27 return new ShortcodesService();
28 };
29
30 $container['surecart.shortcodes.product_context_wrapper'] = function () {
31 return new ProductContextShortcodeWrapper();
32 };
33 }
34
35 /**
36 * Bootstrap the service.
37 *
38 * @param \Pimple\Container $container Service container.
39 * @return void
40 */
41 public function bootstrap( $container ) {
42 $this->container = $container;
43
44 $container['surecart.shortcodes.product_context_wrapper']->bootstrap();
45
46 add_action( 'init', [ $this, 'registerShortcodes' ] );
47 }
48
49 /**
50 * Register shortcodes.
51 *
52 * @return void
53 */
54 public function registerShortcodes() {
55 add_shortcode( 'sc_line_item', '__return_false' );
56 add_shortcode( 'sc_form', [ $this, 'formShortcode' ] );
57 add_shortcode( 'sc_buy_button', [ $this, 'buyButtonShortcode' ], 10, 2 );
58
59 // buttons.
60 $this->container['surecart.shortcodes']->registerBlockShortcodeByName(
61 'sc_customer_dashboard_button',
62 'surecart/customer-dashboard-button',
63 [
64 'show_icon' => true,
65 'type' => 'primary',
66 'size' => 'medium',
67 ]
68 );
69
70 // dashboard.
71 $this->container['surecart.shortcodes']->registerBlockShortcodeByName(
72 'sc_customer_orders',
73 'surecart/customer-orders',
74 [ 'title' => '' ]
75 );
76 $this->container['surecart.shortcodes']->registerBlockShortcodeByName(
77 'sc_customer_invoices',
78 'surecart/customer-invoices',
79 [ 'title' => '' ]
80 );
81 $this->container['surecart.shortcodes']->registerBlockShortcodeByName(
82 'sc_customer_billing_details',
83 'surecart/customer-billing-details',
84 [ 'title' => '' ]
85 );
86
87 $this->container['surecart.shortcodes']->registerBlockShortcodeByName(
88 'sc_customer_charges',
89 'surecart/customer-charges',
90 [ 'title' => '' ]
91 );
92 $this->container['surecart.shortcodes']->registerBlockShortcodeByName(
93 'sc_customer_payment_methods',
94 'surecart/customer-payment-methods',
95 [ 'title' => '' ]
96 );
97 $this->container['surecart.shortcodes']->registerBlockShortcodeByName(
98 'sc_customer_subscriptions',
99 'surecart/customer-subscriptions',
100 [ 'title' => '' ]
101 );
102 $this->container['surecart.shortcodes']->registerBlockShortcodeByName(
103 'sc_customer_downloads',
104 'surecart/customer-downloads',
105 [ 'title' => '' ]
106 );
107
108 $this->container['surecart.shortcodes']->registerBlockShortcodeByName(
109 'sc_customer_wordpress_account',
110 'surecart/wordpress-account',
111 [
112 'title' => '',
113 ]
114 );
115
116 $this->container['surecart.shortcodes']->registerBlockShortcodeByName(
117 'sc_customer_dashboard_page',
118 'surecart/dashboard-page',
119 [
120 'name' => '',
121 ]
122 );
123
124 $this->container['surecart.shortcodes']->registerBlockShortcodeByName(
125 'sc_customer_dashboard',
126 'surecart/customer-dashboard-area',
127 [ 'name' => '' ]
128 );
129 $this->container['surecart.shortcodes']->registerBlockShortcodeByName(
130 'sc_customer_dashboard_tab',
131 'surecart/dashboard-tab',
132 [
133 'icon' => 'shopping-bag',
134 'panel' => '',
135 'title' => 'test',
136 ]
137 );
138
139 $this->container['surecart.shortcodes']->registerBlockShortcodeByName(
140 'sc_cart_menu_icon',
141 'surecart/cart-menu-icon-button',
142 [
143 'cart_icon' => 'shopping-bag',
144 'cart_menu_always_shown' => true,
145 ]
146 );
147
148 // confirmation.
149 $this->container['surecart.shortcodes']->registerBlockShortcodeByName(
150 'sc_order_confirmation',
151 'surecart/order-confirmation',
152 );
153 $this->container['surecart.shortcodes']->registerBlockShortcodeByName(
154 'sc_order_confirmation_line_items',
155 'surecart/order-confirmation-line-items',
156 );
157
158 // product page.
159 $this->container['surecart.shortcodes']->registerBlockShortcodeByName(
160 'sc_product_list',
161 'surecart/product-item-list',
162 [
163 'ids' => [],
164 'columns' => 4,
165 'sort_enabled' => true,
166 'search_enabled' => true,
167 'pagination_enabled' => true,
168 'ajax_pagination' => true,
169 'collection_enabled' => true,
170 'type' => 'all',
171 'limit' => 10,
172 ]
173 );
174
175 // Product collection page.
176 $this->container['surecart.shortcodes']->registerBlockShortcodeByName(
177 'sc_product_collection',
178 'surecart/product-collection',
179 [
180 'collection_id' => '', // mandatory.
181 'columns' => 4,
182 'sort_enabled' => true,
183 'search_enabled' => true,
184 'pagination_enabled' => true,
185 'ajax_pagination' => true,
186 'limit' => 10,
187 ]
188 );
189
190 $this->container['surecart.shortcodes']->registerBlockShortcodeByName(
191 'sc_product_description',
192 'surecart/product-description',
193 [
194 'id' => null,
195 ]
196 );
197 $this->container['surecart.shortcodes']->registerBlockShortcodeByName(
198 'sc_product_title',
199 'surecart/product-title',
200 [
201 'level' => 1,
202 ]
203 );
204 $this->container['surecart.shortcodes']->registerBlockShortcodeByName(
205 'sc_product_price',
206 'surecart/product-price',
207 [
208 'id' => null,
209 ]
210 );
211 $this->container['surecart.shortcodes']->registerBlockShortcodeByName(
212 'sc_product_price_choices',
213 'surecart/product-price-choices',
214 [
215 'label' => __( 'Pricing', 'surecart' ),
216 'columns' => 2,
217 'show_price' => true,
218 'id' => null,
219 ]
220 );
221 $this->container['surecart.shortcodes']->registerBlockShortcodeByName(
222 'sc_product_media',
223 'surecart/product-media',
224 [
225 'auto_height' => true,
226 'id' => null,
227 ]
228 );
229 $this->container['surecart.shortcodes']->registerBlockShortcodeByName(
230 'sc_product_quantity',
231 'surecart/product-quantity',
232 [
233 'id' => null,
234 ]
235 );
236 $this->container['surecart.shortcodes']->registerBlockShortcodeByName(
237 'sc_product_cart_button',
238 'surecart/product-buy-button',
239 [
240 'add_to_cart' => true,
241 'text' => __( 'Add To Cart', 'surecart' ),
242 'width' => 100,
243 'id' => null,
244 ]
245 );
246 $this->container['surecart.shortcodes']->registerBlockShortcodeByName(
247 'sc_product_variant_choices',
248 'surecart/product-variant-choices',
249 [
250 'id' => null,
251 ]
252 );
253 $this->container['surecart.shortcodes']->registerBlockShortcodeByName(
254 'sc_add_to_cart_button',
255 'surecart/add-to-cart-button',
256 [
257 'price_id' => null,
258 'variant_id' => null,
259 'type' => 'primary',
260 'size' => 'medium',
261 'button_text' => __( 'Add To Cart', 'surecart' ),
262 ]
263 );
264 $this->container['surecart.shortcodes']->registerBlockShortcodeByName(
265 'sc_product_collection_tags',
266 'surecart/product-collection-tags',
267 [
268 'id' => null,
269 'count' => 1,
270 ]
271 );
272 $this->container['surecart.shortcodes']->registerBlockShortcodeByName(
273 'sc_product_custom_amount',
274 'surecart/product-selected-price-ad-hoc-amount',
275 [
276 'label' => __( 'Enter an amount', 'surecart' ),
277 ]
278 );
279 $this->container['surecart.shortcodes']->registerBlockShortcodeByName(
280 'sc_product_page',
281 'surecart/product-page',
282 [
283 'id' => null,
284 ]
285 );
286
287 // generate shortcodes for all our blocks.
288 foreach ( glob( SURECART_PLUGIN_DIR . '/packages/blocks-next/build/blocks/**/block.json' ) as $file ) {
289 $metadata = wp_json_file_decode( $file, array( 'associative' => true ) );
290 $name = str_replace( 'surecart/', '', $metadata['name'] );
291 $name = str_replace( '-', '_', sanitize_title_with_dashes( $name ) );
292
293 $old_shortcode_names = [
294 'product_title',
295 'product_description',
296 'product_quantity',
297 'product_media',
298 ];
299
300 if ( in_array( $name, $old_shortcode_names, true ) ) {
301 $name = $name . '_new';
302 }
303
304 $this->container['surecart.shortcodes']->registerBlockShortcodeByName(
305 'sc_' . $name,
306 $metadata['name'],
307 );
308 }
309
310 /*
311 * Review shortcodes.
312 */
313 $this->container['surecart.shortcodes']->registerBlockShortcodeByName(
314 'sc_product_review_rating_stars',
315 'surecart/product-review-average-rating-stars',
316 [
317 'size' => '20px',
318 'fill_color' => '',
319 'link_to_reviews' => false,
320 ],
321 [ 'supports_product_id' => true ]
322 );
323 $this->container['surecart.shortcodes']->registerBlockShortcodeByName(
324 'sc_product_review_rating_value',
325 'surecart/product-review-average-rating-value',
326 [
327 'link_to_reviews' => false,
328 'format' => 'none',
329 ],
330 [ 'supports_product_id' => true ]
331 );
332 $this->container['surecart.shortcodes']->registerBlockShortcodeByName(
333 'sc_product_review_total_count',
334 'surecart/product-review-total-rating',
335 [
336 'show_label' => true,
337 'show_for_zero_reviews' => true,
338 'link_to_reviews' => true,
339 ],
340 [ 'supports_product_id' => true ]
341 );
342 $this->container['surecart.shortcodes']->registerBlockShortcodeByName(
343 'sc_product_review_breakdown',
344 'surecart/product-review-breakdown',
345 [
346 'columns' => 1,
347 'fill_color' => '',
348 'bar_fill_color' => '',
349 'bar_background_color' => '',
350 ],
351 [ 'supports_product_id' => true ]
352 );
353 $this->container['surecart.shortcodes']->registerBlockShortcodeByName(
354 'sc_product_review_add_button',
355 'surecart/product-review-add-button',
356 [
357 'label' => __( 'Write a Review', 'surecart' ),
358 'button_type' => 'both',
359 'icon' => 'edit-2',
360 'className' => 'is-style-fill',
361 ],
362 [ 'supports_product_id' => true ]
363 );
364
365 /*
366 * Pattern shortcodes.
367 * These are shortcodes that render the content of a pattern file.
368 */
369 $this->container['surecart.shortcodes']->registerPatternShortcodeByName(
370 'sc_product_review_list',
371 'product-review-standard', // Pattern file name without path or extension.
372 [ 'supports_product_id' => true ]
373 );
374 }
375
376 /**
377 * Dashboard tab shortcode.
378 *
379 * @param array $attributes Shortcode attributes.
380 * @param string $content Shortcode content.
381 * @return string Shortcode output.
382 */
383 public function dashboardShortcode( $attributes, $content ) {
384 $attributes = shortcode_atts(
385 [],
386 $attributes,
387 'sc_customer_dashboard'
388 );
389
390 return '<sc-tab-group style="font-size:16px;font-family:var(--sc-font-sans)" class="wp-block-surecart-customer-dashboard alignwide">' . ( new \SureCartBlocks\Blocks\Dashboard\CustomerDashboard\Block() )->render( $attributes, $content ) . '</sc-tab-group>';
391 }
392
393 /**
394 * Form shorcode
395 *
396 * @param array $atts Shortcode attributes.
397 * @param string $content Shortcode content.
398 * @param string $name Shortcode tag.
399 *
400 * @return string Shortcode output.
401 */
402 public function formShortcode( $atts, $content, $name ) {
403 $atts = shortcode_atts(
404 [
405 'id' => null,
406 ],
407 $atts,
408 'sc_form'
409 );
410
411 if ( ! $atts['id'] ) {
412 return;
413 }
414
415 $form = \SureCart::forms()->get( $atts['id'] );
416
417 // Validate the post is a published sc_form to prevent private post content disclosure.
418 if ( ! is_a( $form, 'WP_Post' ) || 'sc_form' !== $form->post_type || 'publish' !== $form->post_status ) {
419 return __( 'This form is not available or has been deleted.', 'surecart' );
420 }
421
422 global $load_sc_js;
423 $load_sc_js = true;
424
425 global $sc_form_id;
426 $sc_form_id = $atts['id'];
427
428 return apply_filters( 'surecart/shortcode/render', do_blocks( $form->post_content ), $atts, $name, $form );
429 }
430
431 /**
432 * Buy button shortcode.
433 *
434 * @param array $atts An array of attributes.
435 * @param string $content Content.
436 *
437 * @return string
438 */
439 public function buyButtonShortcode( $atts, $content ) {
440 // Remove inner shortcode from buy button label.
441 $label = strip_shortcodes( $content );
442 $atts = shortcode_atts(
443 [
444 'type' => 'primary',
445 'size' => 'medium',
446 'label' => $label,
447 ],
448 $atts,
449 'sc_buy_button'
450 );
451
452 $atts['line_items'] = (array) $this->getShortcodesAtts(
453 'sc_line_item',
454 $content,
455 [
456 'price_id' => null,
457 'quantity' => 1,
458 'ad_hoc_amount' => null,
459 ]
460 );
461
462 foreach ( $atts['line_items'] as $key => $line_item ) {
463 $atts['line_items'][ $key ]['id'] = $line_item['price_id'];
464 }
465
466 $block = new BuyButtonBlock();
467
468 return $block->render( $atts );
469 }
470
471 /**
472 * Get specific shortcode atts from content
473 *
474 * @param string $name Name of shortcode.
475 * @param string $content Page content.
476 * @param array $defaults Defaults for each.
477 * @return array
478 */
479 public function getShortcodesAtts( $name, $content, $defaults = [] ) {
480 $items = [];
481
482 // if shortcode exists.
483 if (
484 preg_match_all( '/' . get_shortcode_regex() . '/s', $content, $matches )
485 && array_key_exists( 2, $matches )
486 && in_array( $name, $matches[2] )
487 ) {
488 foreach ( (array) $matches[0] as $key => $value ) {
489 if ( strpos( $value, $name ) !== false ) {
490 $items[] = wp_parse_args(
491 shortcode_parse_atts( $matches[3][ $key ] ),
492 $defaults
493 );
494 }
495 }
496 }
497
498 return $items;
499 }
500
501 /**
502 * Convert to block.
503 *
504 * @param string $name The name.
505 * @param stdClass $block The block.
506 * @param array $defaults The defaults.
507 * @param array $atts The atts.
508 * @param string $content The content.
509 *
510 * @return string
511 */
512 protected function convertToBlock( $name, $block, $defaults = [], $atts = [], $content = '' ) {
513 return( new $block() )->render(
514 shortcode_atts(
515 $defaults,
516 $atts,
517 $name
518 ),
519 $content
520 );
521 }
522 }
523