| 1 |
<?php |
| 2 |
/** |
| 3 |
* Callbacks related to block support. |
| 4 |
* |
| 5 |
* @package FaustWP |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WPE\FaustWP\Blocks; |
| 9 |
|
| 10 |
if ( ! defined( 'ABSPATH' ) ) { |
| 11 |
exit; |
| 12 |
} |
| 13 |
|
| 14 |
add_action( 'init', __NAMESPACE__ . '\\register_custom_blocks' ); |
| 15 |
/** |
| 16 |
* Register Gutenberg blocks from block.json and index.asset.json files located in the specified paths. |
| 17 |
*/ |
| 18 |
function register_custom_blocks() { |
| 19 |
static $initialized = false; |
| 20 |
|
| 21 |
// Prevent subsequent runs since blocks should already be registered. |
| 22 |
if ( $initialized ) { |
| 23 |
return; |
| 24 |
} |
| 25 |
|
| 26 |
$uploads = wp_upload_dir(); |
| 27 |
|
| 28 |
// Define the base directory path and URL. |
| 29 |
$base_dir = trailingslashit( $uploads['basedir'] ) . 'faustwp/blocks/'; |
| 30 |
$base_url = trailingslashit( $uploads['baseurl'] ) . 'faustwp/blocks/'; |
| 31 |
|
| 32 |
// Check if the directory exists. |
| 33 |
if ( ! is_dir( $base_dir ) ) { |
| 34 |
return; |
| 35 |
} |
| 36 |
|
| 37 |
// Scan the directory for subdirectories (each representing a block). |
| 38 |
$block_dirs = array_filter( glob( $base_dir . '*' ), 'is_dir' ); |
| 39 |
|
| 40 |
foreach ( $block_dirs as $dir ) { |
| 41 |
$metadata_file = trailingslashit( $dir ) . 'block.json'; |
| 42 |
$asset_file = trailingslashit( $dir ) . 'index.asset.json'; |
| 43 |
|
| 44 |
if ( file_exists( $metadata_file ) ) { |
| 45 |
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
| 46 |
$block_metadata = json_decode( file_get_contents( $metadata_file ), true ); |
| 47 |
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
| 48 |
$asset_data = file_exists( $asset_file ) ? json_decode( file_get_contents( $asset_file ), true ) : array(); |
| 49 |
$block_name = basename( $dir ); |
| 50 |
|
| 51 |
$dependencies = $asset_data['dependencies'] ?? array(); |
| 52 |
$version = $asset_data['version'] ?? ''; |
| 53 |
|
| 54 |
$block_args = array(); |
| 55 |
|
| 56 |
// Register editor script. |
| 57 |
if ( isset( $block_metadata['editorScript'] ) ) { |
| 58 |
$editor_script_handle = register_block_asset( $block_metadata, 'editorScript', $block_name, $dependencies, $version ); |
| 59 |
if ( $editor_script_handle ) { |
| 60 |
$block_args['editor_script'] = $editor_script_handle; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
// Register editor style. |
| 65 |
if ( isset( $block_metadata['editorStyle'] ) ) { |
| 66 |
$editor_style_handle = register_block_asset( $block_metadata, 'editorStyle', $block_name, array(), $version ); |
| 67 |
if ( $editor_style_handle ) { |
| 68 |
$block_args['editor_style'] = $editor_style_handle; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
// Register style. |
| 73 |
if ( isset( $block_metadata['style'] ) ) { |
| 74 |
$style_handle = register_block_asset( $block_metadata, 'style', $block_name, array(), $version ); |
| 75 |
if ( $style_handle ) { |
| 76 |
$block_args['style'] = $style_handle; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
register_block_type( $metadata_file, $block_args ); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
$initialized = true; |
| 85 |
} |
| 86 |
|
| 87 |
add_filter( 'style_loader_src', __NAMESPACE__ . '\\correct_asset_src_for_uploads_dir', 10, 2 ); |
| 88 |
add_filter( 'script_loader_src', __NAMESPACE__ . '\\correct_asset_src_for_uploads_dir', 10, 2 ); |
| 89 |
/** |
| 90 |
* Modify the source URL for enqueued assets stored in the uploads directory. |
| 91 |
* |
| 92 |
* Filters the source URL of specific enqueued styles and scripts to correct their paths, |
| 93 |
* focusing on assets that include "faustwp/blocks" in their URL. |
| 94 |
* |
| 95 |
* @param string $src The source URL of the enqueued asset. |
| 96 |
* @param string $handle The asset's registered handle. |
| 97 |
* |
| 98 |
* @return string Modified or original source URL. |
| 99 |
* |
| 100 |
* @see https://github.com/WordPress/wordpress-develop/blob/6.3/src/wp-includes/blocks.php#L149-L165C3 |
| 101 |
*/ |
| 102 |
function correct_asset_src_for_uploads_dir( $src, $handle ) { |
| 103 |
// Check for the presence of "faustwp/blocks" in the src. |
| 104 |
if ( strpos( $src, 'faustwp/blocks' ) !== false ) { |
| 105 |
// Extract the specific block directory. |
| 106 |
preg_match( '#faustwp/blocks/([^/]+)#', $src, $matches ); |
| 107 |
|
| 108 |
if ( isset( $matches[1] ) ) { |
| 109 |
$uploads_dir = wp_upload_dir(); |
| 110 |
$base_url = trailingslashit( $uploads_dir['baseurl'] ); |
| 111 |
|
| 112 |
$correct_src = $base_url . 'faustwp/blocks/' . $matches[1] . '/' . wp_basename( $src ); |
| 113 |
return $correct_src; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
return $src; |
| 118 |
} |
| 119 |
|