Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix clean up registration of blocks #30

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/blocks/interactive/likes-number/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
"editorScript": "file:./index.js",
"editorStyle": "file:./style.css",
"style": "file:./style-index.css",
"render": "file:./render.php"
"render": "file:./render.php",
"viewScript": "file:./view.js"
}
3 changes: 2 additions & 1 deletion src/blocks/interactive/movie-like-button/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@
},
"editorScript": "file:./index.js",
"render": "file:./render.php",
"style": "file:./style-index.css"
"style": "file:./style-index.css",
"viewScript": "file:./view.js"
}
3 changes: 2 additions & 1 deletion src/blocks/interactive/movie-like-icon/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
"editorScript": "file:./index.js",
"editorStyle": "file:./style.css",
"style": "file:./style-index.css",
"render": "file:./render.php"
"render": "file:./render.php",
"viewScript": "file:./view.js"
}
3 changes: 2 additions & 1 deletion src/blocks/interactive/movie-search/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
"editorScript": "file:./index.js",
"render": "file:./render.php",
"editorStyle": "file:./style.css",
"style": "file:./style-index.css"
"style": "file:./style-index.css",
"viewScript": "file:./view.js"
}
3 changes: 2 additions & 1 deletion src/blocks/interactive/movie-tabs/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
},
"editorScript": "file:./index.js",
"render": "file:./render.php",
"style": "file:./style-index.css"
"style": "file:./style-index.css",
"viewScript": "file:./view.js"
}
3 changes: 2 additions & 1 deletion src/blocks/interactive/video-player/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
},
"editorScript": "file:./index.js",
"render": "file:./render.php",
"style": "file:./style-index.css"
"style": "file:./style-index.css",
"viewScript": "file:./view.js"
}
12 changes: 0 additions & 12 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,6 @@ const { resolve } = require('path');

module.exports = [
defaultConfig,
{
...defaultConfig,
entry: {
'blocks/interactive/likes-number/view': './src/blocks/interactive/likes-number/view',
'blocks/interactive/movie-like-icon/view': './src/blocks/interactive/movie-like-icon/view',
'blocks/interactive/movie-search/view': './src/blocks/interactive/movie-search/view',
'blocks/interactive/video-player/view': './src/blocks/interactive/video-player/view',
'blocks/interactive/movie-tabs/view': './src/blocks/interactive/movie-tabs/view',
'blocks/interactive/movie-like-button/view':
'./src/blocks/interactive/movie-like-button/view',
},
},
{
...defaultConfig,
entry: {
Expand Down
96 changes: 48 additions & 48 deletions wpmovies.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,58 +36,58 @@ function () {
return;
}

add_action(
'init',
function () {
register_block_type( __DIR__ . '/build/blocks/interactive/movie-like-icon' );
register_block_type( __DIR__ . '/build/blocks/interactive/likes-number' );
register_block_type( __DIR__ . '/build/blocks/interactive/movie-search' );
register_block_type( __DIR__ . '/build/blocks/interactive/movie-trailer-button' );
register_block_type( __DIR__ . '/build/blocks/interactive/movie-like-button' );
register_block_type( __DIR__ . '/build/blocks/interactive/video-player' );
register_block_type( __DIR__ . '/build/blocks/interactive/movie-tabs' );
register_block_type( __DIR__ . '/build/blocks/interactive/movie-genres' );
register_block_type( __DIR__ . '/build/blocks/non-interactive/movie-data' );
register_block_type( __DIR__ . '/build/blocks/non-interactive/movie-score' );
register_block_type( __DIR__ . '/build/blocks/non-interactive/page-background' );
register_block_type( __DIR__ . '/build/blocks/non-interactive/movie-release-date' );
register_block_type( __DIR__ . '/build/blocks/non-interactive/movie-runtime' );
register_block_type( __DIR__ . '/build/blocks/non-interactive/actor-birthday' );
register_block_type( __DIR__ . '/build/blocks/non-interactive/actor-birth-place' );
add_action( 'init', 'auto_register_block_types' );

// Register all blocks found in the `build/blocks` folder.
function auto_register_block_types() {
if ( file_exists( __DIR__ . '/build/blocks/' ) ) {
$interactive_block_json_files = glob( __DIR__ . '/build/blocks/interactive/*/block.json' );
$non_interactive_block_json_files = glob( __DIR__ . '/build/blocks/non-interactive/*/block.json' );
$block_json_files = array_merge( $interactive_block_json_files, $non_interactive_block_json_files );

// auto register all blocks that were found.
foreach ( $block_json_files as $filename ) {
$block_folder = dirname( $filename );
register_block_type( $block_folder );
};
};
}

function add_script_dependency( $handle, $dep, $in_footer ) {
global $wp_scripts;

$script = $wp_scripts->query( $handle, 'registered' );
if ( ! $script )
return false;

if ( ! in_array( $dep, $script->deps, true ) ) {
$script->deps[] = $dep;

if ( $in_footer ) {
// move script to the footer
$wp_scripts->add_data( $handle, 'group', 1 );
}
}
);

// We need these filters to ensure the view.js files can access the window.__experimentalInteractivity
// Once the bundling is solved and we stop using
// window.__experimentalInteractivity we can remove them.
enqueue_interactive_blocks_scripts( 'movie-like-icon' );
enqueue_interactive_blocks_scripts( 'likes-number' );
enqueue_interactive_blocks_scripts( 'movie-search' );
enqueue_interactive_blocks_scripts( 'movie-like-button' );
enqueue_interactive_blocks_scripts( 'video-player' );
enqueue_interactive_blocks_scripts( 'movie-tabs' );

/**
* A helper function that enqueues scripts for the interactive blocks.
*
* @param string $block - The block name.
* @return void
*/
function enqueue_interactive_blocks_scripts( $block ) {
$interactive_block_filter = function ( $content ) use ( $block ) {
wp_register_script(
'wpmovies/' . $block,
plugin_dir_url( __FILE__ ) . 'build/blocks/interactive/' . $block . '/view.js',
array( 'wp-directive-runtime' ),
'1.0.0',
true
);
wp_enqueue_script( 'wpmovies/' . $block );
return $content;
};
add_filter( 'render_block_wpmovies/' . $block, $interactive_block_filter );
return true;
}

add_action( 'wp_enqueue_scripts', 'auto_inject_interactivity_dependency' );

function auto_inject_interactivity_dependency() {
$registered_blocks = \WP_Block_Type_Registry::get_instance()->get_all_registered();

foreach ( $registered_blocks as $name => $block ) {
$has_interactivity_support = $block->supports['interactivity'] ?? false;

if ( ! $has_interactivity_support ) {
continue;
}
foreach ( $block->view_script_handles as $handle ) {
add_script_dependency( $handle, 'wp-directive-runtime', true );
}
}
}

// ADD CRON EVENTS TO IMPORT MOVIES DAILY
// Create the necessary hook
Expand Down