PluginProbe ʕ •ᴥ•ʔ
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 3.0.3
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v3.0.3
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
ShortcodesBlockConversionService.php 2 years ago ShortcodesService.php 1 year ago ShortcodesServiceProvider.php 1 year ago
ShortcodesServiceProvider.php
426 lines
1 <?php
2
3 namespace SureCart\WordPress\Shortcodes;
4
5 use SureCartBlocks\Blocks\AddToCartButton\Block as AddtoCartBlock;
6 use SureCartBlocks\Blocks\BuyButton\Block as BuyButtonBlock;
7 use SureCartCore\ServiceProviders\ServiceProviderInterface;
8
9 /**
10 * Register shortcodes.
11 */
12 class ShortcodesServiceProvider implements ServiceProviderInterface {
13 /**
14 * Register all dependencies in the IoC container.
15 *
16 * @param \Pimple\Container $container Service container.
17 * @return void
18 */
19 public function register( $container ) {
20 $container['surecart.shortcodes'] = function () {
21 return new ShortcodesService();
22 };
23 }
24
25 /**
26 * Bootstrap the service.
27 *
28 * @param \Pimple\Container $container Service container.
29 * @return void
30 */
31 public function bootstrap( $container ) {
32 add_shortcode( 'sc_line_item', '__return_false' );
33 add_shortcode( 'sc_form', [ $this, 'formShortcode' ] );
34 add_shortcode( 'sc_buy_button', [ $this, 'buyButtonShortcode' ], 10, 2 );
35
36 // buttons.
37 $container['surecart.shortcodes']->registerBlockShortcode(
38 'sc_customer_dashboard_button',
39 \SureCartBlocks\Blocks\CustomerDashboardButton\Block::class,
40 [
41 'show_icon' => true,
42 'type' => 'primary',
43 'size' => 'medium',
44 ]
45 );
46
47 // dashboard.
48 $container['surecart.shortcodes']->registerBlockShortcode(
49 'sc_customer_orders',
50 \SureCartBlocks\Blocks\Dashboard\CustomerOrders\Block::class,
51 [ 'title' => '' ]
52 );
53 $container['surecart.shortcodes']->registerBlockShortcode(
54 'sc_customer_invoices',
55 \SureCartBlocks\Blocks\Dashboard\CustomerInvoices\Block::class,
56 [ 'title' => '' ]
57 );
58 $container['surecart.shortcodes']->registerBlockShortcode(
59 'sc_customer_billing_details',
60 \SureCartBlocks\Blocks\Dashboard\CustomerBillingDetails\Block::class,
61 [ 'title' => '' ]
62 );
63 $container['surecart.shortcodes']->registerBlockShortcode(
64 'sc_customer_charges',
65 \SureCartBlocks\Blocks\Dashboard\Deprecated\CustomerCharges\Block::class,
66 [ 'title' => '' ]
67 );
68 $container['surecart.shortcodes']->registerBlockShortcode(
69 'sc_customer_payment_methods',
70 \SureCartBlocks\Blocks\Dashboard\CustomerPaymentMethods\Block::class,
71 [ 'title' => '' ]
72 );
73 $container['surecart.shortcodes']->registerBlockShortcode(
74 'sc_customer_subscriptions',
75 \SureCartBlocks\Blocks\Dashboard\CustomerSubscriptions\Block::class,
76 [ 'title' => '' ]
77 );
78 $container['surecart.shortcodes']->registerBlockShortcode(
79 'sc_customer_downloads',
80 \SureCartBlocks\Blocks\Dashboard\CustomerDownloads\Block::class,
81 [ 'title' => '' ]
82 );
83 $container['surecart.shortcodes']->registerBlockShortcode(
84 'sc_customer_wordpress_account',
85 \SureCartBlocks\Blocks\Dashboard\WordPressAccount\Block::class,
86 [ 'title' => '' ]
87 );
88 $container['surecart.shortcodes']->registerBlockShortcode(
89 'sc_customer_dashboard_page',
90 \SureCartBlocks\Blocks\Dashboard\CustomerDashboardArea\Block::class,
91 [ 'name' => '' ]
92 );
93 $container['surecart.shortcodes']->registerBlockShortcode(
94 'sc_customer_dashboard',
95 \SureCartBlocks\Blocks\Dashboard\CustomerDashboardArea\Block::class,
96 [ 'name' => '' ]
97 );
98 $container['surecart.shortcodes']->registerBlockShortcode(
99 'sc_customer_dashboard_tab',
100 \SureCartBlocks\Blocks\Dashboard\DashboardTab\Block::class,
101 [
102 'icon' => 'shopping-bag',
103 'panel' => '',
104 'title' => 'test',
105 ]
106 );
107
108 $container['surecart.shortcodes']->registerBlockShortcode(
109 'sc_cart_menu_icon',
110 \SureCartBlocks\Blocks\CartMenuButton\Block::class,
111 [
112 'cart_icon' => 'shopping-bag',
113 'cart_menu_always_shown' => true,
114 ]
115 );
116
117 // confirmation.
118 $container['surecart.shortcodes']->registerBlockShortcode(
119 'sc_order_confirmation',
120 \SureCartBlocks\Blocks\Confirmation\Block::class,
121 );
122 $container['surecart.shortcodes']->registerBlockShortcode(
123 'sc_order_confirmation_line_items',
124 \SureCartBlocks\Blocks\OrderConfirmationLineItems\Block::class,
125 );
126
127 // product page.
128 $container['surecart.shortcodes']->registerBlockShortcodeByName(
129 'sc_product_list',
130 'surecart/product-item-list',
131 [
132 'ids' => [],
133 'columns' => 4,
134 'sort_enabled' => true,
135 'search_enabled' => true,
136 'pagination_enabled' => true,
137 'ajax_pagination' => true,
138 'collection_enabled' => true,
139 'type' => 'all',
140 'limit' => 10,
141 ]
142 );
143
144 // Product collection page.
145 $container['surecart.shortcodes']->registerBlockShortcodeByName(
146 'sc_product_collection',
147 'surecart/product-collection',
148 [
149 'collection_id' => '', // mandatory.
150 'columns' => 4,
151 'sort_enabled' => true,
152 'search_enabled' => true,
153 'pagination_enabled' => true,
154 'ajax_pagination' => true,
155 'limit' => 10,
156 ]
157 );
158
159 $container['surecart.shortcodes']->registerBlockShortcodeByName(
160 'sc_product_description',
161 'surecart/product-description',
162 [
163 'id' => null,
164 ]
165 );
166 $container['surecart.shortcodes']->registerBlockShortcodeByName(
167 'sc_product_title',
168 'surecart/product-title',
169 [
170 'level' => 1,
171 ]
172 );
173 $container['surecart.shortcodes']->registerBlockShortcodeByName(
174 'sc_product_price',
175 'surecart/product-price',
176 [
177 'id' => null,
178 ]
179 );
180 $container['surecart.shortcodes']->registerBlockShortcodeByName(
181 'sc_product_price_choices',
182 'surecart/product-price-choices',
183 [
184 'label' => __( 'Pricing', 'surecart' ),
185 'columns' => 2,
186 'show_price' => true,
187 'id' => null,
188 ]
189 );
190 $container['surecart.shortcodes']->registerBlockShortcodeByName(
191 'sc_product_media',
192 'surecart/product-media',
193 [
194 'auto_height' => true,
195 'id' => null,
196 ]
197 );
198 $container['surecart.shortcodes']->registerBlockShortcodeByName(
199 'sc_product_quantity',
200 'surecart/product-quantity',
201 [
202 'id' => null,
203 ]
204 );
205 $container['surecart.shortcodes']->registerBlockShortcodeByName(
206 'sc_product_cart_button',
207 'surecart/product-buy-button',
208 [
209 'add_to_cart' => true,
210 'text' => __( 'Add To Cart', 'surecart' ),
211 'width' => 100,
212 'id' => null,
213 ]
214 );
215 $container['surecart.shortcodes']->registerBlockShortcodeByName(
216 'sc_product_variant_choices',
217 'surecart/product-variant-choices',
218 [
219 'id' => null,
220 ]
221 );
222 $container['surecart.shortcodes']->registerBlockShortcodeByName(
223 'sc_add_to_cart_button',
224 'surecart/add-to-cart-button',
225 [
226 'price_id' => null,
227 'variant_id' => null,
228 'type' => 'primary',
229 'size' => 'medium',
230 'button_text' => __( 'Add To Cart', 'surecart' ),
231 ]
232 );
233 $container['surecart.shortcodes']->registerBlockShortcodeByName(
234 'sc_product_collection_tags',
235 'surecart/product-collection-tags',
236 [
237 'id' => null,
238 'count' => 1,
239 ]
240 );
241 $container['surecart.shortcodes']->registerBlockShortcodeByName(
242 'sc_product_custom_amount',
243 'surecart/product-selected-price-ad-hoc-amount',
244 [
245 'label' => __( 'Enter an amount', 'surecart' ),
246 ]
247 );
248 $container['surecart.shortcodes']->registerBlockShortcodeByName(
249 'sc_product_page',
250 'surecart/product-page',
251 [
252 'id' => null,
253 ]
254 );
255
256 // generate shortcodes for all our blocks.
257 foreach ( glob( SURECART_PLUGIN_DIR . '/packages/blocks-next/build/blocks/**/block.json' ) as $file ) {
258 $metadata = wp_json_file_decode( $file, array( 'associative' => true ) );
259 $name = str_replace( 'surecart/', '', $metadata['name'] );
260 $name = str_replace( '-', '_', sanitize_title_with_dashes( $name ) );
261
262 $old_shortcode_names = [
263 'product_title',
264 'product_description',
265 'product_quantity',
266 'product_media',
267 ];
268
269 if ( in_array( $name, $old_shortcode_names, true ) ) {
270 $name = $name . '_new';
271 }
272
273 $container['surecart.shortcodes']->registerBlockShortcodeByName(
274 'sc_' . $name,
275 $metadata['name'],
276 );
277 }
278 }
279
280 /**
281 * Dashboard tab shortcode.
282 *
283 * @param array $attributes Shortcode attributes.
284 * @param string $content Shortcode content.
285 * @return string Shortcode output.
286 */
287 public function dashboardShortcode( $attributes, $content ) {
288 $attributes = shortcode_atts(
289 [],
290 $attributes,
291 'sc_customer_dashboard'
292 );
293
294 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>';
295 }
296
297 /**
298 * Form shorcode
299 *
300 * @param array $atts Shortcode attributes.
301 * @param string $content Shortcode content.
302 * @param string $name Shortcode tag.
303 *
304 * @return string Shortcode output.
305 */
306 public function formShortcode( $atts, $content, $name ) {
307 $atts = shortcode_atts(
308 [
309 'id' => null,
310 ],
311 $atts,
312 'sc_form'
313 );
314
315 if ( ! $atts['id'] ) {
316 return;
317 }
318
319 $form = \SureCart::forms()->get( $atts['id'] );
320
321 global $load_sc_js;
322 $load_sc_js = true;
323
324 global $sc_form_id;
325 $sc_form_id = $atts['id'];
326
327 // check to make sure we have a form post.
328 if ( ! is_a( $form, 'WP_Post' ) ) {
329 return __( 'This form is not available or has been deleted.', 'surecart' );
330 }
331
332 return apply_filters( 'surecart/shortcode/render', do_blocks( $form->post_content ), $atts, $name, $form );
333 }
334
335 /**
336 * Buy button shortcode.
337 *
338 * @param array $atts An array of attributes.
339 * @param string $content Content.
340 *
341 * @return string
342 */
343 public function buyButtonShortcode( $atts, $content ) {
344 // Remove inner shortcode from buy button label.
345 $label = strip_shortcodes( $content );
346 $atts = shortcode_atts(
347 [
348 'type' => 'primary',
349 'size' => 'medium',
350 'label' => $label,
351 ],
352 $atts,
353 'sc_buy_button'
354 );
355
356 $atts['line_items'] = (array) $this->getShortcodesAtts(
357 'sc_line_item',
358 $content,
359 [
360 'price_id' => null,
361 'quantity' => 1,
362 ]
363 );
364
365 foreach ( $atts['line_items'] as $key => $line_item ) {
366 $atts['line_items'][ $key ]['id'] = $line_item['price_id'];
367 }
368
369 $block = new BuyButtonBlock();
370
371 return $block->render( $atts );
372 }
373
374 /**
375 * Get specific shortcode atts from content
376 *
377 * @param string $name Name of shortcode.
378 * @param string $content Page content.
379 * @param array $defaults Defaults for each.
380 * @return array
381 */
382 public function getShortcodesAtts( $name, $content, $defaults = [] ) {
383 $items = [];
384
385 // if shortcode exists.
386 if (
387 preg_match_all( '/' . get_shortcode_regex() . '/s', $content, $matches )
388 && array_key_exists( 2, $matches )
389 && in_array( $name, $matches[2] )
390 ) {
391 foreach ( (array) $matches[0] as $key => $value ) {
392 if ( strpos( $value, $name ) !== false ) {
393 $items[] = wp_parse_args(
394 shortcode_parse_atts( $matches[3][ $key ] ),
395 $defaults
396 );
397 }
398 }
399 }
400
401 return $items;
402 }
403
404 /**
405 * Convert to block.
406 *
407 * @param string $name The name.
408 * @param stdClass $block The block.
409 * @param array $defaults The defaults.
410 * @param array $atts The atts.
411 * @param string $content The content.
412 *
413 * @return string
414 */
415 protected function convertToBlock( $name, $block, $defaults = [], $atts = [], $content = '' ) {
416 return( new $block() )->render(
417 shortcode_atts(
418 $defaults,
419 $atts,
420 $name
421 ),
422 $content
423 );
424 }
425 }
426