PluginProbe
Polylang / 2.3.2
Polylang v2.3.2
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
polylang / admin / admin-strings.php

admin-strings.php in Polylang 2.3.2, at admin/admin-strings.php

138 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * a fully static class to manage strings translations on admin side
5 *
6 * @since 1.6
7 */
8 class PLL_Admin_Strings {
9 static protected $strings = array(); // strings to translate
10 static protected $default_strings; // default strings to register
11
12 /**
13 * init: add filters
14 *
15 * @since 1.6
16 */
17 static public function init() {
18 // default strings translations sanitization
19 add_filter( 'pll_sanitize_string_translation', array( __CLASS__, 'sanitize_string_translation' ), 10, 2 );
20 }
21
22 /**
23 * register strings for translation making sure it is not duplicate or empty
24 *
25 * @since 0.6
26 *
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, whether the string table should display a multiline textarea or a single line input, defaults to single line
31 */
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';
37 }
38
39 if ( $string && is_scalar( $string ) ) {
40 self::$strings[ md5( $string ) ] = compact( 'name', 'string', 'context', 'multiline' );
41 }
42 }
43
44 /**
45 * get registered strings
46 *
47 * @since 0.6.1
48 *
49 * @return array list of all registered strings
50 */
51 static public function &get_strings() {
52 self::$default_strings = array(
53 'options' => array(
54 'blogname' => __( 'Site Title' ),
55 'blogdescription' => __( 'Tagline' ),
56 'date_format' => __( 'Date Format' ),
57 'time_format' => __( 'Time Format' ),
58 ),
59 'widget_title' => __( 'Widget title', 'polylang' ),
60 'widget_text' => __( 'Widget text', 'polylang' ),
61 );
62
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 global $wp_registered_widgets;
70 $sidebars = wp_get_sidebars_widgets();
71 foreach ( $sidebars as $sidebar => $widgets ) {
72 if ( 'wp_inactive_widgets' == $sidebar || empty( $widgets ) ) {
73 continue;
74 }
75
76 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' ) ) {
80 continue;
81 }
82
83 $widget_settings = $wp_registered_widgets[ $widget ]['callback'][0]->get_settings();
84 $number = $wp_registered_widgets[ $widget ]['params'][0]['number'];
85
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 }
91
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 }
96 }
97 }
98
99 /**
100 * Filter the list of strings registered for translation
101 * Mainly for use by our PLL_WPML_Compat class
102 *
103 * @since 1.0.2
104 *
105 * @param array $strings list of strings
106 */
107 self::$strings = apply_filters( 'pll_get_strings', self::$strings );
108 return self::$strings;
109 }
110
111 /**
112 * performs the sanitization ( before saving in DB ) of default strings translations
113 *
114 * @since 1.6
115 *
116 * @param string $translation translation to sanitize
117 * @param string $name unique name for the string
118 * @return string
119 */
120 static public function sanitize_string_translation( $translation, $name ) {
121 $translation = wp_unslash( trim( $translation ) );
122
123 if ( false !== ( $option = array_search( $name, self::$default_strings['options'], true ) ) ) {
124 $translation = sanitize_option( $option, $translation );
125 }
126
127 if ( $name == self::$default_strings['widget_title'] ) {
128 $translation = strip_tags( $translation );
129 }
130
131 if ( $name == self::$default_strings['widget_text'] && ! current_user_can( 'unfiltered_html' ) ) {
132 $translation = wp_unslash( wp_filter_post_kses( addslashes( $translation ) ) ); // wp_filter_post_kses() expects slashed
133 }
134
135 return $translation;
136 }
137 }
138