* * WordPress kebab-cases a preset slug to name its custom property, so the slug `h1` * becomes `--wp--preset--font-size--h-1`. It also rewrites every reference in * theme.json to that form when it loads the file. The editor then reads `h-1` back * from the reference and doesn't find a preset with that slug, so the block's * Typography or Color panel shows no value for what the theme sets. * * Output, one line per slug that kebab-casing changes: * SLUG -> * * @package wpcom-themes */ /** * The same function as WordPress's `_wp_to_kebab_case()` (wp-includes/functions.php). * * @param string $input_string The string to kebab-case. * @return string The kebab-cased string. */ function theme_review_to_kebab_case( $input_string ) { // phpcs:disable WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase $rsLowerRange = 'a-z\\xdf-\\xf6\\xf8-\\xff'; $rsNonCharRange = '\\x00-\\x2f\\x3a-\\x40\\x5b-\\x60\\x7b-\\xbf'; $rsPunctuationRange = '\\x{2000}-\\x{206f}'; $rsSpaceRange = ' \\t\\x0b\\f\\xa0\\x{feff}\\n\\r\\x{2028}\\x{2029}\\x{1680}\\x{180e}\\x{2000}\\x{2001}\\x{2002}\\x{2003}\\x{2004}\\x{2005}\\x{2006}\\x{2007}\\x{2008}\\x{2009}\\x{200a}\\x{202f}\\x{205f}\\x{3000}'; $rsUpperRange = 'A-Z\\xc0-\\xd6\\xd8-\\xde'; $rsBreakRange = $rsNonCharRange . $rsPunctuationRange . $rsSpaceRange; $rsBreak = '[' . $rsBreakRange . ']'; $rsDigits = '\\d+'; $rsLower = '[' . $rsLowerRange . ']'; $rsMisc = '[^' . $rsBreakRange . $rsDigits . $rsLowerRange . $rsUpperRange . ']'; $rsUpper = '[' . $rsUpperRange . ']'; $rsMiscLower = '(?:' . $rsLower . '|' . $rsMisc . ')'; $rsMiscUpper = '(?:' . $rsUpper . '|' . $rsMisc . ')'; $rsOrdLower = '\\d*(?:1st|2nd|3rd|(?![123])\\dth)(?=\\b|[A-Z_])'; $rsOrdUpper = '\\d*(?:1ST|2ND|3RD|(?![123])\\dTH)(?=\\b|[a-z_])'; $regexp = '/' . implode( '|', array( $rsUpper . '?' . $rsLower . '+(?=' . implode( '|', array( $rsBreak, $rsUpper, '$' ) ) . ')', $rsMiscUpper . '+(?=' . implode( '|', array( $rsBreak, $rsUpper . $rsMiscLower, '$' ) ) . ')', $rsUpper . '?' . $rsMiscLower . '+', $rsUpper . '+', $rsOrdUpper, $rsOrdLower, $rsDigits, ) ) . '/u'; preg_match_all( $regexp, str_replace( "'", '', $input_string ), $matches ); return strtolower( implode( '-', $matches[0] ) ); // phpcs:enable WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase } /** * Print every preset under $node whose slug kebab-casing changes. * * @param string $file File being checked. * @param mixed $node Part of the settings tree. * @param string $path Path of $node. */ function theme_review_check_presets( $file, $node, $path ) { if ( ! is_array( $node ) ) { return; } foreach ( $node as $key => $value ) { if ( is_array( $value ) && isset( $value['slug'] ) && is_string( $value['slug'] ) ) { $kebab = theme_review_to_kebab_case( $value['slug'] ); if ( $kebab !== $value['slug'] ) { echo 'SLUG ' . $file . ' ' . $path . ' ' . $value['slug'] . ' -> ' . $kebab . "\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- CLI output. } continue; } theme_review_check_presets( $file, $value, is_int( $key ) ? $path : $path . '.' . $key ); } } $theme_review_theme = isset( $argv[1] ) ? rtrim( $argv[1], '/' ) : ''; if ( '' === $theme_review_theme || ! is_dir( $theme_review_theme ) ) { fwrite( STDERR, "Usage: php preset-slugs.php \n" ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fwrite exit( 1 ); } $theme_review_files = array_merge( glob( $theme_review_theme . '/theme.json' ), glob( $theme_review_theme . '/styles/*.json' ), glob( $theme_review_theme . '/styles/*/*.json' ) ); foreach ( $theme_review_files as $theme_review_file ) { $theme_review_data = json_decode( file_get_contents( $theme_review_file ), true ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents if ( isset( $theme_review_data['settings'] ) ) { theme_review_check_presets( $theme_review_file, $theme_review_data['settings'], 'settings' ); } }