| @@ -1,8 +1,5 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * @package Polylang | |
| 4 | - */ | |
| 5 | 2 | |
| 6 | 3 | /** |
| 7 | 4 | * A fully static class to manage strings translations on admin side |
| 8 | 5 | * |
| @@ -8,33 +5,15 @@ | ||
| 8 | 5 | * |
| 9 | 6 | * @since 1.6 |
| 10 | 7 | */ |
| 11 | 8 | class PLL_Admin_Strings { |
| 12 | - /** | |
| 13 | - * Stores the strings to translate. | |
| 14 | - * | |
| 15 | - * @var array { | |
| 16 | - * @type string $name A unique name for the string. | |
| 17 | - * @type string $string The actual string to translate. | |
| 18 | - * @type string $context The group in which the string is registered. | |
| 19 | - * @type bool $multiline Whether the string table should display a multiline textarea or a single line input. | |
| 20 | - * } | |
| 21 | - */ | |
| 22 | - protected static $strings = array(); | |
| 9 | + protected static $strings = array(); // strings to translate | |
| 10 | + protected static $default_strings; // default strings to register | |
| 23 | 11 | |
| 24 | 12 | /** |
| 25 | - * The strings to register by default. | |
| 26 | - * | |
| 27 | - * @var string[] | |
| 28 | - */ | |
| 29 | - protected static $default_strings; | |
| 30 | - | |
| 31 | - /** | |
| 32 | 13 | * Add filters |
| 33 | 14 | * |
| 34 | 15 | * @since 1.6 |
| 35 | - * | |
| 36 | - * @return void | |
| 37 | 16 | */ |
| 38 | 17 | public static function init() { |
| 39 | 18 | // default strings translations sanitization |
| 40 | 19 | add_filter( 'pll_sanitize_string_translation', array( __CLASS__, 'sanitize_string_translation' ), 10, 2 ); |
| @@ -48,11 +27,16 @@ | ||
| 48 | 27 | * @param string $name A unique name for the string |
| 49 | 28 | * @param string $string The string to register |
| 50 | 29 | * @param string $context Optional, the group in which the string is registered, defaults to 'polylang' |
| 51 | 30 | * @param bool $multiline Optional, whether the string table should display a multiline textarea or a single line input, defaults to single line |
| 52 | - * @return void | |
| 53 | 31 | */ |
| 54 | 32 | public static function register_string( $name, $string, $context = 'Polylang', $multiline = false ) { |
| 33 | + // Backward compatibility with Polylang older than 1.1 | |
| 34 | + if ( is_bool( $context ) ) { | |
| 35 | + $multiline = $context; | |
| 36 | + $context = 'Polylang'; | |
| 37 | + } | |
| 38 | + | |
| 55 | 39 | if ( $string && is_scalar( $string ) ) { |
| 56 | 40 | self::$strings[ md5( $string ) ] = compact( 'name', 'string', 'context', 'multiline' ); |
| 57 | 41 | } |
| 58 | 42 | } |
| @@ -65,12 +49,23 @@ | ||
| 65 | 49 | * @return array list of all registered strings |
| 66 | 50 | */ |
| 67 | 51 | public static function &get_strings() { |
| 68 | 52 | self::$default_strings = array( |
| 53 | + 'options' => array( | |
| 54 | + 'blogname' => __( 'Site Title', 'polylang' ), | |
| 55 | + 'blogdescription' => __( 'Tagline', 'polylang' ), | |
| 56 | + 'date_format' => __( 'Date Format', 'polylang' ), | |
| 57 | + 'time_format' => __( 'Time Format', 'polylang' ), | |
| 58 | + ), | |
| 69 | 59 | 'widget_title' => __( 'Widget title', 'polylang' ), |
| 70 | 60 | 'widget_text' => __( 'Widget text', 'polylang' ), |
| 71 | 61 | ); |
| 72 | 62 | |
| 63 | + // WP strings | |
| 64 | + foreach ( self::$default_strings['options'] as $option => $string ) { | |
| 65 | + self::register_string( $string, get_option( $option ), 'WordPress' ); | |
| 66 | + } | |
| 67 | + | |
| 73 | 68 | // Widgets titles |
| 74 | 69 | global $wp_registered_widgets; |
| 75 | 70 | $sidebars = wp_get_sidebars_widgets(); |
| 76 | 71 | foreach ( $sidebars as $sidebar => $widgets ) { |
| @@ -122,8 +117,13 @@ | ||
| 122 | 117 | * @param string $name unique name for the string |
| 123 | 118 | * @return string |
| 124 | 119 | */ |
| 125 | 120 | public static function sanitize_string_translation( $translation, $name ) { |
| 121 | + | |
| 122 | + if ( false !== ( $option = array_search( $name, self::$default_strings['options'], true ) ) ) { | |
| 123 | + $translation = sanitize_option( $option, $translation ); | |
| 124 | + } | |
| 125 | + | |
| 126 | 126 | if ( $name == self::$default_strings['widget_title'] ) { |
| 127 | 127 | $translation = sanitize_text_field( $translation ); |
| 128 | 128 | } |
| 129 | 129 | |