PluginProbe
Polylang / 3.7.1
Polylang v3.7.1
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 -39 2.7.23.7.1 View file →
@@ -1,5 +1,8 @@
1 1 <?php
2 +/**
3 + * @package Polylang
4 + */
2 5
3 6 /**
4 7 * A fully static class to manage strings translations on admin side
5 8 *
@@ -5,19 +8,37 @@
5 8 *
6 9 * @since 1.6
7 10 */
8 11 class PLL_Admin_Strings {
9 - protected static $strings = array(); // strings to translate
10 - protected static $default_strings; // default strings to register
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();
11 23
12 24 /**
25 + * The strings to register by default.
26 + *
27 + * @var string[]|null
28 + */
29 + protected static $default_strings;
30 +
31 + /**
13 32 * Add filters
14 33 *
15 34 * @since 1.6
35 + *
36 + * @return void
16 37 */
17 38 public static function init() {
18 39 // default strings translations sanitization
19 - add_filter( 'pll_sanitize_string_translation', array( __CLASS__, 'sanitize_string_translation' ), 10, 2 );
40 + add_filter( 'pll_sanitize_string_translation', array( self::class, 'sanitize_string_translation' ), 10, 2 );
20 41 }
21 42
22 43 /**
23 44 * Register strings for translation making sure it is not duplicate or empty
@@ -27,16 +48,11 @@
27 48 * @param string $name A unique name for the string
28 49 * @param string $string The string to register
29 50 * @param string $context Optional, the group in which the string is registered, defaults to 'polylang'
30 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
31 53 */
32 54 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 -
39 55 if ( $string && is_scalar( $string ) ) {
40 56 self::$strings[ md5( $string ) ] = compact( 'name', 'string', 'context', 'multiline' );
41 57 }
42 58 }
@@ -49,24 +65,12 @@
49 65 * @return array list of all registered strings
50 66 */
51 67 public static function &get_strings() {
52 68 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 - ),
59 69 'widget_title' => __( 'Widget title', 'polylang' ),
60 70 'widget_text' => __( 'Widget text', 'polylang' ),
61 71 );
62 72
63 - // WP strings
64 - foreach ( self::$default_strings['options'] as $option => $string ) {
65 - self::register_string( $string, get_option( $option ), 'WordPress' );
66 - }
67 -
68 - // Widgets titles
69 73 global $wp_registered_widgets;
70 74 $sidebars = wp_get_sidebars_widgets();
71 75 foreach ( $sidebars as $sidebar => $widgets ) {
72 76 if ( 'wp_inactive_widgets' == $sidebar || empty( $widgets ) ) {
@@ -73,27 +77,36 @@
73 77 continue;
74 78 }
75 79
76 80 foreach ( $widgets as $widget ) {
77 - // Nothing can be done if the widget is created using pre WP2.8 API :(
78 - // There is no object, so we can't access it to get the widget options
79 - 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' ) ) {
81 + // Nothing can be done if the widget is created using pre WP2.8 API. There is no object, so we can't access it to get the widget options.
82 + if ( ! isset( $wp_registered_widgets[ $widget ]['callback'][0] ) || ! $wp_registered_widgets[ $widget ]['callback'][0] instanceof WP_Widget ) {
80 83 continue;
81 84 }
82 85
83 - $widget_settings = $wp_registered_widgets[ $widget ]['callback'][0]->get_settings();
84 - $number = $wp_registered_widgets[ $widget ]['params'][0]['number'];
86 + $widget_instance = $wp_registered_widgets[ $widget ]['callback'][0];
87 + $widget_settings = $widget_instance->get_settings();
88 + $number = $wp_registered_widgets[ $widget ]['params'][0]['number'];
85 89
86 - // Don't enable widget translation if the widget is visible in only one language or if there is no title
87 - if ( empty( $widget_settings[ $number ]['pll_lang'] ) ) {
88 - if ( isset( $widget_settings[ $number ]['title'] ) && $title = $widget_settings[ $number ]['title'] ) {
89 - self::register_string( self::$default_strings['widget_title'], $title, 'Widget' );
90 - }
90 + // Don't enable widget translation if the widget is visible in only one language or if there is no title.
91 + if ( ! empty( $widget_settings[ $number ]['pll_lang'] ) ) {
92 + continue;
93 + }
91 94
92 - if ( isset( $widget_settings[ $number ]['text'] ) && $text = $widget_settings[ $number ]['text'] ) {
93 - self::register_string( self::$default_strings['widget_text'], $text, 'Widget', true );
94 - }
95 + // Widget title.
96 + if ( ! empty( $widget_settings[ $number ]['title'] ) ) {
97 + self::register_string( self::$default_strings['widget_title'], $widget_settings[ $number ]['title'], 'Widget' );
95 98 }
99 +
100 + // Text of the Widget text.
101 + if ( ! empty( $widget_settings[ $number ]['text'] ) ) {
102 + self::register_string( self::$default_strings['widget_text'], $widget_settings[ $number ]['text'], 'Widget', true );
103 + }
104 +
105 + // Content of the widget custom html.
106 + if ( $widget_instance instanceof WP_Widget_Custom_HTML && ! empty( $widget_settings[ $number ]['content'] ) ) {
107 + self::register_string( self::$default_strings['widget_text'], $widget_settings[ $number ]['content'], 'Widget', true );
108 + }
96 109 }
97 110 }
98 111
99 112 /**
@@ -117,13 +130,8 @@
117 130 * @param string $name unique name for the string
118 131 * @return string
119 132 */
120 133 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 134 if ( $name == self::$default_strings['widget_title'] ) {
127 135 $translation = sanitize_text_field( $translation );
128 136 }
129 137