: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* This class registers all the necessary styles and scripts.
* Also has methods for the enqueing of scripts and styles.
* It automatically adds a prefix to the handle.
class WPSEO_Admin_Asset_Manager {
* Prefix for naming the assets.
public const PREFIX = 'yoast-seo-';
* Class that manages the assets' location.
* @var WPSEO_Admin_Asset_Location
protected $asset_location;
* Prefix for naming the assets.
* Constructs a manager of assets. Needs a location to know where to register assets at.
* @param WPSEO_Admin_Asset_Location|null $asset_location The provider of the asset location.
* @param string $prefix The prefix for naming assets.
public function __construct( ?WPSEO_Admin_Asset_Location $asset_location = null, $prefix = self::PREFIX ) {
if ( $asset_location === null ) {
$asset_location = self::create_default_location();
$this->asset_location = $asset_location;
* @param string $script The name of the script to enqueue.
public function enqueue_script( $script ) {
wp_enqueue_script( $this->prefix . $script );
* @param string $style The name of the style to enqueue.
public function enqueue_style( $style ) {
wp_enqueue_style( $this->prefix . $style );
* Enqueues the appropriate language for the user.
public function enqueue_user_language_script() {
$this->enqueue_script( 'language-' . YoastSEO()->helpers->language->get_researcher_language() );
* Registers scripts based on it's parameters.
* @param WPSEO_Admin_Asset $script The script to register.
public function register_script( WPSEO_Admin_Asset $script ) {
$url = $script->get_src() ? $this->get_url( $script, WPSEO_Admin_Asset::TYPE_JS ) : false;
$this->prefix . $script->get_name(),
if ( in_array( 'wp-i18n', $script->get_deps(), true ) ) {
wp_set_script_translations( $this->prefix . $script->get_name(), 'wordpress-seo' );
* Registers styles based on it's parameters.
* @param WPSEO_Admin_Asset $style The style to register.
public function register_style( WPSEO_Admin_Asset $style ) {
$this->prefix . $style->get_name(),
$this->get_url( $style, WPSEO_Admin_Asset::TYPE_CSS ),
* Calls the functions that register scripts and styles with the scripts and styles to be registered as arguments.
public function register_assets() {
$this->register_scripts( $this->scripts_to_be_registered() );
$this->register_styles( $this->styles_to_be_registered() );
* Registers all the scripts passed to it.
* @param array $scripts The scripts passed to it.
public function register_scripts( $scripts ) {
foreach ( $scripts as $script ) {
$script = new WPSEO_Admin_Asset( $script );
$this->register_script( $script );
* Registers all the styles it receives.
* @param array $styles Styles that need to be registered.
public function register_styles( $styles ) {
foreach ( $styles as $style ) {
$style = new WPSEO_Admin_Asset( $style );
$this->register_style( $style );
* @param string $handle The script handle.
* @param string $object_name The object name.
* @param array $data The l10n data.
public function localize_script( $handle, $object_name, $data ) {
wp_localize_script( $this->prefix . $handle, $object_name, $data );
* @param string $handle The script handle.
* @param string $data The l10n data.
* @param string $position Optional. Whether to add the inline script before the handle or after.
public function add_inline_script( $handle, $data, $position = 'after' ) {
wp_add_inline_script( $this->prefix . $handle, $data, $position );
* A list of styles that shouldn't be registered but are needed in other locations in the plugin.
public function special_styles() {
$flat_version = $this->flatten_version( WPSEO_VERSION );
'name' => 'inside-editor',
'src' => 'inside-editor-' . $flat_version,
return [ 'inside-editor' => new WPSEO_Admin_Asset( $asset_args ) ];
* Flattens a version number for use in a filename.
* @param string $version The original version number.
* @return string The flattened version number.
public function flatten_version( $version ) {
$parts = explode( '.', $version );
if ( count( $parts ) === 2 && preg_match( '/^\d+$/', $parts[1] ) === 1 ) {
return implode( '', $parts );
* Creates a default location object for use in the admin asset manager.
* @return WPSEO_Admin_Asset_Location The location to use in the asset manager.
public static function create_default_location() {
if ( defined( 'YOAST_SEO_DEV_SERVER' ) && YOAST_SEO_DEV_SERVER ) {
$url = defined( 'YOAST_SEO_DEV_SERVER_URL' ) ? YOAST_SEO_DEV_SERVER_URL : WPSEO_Admin_Asset_Dev_Server_Location::DEFAULT_URL;
return new WPSEO_Admin_Asset_Dev_Server_Location( $url );
return new WPSEO_Admin_Asset_SEO_Location( WPSEO_FILE, false );
* Checks if the given script is enqueued.
* @param string $script The script to check.
* @return bool True when the script is enqueued.
public function is_script_enqueued( $script ) {
return wp_script_is( $this->prefix . $script );
* Returns the scripts that need to be registered.
* @todo Data format is not self-documenting. Needs explanation inline. R.
* @return array The scripts that need to be registered.
protected function scripts_to_be_registered() {
'redirect-old-features-tab',
$additional_dependencies = [
'analysis-worker' => [ self::PREFIX . 'analysis-package' ],
'api-client' => [ 'wp-api' ],
'crawl-settings' => [ 'jquery' ],
'dashboard-widget' => [ self::PREFIX . 'api-client' ],
'wincher-dashboard-widget' => [ self::PREFIX . 'api-client' ],
'editor-modules' => [ 'jquery' ],
self::PREFIX . 'api-client',
self::PREFIX . 'externals-components',
self::PREFIX . 'externals-contexts',
self::PREFIX . 'externals-redux',
'first-time-configuration' => [
self::PREFIX . 'api-client',
self::PREFIX . 'externals-components',
self::PREFIX . 'externals-contexts',
self::PREFIX . 'externals-redux',
self::PREFIX . 'api-client',
self::PREFIX . 'externals-components',
self::PREFIX . 'externals-contexts',
self::PREFIX . 'externals-redux',
self::PREFIX . 'api-client',
self::PREFIX . 'block-editor',
self::PREFIX . 'externals-components',
self::PREFIX . 'externals-contexts',
self::PREFIX . 'externals-redux',
self::PREFIX . 'api-client',
self::PREFIX . 'externals-components',
self::PREFIX . 'externals-contexts',
self::PREFIX . 'externals-redux',
self::PREFIX . 'api-client',
self::PREFIX . 'classic-editor',
self::PREFIX . 'externals-components',
self::PREFIX . 'externals-contexts',
self::PREFIX . 'externals-redux',
$plugin_scripts = $this->load_generated_asset_file(
'asset_file' => __DIR__ . '/../src/generated/assets/plugin.php',
'additional_deps' => $additional_dependencies,
'header_scripts' => $header_scripts,
$external_scripts = $this->load_generated_asset_file(
'asset_file' => __DIR__ . '/../src/generated/assets/externals.php',
'base_dir' => 'externals/',
'additional_deps' => $additional_dependencies,
'header_scripts' => $header_scripts,
$language_scripts = $this->load_generated_asset_file(
'asset_file' => __DIR__ . '/../src/generated/assets/languages.php',
'base_dir' => 'languages/',
'additional_deps' => $additional_dependencies,
'header_scripts' => $header_scripts,
$renamed_scripts = $this->load_renamed_scripts();
$scripts['installation-success'] = [
'name' => 'installation-success',
'src' => 'installation-success.js',
self::PREFIX . 'components-new-package',
self::PREFIX . 'externals-components',
'version' => $scripts['installation-success']['version'],
$scripts['post-edit-classic'] = [
'name' => 'post-edit-classic',
'src' => $scripts['post-edit']['src'],
static function ( $dep ) {
if ( $dep === self::PREFIX . 'block-editor' ) {
return self::PREFIX . 'classic-editor';
$scripts['post-edit']['deps']
'in_footer' => ! in_array( 'post-edit-classic', $header_scripts, true ),
'version' => $scripts['post-edit']['version'],
self::PREFIX . 'externals-components',
self::PREFIX . 'externals-contexts',
self::PREFIX . 'externals-redux',
self::PREFIX . 'analysis',
self::PREFIX . 'react-select',
self::PREFIX . 'components-new-package',
'version' => $scripts['workouts']['version'],
// Add the current language to every script that requires the analysis package.
foreach ( $scripts as $name => $script ) {
if ( substr( $name, -8 ) === 'language' ) {
if ( in_array( self::PREFIX . 'analysis-package', $script['deps'], true ) ) {
$scripts[ $name ]['deps'][] = self::PREFIX . YoastSEO()->helpers->language->get_researcher_language() . '-language';
* Loads a generated asset file.
* @type string $asset_file The asset file to load.
* @type int $ext_length The length of the extension, including suffix, of the filename.
* @type string $suffix Optional. The suffix of the asset name.
* @type array<string, string[]> $additional_deps Optional. The additional dependencies assets may have.
* @type string $base_dir Optional. The base directory of the asset.
* @type string[] $header_scripts Optional. The script names that should be in the header.
* The scripts to be registered.
* @type string $name The name of the asset.
* @type string $src The src of the asset.
* @type string[] $deps The dependenies of the asset.
* @type bool $in_footer Whether or not the asset should be in the footer.
protected function load_generated_asset_file( $args ) {
$assets = require $args['asset_file'];
foreach ( $assets as $file => $data ) {
$name = substr( $file, 0, -$args['ext_length'] );
$name = strtolower( preg_replace( '/([A-Z])/', '-$1', $name ) );
$name .= $args['suffix'];
$deps = $data['dependencies'];
if ( isset( $args['additional_deps'][ $name ] ) ) {
$deps = array_merge( $deps, $args['additional_deps'][ $name ] );
'src' => $args['base_dir'] . $file,
'in_footer' => ! in_array( $name, $args['header_scripts'], true ),
'version' => $data['version'],
* Loads the scripts that should be renamed for BC.
* The scripts to be registered.
* @type string $name The name of the asset.
* @type string $src The src of the asset.
* @type string[] $deps The dependenies of the asset.
* @type bool $in_footer Whether or not the asset should be in the footer.
protected function load_renamed_scripts() {
'admin-global-script' => 'admin-global',
'analysis' => 'analysis-package',
'analysis-report' => 'analysis-report-package',
'commons' => 'commons-package',
'edit-page' => 'edit-page-script',