| 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 |
protected static $strings = array(); // strings to translate |
| 13 |
protected static $default_strings; // default strings to register |
| 14 |
|
| 15 |
/** |
| 16 |
* Add filters |
| 17 |
* |
| 18 |
* @since 1.6 |
| 19 |
*/ |
| 20 |
public static function init() { |
| 21 |
// default strings translations sanitization |
| 22 |
add_filter( 'pll_sanitize_string_translation', array( __CLASS__, 'sanitize_string_translation' ), 10, 2 ); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Register strings for translation making sure it is not duplicate or empty |
| 27 |
* |
| 28 |
* @since 0.6 |
| 29 |
* |
| 30 |
* @param string $name A unique name for the string |
| 31 |
* @param string $string The string to register |
| 32 |
* @param string $context Optional, the group in which the string is registered, defaults to 'polylang' |
| 33 |
* @param bool $multiline Optional, whether the string table should display a multiline textarea or a single line input, defaults to single line |
| 34 |
*/ |
| 35 |
public static function register_string( $name, $string, $context = 'Polylang', $multiline = false ) { |
| 36 |
// Backward compatibility with Polylang older than 1.1 |
| 37 |
if ( is_bool( $context ) ) { |
| 38 |
$multiline = $context; |
| 39 |
$context = 'Polylang'; |
| 40 |
} |
| 41 |
|
| 42 |
if ( $string && is_scalar( $string ) ) { |
| 43 |
self::$strings[ md5( $string ) ] = compact( 'name', 'string', 'context', 'multiline' ); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Get registered strings |
| 49 |
* |
| 50 |
* @since 0.6.1 |
| 51 |
* |
| 52 |
* @return array list of all registered strings |
| 53 |
*/ |
| 54 |
public static function &get_strings() { |
| 55 |
self::$default_strings = array( |
| 56 |
'options' => array( |
| 57 |
'blogname' => __( 'Site Title', 'polylang' ), |
| 58 |
'blogdescription' => __( 'Tagline', 'polylang' ), |
| 59 |
'date_format' => __( 'Date Format', 'polylang' ), |
| 60 |
'time_format' => __( 'Time Format', 'polylang' ), |
| 61 |
), |
| 62 |
'widget_title' => __( 'Widget title', 'polylang' ), |
| 63 |
'widget_text' => __( 'Widget text', 'polylang' ), |
| 64 |
); |
| 65 |
|
| 66 |
// WP strings |
| 67 |
foreach ( self::$default_strings['options'] as $option => $string ) { |
| 68 |
self::register_string( $string, get_option( $option ), 'WordPress' ); |
| 69 |
} |
| 70 |
|
| 71 |
// Widgets titles |
| 72 |
global $wp_registered_widgets; |
| 73 |
$sidebars = wp_get_sidebars_widgets(); |
| 74 |
foreach ( $sidebars as $sidebar => $widgets ) { |
| 75 |
if ( 'wp_inactive_widgets' == $sidebar || empty( $widgets ) ) { |
| 76 |
continue; |
| 77 |
} |
| 78 |
|
| 79 |
foreach ( $widgets as $widget ) { |
| 80 |
// Nothing can be done if the widget is created using pre WP2.8 API :( |
| 81 |
// There is no object, so we can't access it to get the widget options |
| 82 |
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' ) ) { |
| 83 |
continue; |
| 84 |
} |
| 85 |
|
| 86 |
$widget_settings = $wp_registered_widgets[ $widget ]['callback'][0]->get_settings(); |
| 87 |
$number = $wp_registered_widgets[ $widget ]['params'][0]['number']; |
| 88 |
|
| 89 |
// Don't enable widget translation if the widget is visible in only one language or if there is no title |
| 90 |
if ( empty( $widget_settings[ $number ]['pll_lang'] ) ) { |
| 91 |
if ( isset( $widget_settings[ $number ]['title'] ) && $title = $widget_settings[ $number ]['title'] ) { |
| 92 |
self::register_string( self::$default_strings['widget_title'], $title, 'Widget' ); |
| 93 |
} |
| 94 |
|
| 95 |
if ( isset( $widget_settings[ $number ]['text'] ) && $text = $widget_settings[ $number ]['text'] ) { |
| 96 |
self::register_string( self::$default_strings['widget_text'], $text, 'Widget', true ); |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Filter the list of strings registered for translation |
| 104 |
* Mainly for use by our PLL_WPML_Compat class |
| 105 |
* |
| 106 |
* @since 1.0.2 |
| 107 |
* |
| 108 |
* @param array $strings list of strings |
| 109 |
*/ |
| 110 |
self::$strings = apply_filters( 'pll_get_strings', self::$strings ); |
| 111 |
return self::$strings; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Performs the sanitization ( before saving in DB ) of default strings translations |
| 116 |
* |
| 117 |
* @since 1.6 |
| 118 |
* |
| 119 |
* @param string $translation translation to sanitize |
| 120 |
* @param string $name unique name for the string |
| 121 |
* @return string |
| 122 |
*/ |
| 123 |
public static function sanitize_string_translation( $translation, $name ) { |
| 124 |
|
| 125 |
if ( false !== ( $option = array_search( $name, self::$default_strings['options'], true ) ) ) { |
| 126 |
$translation = sanitize_option( $option, $translation ); |
| 127 |
} |
| 128 |
|
| 129 |
if ( $name == self::$default_strings['widget_title'] ) { |
| 130 |
$translation = sanitize_text_field( $translation ); |
| 131 |
} |
| 132 |
|
| 133 |
if ( $name == self::$default_strings['widget_text'] && ! current_user_can( 'unfiltered_html' ) ) { |
| 134 |
$translation = wp_kses_post( $translation ); |
| 135 |
} |
| 136 |
|
| 137 |
return $translation; |
| 138 |
} |
| 139 |
} |
| 140 |
|