diff --git a/includes/create-theme/theme-utils.php b/includes/create-theme/theme-utils.php index 4a3b8feb..0dad468f 100644 --- a/includes/create-theme/theme-utils.php +++ b/includes/create-theme/theme-utils.php @@ -24,10 +24,228 @@ public static function get_file_extension_from_url( $url ) { return $extension; } - public static function replace_namespace( $content, $old_slug, $new_slug, $old_name, $new_name ) { - $new_slug_underscore = str_replace( '-', '_', $new_slug ) . '_'; + public static function replace_namespace( $content, $old_slug, $new_slug, $old_name, $new_name, $file_extension = '' ) { + $new_slug_underscore = self::get_php_identifier( $new_slug ) . '_'; $old_slug_underscore = str_replace( '-', '_', $old_slug ) . '_'; + if ( false !== strpos( $content, ' $new_identifier, + $old_capitalized => $new_capitalized, + $old_name_identifier => $new_capitalized, + ); + $filtered_replacements = array_filter( $replacements, 'strlen', ARRAY_FILTER_USE_KEY ); + + uksort( + $filtered_replacements, + function ( $first, $second ) { + return strlen( $second ) - strlen( $first ); + } + ); + + return $filtered_replacements; + } + + private static function replace_javascript_identifier( $identifier, $old_slug, $new_slug, $old_name, $preserve_plain_values = false ) { + $replacements = self::get_javascript_identifier_replacements( $old_slug, $new_slug, $old_name ); + + foreach ( $replacements as $old_identifier => $new_identifier ) { + if ( 0 !== strpos( $identifier, $old_identifier ) ) { + continue; + } + + $suffix = substr( $identifier, strlen( $old_identifier ) ); + + if ( + $preserve_plain_values && + ( + ( '' === $suffix && ( $identifier === $old_slug || $identifier === $old_name ) ) || + ( '' !== $suffix && ! preg_match( '/^[A-Z0-9]/', $suffix ) ) + ) + ) { + return $identifier; + } + + return $new_identifier . $suffix; + } + + return $identifier; + } + + private static function replace_embedded_javascript_identifiers( $content, $old_slug, $new_slug, $old_name ) { + return preg_replace_callback( + '/[$A-Z_a-z][$0-9A-Z_a-z]*/', + function ( $matches ) use ( $old_slug, $new_slug, $old_name ) { + return self::replace_javascript_identifier( $matches[0], $old_slug, $new_slug, $old_name, true ); + }, + $content + ); + } + + private static function replace_localized_javascript_identifier( $content, $old_slug, $new_slug, $old_name ) { + $pattern = '/(wp_localize_script\s*\(\s*[^,]+,\s*)([\'\"])([$A-Z_a-z][$0-9A-Z_a-z]*)(\2)/'; + + return preg_replace_callback( + $pattern, + function ( $matches ) use ( $old_slug, $new_slug, $old_name ) { + $identifier = self::replace_javascript_identifier( $matches[3], $old_slug, $new_slug, $old_name ); + + return $matches[1] . $matches[2] . $identifier . $matches[4]; + }, + $content + ); + } + + private static function replace_javascript_namespace( $content, $old_slug, $new_slug, $old_name, $new_name, $old_slug_underscore, $new_slug_underscore ) { + $content = self::replace_embedded_javascript_identifiers( $content, $old_slug, $new_slug, $old_name ); + $pattern = <<<'REGEX' +~(?P"(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*') +|(?P