|
20 | 20 | die; |
21 | 21 | } |
22 | 22 |
|
| 23 | +define( 'SRFBE', 'search-replace-for-block-editor' ); |
| 24 | + |
23 | 25 | /** |
24 | 26 | * Load Search & Replace Script for Block Editor. |
25 | 27 | * |
26 | 28 | * @since 1.0.0 |
27 | 29 | * @since 1.0.2 Load asset via plugin directory URL. |
28 | 30 | * @since 1.2.2 Localise WP version. |
| 31 | + * @since 1.7.0 Use webpack generated PHP asset file. |
29 | 32 | * |
30 | 33 | * @wp-hook 'enqueue_block_editor_assets' |
31 | 34 | */ |
32 | 35 | add_action( 'enqueue_block_editor_assets', function() { |
33 | 36 | global $wp_version; |
34 | 37 |
|
| 38 | + $assets = get_assets( plugin_dir_path( __FILE__ ) . './dist/app.asset.php' ); |
| 39 | + |
35 | 40 | wp_enqueue_script( |
36 | | - 'search-replace-for-block-editor', |
| 41 | + SRFBE, |
37 | 42 | trailingslashit( plugin_dir_url( __FILE__ ) ) . 'dist/app.js', |
38 | | - [ |
39 | | - 'wp-i18n', |
40 | | - 'wp-element', |
41 | | - 'wp-blocks', |
42 | | - 'wp-components', |
43 | | - 'wp-editor', |
44 | | - 'wp-hooks', |
45 | | - 'wp-compose', |
46 | | - 'wp-plugins', |
47 | | - 'wp-edit-post', |
48 | | - ], |
49 | | - '1.6.0', |
| 43 | + $assets['dependencies'], |
| 44 | + $assets['version'], |
50 | 45 | false, |
51 | 46 | ); |
52 | 47 |
|
53 | 48 | wp_set_script_translations( |
54 | | - 'search-replace-for-block-editor', |
55 | | - 'search-replace-for-block-editor', |
| 49 | + SRFBE, |
| 50 | + SRFBE, |
56 | 51 | plugin_dir_path( __FILE__ ) . 'languages' |
57 | 52 | ); |
58 | 53 |
|
59 | 54 | wp_localize_script( |
60 | | - 'search-replace-for-block-editor', |
| 55 | + SRFBE, |
61 | 56 | 'srfbe', |
62 | 57 | [ |
63 | 58 | 'wpVersion' => $wp_version, |
|
74 | 69 | */ |
75 | 70 | add_action( 'init', function() { |
76 | 71 | load_plugin_textdomain( |
77 | | - 'search-replace-for-block-editor', |
| 72 | + SRFBE, |
78 | 73 | false, |
79 | 74 | dirname( plugin_basename( __FILE__ ) ) . '/languages' |
80 | 75 | ); |
81 | 76 | } ); |
| 77 | + |
| 78 | +/** |
| 79 | + * Get Asset dependencies. |
| 80 | + * |
| 81 | + * @since 1.7.0 |
| 82 | + * |
| 83 | + * @param string $path Path to webpack generated PHP asset file. |
| 84 | + * @return array |
| 85 | + */ |
| 86 | +function get_assets( string $path ): array { |
| 87 | + $assets = [ |
| 88 | + 'version' => strval( time() ), |
| 89 | + 'dependencies' => [], |
| 90 | + ]; |
| 91 | + |
| 92 | + if ( ! file_exists( $path ) ) { |
| 93 | + return $assets; |
| 94 | + } |
| 95 | + |
| 96 | + // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.UsingVariable |
| 97 | + $assets = require_once $path; |
| 98 | + |
| 99 | + return $assets; |
| 100 | +} |
0 commit comments