PluginProbe
Polylang / 2.0.3
Polylang v2.0.3
3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 3.0.3 All 233 releases
← All changes | admin/admin-strings.php +47 -45 3.02.0.3 View file →
@@ -1,77 +1,73 @@
1 1 <?php
2 -/**
3 - * @package Polylang
4 - */
5 2
6 3 /**
7 - * A fully static class to manage strings translations on admin side
4 + * a fully static class to manage strings translations on admin side
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 + static protected $strings = array(); // strings to translate
10 + static protected $default_strings; // default strings to register
23 11
24 12 /**
25 - * The strings to register by default.
13 + * init: add filters
26 14 *
27 - * @var string[]
28 - */
29 - protected static $default_strings;
30 -
31 - /**
32 - * Add filters
33 - *
34 15 * @since 1.6
35 - *
36 - * @return void
37 16 */
38 - public static function init() {
17 + static public function init() {
39 18 // default strings translations sanitization
40 19 add_filter( 'pll_sanitize_string_translation', array( __CLASS__, 'sanitize_string_translation' ), 10, 2 );
41 20 }
42 21
43 22 /**
44 - * Register strings for translation making sure it is not duplicate or empty
23 + * register strings for translation making sure it is not duplicate or empty
45 24 *
46 25 * @since 0.6
47 26 *
48 - * @param string $name A unique name for the string
49 - * @param string $string The string to register
50 - * @param string $context Optional, the group in which the string is registered, defaults to 'polylang'
51 - * @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
27 + * @param string $name a unique name for the string
28 + * @param string $string the string to register
29 + * @param string $context optional the group in which the string is registered, defaults to 'polylang'
30 + * @param bool $multiline optional wether the string table should display a multiline textarea or a single line input, defaults to single line
53 31 */
54 - public static function register_string( $name, $string, $context = 'Polylang', $multiline = false ) {
55 - if ( $string && is_scalar( $string ) ) {
56 - self::$strings[ md5( $string ) ] = compact( 'name', 'string', 'context', 'multiline' );
32 + static public 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';
57 37 }
38 +
39 + $to_register = compact( 'name', 'string', 'context', 'multiline' );
40 + if ( ! in_array( $to_register, self::$strings ) && $to_register['string'] ) {
41 + self::$strings[] = $to_register;
42 + }
58 43 }
59 44
60 45 /**
61 - * Get registered strings
46 + * get registered strings
62 47 *
63 48 * @since 0.6.1
64 49 *
65 50 * @return array list of all registered strings
66 51 */
67 - public static function &get_strings() {
52 + static public function &get_strings() {
68 53 self::$default_strings = array(
54 + 'options' => array(
55 + 'blogname' => __( 'Site Title' ),
56 + 'blogdescription' => __( 'Tagline' ),
57 + 'date_format' => __( 'Date Format' ),
58 + 'time_format' => __( 'Time Format' ),
59 + ),
69 60 'widget_title' => __( 'Widget title', 'polylang' ),
70 61 'widget_text' => __( 'Widget text', 'polylang' ),
71 62 );
72 63
73 - // Widgets titles
64 + // WP strings
65 + foreach ( self::$default_strings['options'] as $option => $string ) {
66 + self::register_string( $string, get_option( $option ), 'WordPress' );
67 + }
68 +
69 + // widgets titles
74 70 global $wp_registered_widgets;
75 71 $sidebars = wp_get_sidebars_widgets();
76 72 foreach ( $sidebars as $sidebar => $widgets ) {
77 73 if ( 'wp_inactive_widgets' == $sidebar || empty( $widgets ) ) {
@@ -78,10 +74,10 @@
78 74 continue;
79 75 }
80 76
81 77 foreach ( $widgets as $widget ) {
82 - // Nothing can be done if the widget is created using pre WP2.8 API :(
83 - // There is no object, so we can't access it to get the widget options
78 + // nothing can be done if the widget is created using pre WP2.8 API :(
79 + // there is no object, so we can't access it to get the widget options
84 80 if ( ! isset( $wp_registered_widgets[ $widget ]['callback'][0] ) || ! is_object( $wp_registered_widgets[ $widget ]['callback'][0] ) || ! method_exists( $wp_registered_widgets[ $widget ]['callback'][0], 'get_settings' ) ) {
85 81 continue;
86 82 }
87 83
@@ -87,9 +83,9 @@
87 83
88 84 $widget_settings = $wp_registered_widgets[ $widget ]['callback'][0]->get_settings();
89 85 $number = $wp_registered_widgets[ $widget ]['params'][0]['number'];
90 86
91 - // Don't enable widget translation if the widget is visible in only one language or if there is no title
87 + // don't enable widget translation if the widget is visible in only one language or if there is no title
92 88 if ( empty( $widget_settings[ $number ]['pll_lang'] ) ) {
93 89 if ( isset( $widget_settings[ $number ]['title'] ) && $title = $widget_settings[ $number ]['title'] ) {
94 90 self::register_string( self::$default_strings['widget_title'], $title, 'Widget' );
95 91 }
@@ -113,9 +109,9 @@
113 109 return self::$strings;
114 110 }
115 111
116 112 /**
117 - * Performs the sanitization ( before saving in DB ) of default strings translations
113 + * performs the sanitization ( before saving in DB ) of default strings translations
118 114 *
119 115 * @since 1.6
120 116 *
121 117 * @param string $translation translation to sanitize
@@ -121,15 +117,21 @@
121 117 * @param string $translation translation to sanitize
122 118 * @param string $name unique name for the string
123 119 * @return string
124 120 */
125 - public static function sanitize_string_translation( $translation, $name ) {
121 + static public function sanitize_string_translation( $translation, $name ) {
122 + $translation = wp_unslash( trim( $translation ) );
123 +
124 + if ( false !== ( $option = array_search( $name, self::$default_strings['options'], true ) ) ) {
125 + $translation = sanitize_option( $option, $translation );
126 + }
127 +
126 128 if ( $name == self::$default_strings['widget_title'] ) {
127 - $translation = sanitize_text_field( $translation );
129 + $translation = strip_tags( $translation );
128 130 }
129 131
130 132 if ( $name == self::$default_strings['widget_text'] && ! current_user_can( 'unfiltered_html' ) ) {
131 - $translation = wp_kses_post( $translation );
133 + $translation = wp_unslash( wp_filter_post_kses( addslashes( $translation ) ) ); // wp_filter_post_kses() expects slashed
132 134 }
133 135
134 136 return $translation;
135 137 }