Skip to content

Commit

Permalink
Merge pull request #21 from Travelopia/feat/WP-88-table-block-refactor
Browse files Browse the repository at this point in the history
Refactor Table Block
  • Loading branch information
junaidbhura authored Aug 4, 2024
2 parents 7cb74d3 + e3d9ef4 commit 0f470f4
Show file tree
Hide file tree
Showing 50 changed files with 1,294 additions and 976 deletions.
125 changes: 125 additions & 0 deletions .bin/block-manifest-generator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php
/**
* Block Manifest Generator.
*
* @package wordpress-blocks
*/

// Theme info.
$theme_dir = dirname( __DIR__ );
$_cache_file = dirname( __DIR__ ) . '/node_modules/.cache/blocks-info.json';
$blocks_dir = dirname( __DIR__ ) . '/src/editor/blocks';
$blocks_manifest = dirname( __DIR__ ) . '/dist/blocks.php';

// Iterator to get all the block bootstrap files.
$files_iterator = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( dirname( __DIR__ ) . '/src/editor/blocks' ) );

// Namespace to block relative path mapping.
$blocks_mapping = [];

// Get local cache.
$cache = read_cache( $_cache_file );

// Loop through the iterator.
foreach ( $files_iterator as $file ) {
if ( $file->getFilename() !== 'index.php' ) {
continue;
}

// Get the block info.
$block = $file->getPath();
$block_file = $block . '/index.php';
$file_modified_time = filemtime( $block_file );
$block_path = str_replace( $theme_dir . '/', '', $block ) . '/index.php';

// If the block file is already in cache and the file is not modified, skip.
if ( isset( $cache[ $block_file ] ) && $cache[ $block_file ]['mtime'] === $file_modified_time ) {
$blocks_mapping[ $block_path ] = $cache[ $block_file ]['namespace'];
continue;
}

// Tokenize the file to get the namespace.
$tokens = token_get_all( file_get_contents( $block . '/index.php' ) );

// Store the namespace.
$namespace = '';

// Loop through the tokens to get the namespace.
foreach ( $tokens as $token ) {
// If the token is a namespace token, store the namespace.
// @see: <https://www.php.net/manual/en/tokens.php#constant.t-name-qualified>.
if ( T_NAME_QUALIFIED === $token[0] ) {
$namespace = $token[1];
break;
}
}

// Bail with error if namespace not found.
if ( empty( $namespace ) ) {
logger( 'Error: Namespace not found in ' . $block_path, 'e' );
continue;
}

// Add the namespace to the mapping.
$blocks_mapping[ $block_path ] = $namespace;

// Update cache.
$cache[ $block_file ] = [
'namespace' => $namespace,
'mtime' => $file_modified_time,
];
}

// If manifest path not exists, create it recursively.
if ( ! file_exists( dirname( $blocks_manifest ) ) ) {
mkdir( dirname( $blocks_manifest ), 0755, true );
}

// Add the manifest comment.
$manifest_comment = <<<EOT
<?php
/**
* Warning: This is an auto-generated file. Do not modify this file directly.
*/
EOT;

// Write the manifest file.
file_put_contents( $blocks_manifest, $manifest_comment . PHP_EOL . PHP_EOL . 'return ' . var_export( $blocks_mapping, true ) . ';' );

// Log the success message.
logger( 'Success: Blocks manifest file updated.', 's' );

// Write cache.
write_cache( $_cache_file, $cache );

// Helpers.
// Logger function.
function logger( $message, $type = 'i' ) {
$message = match ( $type ) {
'e' => "\033[31m$message \033[0m\n", // error
's' => "\033[32m$message \033[0m\n", // success
'w' => "\033[33m$message \033[0m\n", // warning
'i' => "\033[36m$message \033[0m\n", // info
default => $message,
};

echo $message;
}

// Read cache file.
function read_cache( $cache_file ) {
if ( ! file_exists( $cache_file ) ) {
return [];
}

return json_decode( file_get_contents( $cache_file ), true );
}

// Write cache file.
function write_cache( $cache_file, $data ) {
if ( ! file_exists( dirname( $cache_file ) ) ) {
mkdir( dirname( $cache_file ), 0755, true );
}

file_put_contents( $cache_file, json_encode( $data, JSON_PRETTY_PRINT ) );
}
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ vendor

# --- Root
/*
!.bin
!.browserslistrc
!.eslintignore
!.eslintrc.json
Expand Down Expand Up @@ -36,4 +37,4 @@ vendor
# --- Source Code
!inc
!src
!dist
!dist
12 changes: 12 additions & 0 deletions dist/blocks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
/**
* Warning: This is an auto-generated file. Do not modify this file directly.
*/

return array (
'src/editor/blocks/table/index.php' => 'Travelopia\\Blocks\\Table',
'src/editor/blocks/table/children/column/index.php' => 'Travelopia\\Blocks\\Table\\Column',
'src/editor/blocks/table/children/cell/index.php' => 'Travelopia\\Blocks\\Table\\Cell',
'src/editor/blocks/table/children/row/index.php' => 'Travelopia\\Blocks\\Table\\Row',
'src/editor/blocks/table/children/row-container/index.php' => 'Travelopia\\Blocks\\Table\\RowContainer',
);
2 changes: 1 addition & 1 deletion dist/editor/blocks.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('react', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-data', 'wp-element', 'wp-hooks', 'wp-i18n', 'wp-primitives'), 'version' => 'a9d2811d6f17d28c57df');
<?php return array('dependencies' => array('react', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-data', 'wp-element', 'wp-hooks', 'wp-i18n', 'wp-primitives'), 'version' => '326f13cf548360be4989');
2 changes: 1 addition & 1 deletion dist/editor/blocks.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/editor/blocks.css.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/editor/blocks.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/editor/blocks.js.map

Large diffs are not rendered by default.

54 changes: 0 additions & 54 deletions inc/blocks/table-cell.php

This file was deleted.

91 changes: 0 additions & 91 deletions inc/blocks/table-column.php

This file was deleted.

Loading

0 comments on commit 0f470f4

Please sign in to comment.