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