WordPress.org

Make WordPress Core

Ticket #41305: 41305.implementation.diff

File 41305.implementation.diff, 14.6 KB (added by schlessera, 23 months ago)

No changes, just added .diff extension for syntax highlighting.

  • src/wp-includes/l10n.php

    diff --git src/wp-includes/l10n.php src/wp-includes/l10n.php
    index a81470ff7d..917e402ec8 100644
    function __( $text, $domain = 'default' ) { 
    203203}
    204204
    205205/**
     206 * Lazily retrieves the translation of $text.
     207 *
     208 * If there is no translation, or the text domain isn't loaded, the original
     209 * text is returned.
     210 *
     211 * @since 4.9.0
     212 *
     213 * @param string $text   Text to translate.
     214 * @param string $domain Optional. Text domain. Unique identifier for
     215 *                       retrieving translated strings. Default 'default'.
     216 *
     217 * @return WP_String_Proxy Proxy object representing a string.
     218 */
     219function _l( $text, $domain = 'default' ) {
     220        return new WP_Translation_Proxy( $text, $domain );
     221}
     222
     223/**
    206224 * Retrieve the translation of $text and escapes it for safe use in an attribute.
    207225 *
    208226 * If there is no translation, or the text domain isn't loaded, the original text is returned.
    function esc_attr__( $text, $domain = 'default' ) { 
    219237}
    220238
    221239/**
     240 * Lazily retrieve the translation of $text and escape it for safe use in an
     241 * attribute.
     242 *
     243 * If there is no translation, or the text domain isn't loaded, the original
     244 * text is returned.
     245 *
     246 * @since 4.9.0
     247 *
     248 * @param string $text   Text to translate.
     249 * @param string $domain Optional. Text domain. Unique identifier for
     250 *                       retrieving translated strings. Default 'default'.
     251 *
     252 * @return WP_String_Proxy Proxy object representing a string.
     253 */
     254function esc_attr_l( $text, $domain = 'default' ) {
     255        return new WP_Escape_Attribute_Proxy(
     256                new WP_Translation_Proxy( $text, $domain )
     257        );
     258}
     259
     260/**
    222261 * Retrieve the translation of $text and escapes it for safe use in HTML output.
    223262 *
    224263 * If there is no translation, or the text domain isn't loaded, the original text
    function esc_html__( $text, $domain = 'default' ) { 
    236275}
    237276
    238277/**
     278 * Lazily retrieve the translation of $text and escape it for safe use in HTML
     279 * output.
     280 *
     281 * If there is no translation, or the text domain isn't loaded, the original
     282 * text is escaped and returned..
     283 *
     284 * @since 4.9.0
     285 *
     286 * @param string $text   Text to translate.
     287 * @param string $domain Optional. Text domain. Unique identifier for
     288 *                       retrieving translated strings. Default 'default'.
     289 *
     290 * @return WP_String_Proxy Proxy object representing a string.
     291 */
     292function esc_html_l( $text, $domain = 'default' ) {
     293        return new WP_Escape_HTML_Proxy(
     294                new WP_Translation_Proxy( $text, $domain )
     295        );
     296}
     297
     298/**
    239299 * Display translated text.
    240300 *
    241301 * @since 1.2.0
    function _x( $text, $context, $domain = 'default' ) { 
    296356}
    297357
    298358/**
     359 * Lazily retrieve translated string with gettext context.
     360 *
     361 * Quite a few times, there will be collisions with similar translatable text
     362 * found in more than two places, but with different translated context.
     363 *
     364 * By including the context in the pot file, translators can translate the two
     365 * strings differently.
     366 *
     367 * @since 4.9.0
     368 *
     369 * @param string $text    Text to translate.
     370 * @param string $context Context information for the translators.
     371 * @param string $domain  Optional. Text domain. Unique identifier for
     372 *                        retrieving translated strings. Default 'default'.
     373 *
     374 * @return WP_String_Proxy Proxy object representing a string.
     375 */
     376function _lx( $text, $context, $domain = 'default' ) {
     377        return new WP_Contextual_Translation_Proxy( $text, $context, $domain );
     378}
     379
     380/**
    299381 * Display translated string with gettext context.
    300382 *
    301383 * @since 3.0.0
    function esc_attr_x( $text, $context, $domain = 'default' ) { 
    326408}
    327409
    328410/**
     411 * Lazily translate string with gettext context, and escape it for safe use in
     412 * an attribute.
     413 *
     414 * @since 4.9.0
     415 *
     416 * @param string $text    Text to translate.
     417 * @param string $context Context information for the translators.
     418 * @param string $domain  Optional. Text domain. Unique identifier for
     419 *                        retrieving translated strings. Default 'default'.
     420 *
     421 * @return WP_String_Proxy Proxy object representing a string.
     422 */
     423function esc_attr_lx( $text, $context, $domain = 'default' ) {
     424        return new WP_Escape_Attribute_Proxy(
     425                new WP_Contextual_Translation_Proxy( $text, $context, $domain )
     426        );
     427}
     428
     429/**
    329430 * Translate string with gettext context, and escapes it for safe use in HTML output.
    330431 *
    331432 * @since 2.9.0
    function esc_html_x( $text, $context, $domain = 'default' ) { 
    341442}
    342443
    343444/**
     445 * Lazily translate string with gettext context, and escape it for safe use in
     446 * HTML output.
     447 *
     448 * @since 4.9.0
     449 *
     450 * @param string $text    Text to translate.
     451 * @param string $context Context information for the translators.
     452 * @param string $domain  Optional. Text domain. Unique identifier for
     453 *                        retrieving translated strings. Default 'default'.
     454 *
     455 * @return WP_String_Proxy Proxy object representing a string.
     456 */
     457function esc_html_lx( $text, $context, $domain = 'default' ) {
     458        return new WP_Escape_HTML_Proxy(
     459                new WP_Contextual_Translation_Proxy( $text, $context, $domain )
     460        );
     461}
     462
     463/**
    344464 * Translates and retrieves the singular or plural form based on the supplied number.
    345465 *
    346466 * Used when you want to use the appropriate form of a string based on whether a
  • new file src/wp-includes/l10n/class-wp-contextual_translation-proxy.php

    diff --git src/wp-includes/l10n/class-wp-contextual_translation-proxy.php src/wp-includes/l10n/class-wp-contextual_translation-proxy.php
    new file mode 100644
    index 0000000000..832caf4839
    - +  
     1<?php
     2
     3/**
     4 * L10n: WP_Contextual_Translation_Proxy class.
     5 *
     6 * @package    WordPress
     7 * @subpackage L10n
     8 * @since      4.9.0
     9 */
     10final class WP_Contextual_Translation_Proxy extends WP_String_Proxy {
     11
     12        private $text;
     13        private $context;
     14        private $domain;
     15
     16        /**
     17         * Instantiate a WP_Translation_Proxy object.
     18         *
     19         * @since 4.9.0
     20         *
     21         * @param string $text    Text to translate.
     22         * @param string $context Context of the test to translate.
     23         * @param string $domain  Optional. Text domain to use for the translation.
     24         */
     25        public function __construct( $text, $context, $domain = 'default' ) {
     26                $this->text    = $text;
     27                $this->context = $context;
     28                $this->domain  = $domain;
     29        }
     30
     31        /**
     32         * Get the result of evaluating the string proxy object.
     33         *
     34         * @since 4.9.0
     35         *
     36         * @return string
     37         */
     38        protected function get_result() {
     39                return translate_with_gettext_context(
     40                        $this->text,
     41                        $this->context,
     42                        $this->domain
     43                );
     44        }
     45}
  • new file src/wp-includes/l10n/class-wp-escape-attribute-proxy.php

    diff --git src/wp-includes/l10n/class-wp-escape-attribute-proxy.php src/wp-includes/l10n/class-wp-escape-attribute-proxy.php
    new file mode 100644
    index 0000000000..cbcea2ee0b
    - +  
     1<?php
     2
     3/**
     4 * L10n: WP_Escape_Attribute_Proxy class.
     5 *
     6 * @package    WordPress
     7 * @subpackage L10n
     8 * @since      4.9.0
     9 */
     10final class WP_Escape_Attribute_Proxy extends WP_String_Proxy {
     11
     12        private $value;
     13
     14        /**
     15         * Instantiate a WP_Translation_Proxy object.
     16         *
     17         * @since 4.9.0
     18         *
     19         * @param mixed $value Value to be escaped. Needs to be castable to a
     20         *                     string.
     21         */
     22        public function __construct( $value ) {
     23                $this->value = $value;
     24        }
     25
     26        /**
     27         * Get the result of evaluating the string proxy object.
     28         *
     29         * @since 4.9.0
     30         *
     31         * @return string
     32         */
     33        protected function get_result() {
     34                return esc_attr( (string) $this->value );
     35        }
     36}
  • new file src/wp-includes/l10n/class-wp-escape-html-proxy.php

    diff --git src/wp-includes/l10n/class-wp-escape-html-proxy.php src/wp-includes/l10n/class-wp-escape-html-proxy.php
    new file mode 100644
    index 0000000000..97e73999f2
    - +  
     1<?php
     2
     3/**
     4 * L10n: WP_Escape_HTML_Proxy class.
     5 *
     6 * @package    WordPress
     7 * @subpackage L10n
     8 * @since      4.9.0
     9 */
     10final class WP_Escape_HTML_Proxy extends WP_String_Proxy {
     11
     12        private $value;
     13
     14        /**
     15         * Instantiate a WP_Translation_Proxy object.
     16         *
     17         * @since 4.9.0
     18         *
     19         * @param mixed $value Value to be escaped. Needs to be castable to a
     20         *                     string.
     21         */
     22        public function __construct( $value ) {
     23                $this->value = $value;
     24        }
     25
     26        /**
     27         * Get the result of evaluating the string proxy object.
     28         *
     29         * @since 4.9.0
     30         *
     31         * @return string
     32         */
     33        protected function get_result() {
     34                return esc_html( (string) $this->value );
     35        }
     36}
  • new file src/wp-includes/l10n/class-wp-string-proxy.php

    diff --git src/wp-includes/l10n/class-wp-string-proxy.php src/wp-includes/l10n/class-wp-string-proxy.php
    new file mode 100644
    index 0000000000..3795c0b9a7
    - +  
     1<?php
     2
     3/**
     4 * L10n: WP_String_Proxy class.
     5 *
     6 * Implements `JsonSerializable` interface so that it gets converted to a
     7 * translated string on `json_encode()`.
     8 *
     9 * Implements the `ArrayAccess` interface so that you can directly access
     10 * individual characters in the translated string
     11 *
     12 * Uses magic `__get()` to make sure the result is only generated once.
     13 *
     14 * @package    WordPress
     15 * @subpackage L10n
     16 * @since      4.9.0
     17 */
     18abstract class WP_String_Proxy implements JsonSerializable, ArrayAccess {
     19
     20        /**
     21         * Return the string representation of the proxy object.
     22         *
     23         * @since 4.9.0
     24         *
     25         * @return string
     26         */
     27        public function __toString() {
     28                return $this->result;
     29        }
     30
     31        /**
     32         * Return the JSON representation of the proxy object.
     33         *
     34         * @since 4.9.0
     35         *
     36         * @return string
     37         */
     38        public function jsonSerialize() {
     39                return $this->result;
     40        }
     41
     42        /**
     43         * Lazily evaluate the `result` property the first time it is being
     44         * requested.
     45         *
     46         * The property is then set to the resulting value, so that the
     47         * `__get()` magic method will not be called again.
     48         *
     49         * @since 4.9.0
     50         *
     51         * @param string $property Property that was requested.
     52         *
     53         * @return string
     54         */
     55        public function __get( $property ) {
     56                if ( 'result' === $property ) {
     57                        $this->result = $this->get_result();
     58
     59                        return $this->result;
     60                }
     61
     62                $message = sprintf(
     63                        'Undefined property: %s::$%s',
     64                        get_class(),
     65                        $property
     66                );
     67
     68                trigger_error( $message, E_USER_NOTICE );
     69
     70                return null;
     71        }
     72
     73        /**
     74         * Check whether an offset into the array exists.
     75         *
     76         * @since 4.9.0
     77         *
     78         * @param mixed $offset Offset to check for.
     79         *
     80         * @return bool
     81         */
     82        public function offsetExists( $offset ) {
     83                return mb_strlen( $this->result ) > $offset;
     84        }
     85
     86        /**
     87         * Retrieve a specific offset into the array.
     88         *
     89         * @since 4.9.0
     90         *
     91         * @param mixed $offset The offset to retrieve.
     92         *
     93         * @return mixed
     94         */
     95        public function offsetGet( $offset ) {
     96                return $this->result[ $offset ];
     97        }
     98
     99        /**
     100         * Set a specific offset in the array.
     101         *
     102         * @since 4.9.0
     103         *
     104         * @param mixed $offset The offset to assign the value to.
     105         * @param mixed $value  The value to set the offset to.
     106         */
     107        public function offsetSet( $offset, $value ) {
     108                $this->result[ $offset ] = $value;
     109        }
     110
     111        /**
     112         * Unset a specific offset in the array.
     113         *
     114         * @since 4.9.0
     115         *
     116         * @param mixed $offset The offset to unset.
     117         */
     118        public function offsetUnset( $offset ) {
     119                trigger_error( 'Cannot unset string offset', E_USER_ERROR );
     120        }
     121
     122        /**
     123         * Get the result of evaluating the string proxy object.
     124         *
     125         * @since 4.9.0
     126         *
     127         * @return string
     128         */
     129        abstract protected function get_result();
     130}
  • new file src/wp-includes/l10n/class-wp-translation-proxy.php

    diff --git src/wp-includes/l10n/class-wp-translation-proxy.php src/wp-includes/l10n/class-wp-translation-proxy.php
    new file mode 100644
    index 0000000000..988eec097f
    - +  
     1<?php
     2
     3/**
     4 * L10n: WP_Translation_Proxy class.
     5 *
     6 * @package    WordPress
     7 * @subpackage L10n
     8 * @since      4.9.0
     9 */
     10final class WP_Translation_Proxy extends WP_String_Proxy {
     11
     12        private $text;
     13        private $domain;
     14
     15        /**
     16         * Instantiate a WP_Translation_Proxy object.
     17         *
     18         * @since 4.9.0
     19         *
     20         * @param string $text   Text to translate.
     21         * @param string $domain Optional. Text domain to use for the translation.
     22         */
     23        public function __construct( $text, $domain = 'default' ) {
     24                $this->text   = $text;
     25                $this->domain = $domain;
     26        }
     27
     28        /**
     29         * Get the result of evaluating the string proxy object.
     30         *
     31         * @since 4.9.0
     32         *
     33         * @return string
     34         */
     35        protected function get_result() {
     36                return translate( $this->text, $this->domain );
     37        }
     38}
  • src/wp-settings.php

    diff --git src/wp-settings.php src/wp-settings.php
    index d1505c502d..748aa7a99d 100644
    require( ABSPATH . WPINC . '/rest-api/fields/class-wp-rest-comment-meta-fields.p 
    239239require( ABSPATH . WPINC . '/rest-api/fields/class-wp-rest-post-meta-fields.php' );
    240240require( ABSPATH . WPINC . '/rest-api/fields/class-wp-rest-term-meta-fields.php' );
    241241require( ABSPATH . WPINC . '/rest-api/fields/class-wp-rest-user-meta-fields.php' );
     242require( ABSPATH . WPINC . '/l10n/class-wp-string-proxy.php' );
     243require( ABSPATH . WPINC . '/l10n/class-wp-translation-proxy.php' );
     244require( ABSPATH . WPINC . '/l10n/class-wp-contextual_translation-proxy.php' );
     245require( ABSPATH . WPINC . '/l10n/class-wp-escape-attribute-proxy.php' );
     246require( ABSPATH . WPINC . '/l10n/class-wp-escape-html-proxy.php' );
    242247
    243248$GLOBALS['wp_embed'] = new WP_Embed();
    244249
  • tools/i18n/extract.php

    diff --git tools/i18n/extract.php tools/i18n/extract.php
    index 023b28bc69..0449cc7aca 100644
    class StringExtractor { 
    1212        var $rules = array(
    1313                '__' => array( 'string' ),
    1414                '_e' => array( 'string' ),
     15                '_l' => array( 'string' ),
    1516                '_n' => array( 'singular', 'plural' ),
    1617        );
    1718        var $comment_prefix = 'translators:';
  • tools/i18n/makepot.php

    diff --git tools/i18n/makepot.php tools/i18n/makepot.php
    index 75b95e1bd2..0f4d76aca0 100644
    class MakePOT { 
    4444                '__' => array('string'),
    4545                '_e' => array('string'),
    4646                '_c' => array('string'),
     47                '_l' => array('string'),
    4748                '_n' => array('singular', 'plural'),
    4849                '_n_noop' => array('singular', 'plural'),
    4950                '_nc' => array('singular', 'plural'),
    class MakePOT { 
    5152                '__ngettext_noop' => array('singular', 'plural'),
    5253                '_x' => array('string', 'context'),
    5354                '_ex' => array('string', 'context'),
     55                '_lx' => array('string', 'context'),
    5456                '_nx' => array('singular', 'plural', null, 'context'),
    5557                '_nx_noop' => array('singular', 'plural', 'context'),
    5658                '_n_js' => array('singular', 'plural'),
    class MakePOT { 
    5961                'esc_html__' => array('string'),
    6062                'esc_attr_e' => array('string'),
    6163                'esc_html_e' => array('string'),
     64                'esc_attr_l' => array('string'),
     65                'esc_html_l' => array('string'),
    6266                'esc_attr_x' => array('string', 'context'),
    6367                'esc_html_x' => array('string', 'context'),
    6468                'comments_number_link' => array('string', 'singular', 'plural'),