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 |