AssetsService.php
3 years ago
AssetsServiceProvider.php
3 years ago
BlockAssetsLoadService.php
3 years ago
PreloadService.php
3 years ago
ScriptsService.php
3 years ago
StylesService.php
3 years ago
AssetsService.php
209 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\WordPress\Assets; |
| 4 | |
| 5 | /** |
| 6 | * Our assets service. |
| 7 | */ |
| 8 | class AssetsService { |
| 9 | /** |
| 10 | * Holds the loader. |
| 11 | * |
| 12 | * @var Object |
| 13 | */ |
| 14 | protected $loader; |
| 15 | |
| 16 | /** |
| 17 | * Holds the styles |
| 18 | * |
| 19 | * @var Object |
| 20 | */ |
| 21 | protected $styles; |
| 22 | |
| 23 | /** |
| 24 | * Holds the scripts service. |
| 25 | * |
| 26 | * @var Object |
| 27 | */ |
| 28 | protected $scripts; |
| 29 | |
| 30 | /** |
| 31 | * The service container. |
| 32 | * |
| 33 | * @var \Pimple\Container $container Service Container. |
| 34 | */ |
| 35 | protected $container; |
| 36 | |
| 37 | /** |
| 38 | * The preload Service |
| 39 | * |
| 40 | * @var array |
| 41 | */ |
| 42 | protected $config; |
| 43 | |
| 44 | /** |
| 45 | * Get the loader. |
| 46 | * |
| 47 | * @param Object $loader The loader. |
| 48 | */ |
| 49 | public function __construct( $loader, $scripts, $styles, $container ) { |
| 50 | $this->loader = $loader; |
| 51 | $this->scripts = $scripts; |
| 52 | $this->styles = $styles; |
| 53 | $this->container = $container; |
| 54 | $this->config = \SureCart::resolve( SURECART_CONFIG_KEY ); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Bootstrap the service. |
| 59 | * |
| 60 | * @return void |
| 61 | */ |
| 62 | public function bootstrap() { |
| 63 | // register assets we will reuse. |
| 64 | add_action( 'init', [ $this->scripts, 'register' ] ); |
| 65 | add_action( 'init', [ $this->styles, 'register' ] ); |
| 66 | add_filter( 'render_block_data', [ $this, 'preloadComponents' ] ); |
| 67 | |
| 68 | // block editor. |
| 69 | add_action( 'enqueue_block_editor_assets', [ $this, 'editorAssets' ] ); |
| 70 | |
| 71 | // Shortcode usages scripts load. |
| 72 | add_action( 'wp_head', [ $this, 'maybeEnqueueShortcodeScripts' ] ); |
| 73 | |
| 74 | // front-end styles. These only load when the block is being rendered on the page. |
| 75 | $this->loader->whenRendered( 'surecart/form', [ $this, 'enqueueForm' ] ); |
| 76 | $this->loader->whenRendered( 'surecart/buy-button', [ $this, 'enqueueComponents' ] ); |
| 77 | $this->loader->whenRendered( 'surecart/customer-dashboard', [ $this, 'enqueueComponents' ] ); |
| 78 | $this->loader->whenRendered( 'surecart/checkout-form', [ $this, 'enqueueComponents' ] ); |
| 79 | $this->loader->whenRendered( 'surecart/order-confirmation', [ $this, 'enqueueComponents' ] ); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Preload any components needed for block display. |
| 84 | * |
| 85 | * @param array $parsed_block Parsed block data. |
| 86 | * |
| 87 | * @return array |
| 88 | */ |
| 89 | public function preloadComponents( $parsed_block ) { |
| 90 | if ( ! empty( $this->config['preload'][ $parsed_block['blockName'] ] ) ) { |
| 91 | \SureCart::preload()->add( $this->config['preload'][ $parsed_block['blockName'] ] ); |
| 92 | } |
| 93 | return $parsed_block; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Enqueue form scripts. |
| 98 | * |
| 99 | * @return void |
| 100 | */ |
| 101 | public function enqueueForm() { |
| 102 | // add recaptcha if enabled. |
| 103 | if ( \SureCart::settings()->recaptcha()->isEnabled() ) { |
| 104 | wp_enqueue_script( 'surecart-google-recaptcha' ); |
| 105 | } |
| 106 | $this->enqueueComponents(); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Enqueue editor styles and scripts. |
| 111 | */ |
| 112 | public function editorAssets() { |
| 113 | $this->scripts->enqueueEditor(); |
| 114 | $this->styles->enqueueEditor(); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * EnqueueComponents. |
| 119 | * |
| 120 | * @return void |
| 121 | */ |
| 122 | public function enqueueComponents() { |
| 123 | $this->scripts->enqueueFront(); |
| 124 | $this->styles->enqueueFront(); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Output brand colors. |
| 129 | * |
| 130 | * @return void |
| 131 | */ |
| 132 | public function printBrandColors() { |
| 133 | $this->styles->addInlineBrandColors( 'surecart-themes-default' ); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Shortcodes scripts add. |
| 138 | * |
| 139 | * @return void |
| 140 | */ |
| 141 | public function maybeEnqueueShortcodeScripts() { |
| 142 | global $post; |
| 143 | |
| 144 | // match all of our shortcodes. |
| 145 | if ( false === strpos( $post->post_content ?? '', '[sc_' ) ) { |
| 146 | return; |
| 147 | } |
| 148 | |
| 149 | $this->enqueueComponents(); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * This adds component data to the component when it's defined at runtime. |
| 154 | * |
| 155 | * @param string $tag Tag of the web component. |
| 156 | * @param string $selector Specific selector (class or id). |
| 157 | * @param array $data Data to add. |
| 158 | * @return void |
| 159 | */ |
| 160 | public function addComponentData( $tag, $selector, $data = [] ) { |
| 161 | if ( $this->loader->isUsingPageBuilder() || wp_doing_ajax() ) { |
| 162 | return $this->outputComponentScript( $tag, $selector, $data ); |
| 163 | } |
| 164 | add_action( |
| 165 | 'wp_footer', |
| 166 | function () use ( $tag, $selector, $data ) { |
| 167 | return $this->outputComponentScript( $tag, $selector, $data ); |
| 168 | } |
| 169 | ); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Should we use the esm loader directly? |
| 174 | * If false, we inject the loader script at runtime. |
| 175 | * |
| 176 | * @return boolean |
| 177 | */ |
| 178 | public function usesEsmLoader() { |
| 179 | return (bool) get_option( 'surecart_use_esm_loader', false ); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Output the component initialization script. |
| 184 | * |
| 185 | * @param string $tag Tag of the web component. |
| 186 | * @param string $selector Specific selector (class or id). |
| 187 | * @param array $data Data to add. |
| 188 | */ |
| 189 | public function outputComponentScript( $tag, $selector, $data = [] ) { |
| 190 | ?> |
| 191 | <script> |
| 192 | (async () => { |
| 193 | await customElements.whenDefined('<?php echo esc_js( $tag ); ?>'); |
| 194 | var component = document.querySelector('<?php echo esc_js( $tag . $selector ); ?>'); |
| 195 | if (!component) return; |
| 196 | <?php |
| 197 | foreach ( $data as $key => $value ) { |
| 198 | echo "\n"; |
| 199 | echo esc_js( "component.$key = " ); |
| 200 | echo wp_json_encode( $value ); |
| 201 | echo ';'; |
| 202 | } |
| 203 | ?> |
| 204 | })(); |
| 205 | </script> |
| 206 | <?php |
| 207 | } |
| 208 | } |
| 209 |