| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package Polylang |
| 4 |
*/ |
| 5 |
|
| 6 |
/** |
| 7 |
* A fully static class to manage strings translations on admin side |
| 8 |
* |
| 9 |
* @since 1.6 |
| 10 |
*/ |
| 11 |
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(); |
| 23 |
|
| 24 |
/** |
| 25 |
* The strings to register by default. |
| 26 |
* |
| 27 |
* @var string[]|null |
| 28 |
*/ |
| 29 |
protected static $default_strings; |
| 30 |
|
| 31 |
/** |
| 32 |
* Add filters |
| 33 |
* |
| 34 |
* @since 1.6 |
| 35 |
* |
| 36 |
* @return void |
| 37 |
*/ |
| 38 |
public static function init() { |
| 39 |
// default strings translations sanitization |
| 40 |
add_filter( 'pll_sanitize_string_translation', array( self::class, 'sanitize_string_translation' ), 10, 5 ); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Register strings for translation making sure it is not duplicate or empty |
| 45 |
* |
| 46 |
* @since 0.6 |
| 47 |
* |
| 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 |
| 53 |
*/ |
| 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' ); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Get registered strings |
| 62 |
* |
| 63 |
* @since 0.6.1 |
| 64 |
* |
| 65 |
* @return array list of all registered strings |
| 66 |
*/ |
| 67 |
public static function &get_strings() { |
| 68 |
self::$default_strings = array( |
| 69 |
'widget_title' => __( 'Widget title', 'polylang' ), |
| 70 |
'widget_text' => __( 'Widget text', 'polylang' ), |
| 71 |
); |
| 72 |
|
| 73 |
global $wp_registered_widgets; |
| 74 |
$sidebars = wp_get_sidebars_widgets(); |
| 75 |
foreach ( $sidebars as $sidebar => $widgets ) { |
| 76 |
if ( 'wp_inactive_widgets' == $sidebar || empty( $widgets ) ) { |
| 77 |
continue; |
| 78 |
} |
| 79 |
|
| 80 |
foreach ( $widgets as $widget ) { |
| 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 ) { |
| 83 |
continue; |
| 84 |
} |
| 85 |
|
| 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']; |
| 89 |
|
| 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 |
} |
| 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' ); |
| 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 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Filter the list of strings registered for translation |
| 114 |
* Mainly for use by our PLL_WPML_Compat class |
| 115 |
* |
| 116 |
* @since 1.0.2 |
| 117 |
* |
| 118 |
* @param array $strings list of strings |
| 119 |
*/ |
| 120 |
self::$strings = apply_filters( 'pll_get_strings', self::$strings ); |
| 121 |
return self::$strings; |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Performs the sanitization ( before saving in DB ) of default strings translations |
| 126 |
* |
| 127 |
* @since 1.6 |
| 128 |
* |
| 129 |
* @param string $translation The string translation. |
| 130 |
* @param string $name The name as defined in pll_register_string. Unused. |
| 131 |
* @param string $context The context as defined in pll_register_string. Unused. |
| 132 |
* @param string $original The original string to translate. Unused. |
| 133 |
* @param string $previous The previous string translation. |
| 134 |
* @return string |
| 135 |
*/ |
| 136 |
public static function sanitize_string_translation( $translation, $name, $context, $original, $previous ) { |
| 137 |
if ( trim( $previous ) === trim( $translation ) ) { |
| 138 |
// Don't overwrite the translation to prevent breaking the string. |
| 139 |
return $translation; |
| 140 |
} |
| 141 |
|
| 142 |
if ( $name === self::$default_strings['widget_title'] ) { |
| 143 |
return sanitize_text_field( $translation ); |
| 144 |
} |
| 145 |
|
| 146 |
if ( ! current_user_can( 'unfiltered_html' ) ) { |
| 147 |
$translation = wp_kses_post( $translation ); |
| 148 |
} |
| 149 |
|
| 150 |
return $translation; |
| 151 |
} |
| 152 |
} |
| 153 |
|