WordPress.org

Make WordPress Core


Ignore:
Timestamp:
06/22/2021 09:49:13 AM (5 months ago)
Author:
jorgefilipecosta
Message:

Ports theme.json changes for beta 3.

  • Add _wp_to_kebab_case function
  • Add CSS Custom Properties within preset classes.

Props nosolosw.
See #53397.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/functions.php

    r51182 r51198  
    46994699
    47004700/**
     4701 * This function is trying to replicate what
     4702 * lodash's kebabCase (JS library) does in the client.
     4703 *
     4704 * The reason we need this function is that we do some processing
     4705 * in both the client and the server (e.g.: we generate
     4706 * preset classes from preset slugs) that needs to
     4707 * create the same output.
     4708 *
     4709 * We can't remove or update the client's library due to backward compatibility
     4710 * (some of the output of lodash's kebabCase is saved in the post content).
     4711 * We have to make the server behave like the client.
     4712 *
     4713 * Changes to this function should follow updates in the client
     4714 * with the same logic.
     4715 *
     4716 * @link https://github.com/lodash/lodash/blob/4.17/dist/lodash.js#L14369
     4717 * @link https://github.com/lodash/lodash/blob/4.17/dist/lodash.js#L278
     4718 * @link https://github.com/lodash-php/lodash-php/blob/master/src/String/kebabCase.php
     4719 * @link https://github.com/lodash-php/lodash-php/blob/master/src/internal/unicodeWords.php
     4720 *
     4721 * @param string $string The string to kebab-case.
     4722 *
     4723 * @return string kebab-cased-string.
     4724 */
     4725function _wp_to_kebab_case( $string ) {
     4726    //phpcs:disable WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
     4727    // ignore the camelCase names for variables so the names are the same as lodash
     4728    // so comparing and porting new changes is easier.
     4729
     4730    /*
     4731     * Some notable things we've removed compared to the lodash version are:
     4732     *
     4733     * - non-alphanumeric characters: rsAstralRange, rsEmoji, etc
     4734     * - the groups that processed the apostrophe, as it's removed before passing the string to preg_match: rsApos, rsOptContrLower, and rsOptContrUpper
     4735     *
     4736     */
     4737
     4738    /** Used to compose unicode character classes. */
     4739    $rsLowerRange       = 'a-z\\xdf-\\xf6\\xf8-\\xff';
     4740    $rsNonCharRange     = '\\x00-\\x2f\\x3a-\\x40\\x5b-\\x60\\x7b-\\xbf';
     4741    $rsPunctuationRange = '\\x{2000}-\\x{206f}';
     4742    $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}';
     4743    $rsUpperRange       = 'A-Z\\xc0-\\xd6\\xd8-\\xde';
     4744    $rsBreakRange       = $rsNonCharRange . $rsPunctuationRange . $rsSpaceRange;
     4745
     4746    /** Used to compose unicode capture groups. */
     4747    $rsBreak  = '[' . $rsBreakRange . ']';
     4748    $rsDigits = '\\d+'; // The last lodash version in GitHub uses a single digit here and expands it when in use.
     4749    $rsLower  = '[' . $rsLowerRange . ']';
     4750    $rsMisc   = '[^' . $rsBreakRange . $rsDigits . $rsLowerRange . $rsUpperRange . ']';
     4751    $rsUpper  = '[' . $rsUpperRange . ']';
     4752
     4753    /** Used to compose unicode regexes. */
     4754    $rsMiscLower = '(?:' . $rsLower . '|' . $rsMisc . ')';
     4755    $rsMiscUpper = '(?:' . $rsUpper . '|' . $rsMisc . ')';
     4756    $rsOrdLower  = '\\d*(?:1st|2nd|3rd|(?![123])\\dth)(?=\\b|[A-Z_])';
     4757    $rsOrdUpper  = '\\d*(?:1ST|2ND|3RD|(?![123])\\dTH)(?=\\b|[a-z_])';
     4758
     4759    $regexp = '/' . implode(
     4760        '|',
     4761        array(
     4762            $rsUpper . '?' . $rsLower . '+' . '(?=' . implode( '|', array( $rsBreak, $rsUpper, '$' ) ) . ')',
     4763            $rsMiscUpper . '+' . '(?=' . implode( '|', array( $rsBreak, $rsUpper . $rsMiscLower, '$' ) ) . ')',
     4764            $rsUpper . '?' . $rsMiscLower . '+',
     4765            $rsUpper . '+',
     4766            $rsOrdUpper,
     4767            $rsOrdLower,
     4768            $rsDigits,
     4769        )
     4770    ) . '/u';
     4771
     4772    preg_match_all( $regexp, str_replace( "'", '', $string ), $matches );
     4773    return strtolower( implode( '-', $matches[0] ) );
     4774    //phpcs:enable WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
     4775}
     4776
     4777/**
    47014778 * Determines if the variable is a numeric-indexed array.
    47024779 *
Note: See TracChangeset for help on using the changeset viewer.