PLL_Admin_Strings

A fully static class to manage strings translations on admin side.

Defined (1)

The class is defined in the following location(s).

/admin/admin-strings.php  
  1. class PLL_Admin_Strings { 
  2. static protected $strings = array(); // strings to translate 
  3. static protected $default_strings; // default strings to register 
  4.  
  5. /** 
  6. * init: add filters 
  7. * @since 1.6 
  8. */ 
  9. static public function init() { 
  10. // default strings translations sanitization 
  11. add_filter( 'pll_sanitize_string_translation', array( __CLASS__, 'sanitize_string_translation' ), 10, 2 ); 
  12.  
  13. /** 
  14. * register strings for translation making sure it is not duplicate or empty 
  15. * @since 0.6 
  16. * @param string $name a unique name for the string 
  17. * @param string $string the string to register 
  18. * @param string $context optional the group in which the string is registered, defaults to 'polylang' 
  19. * @param bool $multiline optional wether the string table should display a multiline textarea or a single line input, defaults to single line 
  20. */ 
  21. static public function register_string( $name, $string, $context = 'Polylang', $multiline = false ) { 
  22. // backward compatibility with Polylang older than 1.1 
  23. if ( is_bool( $context ) ) { 
  24. $multiline = $context; 
  25. $context = 'Polylang'; 
  26.  
  27. if ( $string && is_scalar( $string ) ) { 
  28. self::$strings[ md5( $string ) ] = compact( 'name', 'string', 'context', 'multiline' ); 
  29.  
  30. /** 
  31. * get registered strings 
  32. * @since 0.6.1 
  33. * @return array list of all registered strings 
  34. */ 
  35. static public function &get_strings() { 
  36. self::$default_strings = array( 
  37. 'options' => array( 
  38. 'blogname' => __( 'Site Title' ),  
  39. 'blogdescription' => __( 'Tagline' ),  
  40. 'date_format' => __( 'Date Format' ),  
  41. 'time_format' => __( 'Time Format' ),  
  42. ),  
  43. 'widget_title' => __( 'Widget title', 'polylang' ),  
  44. 'widget_text' => __( 'Widget text', 'polylang' ),  
  45. ); 
  46.  
  47. // WP strings 
  48. foreach ( self::$default_strings['options'] as $option => $string ) { 
  49. self::register_string( $string, get_option( $option ), 'WordPress' ); 
  50.  
  51. // widgets titles 
  52. global $wp_registered_widgets; 
  53. $sidebars = wp_get_sidebars_widgets(); 
  54. foreach ( $sidebars as $sidebar => $widgets ) { 
  55. if ( 'wp_inactive_widgets' == $sidebar || empty( $widgets ) ) { 
  56. continue; 
  57.  
  58. foreach ( $widgets as $widget ) { 
  59. // nothing can be done if the widget is created using pre WP2.8 API :( 
  60. // there is no object, so we can't access it to get the widget options 
  61. 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' ) ) { 
  62. continue; 
  63.  
  64. $widget_settings = $wp_registered_widgets[ $widget ]['callback'][0]->get_settings(); 
  65. $number = $wp_registered_widgets[ $widget ]['params'][0]['number']; 
  66.  
  67. // don't enable widget translation if the widget is visible in only one language or if there is no title 
  68. if ( empty( $widget_settings[ $number ]['pll_lang'] ) ) { 
  69. if ( isset( $widget_settings[ $number ]['title'] ) && $title = $widget_settings[ $number ]['title'] ) { 
  70. self::register_string( self::$default_strings['widget_title'], $title, 'Widget' ); 
  71.  
  72. if ( isset( $widget_settings[ $number ]['text'] ) && $text = $widget_settings[ $number ]['text'] ) { 
  73. self::register_string( self::$default_strings['widget_text'], $text, 'Widget', true ); 
  74.  
  75. /** 
  76. * Filter the list of strings registered for translation 
  77. * Mainly for use by our PLL_WPML_Compat class 
  78. * @since 1.0.2 
  79. * @param array $strings list of strings 
  80. */ 
  81. self::$strings = apply_filters( 'pll_get_strings', self::$strings ); 
  82. return self::$strings; 
  83.  
  84. /** 
  85. * performs the sanitization ( before saving in DB ) of default strings translations 
  86. * @since 1.6 
  87. * @param string $translation translation to sanitize 
  88. * @param string $name unique name for the string 
  89. * @return string 
  90. */ 
  91. static public function sanitize_string_translation( $translation, $name ) { 
  92. $translation = wp_unslash( trim( $translation ) ); 
  93.  
  94. if ( false !== ( $option = array_search( $name, self::$default_strings['options'], true ) ) ) { 
  95. $translation = sanitize_option( $option, $translation ); 
  96.  
  97. if ( $name == self::$default_strings['widget_title'] ) { 
  98. $translation = strip_tags( $translation ); 
  99.  
  100. if ( $name == self::$default_strings['widget_text'] && ! current_user_can( 'unfiltered_html' ) ) { 
  101. $translation = wp_unslash( wp_filter_post_kses( addslashes( $translation ) ) ); // wp_filter_post_kses() expects slashed 
  102.  
  103. return $translation;