Skip to content
Closed
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
51 changes: 49 additions & 2 deletions packages/wp-build/lib/php-generator.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,19 @@ export async function getPhpReplacements( rootDir, baseUrlExpression ) {
};
}

/**
* Checks if the build script is being run for WordPress Core.
*
* @param {Record<string, string>} replacements Replacements object from getPhpReplacements().
* @return {boolean} Whether this is a WordPress Core build.
*/
function isWordPressCoreBuild( replacements ) {
return (
Boolean( process.env.npm_package_config_IS_WORDPRESS_CORE ) &&
replacements[ '{{PREFIX}}' ] === 'wp'
);
}

/**
* Apply template replacements to a template string.
*
Expand All @@ -56,6 +69,11 @@ export function applyTemplateReplacements( template, replacements ) {
/**
* Render a template to a string with replacements.
*
* Performs two passes:
* 1. Static placeholder replacements ({{PREFIX}}, {{VERSION}}, {{BASE_URL}}, etc.)
* 2. Pluggable guard resolution — {{IF_PLUGGABLE:fname}} / {{END_IF_PLUGGABLE}} markers
* are expanded to `! function_exists( 'fname' )` checks or removed entirely.
*
* @param {string} templateName Template file name.
* @param {Record<string, string>} replacements Replacements object (e.g. {'{{PREFIX}}': 'gutenberg'}).
* @return {Promise<string>} Rendered template string.
Expand All @@ -70,8 +88,37 @@ export async function renderTemplateToString( templateName, replacements ) {
'utf8'
);

// Apply replacements
return applyTemplateReplacements( template, replacements );
// Apply static replacements
let content = applyTemplateReplacements( template, replacements );

// Resolve pluggable guard markers.
const pluggableBlockRegex =
/\{\{IF_PLUGGABLE:([^}]+)\}\}\n([\s\S]*?)\n\{\{END_IF_PLUGGABLE\}\}/g;

if ( isWordPressCoreBuild( replacements ) ) {
// Remove the guard wrappers entirely.
content = content.replace(
pluggableBlockRegex,
( _match, _fnName, body ) => body
);
} else {
// Wrap block body in `if ( ! function_exists() )` and indent correctly.
content = content.replace(
pluggableBlockRegex,
( _match, fnName, body ) => {
const indented = body
.split( '\n' )
.map(
/** @param {string} line */
( line ) => ( line.length ? `\t${ line }` : line )
)
.join( '\n' );
return `if ( ! function_exists( '${ fnName }' ) ) {\n${ indented }\n}`;
}
);
}

return content;
}

/**
Expand Down
68 changes: 34 additions & 34 deletions packages/wp-build/templates/module-registration.php.template
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,44 @@
* @package {{PREFIX}}
*/

if ( ! function_exists( '{{PREFIX}}_register_script_modules' ) ) {
/**
* Register all script modules.
*/
function {{PREFIX}}_register_script_modules() {
// Load build constants
$build_constants = require __DIR__ . '/constants.php';
$modules_dir = __DIR__ . '/modules';
$modules_file = $modules_dir . '/registry.php';
{{IF_PLUGGABLE:{{PREFIX}}_register_script_modules}}
/**
* Register all script modules.
*/
function {{PREFIX}}_register_script_modules() {
// Load build constants
$build_constants = require __DIR__ . '/constants.php';
$modules_dir = __DIR__ . '/modules';
$modules_file = $modules_dir . '/registry.php';

if ( ! file_exists( $modules_file ) ) {
return;
}
if ( ! file_exists( $modules_file ) ) {
return;
}

$modules = require $modules_file;
$base_url = $build_constants['build_url'] . 'modules/';
$extension = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '.js' : '.min.js';
$modules = require $modules_file;
$base_url = $build_constants['build_url'] . 'modules/';
$extension = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '.js' : '.min.js';

foreach ( $modules as $module ) {
$asset_path = $modules_dir . '/' . $module['asset'];
$asset = file_exists( $asset_path ) ? require $asset_path : array();
foreach ( $modules as $module ) {
$asset_path = $modules_dir . '/' . $module['asset'];
$asset = file_exists( $asset_path ) ? require $asset_path : array();

// Deregister first to override any previously registered version
// (e.g., Core's default modules when running as a plugin).
wp_deregister_script_module( $module['id'] );
// Deregister first to override any previously registered version
// (e.g., Core's default modules when running as a plugin).
wp_deregister_script_module( $module['id'] );

wp_register_script_module(
$module['id'],
$base_url . $module['path'] . $extension,
$asset['module_dependencies'] ?? array(),
$asset['version'] ?? false,
array(
'fetchpriority' => 'low',
'in_footer' => true,
)
);
}
wp_register_script_module(
$module['id'],
$base_url . $module['path'] . $extension,
$asset['module_dependencies'] ?? array(),
$asset['version'] ?? false,
array(
'fetchpriority' => 'low',
'in_footer' => true,
)
);
}

add_action( 'wp_default_scripts', '{{PREFIX}}_register_script_modules' );
}

add_action( 'wp_default_scripts', '{{PREFIX}}_register_script_modules' );
{{END_IF_PLUGGABLE}}
Loading
Loading