ShortcodesBlockConversionService.php
2 years ago
ShortcodesService.php
1 year ago
ShortcodesServiceProvider.php
1 year ago
ShortcodesService.php
207 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\WordPress\Shortcodes; |
| 4 | |
| 5 | use SureCart\WordPress\Assets\BlockAssetsLoadService; |
| 6 | /** |
| 7 | * The shortcodes service. |
| 8 | */ |
| 9 | class ShortcodesService { |
| 10 | |
| 11 | /** |
| 12 | * Old Shortcode block names which we want to not render through interactivity. |
| 13 | * |
| 14 | * @var array |
| 15 | */ |
| 16 | protected $old_blocks_by_name = [ |
| 17 | 'surecart/product-collection-tags' => 'surecart/product-collection-tags', |
| 18 | 'surecart/product-media' => 'surecart/product-media-old', |
| 19 | 'surecart/product-title' => 'surecart/product-title-old', |
| 20 | 'surecart/product-description' => 'surecart/product-description-old', |
| 21 | 'surecart/product-price' => 'surecart/product-price', |
| 22 | 'surecart/product-variant-choices' => 'surecart/product-variant-choices', |
| 23 | 'surecart/product-price-choices' => 'surecart/product-price-choices', |
| 24 | 'surecart/product-quantity' => 'surecart/product-quantity-old', |
| 25 | 'surecart/product-buy-button' => 'surecart/product-buy-button-old', |
| 26 | ]; |
| 27 | |
| 28 | /** |
| 29 | * Convert the block |
| 30 | * |
| 31 | * @param string $name Block name. |
| 32 | * @param string $block Block block. |
| 33 | * @param array $defaults Default attributes. |
| 34 | * |
| 35 | * @return void |
| 36 | */ |
| 37 | public function registerBlockShortcode( $name, $block, $defaults = array() ) { |
| 38 | add_shortcode( |
| 39 | $name, |
| 40 | function ( $attributes, $content ) use ( $name, $block, $defaults ) { |
| 41 | return ( new ShortcodesBlockConversionService( $attributes, $content ) )->convert( |
| 42 | $name, |
| 43 | $block, |
| 44 | $defaults |
| 45 | ); |
| 46 | }, |
| 47 | 10, |
| 48 | 2 |
| 49 | ); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Render shortcode notice |
| 54 | * |
| 55 | * @param string $name Name. |
| 56 | * |
| 57 | * @return string |
| 58 | */ |
| 59 | public function renderShortcodeNotice( $name ) { |
| 60 | return sprintf( |
| 61 | '<h5 style="%s">%s</h5>', |
| 62 | 'background: #F1F1F1; color: #434242; padding: 2em; border-radius: 0.5em;', |
| 63 | esc_html( |
| 64 | sprintf( |
| 65 | /* translators: %s: shortcode name */ |
| 66 | __( 'Please visit the frontend of your page builder to view the %s shortcode contents.', 'surecart' ), |
| 67 | esc_html( $name ) |
| 68 | ) |
| 69 | ) |
| 70 | ); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Check if shortcode should render itself |
| 75 | * |
| 76 | * @param string $name Name of the shortcode. |
| 77 | * @return boolean |
| 78 | */ |
| 79 | public function cannotRenderShortcode( $name ) { |
| 80 | if ( 'sc_product_list' !== $name ) { // If the shortcode is not Product List, return false. |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | $assets_service = new BlockAssetsLoadService(); |
| 85 | |
| 86 | return $assets_service->isUsingPageBuilder(); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Register shortcode by name |
| 91 | * |
| 92 | * @param string $name Name of the shortcode. |
| 93 | * @param string $block_name The registered block name. |
| 94 | * @param array $defaults Default attributes. |
| 95 | * |
| 96 | * @return void |
| 97 | */ |
| 98 | public function registerBlockShortcodeByName( $name, $block_name, $defaults = array() ) { |
| 99 | add_shortcode( |
| 100 | $name, |
| 101 | function ( $attributes, $content ) use ( $name, $block_name, $defaults ) { |
| 102 | if ( empty( $block_name ) ) { |
| 103 | return ''; |
| 104 | } |
| 105 | if ( $this->cannotRenderShortcode( $name ) ) { // If we are in the editor of any Page Builders & Block is Product List, render the shortcode itself. |
| 106 | return $this->renderShortcodeNotice( $name ); |
| 107 | } |
| 108 | |
| 109 | add_filter( 'should_load_separate_core_block_assets', '__return_false', 11 ); // Disable loading separate core block assets. |
| 110 | wp_enqueue_global_styles(); // Enqueue global styles. |
| 111 | |
| 112 | // convert comma separated attributes to array. |
| 113 | if ( is_array( $attributes ) ) { |
| 114 | foreach ( $attributes as $key => $value ) { |
| 115 | if ( strpos( $value, ',' ) !== 0 && isset( $defaults[ $key ] ) && is_array( $defaults[ $key ] ) ) { |
| 116 | $attributes[ $key ] = explode( ',', $value ); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | $shortcode_attrs = wp_parse_args( |
| 122 | $attributes, |
| 123 | $defaults |
| 124 | ); |
| 125 | |
| 126 | $shortcode_attrs = apply_filters( "shortcode_atts_{$name}", $shortcode_attrs, $shortcode_attrs, $shortcode_attrs, $name ); |
| 127 | |
| 128 | // If the block is old block, we need to process it differently. |
| 129 | $block = (object) [ |
| 130 | 'parsed_block' => [ |
| 131 | 'blockName' => $block_name, |
| 132 | 'attrs' => $shortcode_attrs, |
| 133 | ], |
| 134 | ]; |
| 135 | |
| 136 | // we need to remove this since this is processed twice for some blocks. |
| 137 | add_filter( 'doing_it_wrong_trigger_error', [ $this, 'removeInteractivityDoingItWrong' ], 10, 2 ); |
| 138 | |
| 139 | if ( ! empty( $this->old_blocks_by_name[ $block_name ] ) ) { |
| 140 | $old_block = \SureCart::block() |
| 141 | ->productPageBlocksMigration( $block, $this->old_blocks_by_name[ $block_name ] ) |
| 142 | ->maybeRenderOldBlockFromShortcode( $name ); |
| 143 | } |
| 144 | |
| 145 | if ( ! empty( $old_block ) ) { |
| 146 | remove_filter( 'doing_it_wrong_trigger_error', [ $this, 'removeInteractivityDoingItWrong' ], 10 ); |
| 147 | return $old_block; |
| 148 | } |
| 149 | |
| 150 | $content = $this->processBlock( $block_name, $shortcode_attrs, $content ); |
| 151 | |
| 152 | remove_filter( 'doing_it_wrong_trigger_error', [ $this, 'removeInteractivityDoingItWrong' ], 10 ); |
| 153 | |
| 154 | return $content; |
| 155 | } |
| 156 | ); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Process the block |
| 161 | * |
| 162 | * @param string $block_name Block name. |
| 163 | * @param array $shortcode_attrs Shortcode attributes. |
| 164 | * @param string $content Content. |
| 165 | * |
| 166 | * @return string |
| 167 | */ |
| 168 | public function processBlock( $block_name, $shortcode_attrs, $content ) { |
| 169 | switch ( $block_name ) { |
| 170 | case 'surecart/product-list': |
| 171 | case 'surecart/product-collection': |
| 172 | return wp_interactivity_process_directives( \SureCart::block()->productListMigration( $shortcode_attrs )->render() ); |
| 173 | |
| 174 | case 'surecart/product-collection-tags': |
| 175 | return wp_interactivity_process_directives( \SureCart::block()->productCollectionBadgesMigration( $shortcode_attrs )->render() ); |
| 176 | |
| 177 | case 'surecart/product-price': |
| 178 | return wp_interactivity_process_directives( \SureCart::block()->productSelectedPriceMigration( $shortcode_attrs )->render() ); |
| 179 | |
| 180 | case 'surecart/product-price-choices': |
| 181 | return wp_interactivity_process_directives( \SureCart::block()->productPriceChoicesMigration( $shortcode_attrs )->render() ); |
| 182 | |
| 183 | case 'surecart/product-variant-choices': |
| 184 | return wp_interactivity_process_directives( \SureCart::block()->productVariantsMigration( $shortcode_attrs )->render() ); |
| 185 | } |
| 186 | |
| 187 | return wp_interactivity_process_directives( |
| 188 | do_blocks( '<!-- wp:' . $block_name . ' ' . wp_json_encode( $shortcode_attrs, JSON_FORCE_OBJECT ) . ' -->' . do_shortcode( $content ) . '<!-- /wp:' . $block_name . ' -->' ) |
| 189 | ); |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Remove interactivity doing it wrong |
| 194 | * |
| 195 | * @param string $trigger Trigger. |
| 196 | * @param string $function_name Function name. |
| 197 | * |
| 198 | * @return string|false |
| 199 | */ |
| 200 | public function removeInteractivityDoingItWrong( $trigger, $function_name ) { |
| 201 | if ( 'WP_Interactivity_API::evaluate' !== $function_name ) { |
| 202 | return $trigger; |
| 203 | } |
| 204 | return false; |
| 205 | } |
| 206 | } |
| 207 |