index.php
363 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 | add_action( |
| 150 | 'wp_footer', |
| 151 | function () { |
| 152 | ?> |
| 153 | <script> |
| 154 | window.scFetchData = |
| 155 | <?php |
| 156 | echo wp_json_encode( |
| 157 | [ |
| 158 | 'root_url' => esc_url_raw( get_rest_url() ), |
| 159 | 'nonce' => ( wp_installing() && ! is_multisite() ) ? '' : wp_create_nonce( 'wp_rest' ), |
| 160 | 'nonce_endpoint' => admin_url( 'admin-ajax.php?action=sc-rest-nonce' ), |
| 161 | ] |
| 162 | ); |
| 163 | ?> |
| 164 | </script> |
| 165 | <?php |
| 166 | } |
| 167 | ); |
| 168 | |
| 169 | // instead, use a static loader that injects the script at runtime. |
| 170 | $static_assets = include trailingslashit( plugin_dir_path( __FILE__ ) ) . 'build/scripts/dialog/index.asset.php'; |
| 171 | wp_register_script_module( |
| 172 | '@surecart/dialog', |
| 173 | trailingslashit( plugin_dir_url( __FILE__ ) ) . 'build/scripts/dialog/index.js', |
| 174 | array( |
| 175 | array( |
| 176 | 'id' => '@wordpress/interactivity', |
| 177 | 'import' => 'dynamic', |
| 178 | ), |
| 179 | ), |
| 180 | $static_assets['version'] |
| 181 | ); |
| 182 | |
| 183 | // instead, use a static loader that injects the script at runtime. |
| 184 | $static_assets = include trailingslashit( plugin_dir_path( __FILE__ ) ) . 'build/scripts/dropdown/index.asset.php'; |
| 185 | wp_register_script_module( |
| 186 | '@surecart/dropdown', |
| 187 | trailingslashit( plugin_dir_url( __FILE__ ) ) . 'build/scripts/dropdown/index.js', |
| 188 | array( |
| 189 | array( |
| 190 | 'id' => '@wordpress/interactivity', |
| 191 | 'import' => 'dynamic', |
| 192 | ), |
| 193 | ), |
| 194 | $static_assets['version'] |
| 195 | ); |
| 196 | |
| 197 | // instead, use a static loader that injects the script at runtime. |
| 198 | $static_assets = include trailingslashit( plugin_dir_path( __FILE__ ) ) . 'build/scripts/google/index.asset.php'; |
| 199 | wp_register_script_module( |
| 200 | '@surecart/google-events', |
| 201 | trailingslashit( plugin_dir_url( __FILE__ ) ) . 'build/scripts/google/index.js', |
| 202 | [], |
| 203 | $static_assets['version'] |
| 204 | ); |
| 205 | |
| 206 | // instead, use a static loader that injects the script at runtime. |
| 207 | $static_assets = include trailingslashit( plugin_dir_path( __FILE__ ) ) . 'build/scripts/facebook/index.asset.php'; |
| 208 | wp_register_script_module( |
| 209 | '@surecart/facebook-events', |
| 210 | trailingslashit( plugin_dir_url( __FILE__ ) ) . 'build/scripts/facebook/index.js', |
| 211 | [], |
| 212 | $static_assets['version'] |
| 213 | ); |
| 214 | |
| 215 | // instead, use a static loader that injects the script at runtime. |
| 216 | $static_assets = include trailingslashit( plugin_dir_path( __FILE__ ) ) . 'build/scripts/product-page/index.asset.php'; |
| 217 | wp_register_script_module( |
| 218 | '@surecart/product-page', |
| 219 | trailingslashit( plugin_dir_url( __FILE__ ) ) . 'build/scripts/product-page/index.js', |
| 220 | [ |
| 221 | [ |
| 222 | 'id' => '@wordpress/interactivity', |
| 223 | 'import' => 'dynamic', |
| 224 | ], |
| 225 | [ |
| 226 | 'id' => '@surecart/checkout-service', |
| 227 | 'import' => 'dynamic', |
| 228 | ], |
| 229 | [ |
| 230 | 'id' => '@surecart/google-events', |
| 231 | 'import' => 'dynamic', |
| 232 | ], |
| 233 | [ |
| 234 | 'id' => '@surecart/facebook-events', |
| 235 | 'import' => 'dynamic', |
| 236 | ], |
| 237 | ], |
| 238 | $static_assets['version'] |
| 239 | ); |
| 240 | |
| 241 | // instead, use a static loader that injects the script at runtime. |
| 242 | $static_assets = include trailingslashit( plugin_dir_path( __FILE__ ) ) . 'build/scripts/product-list/index.asset.php'; |
| 243 | wp_register_script_module( |
| 244 | '@surecart/product-list', |
| 245 | trailingslashit( plugin_dir_url( __FILE__ ) ) . 'build/scripts/product-list/index.js', |
| 246 | [ |
| 247 | [ |
| 248 | 'id' => '@wordpress/interactivity', |
| 249 | 'import' => 'dynamic', |
| 250 | ], |
| 251 | [ |
| 252 | 'id' => '@wordpress/interactivity-router', |
| 253 | 'import' => 'dynamic', |
| 254 | ], |
| 255 | [ |
| 256 | 'id' => '@surecart/google-events', |
| 257 | 'import' => 'dynamic', |
| 258 | ], |
| 259 | [ |
| 260 | 'id' => '@surecart/facebook-events', |
| 261 | 'import' => 'dynamic', |
| 262 | ], |
| 263 | ], |
| 264 | $static_assets['version'] |
| 265 | ); |
| 266 | |
| 267 | // instead, use a static loader that injects the script at runtime. |
| 268 | $static_assets = include trailingslashit( plugin_dir_path( __FILE__ ) ) . 'build/scripts/image-slider/index.asset.php'; |
| 269 | wp_register_script_module( |
| 270 | '@surecart/image-slider', |
| 271 | trailingslashit( plugin_dir_url( __FILE__ ) ) . 'build/scripts/image-slider/index.js', |
| 272 | array( |
| 273 | array( |
| 274 | 'id' => '@wordpress/interactivity', |
| 275 | 'import' => 'dynamic', |
| 276 | ), |
| 277 | ), |
| 278 | $static_assets['version'] |
| 279 | ); |
| 280 | |
| 281 | // Checkout actions. |
| 282 | $static_assets = include trailingslashit( plugin_dir_path( __FILE__ ) ) . 'build/scripts/checkout-actions/index.asset.php'; |
| 283 | wp_register_script_module( |
| 284 | '@surecart/checkout-service', |
| 285 | trailingslashit( plugin_dir_url( __FILE__ ) ) . 'build/scripts/checkout-actions/index.js', |
| 286 | array( |
| 287 | array( |
| 288 | 'id' => '@surecart/api-fetch', |
| 289 | 'import' => 'dynamic', |
| 290 | ), |
| 291 | array( |
| 292 | 'id' => '@wordpress/interactivity', |
| 293 | 'import' => 'dynamic', |
| 294 | ), |
| 295 | ), |
| 296 | $static_assets['version'] |
| 297 | ); |
| 298 | |
| 299 | // Checkout events. |
| 300 | $static_assets = include trailingslashit( plugin_dir_path( __FILE__ ) ) . 'build/scripts/checkout-events/index.asset.php'; |
| 301 | wp_register_script_module( |
| 302 | '@surecart/checkout-events', |
| 303 | trailingslashit( plugin_dir_url( __FILE__ ) ) . 'build/scripts/checkout-events/index.js', |
| 304 | [], |
| 305 | $static_assets['version'] |
| 306 | ); |
| 307 | |
| 308 | // Cart side drawer. |
| 309 | $static_assets = include trailingslashit( plugin_dir_path( __FILE__ ) ) . 'build/scripts/cart/index.asset.php'; |
| 310 | wp_register_script_module( |
| 311 | '@surecart/cart', |
| 312 | trailingslashit( plugin_dir_url( __FILE__ ) ) . 'build/scripts/cart/index.js', |
| 313 | array( |
| 314 | array( |
| 315 | 'id' => '@surecart/checkout', |
| 316 | 'import' => 'dynamic', |
| 317 | ), |
| 318 | array( |
| 319 | 'id' => '@surecart/checkout-events', |
| 320 | 'import' => 'dynamic', |
| 321 | ), |
| 322 | array( |
| 323 | 'id' => '@wordpress/interactivity', |
| 324 | 'import' => 'dynamic', |
| 325 | ), |
| 326 | ), |
| 327 | $static_assets['version'] |
| 328 | ); |
| 329 | |
| 330 | // SureCart Checkout. |
| 331 | $static_assets = include trailingslashit( plugin_dir_path( __FILE__ ) ) . 'build/scripts/checkout/index.asset.php'; |
| 332 | wp_register_script_module( |
| 333 | '@surecart/checkout', |
| 334 | trailingslashit( plugin_dir_url( __FILE__ ) ) . 'build/scripts/checkout/index.js', |
| 335 | array( |
| 336 | array( |
| 337 | 'id' => '@wordpress/interactivity', |
| 338 | 'import' => 'dynamic', |
| 339 | ), |
| 340 | array( |
| 341 | 'id' => '@surecart/checkout-service', |
| 342 | 'import' => 'dynamic', |
| 343 | ), |
| 344 | array( |
| 345 | 'id' => '@surecart/checkout-events', |
| 346 | 'import' => 'dynamic', |
| 347 | ), |
| 348 | array( |
| 349 | 'id' => '@surecart/google-events', |
| 350 | 'import' => 'dynamic', |
| 351 | ), |
| 352 | array( |
| 353 | 'id' => '@surecart/facebook-events', |
| 354 | 'import' => 'dynamic', |
| 355 | ), |
| 356 | ), |
| 357 | $static_assets['version'] |
| 358 | ); |
| 359 | }, |
| 360 | 10, |
| 361 | 3 |
| 362 | ); |
| 363 |