| 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 wether 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 |
$to_register = compact( 'name', 'string', 'context', 'multiline' ); |
| 40 |
if ( ! in_array( $to_register, self::$strings ) && $to_register['string'] ) { |
| 41 |
self::$strings[] = $to_register; |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
/* |
| 46 |
* get registered strings |
| 47 |
* |
| 48 |
* @since 0.6.1 |
| 49 |
* |
| 50 |
* @return array list of all registered strings |
| 51 |
*/ |
| 52 |
static public function &get_strings() { |
| 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 |
), |
| 60 |
'widget_title' => __( 'Widget title', 'polylang' ), |
| 61 |
'widget_text' => __( 'Widget text', 'polylang' ), |
| 62 |
); |
| 63 |
|
| 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 |
| 70 |
global $wp_registered_widgets; |
| 71 |
$sidebars = wp_get_sidebars_widgets(); |
| 72 |
foreach ( $sidebars as $sidebar => $widgets ) { |
| 73 |
if ( 'wp_inactive_widgets' == $sidebar || empty( $widgets ) ) { |
| 74 |
continue; |
| 75 |
} |
| 76 |
|
| 77 |
foreach ( $widgets as $widget ) { |
| 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 |
| 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' ) ) { |
| 81 |
continue; |
| 82 |
} |
| 83 |
|
| 84 |
$widget_settings = $wp_registered_widgets[ $widget ]['callback'][0]->get_settings(); |
| 85 |
$number = $wp_registered_widgets[ $widget ]['params'][0]['number']; |
| 86 |
|
| 87 |
// don't enable widget translation if the widget is visible in only one language or if there is no title |
| 88 |
if ( empty( $widget_settings[ $number ]['pll_lang'] ) ) { |
| 89 |
if ( isset( $widget_settings[ $number ]['title'] ) && $title = $widget_settings[ $number ]['title'] ) { |
| 90 |
self::register_string( self::$default_strings['widget_title'], $title, 'Widget' ); |
| 91 |
} |
| 92 |
|
| 93 |
if ( isset( $widget_settings[ $number ]['text'] ) && $text = $widget_settings[ $number ]['text'] ) { |
| 94 |
self::register_string( self::$default_strings['widget_text'], $text, 'Widget', true ); |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
// allow plugins to modify our list of strings, mainly for use by our PLL_WPML_Compat class |
| 101 |
self::$strings = apply_filters( 'pll_get_strings', self::$strings ); |
| 102 |
return self::$strings; |
| 103 |
} |
| 104 |
|
| 105 |
/* |
| 106 |
* performs the sanitization ( before saving in DB ) of default strings translations |
| 107 |
* |
| 108 |
* @since 1.6 |
| 109 |
* |
| 110 |
* @param string $translation, translation to sanitize |
| 111 |
* @param string $name unique name for the string |
| 112 |
* @return string |
| 113 |
*/ |
| 114 |
static public function sanitize_string_translation( $translation, $name ) { |
| 115 |
$translation = wp_unslash( trim( $translation ) ); |
| 116 |
|
| 117 |
if ( false !== ( $option = array_search( $name, self::$default_strings['options'], true ) ) ) { |
| 118 |
$translation = sanitize_option( $option, $translation ); |
| 119 |
} |
| 120 |
|
| 121 |
if ( $name == self::$default_strings['widget_title'] ) { |
| 122 |
$translation = strip_tags( $translation ); |
| 123 |
} |
| 124 |
|
| 125 |
if ( $name == self::$default_strings['widget_text'] && !current_user_can( 'unfiltered_html' ) ) { |
| 126 |
$translation = wp_unslash( wp_filter_post_kses( addslashes( $translation ) ) ); // wp_filter_post_kses() expects slashed |
| 127 |
} |
| 128 |
|
| 129 |
return $translation; |
| 130 |
} |
| 131 |
} |
| 132 |
|