Skip to content

Commit 18808d0

Browse files
committed
Refactor block assets loading
Now declares Plyr dependencies via registrations.
1 parent f1e3fcd commit 18808d0

1 file changed

Lines changed: 49 additions & 25 deletions

File tree

includes/class-protected-video-admin.php

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -47,59 +47,83 @@ public function __construct( $plugin_name, $version ) {
4747
* @return void
4848
*/
4949
public function register_block() {
50-
register_block_type( __DIR__ . '/../build' );
50+
$build_url = plugin_dir_url( __FILE__ ) . '../build/';
51+
$build_path = plugin_dir_path( __FILE__ ) . '../build/';
5152

5253
$plyr_style_handle = 'protected-video-plyr-style';
5354
$plyr_script_handle = 'protected-video-plyr-script';
5455

5556
if ( ! wp_style_is( $plyr_style_handle, 'registered' ) ) {
5657
wp_register_style(
5758
$plyr_style_handle,
58-
plugin_dir_url( __FILE__ ) . '../build/vendor/plyr/plyr.css',
59+
$build_url . 'vendor/plyr/plyr.css',
5960
array(),
6061
$this->version
6162
);
63+
wp_style_add_data(
64+
$plyr_style_handle,
65+
'path',
66+
$build_path . 'vendor/plyr/plyr.css'
67+
);
6268
}
6369

6470
if ( ! wp_script_is( $plyr_script_handle, 'registered' ) ) {
6571
wp_register_script(
6672
$plyr_script_handle,
67-
plugin_dir_url( __FILE__ ) . '../build/vendor/plyr/plyr.min.js',
73+
$build_url . 'vendor/plyr/plyr.min.js',
6874
array(),
6975
$this->version,
7076
array(
7177
'in_footer' => true,
7278
'strategy' => 'defer',
7379
)
7480
);
81+
wp_script_add_data(
82+
$plyr_script_handle,
83+
'path',
84+
$build_path . 'vendor/plyr/plyr.min.js'
85+
);
7586
}
7687

88+
register_block_type( __DIR__ . '/../build' );
89+
7790
$block_style_handle = 'protected-video-protected-video-style';
78-
$wp_styles = wp_styles();
79-
if (
80-
isset( $wp_styles->registered[ $block_style_handle ] ) &&
81-
! in_array(
82-
$plyr_style_handle,
83-
$wp_styles->registered[ $block_style_handle ]->deps,
84-
true
85-
)
86-
) {
87-
$wp_styles->registered[ $block_style_handle ]->deps[] = $plyr_style_handle;
88-
}
91+
wp_deregister_style( $block_style_handle );
92+
wp_register_style(
93+
$block_style_handle,
94+
$build_url . 'style-index.css',
95+
array( $plyr_style_handle ),
96+
$this->version
97+
);
98+
wp_style_add_data( $block_style_handle, 'path', $build_path . 'style-index.css' );
99+
wp_style_add_data( $block_style_handle, 'rtl', 'replace' );
89100

90101
$block_script_handle = 'protected-video-protected-video-view-script';
91-
$wp_scripts = wp_scripts();
92-
if (
93-
isset( $wp_scripts->registered[ $block_script_handle ] ) &&
94-
! in_array(
95-
$plyr_script_handle,
96-
$wp_scripts->registered[ $block_script_handle ]->deps,
97-
true
98-
)
99-
) {
100-
$wp_scripts->registered[ $block_script_handle ]->deps[] = $plyr_script_handle;
102+
$asset_deps = array();
103+
$asset_version = $this->version;
104+
if ( file_exists( $build_path . 'view.asset.php' ) ) {
105+
$asset = require $build_path . 'view.asset.php';
106+
if ( isset( $asset['dependencies'] ) && is_array( $asset['dependencies'] ) ) {
107+
$asset_deps = $asset['dependencies'];
108+
}
109+
if ( isset( $asset['version'] ) && is_string( $asset['version'] ) ) {
110+
$asset_version = $asset['version'];
111+
}
101112
}
102113

114+
wp_deregister_script( $block_script_handle );
115+
wp_register_script(
116+
$block_script_handle,
117+
$build_url . 'view.js',
118+
array_merge( array( $plyr_script_handle ), $asset_deps ),
119+
$asset_version,
120+
array(
121+
'in_footer' => true,
122+
'strategy' => 'defer',
123+
)
124+
);
125+
wp_script_add_data( $block_script_handle, 'path', $build_path . 'view.js' );
126+
103127
wp_script_add_data( $block_script_handle, 'strategy', 'defer' );
104128
wp_script_add_data( $block_script_handle, 'group', 1 );
105129

@@ -108,7 +132,7 @@ public function register_block() {
108132
'window.ProtectedVideoPlyr = ' .
109133
wp_json_encode(
110134
array(
111-
'iconUrl' => plugin_dir_url( __FILE__ ) . '../build/vendor/plyr/plyr.svg',
135+
'iconUrl' => $build_url . 'vendor/plyr/plyr.svg',
112136
)
113137
) .
114138
';',

0 commit comments

Comments
 (0)