index.php
340 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; // Exit if accessed directly. |
| 5 | } |
| 6 | |
| 7 | /** |
| 8 | * Register all blocks. |
| 9 | */ |
| 10 | add_action( |
| 11 | 'init', |
| 12 | function () { |
| 13 | foreach ( glob( __DIR__ . '/build/blocks/**/block.json' ) as $file ) { |
| 14 | register_block_type( dirname( $file ) ); |
| 15 | } |
| 16 | } |
| 17 | ); |
| 18 | |
| 19 | /** |
| 20 | * Use our controller view pattern. |
| 21 | */ |
| 22 | add_filter( |
| 23 | 'block_type_metadata_settings', |
| 24 | function ( $settings, $metadata ) { |
| 25 | // if there is a controller file, use it. |
| 26 | $controller_path = wp_normalize_path( |
| 27 | realpath( |
| 28 | dirname( $metadata['file'] ) . '/' . |
| 29 | remove_block_asset_path_prefix( 'file:./controller.php' ) |
| 30 | ) |
| 31 | ); |
| 32 | |
| 33 | if ( ! file_exists( $controller_path ) ) { |
| 34 | return $settings; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Renders the block on the server. |
| 39 | * |
| 40 | * @since 6.1.0 |
| 41 | * |
| 42 | * @param array $attributes Block attributes. |
| 43 | * @param string $content Block default content. |
| 44 | * @param WP_Block $block Block instance. |
| 45 | * |
| 46 | * @return string Returns the block content. |
| 47 | */ |
| 48 | $settings['render_callback'] = static function ( $attributes, $content, $block ) use ( $controller_path, $metadata ) { |
| 49 | $view = require $controller_path; |
| 50 | |
| 51 | if ( ! $view ) { |
| 52 | return ''; |
| 53 | } |
| 54 | |
| 55 | // if not using 'file:', then it's content output. |
| 56 | $view_path = remove_block_asset_path_prefix( $view ); |
| 57 | if ( $view_path === $view ) { |
| 58 | return $view; |
| 59 | } |
| 60 | |
| 61 | $template_path = wp_normalize_path( |
| 62 | realpath( |
| 63 | dirname( $metadata['file'] ) . '/' . |
| 64 | remove_block_asset_path_prefix( $view ) |
| 65 | ) |
| 66 | ); |
| 67 | |
| 68 | ob_start(); |
| 69 | require $template_path; |
| 70 | return ob_get_clean(); |
| 71 | }; |
| 72 | |
| 73 | return $settings; |
| 74 | }, |
| 75 | 11, |
| 76 | 2 |
| 77 | ); |
| 78 | |
| 79 | add_filter( |
| 80 | 'render_block_context', |
| 81 | function ( $context, $parsed_block ) { |
| 82 | // we have product context. |
| 83 | if ( get_query_var( 'surecart_current_product' ) ) { |
| 84 | $context['surecart/product'] = sc_get_product(); |
| 85 | } |
| 86 | |
| 87 | // pass a unique id to each product list block. |
| 88 | if ( 'surecart/product-list' === $parsed_block['blockName'] ) { |
| 89 | // we use our own counter to ensure uniqueness so that product page urls don't have ids. |
| 90 | global $sc_query_id; |
| 91 | $sc_query_id = sc_unique_product_list_id(); |
| 92 | } |
| 93 | |
| 94 | // pass a unique id to each product list block. |
| 95 | if ( 'surecart/product-page' === $parsed_block['blockName'] ) { |
| 96 | // we use our own counter to ensure uniqueness so that product page urls don't have ids. |
| 97 | global $sc_query_id; |
| 98 | $sc_query_id = sc_unique_product_page_id(); |
| 99 | } |
| 100 | return $context; |
| 101 | }, |
| 102 | 10, |
| 103 | 2 |
| 104 | ); |
| 105 | |
| 106 | /** |
| 107 | * Register all css at src/styles folder. |
| 108 | */ |
| 109 | add_action( |
| 110 | 'init', |
| 111 | function () { |
| 112 | $css_files = glob( __DIR__ . '/build/styles/*.css' ) ?? []; |
| 113 | |
| 114 | foreach ( $css_files as $css_file ) { |
| 115 | // Extract the file name without the extension and prepend with 'surecart-'. |
| 116 | $handle = 'surecart-' . basename( $css_file, '.css' ); |
| 117 | |
| 118 | wp_register_style( |
| 119 | $handle, |
| 120 | plugins_url( 'build/styles/' . basename( $css_file ), __FILE__ ), |
| 121 | [], |
| 122 | filemtime( $css_file ) |
| 123 | ); |
| 124 | } |
| 125 | } |
| 126 | ); |
| 127 | |
| 128 | add_action( |
| 129 | 'init', |
| 130 | function () { |
| 131 | // instead, use a static loader that injects the script at runtime. |
| 132 | $static_assets = include trailingslashit( plugin_dir_path( __FILE__ ) ) . 'build/scripts/fetch/index.asset.php'; |
| 133 | wp_register_script_module( |
| 134 | '@surecart/api-fetch', |
| 135 | trailingslashit( plugin_dir_url( __FILE__ ) ) . 'build/scripts/fetch/index.js', |
| 136 | array( |
| 137 | array( |
| 138 | 'id' => 'wp-url', |
| 139 | 'import' => 'dynamic', |
| 140 | ), |
| 141 | array( |
| 142 | 'id' => 'wp-api-fetch', |
| 143 | 'import' => 'dynamic', |
| 144 | ), |
| 145 | ), |
| 146 | $static_assets['version'] |
| 147 | ); |
| 148 | |
| 149 | // instead, use a static loader that injects the script at runtime. |
| 150 | $static_assets = include trailingslashit( plugin_dir_path( __FILE__ ) ) . 'build/scripts/dialog/index.asset.php'; |
| 151 | wp_register_script_module( |
| 152 | '@surecart/dialog', |
| 153 | trailingslashit( plugin_dir_url( __FILE__ ) ) . 'build/scripts/dialog/index.js', |
| 154 | array( |
| 155 | array( |
| 156 | 'id' => '@wordpress/interactivity', |
| 157 | 'import' => 'dynamic', |
| 158 | ), |
| 159 | ), |
| 160 | $static_assets['version'] |
| 161 | ); |
| 162 | |
| 163 | // instead, use a static loader that injects the script at runtime. |
| 164 | $static_assets = include trailingslashit( plugin_dir_path( __FILE__ ) ) . 'build/scripts/dropdown/index.asset.php'; |
| 165 | wp_register_script_module( |
| 166 | '@surecart/dropdown', |
| 167 | trailingslashit( plugin_dir_url( __FILE__ ) ) . 'build/scripts/dropdown/index.js', |
| 168 | array( |
| 169 | array( |
| 170 | 'id' => '@surecart/dialog', |
| 171 | 'import' => 'dynamic', |
| 172 | ), |
| 173 | array( |
| 174 | 'id' => '@wordpress/interactivity', |
| 175 | 'import' => 'dynamic', |
| 176 | ), |
| 177 | ), |
| 178 | $static_assets['version'] |
| 179 | ); |
| 180 | |
| 181 | // instead, use a static loader that injects the script at runtime. |
| 182 | $static_assets = include trailingslashit( plugin_dir_path( __FILE__ ) ) . 'build/scripts/google/index.asset.php'; |
| 183 | wp_register_script_module( |
| 184 | '@surecart/google-events', |
| 185 | trailingslashit( plugin_dir_url( __FILE__ ) ) . 'build/scripts/google/index.js', |
| 186 | [], |
| 187 | $static_assets['version'] |
| 188 | ); |
| 189 | |
| 190 | // instead, use a static loader that injects the script at runtime. |
| 191 | $static_assets = include trailingslashit( plugin_dir_path( __FILE__ ) ) . 'build/scripts/facebook/index.asset.php'; |
| 192 | wp_register_script_module( |
| 193 | '@surecart/facebook-events', |
| 194 | trailingslashit( plugin_dir_url( __FILE__ ) ) . 'build/scripts/facebook/index.js', |
| 195 | [], |
| 196 | $static_assets['version'] |
| 197 | ); |
| 198 | |
| 199 | // instead, use a static loader that injects the script at runtime. |
| 200 | $static_assets = include trailingslashit( plugin_dir_path( __FILE__ ) ) . 'build/scripts/product-page/index.asset.php'; |
| 201 | wp_register_script_module( |
| 202 | '@surecart/product-page', |
| 203 | trailingslashit( plugin_dir_url( __FILE__ ) ) . 'build/scripts/product-page/index.js', |
| 204 | [ |
| 205 | [ |
| 206 | 'id' => '@surecart/dialog', |
| 207 | 'import' => 'dynamic', |
| 208 | ], |
| 209 | [ |
| 210 | 'id' => '@surecart/google-events', |
| 211 | 'import' => 'dynamic', |
| 212 | ], |
| 213 | [ |
| 214 | 'id' => '@surecart/facebook-events', |
| 215 | 'import' => 'dynamic', |
| 216 | ], |
| 217 | [ |
| 218 | 'id' => '@wordpress/interactivity-router', |
| 219 | 'import' => 'dynamic', |
| 220 | ], |
| 221 | ], |
| 222 | $static_assets['version'] |
| 223 | ); |
| 224 | |
| 225 | // instead, use a static loader that injects the script at runtime. |
| 226 | $static_assets = include trailingslashit( plugin_dir_path( __FILE__ ) ) . 'build/scripts/product-list/index.asset.php'; |
| 227 | wp_register_script_module( |
| 228 | '@surecart/product-list', |
| 229 | trailingslashit( plugin_dir_url( __FILE__ ) ) . 'build/scripts/product-list/index.js', |
| 230 | [ |
| 231 | [ |
| 232 | 'id' => '@wordpress/interactivity-router', |
| 233 | 'import' => 'dynamic', |
| 234 | ], |
| 235 | [ |
| 236 | 'id' => '@surecart/google-events', |
| 237 | 'import' => 'dynamic', |
| 238 | ], |
| 239 | [ |
| 240 | 'id' => '@surecart/facebook-events', |
| 241 | 'import' => 'dynamic', |
| 242 | ], |
| 243 | ], |
| 244 | $static_assets['version'] |
| 245 | ); |
| 246 | |
| 247 | // instead, use a static loader that injects the script at runtime. |
| 248 | $static_assets = include trailingslashit( plugin_dir_path( __FILE__ ) ) . 'build/scripts/image-slider/index.asset.php'; |
| 249 | wp_register_script_module( |
| 250 | '@surecart/image-slider', |
| 251 | trailingslashit( plugin_dir_url( __FILE__ ) ) . 'build/scripts/image-slider/index.js', |
| 252 | array( |
| 253 | array( |
| 254 | 'id' => '@wordpress/interactivity', |
| 255 | 'import' => 'dynamic', |
| 256 | ), |
| 257 | ), |
| 258 | $static_assets['version'] |
| 259 | ); |
| 260 | |
| 261 | // Checkout actions. |
| 262 | $static_assets = include trailingslashit( plugin_dir_path( __FILE__ ) ) . 'build/scripts/checkout-actions/index.asset.php'; |
| 263 | wp_register_script_module( |
| 264 | '@surecart/checkout-service', |
| 265 | trailingslashit( plugin_dir_url( __FILE__ ) ) . 'build/scripts/checkout-actions/index.js', |
| 266 | array( |
| 267 | array( |
| 268 | 'id' => '@surecart/api-fetch', |
| 269 | 'import' => 'dynamic', |
| 270 | ), |
| 271 | ), |
| 272 | $static_assets['version'] |
| 273 | ); |
| 274 | |
| 275 | // Checkout events. |
| 276 | $static_assets = include trailingslashit( plugin_dir_path( __FILE__ ) ) . 'build/scripts/checkout-events/index.asset.php'; |
| 277 | wp_register_script_module( |
| 278 | '@surecart/checkout-events', |
| 279 | trailingslashit( plugin_dir_url( __FILE__ ) ) . 'build/scripts/checkout-events/index.js', |
| 280 | array( |
| 281 | array( |
| 282 | 'id' => '@surecart/api-fetch', |
| 283 | 'import' => 'dynamic', |
| 284 | ), |
| 285 | ), |
| 286 | $static_assets['version'] |
| 287 | ); |
| 288 | |
| 289 | // Cart side drawer. |
| 290 | $static_assets = include trailingslashit( plugin_dir_path( __FILE__ ) ) . 'build/scripts/cart/index.asset.php'; |
| 291 | wp_register_script_module( |
| 292 | '@surecart/cart', |
| 293 | trailingslashit( plugin_dir_url( __FILE__ ) ) . 'build/scripts/cart/index.js', |
| 294 | array( |
| 295 | array( |
| 296 | 'id' => '@wordpress/interactivity', |
| 297 | 'import' => 'dynamic', |
| 298 | ), |
| 299 | array( |
| 300 | 'id' => 'wp-i18n', |
| 301 | 'import' => 'dynamic', |
| 302 | ), |
| 303 | ), |
| 304 | $static_assets['version'] |
| 305 | ); |
| 306 | |
| 307 | // SureCart Checkout. |
| 308 | $static_assets = include trailingslashit( plugin_dir_path( __FILE__ ) ) . 'build/scripts/checkout/index.asset.php'; |
| 309 | wp_register_script_module( |
| 310 | '@surecart/checkout', |
| 311 | trailingslashit( plugin_dir_url( __FILE__ ) ) . 'build/scripts/checkout/index.js', |
| 312 | array( |
| 313 | array( |
| 314 | 'id' => '@surecart/checkout-service', |
| 315 | 'import' => 'dynamic', |
| 316 | ), |
| 317 | array( |
| 318 | 'id' => '@surecart/checkout-events', |
| 319 | 'import' => 'dynamic', |
| 320 | ), |
| 321 | [ |
| 322 | 'id' => '@surecart/google-events', |
| 323 | 'import' => 'dynamic', |
| 324 | ], |
| 325 | [ |
| 326 | 'id' => '@surecart/facebook-events', |
| 327 | 'import' => 'dynamic', |
| 328 | ], |
| 329 | array( |
| 330 | 'id' => '@surecart/cart', |
| 331 | 'import' => 'dynamic', |
| 332 | ), |
| 333 | ), |
| 334 | $static_assets['version'] |
| 335 | ); |
| 336 | }, |
| 337 | 10, |
| 338 | 3 |
| 339 | ); |
| 340 |