PluginProbe ʕ •ᴥ•ʔ
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 4.6.0
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v4.6.0
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 / 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 5 days ago
ShortcodesServiceProvider.php
524 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 'id' => null,
203 ]
204 );
205 $this->container['surecart.shortcodes']->registerBlockShortcodeByName(
206 'sc_product_price',
207 'surecart/product-price',
208 [
209 'id' => null,
210 ]
211 );
212 $this->container['surecart.shortcodes']->registerBlockShortcodeByName(
213 'sc_product_price_choices',
214 'surecart/product-price-choices',
215 [
216 'label' => __( 'Pricing', 'surecart' ),
217 'columns' => 2,
218 'show_price' => true,
219 'id' => null,
220 ]
221 );
222 $this->container['surecart.shortcodes']->registerBlockShortcodeByName(
223 'sc_product_media',
224 'surecart/product-media',
225 [
226 'auto_height' => true,
227 'id' => null,
228 ]
229 );
230 $this->container['surecart.shortcodes']->registerBlockShortcodeByName(
231 'sc_product_quantity',
232 'surecart/product-quantity',
233 [
234 'id' => null,
235 ]
236 );
237 $this->container['surecart.shortcodes']->registerBlockShortcodeByName(
238 'sc_product_cart_button',
239 'surecart/product-buy-button',
240 [
241 'add_to_cart' => true,
242 'text' => __( 'Add To Cart', 'surecart' ),
243 'width' => 100,
244 'id' => null,
245 ]
246 );
247 $this->container['surecart.shortcodes']->registerBlockShortcodeByName(
248 'sc_product_variant_choices',
249 'surecart/product-variant-choices',
250 [
251 'id' => null,
252 ]
253 );
254 $this->container['surecart.shortcodes']->registerBlockShortcodeByName(
255 'sc_add_to_cart_button',
256 'surecart/add-to-cart-button',
257 [
258 'price_id' => null,
259 'variant_id' => null,
260 'type' => 'primary',
261 'size' => 'medium',
262 'button_text' => __( 'Add To Cart', 'surecart' ),
263 ]
264 );
265 $this->container['surecart.shortcodes']->registerBlockShortcodeByName(
266 'sc_product_collection_tags',
267 'surecart/product-collection-tags',
268 [
269 'id' => null,
270 'count' => 1,
271 ]
272 );
273 $this->container['surecart.shortcodes']->registerBlockShortcodeByName(
274 'sc_product_custom_amount',
275 'surecart/product-selected-price-ad-hoc-amount',
276 [
277 'label' => __( 'Enter an amount', 'surecart' ),
278 ]
279 );
280 $this->container['surecart.shortcodes']->registerBlockShortcodeByName(
281 'sc_product_page',
282 'surecart/product-page',
283 [
284 'id' => null,
285 ]
286 );
287
288 // generate shortcodes for all our blocks.
289 foreach ( glob( SURECART_PLUGIN_DIR . '/packages/blocks-next/build/blocks/**/block.json' ) as $file ) {
290 $metadata = wp_json_file_decode( $file, array( 'associative' => true ) );
291 $name = str_replace( 'surecart/', '', $metadata['name'] );
292 $name = str_replace( '-', '_', sanitize_title_with_dashes( $name ) );
293
294 $old_shortcode_names = [
295 'product_title',
296 'product_description',
297 'product_quantity',
298 'product_media',
299 ];
300
301 if ( in_array( $name, $old_shortcode_names, true ) ) {
302 $name = $name . '_new';
303 }
304
305 $this->container['surecart.shortcodes']->registerBlockShortcodeByName(
306 'sc_' . $name,
307 $metadata['name'],
308 );
309 }
310
311 /*
312 * Review shortcodes.
313 */
314 $this->container['surecart.shortcodes']->registerBlockShortcodeByName(
315 'sc_product_review_rating_stars',
316 'surecart/product-review-average-rating-stars',
317 [
318 'size' => '20px',
319 'fill_color' => '',
320 'link_to_reviews' => false,
321 ],
322 [ 'supports_product_id' => true ]
323 );
324 $this->container['surecart.shortcodes']->registerBlockShortcodeByName(
325 'sc_product_review_rating_value',
326 'surecart/product-review-average-rating-value',
327 [
328 'link_to_reviews' => false,
329 'format' => 'none',
330 ],
331 [ 'supports_product_id' => true ]
332 );
333 $this->container['surecart.shortcodes']->registerBlockShortcodeByName(
334 'sc_product_review_total_count',
335 'surecart/product-review-total-rating',
336 [
337 'show_label' => true,
338 'show_for_zero_reviews' => true,
339 'link_to_reviews' => true,
340 ],
341 [ 'supports_product_id' => true ]
342 );
343 $this->container['surecart.shortcodes']->registerBlockShortcodeByName(
344 'sc_product_review_breakdown',
345 'surecart/product-review-breakdown',
346 [
347 'columns' => 1,
348 'fill_color' => '',
349 'bar_fill_color' => '',
350 'bar_background_color' => '',
351 ],
352 [ 'supports_product_id' => true ]
353 );
354 $this->container['surecart.shortcodes']->registerBlockShortcodeByName(
355 'sc_product_review_add_button',
356 'surecart/product-review-add-button',
357 [
358 'label' => __( 'Write a Review', 'surecart' ),
359 'button_type' => 'both',
360 'icon' => 'edit-2',
361 'className' => 'is-style-fill',
362 ],
363 [ 'supports_product_id' => true ]
364 );
365
366 /*
367 * Pattern shortcodes.
368 * These are shortcodes that render the content of a pattern file.
369 */
370 $this->container['surecart.shortcodes']->registerPatternShortcodeByName(
371 'sc_product_review_list',
372 'product-review-standard', // Pattern file name without path or extension.
373 [ 'supports_product_id' => true ]
374 );
375 }
376
377 /**
378 * Dashboard tab shortcode.
379 *
380 * @param array $attributes Shortcode attributes.
381 * @param string $content Shortcode content.
382 * @return string Shortcode output.
383 */
384 public function dashboardShortcode( $attributes, $content ) {
385 $attributes = shortcode_atts(
386 [],
387 $attributes,
388 'sc_customer_dashboard'
389 );
390
391 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>';
392 }
393
394 /**
395 * Form shorcode
396 *
397 * @param array $atts Shortcode attributes.
398 * @param string $content Shortcode content.
399 * @param string $name Shortcode tag.
400 *
401 * @return string Shortcode output.
402 */
403 public function formShortcode( $atts, $content, $name ) {
404 $atts = shortcode_atts(
405 [
406 'id' => null,
407 ],
408 $atts,
409 'sc_form'
410 );
411
412 if ( ! $atts['id'] ) {
413 return;
414 }
415
416 $form = \SureCart::forms()->get( $atts['id'] );
417
418 // Validate the post is a published sc_form to prevent private post content disclosure.
419 if ( ! is_a( $form, 'WP_Post' ) || 'sc_form' !== $form->post_type || 'publish' !== $form->post_status ) {
420 return __( 'This form is not available or has been deleted.', 'surecart' );
421 }
422
423 global $load_sc_js;
424 $load_sc_js = true;
425
426 global $sc_form_id;
427 $sc_form_id = $atts['id'];
428
429 return apply_filters( 'surecart/shortcode/render', do_blocks( $form->post_content ), $atts, $name, $form );
430 }
431
432 /**
433 * Buy button shortcode.
434 *
435 * @param array $atts An array of attributes.
436 * @param string $content Content.
437 *
438 * @return string
439 */
440 public function buyButtonShortcode( $atts, $content ) {
441 // Remove inner shortcode from buy button label.
442 $label = strip_shortcodes( $content );
443 $atts = shortcode_atts(
444 [
445 'type' => 'primary',
446 'size' => 'medium',
447 'label' => $label,
448 ],
449 $atts,
450 'sc_buy_button'
451 );
452
453 $atts['line_items'] = (array) $this->getShortcodesAtts(
454 'sc_line_item',
455 $content,
456 [
457 'price_id' => null,
458 'quantity' => 1,
459 'ad_hoc_amount' => null,
460 ]
461 );
462
463 foreach ( $atts['line_items'] as $key => $line_item ) {
464 $atts['line_items'][ $key ]['id'] = $line_item['price_id'];
465 }
466
467 $block = new BuyButtonBlock();
468
469 return $block->render( $atts );
470 }
471
472 /**
473 * Get specific shortcode atts from content
474 *
475 * @param string $name Name of shortcode.
476 * @param string $content Page content.
477 * @param array $defaults Defaults for each.
478 * @return array
479 */
480 public function getShortcodesAtts( $name, $content, $defaults = [] ) {
481 $items = [];
482
483 // if shortcode exists.
484 if (
485 preg_match_all( '/' . get_shortcode_regex() . '/s', $content, $matches )
486 && array_key_exists( 2, $matches )
487 && in_array( $name, $matches[2] )
488 ) {
489 foreach ( (array) $matches[0] as $key => $value ) {
490 if ( strpos( $value, $name ) !== false ) {
491 $items[] = wp_parse_args(
492 shortcode_parse_atts( $matches[3][ $key ] ),
493 $defaults
494 );
495 }
496 }
497 }
498
499 return $items;
500 }
501
502 /**
503 * Convert to block.
504 *
505 * @param string $name The name.
506 * @param stdClass $block The block.
507 * @param array $defaults The defaults.
508 * @param array $atts The atts.
509 * @param string $content The content.
510 *
511 * @return string
512 */
513 protected function convertToBlock( $name, $block, $defaults = [], $atts = [], $content = '' ) {
514 return( new $block() )->render(
515 shortcode_atts(
516 $defaults,
517 $atts,
518 $name
519 ),
520 $content
521 );
522 }
523 }
524