Make WordPress Core

Ticket #32380: fix_wptexturize_locale_switch.2.patch

File fix_wptexturize_locale_switch.2.patch, 3.6 KB (added by ivanblagdan, 10 years ago)

Test and Fix for wptexturize not handling locale switching properly. PROPER

  • tests/phpunit/tests/formatting/WPTexturize.php

    From 3e18341c5c4eda7674579ee54bb678b20ff687ab Mon Sep 17 00:00:00 2001
    From: Ivan Blagdan <ivan.blagdan@gmail.com>
    Date: Wed, 13 May 2015 20:39:07 +0200
    Subject: [PATCH 1/2] test if wptexturize is switching locale properly
    
    ---
     tests/phpunit/tests/formatting/WPTexturize.php | 32 ++++++++++++++++++++++++++
     1 file changed, 32 insertions(+)
    
    diff --git a/tests/phpunit/tests/formatting/WPTexturize.php b/tests/phpunit/tests/formatting/WPTexturize.php
    index 0eb08e2..001e86e 100644
    a b class Tests_Formatting_WPTexturize extends WP_UnitTestCase { 
    18491849                        ),
    18501850                );
    18511851        }
     1852
     1853        /**
     1854         * Test proper wptexturize behaviour between two switched locales
     1855         */
     1856        function test_locale_change_handling() {
     1857                $input = '""'; // Testing against a pair of quotes for wptexturize to handle.
     1858                wptexturize( $input ); // 1 fake run
     1859               
     1860                // Switch to a different locale;
     1861                add_filter( 'locale', create_function('$lang', 'return "fr_FR";' ), 10, 1 );
     1862                // Hook to a gettext call to simulate a switch to .PO localized with guillemets
     1863                add_filter( 'gettext_with_context', array($this, '_filter_curly_double_quotes'), 10, 4 );
     1864
     1865                // Poper entities are translated via a gettext call
     1866                $opening_quote = _x( '&#8220;', 'opening curly double quote' );
     1867                $closing_quote = _x( '&#8221;', 'closing curly double quote' );
     1868
     1869                return $this->assertEquals( $opening_quote . $closing_quote, wptexturize( $input ) );
     1870        }
     1871
     1872        /**
     1873         * Switch smart quote style via gettext.
     1874         */
     1875        function _filter_curly_double_quotes( $translations, $text, $context, $domain ) {
     1876                if ( 'opening curly double quote' == $context && '&#8220;' == $text ) {
     1877                        $translations = '&#0171;';
     1878                } else if ( 'closing curly double quote' == $context && '&#8221;' == $text ) {
     1879                        $translations = '&#0187;';
     1880                }
     1881
     1882                return $translations;
     1883        }
    18521884}
     1885 No newline at end of file
  • src/wp-includes/formatting.php

    -- 
    2.3.2 (Apple Git-55)
    
    
    From de4b711f93b8542bf8aa1cc260f8d5576b1d4a8a Mon Sep 17 00:00:00 2001
    From: Ivan Blagdan <ivan.blagdan@gmail.com>
    Date: Wed, 13 May 2015 20:40:45 +0200
    Subject: [PATCH 2/2] fix wptexturize to reset if the locale changed
    
    ---
     src/wp-includes/formatting.php | 9 ++++++---
     1 file changed, 6 insertions(+), 3 deletions(-)
    
    diff --git a/src/wp-includes/formatting.php b/src/wp-includes/formatting.php
    index eacc260..94a702d 100644
    a b  
    3030function wptexturize($text, $reset = false) {
    3131        global $wp_cockneyreplace, $shortcode_tags;
    3232        static $static_characters, $static_replacements, $dynamic_characters, $dynamic_replacements,
    33                 $default_no_texturize_tags, $default_no_texturize_shortcodes, $run_texturize = true;
     33                $default_no_texturize_tags, $default_no_texturize_shortcodes, $run_texturize = true, $locale;
    3434
    3535        // If there's nothing to do, just stop.
    3636        if ( empty( $text ) || false === $run_texturize ) {
    3737                return $text;
    3838        }
    3939
    40         // Set up static variables. Run once only.
    41         if ( $reset || ! isset( $static_characters ) ) {
     40        // Set up static variables. Run once only, unless the locale has been changed.
     41        if ( $reset || ! isset( $static_characters ) || $locale != get_locale() ) {
    4242                /**
    4343                 * Filter whether to skip running wptexturize().
    4444                 *
    function wptexturize($text, $reset = false) { 
    5858                        return $text;
    5959                }
    6060
     61                // Get the current locale and store it on the first run.
     62                $locale = get_locale();
     63
    6164                /* translators: opening curly double quote */
    6265                $opening_quote = _x( '&#8220;', 'opening curly double quote' );
    6366                /* translators: closing curly double quote */