Make WordPress Core

Changeset 36538


Ignore:
Timestamp:
02/16/2016 09:14:45 PM (9 years ago)
Author:
ocean90
Message:

i18n: Prevent is_textdomain_loaded() from returning true even if there are no translations for the domain.

In get_translations_for_domain() don't fill the global $l10n with NOOP_Translations instances, return a NOOP_Translations instance instead.

Props nacin, jrf.
Fixes #21319.

Location:
trunk
Files:
2 edited

Legend:

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

    r36290 r36538  
    794794function get_translations_for_domain( $domain ) {
    795795    global $l10n;
    796     if ( !isset( $l10n[$domain] ) ) {
    797         $l10n[$domain] = new NOOP_Translations;
    798     }
    799     return $l10n[$domain];
     796    if ( isset( $l10n[ $domain ] ) ) {
     797        return $l10n[ $domain ];
     798    }
     799
     800    static $noop_translations = null;
     801    if ( null === $noop_translations ) {
     802        $noop_translations = new NOOP_Translations;
     803    }
     804
     805    return $noop_translations;
    800806}
    801807
     
    812818function is_textdomain_loaded( $domain ) {
    813819    global $l10n;
    814     return isset( $l10n[$domain] );
     820    return isset( $l10n[ $domain ] );
    815821}
    816822
  • trunk/tests/phpunit/tests/l10n.php

    r35959 r36538  
    2727    }
    2828
     29    /**
     30     * @ticket 21319
     31     */
     32    function test_is_textdomain_loaded_for_no_translations() {
     33        $this->assertFalse( load_textdomain( 'wp-tests-domain', DIR_TESTDATA . '/non-existent-file' ) );
     34        $this->assertFalse( is_textdomain_loaded( 'wp-tests-domain' ) );
     35        $this->assertInstanceOf( 'NOOP_Translations', get_translations_for_domain( 'wp-tests-domain' ) );
     36        // Ensure that we don't confuse NOOP_Translations to be a loaded text domain.
     37        $this->assertFalse( is_textdomain_loaded( 'wp-tests-domain' ) );
     38        $this->assertFalse( unload_textdomain( 'wp-tests-domain' ) );
     39    }
     40
     41    /**
     42     * @ticket 21319
     43     */
     44    function test_is_textdomain_is_not_loaded_after_gettext_call_with_no_translations() {
     45        $this->assertFalse( is_textdomain_loaded( 'wp-tests-domain' ) );
     46        __( 'just some string', 'wp-tests-domain' );
     47        $this->assertFalse( is_textdomain_loaded( 'wp-tests-domain' ) );
     48    }
    2949}
Note: See TracChangeset for help on using the changeset viewer.