PLL_Admin_Filters

Setup miscellaneous admin filters as well as filters common to admin and frontend.

Defined (1)

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

/admin/admin-filters.php  
  1. class PLL_Admin_Filters extends PLL_Filters { 
  2.  
  3. /** 
  4. * Constructor: setups filters and actions 
  5. * @since 1.2 
  6. * @param object $polylang 
  7. */ 
  8. public function __construct( &$polylang ) { 
  9. parent::__construct( $polylang ); 
  10.  
  11. // Widgets languages filter 
  12. add_action( 'in_widget_form', array( $this, 'in_widget_form' ), 10, 3 ); 
  13. add_filter( 'widget_update_callback', array( $this, 'widget_update_callback' ), 10, 4 ); 
  14.  
  15. // Language management for users 
  16. add_action( 'personal_options_update', array( $this, 'personal_options_update' ) ); 
  17. add_action( 'edit_user_profile_update', array( $this, 'personal_options_update' ) ); 
  18. add_action( 'personal_options', array( $this, 'personal_options' ) ); 
  19.  
  20. // Ugrades languages files after a core upgrade ( timing is important ) 
  21. // Backward compatibility WP < 4.0 *AND* Polylang < 1.6 
  22. add_action( '_core_updated_successfully', array( $this, 'upgrade_languages' ), 1 ); // since WP 3.3 
  23.  
  24. // Upgrades plugins and themes translations files 
  25. add_filter( 'themes_update_check_locales', array( $this, 'update_check_locales' ) ); 
  26. add_filter( 'plugins_update_check_locales', array( $this, 'update_check_locales' ) ); 
  27.  
  28. // We need specific filters for German and Danish 
  29. $specific_locales = array( 'da_DK', 'de_DE', 'de_DE_formal', 'de_CH', 'de_CH_informal', 'ca', 'sr_RS', 'bs_BA' ); 
  30. if ( array_intersect( $this->model->get_languages_list( array( 'fields' => 'locale' ) ), $specific_locales ) ) { 
  31. add_filter( 'sanitize_title', array( $this, 'sanitize_title' ), 10, 3 ); 
  32. add_filter( 'sanitize_user', array( $this, 'sanitize_user' ), 10, 3 ); 
  33.  
  34. /** 
  35. * Modifies the widgets forms to add our language dropdwown list 
  36. * @since 0.3 
  37. * @param object $widget 
  38. */ 
  39. public function in_widget_form( $widget, $return, $instance ) { 
  40. $dropdown = new PLL_Walker_Dropdown(); 
  41. printf( '<p><label for="%1$s">%2$s %3$s</label></p>',  
  42. esc_attr( $widget->id.'_lang_choice' ),  
  43. esc_html__( 'The widget is displayed for:', 'polylang' ),  
  44. $dropdown->walk( 
  45. array_merge( 
  46. array( (object) array( 'slug' => 0, 'name' => __( 'All languages', 'polylang' ) ) ),  
  47. $this->model->get_languages_list() 
  48. ),  
  49. array( 
  50. 'name' => $widget->id.'_lang_choice',  
  51. 'class' => 'tags-input',  
  52. 'selected' => empty( $instance['pll_lang'] ) ? '' : $instance['pll_lang'],  
  53. ); 
  54.  
  55. /** 
  56. * Called when widget options are saved 
  57. * saves the language associated to the widget 
  58. * @since 0.3 
  59. * @param array $instance Widget options 
  60. * @param array $new_instance Not used 
  61. * @param array $old_instance Not used 
  62. * @param object $widget WP_Widget object 
  63. * @return array Widget options 
  64. */ 
  65. public function widget_update_callback( $instance, $new_instance, $old_instance, $widget ) { 
  66. if ( ! empty( $_POST[ $key = $widget->id.'_lang_choice' ] ) && in_array( $_POST[ $key ], $this->model->get_languages_list( array( 'fields' => 'slug' ) ) ) ) { 
  67. $instance['pll_lang'] = $_POST[ $key ]; 
  68. } else { 
  69. unset( $instance['pll_lang'] ); 
  70.  
  71. return $instance; 
  72.  
  73. /** 
  74. * Updates language user preference set in user profile 
  75. * @since 0.4 
  76. * @param int $user_id 
  77. */ 
  78. public function personal_options_update( $user_id ) { 
  79. // Admin language 
  80. // FIXME Backward compatibility with WP < 4.7 
  81. if ( version_compare( $GLOBALS['wp_version'], '4.7alpha', '<' ) ) { 
  82. $user_lang = in_array( $_POST['user_lang'], $this->model->get_languages_list( array( 'fields' => 'locale' ) ) ) ? $_POST['user_lang'] : 0; 
  83. update_user_meta( $user_id, 'locale', $user_lang ); 
  84.  
  85. // Biography translations 
  86. foreach ( $this->model->get_languages_list() as $lang ) { 
  87. $meta = $lang->slug == $this->options['default_lang'] ? 'description' : 'description_' . $lang->slug; 
  88. $description = empty( $_POST[ 'description_' . $lang->slug ] ) ? '' : trim( $_POST[ 'description_' . $lang->slug ] ); 
  89.  
  90. /** This filter is documented in wp-includes/user.php */ 
  91. $description = apply_filters( 'pre_user_description', $description ); // Applies WP default filter wp_filter_kses 
  92. update_user_meta( $user_id, $meta, $description ); 
  93.  
  94. /** 
  95. * Form for language user preference in user profile 
  96. * @since 0.4 
  97. * @param object $profileuser 
  98. */ 
  99. public function personal_options( $profileuser ) { 
  100. // FIXME: Backward compatibility with WP < 4.7 
  101. if ( version_compare( $GLOBALS['wp_version'], '4.7alpha', '<' ) ) { 
  102. $dropdown = new PLL_Walker_Dropdown(); 
  103. printf( ' 
  104. <tr> 
  105. <th><label for="user_lang">%s</label></th> 
  106. <td>%s</td> 
  107. </tr>',  
  108. esc_html__( 'Admin language', 'polylang' ),  
  109. $dropdown->walk( 
  110. array_merge( 
  111. array( (object) array( 'locale' => 0, 'name' => __( 'WordPress default', 'polylang' ) ) ),  
  112. $this->model->get_languages_list() 
  113. ),  
  114. array( 
  115. 'name' => 'user_lang',  
  116. 'value' => 'locale',  
  117. 'selected' => get_user_meta( $profileuser->ID, 'locale', true ),  
  118. ); 
  119.  
  120. // Hidden informations to modify the biography form with js 
  121. foreach ( $this->model->get_languages_list() as $lang ) { 
  122. $meta = $lang->slug == $this->options['default_lang'] ? 'description' : 'description_' . $lang->slug; 
  123.  
  124. /** This filter is documented in wp-includes/user.php */ 
  125. $description = apply_filters( 'user_description', get_user_meta( $profileuser->ID, $meta, true ) ); // Applies WP default filter wp_kses_data 
  126.  
  127. printf( '<input type="hidden" class="biography" name="%s___%s" value="%s" />',  
  128. esc_attr( $lang->slug ),  
  129. esc_attr( $lang->name ),  
  130. esc_attr( $description ) 
  131. ); 
  132.  
  133. /** 
  134. * Ugprades languages files after a core upgrade 
  135. * only for backward compatibility WP < 4.0 *AND* Polylang < 1.6 
  136. * @since 0.6 
  137. * @param string $version new WP version 
  138. */ 
  139. public function upgrade_languages( $version ) { 
  140. // $GLOBALS['wp_version'] is the old WP version 
  141. if ( version_compare( $version, '4.0', '>=' ) && version_compare( $GLOBALS['wp_version'], '4.0', '<' ) ) { 
  142.  
  143. /** This filter is documented in wp-admin/includes/update-core.php */ 
  144. apply_filters( 'update_feedback', __( 'Upgrading language files…', 'polylang' ) ); 
  145. PLL_Upgrade::download_language_packs(); 
  146.  
  147. /** 
  148. * Allows to update translations files for plugins and themes 
  149. * @since 1.6 
  150. * @param array $locale not used 
  151. * @return array list of locales to update 
  152. */ 
  153. function update_check_locales( $locales ) { 
  154. return $this->model->get_languages_list( array( 'fields' => 'locale' ) ); 
  155.  
  156. /** 
  157. * Filters the locale according to the current language instead of the language 
  158. * of the admin interface 
  159. * @since 2.0 
  160. * @param string $locale 
  161. * @return string 
  162. */ 
  163. public function get_locale( $locale ) { 
  164. return $this->curlang->locale; 
  165.  
  166. /** 
  167. * Maybe fix the result of sanitize_title() in case the languages include German or Danish 
  168. * Without this filter, if language of the title being sanitized is different from the language 
  169. * used for the admin interface and if one this language is German or Danish, some specific 
  170. * characters such as , , , are incorrectly sanitized. 
  171. * @since 2.0 
  172. * @param string $title Sanitized title. 
  173. * @param string $raw_title The title prior to sanitization. 
  174. * @param string $context The context for which the title is being sanitized. 
  175. * @return string 
  176. */ 
  177. public function sanitize_title( $title, $raw_title, $context ) { 
  178. static $once = false; 
  179.  
  180. if ( ! $once && 'save' == $context && ! empty( $this->curlang ) && ! empty( $title ) ) { 
  181. $once = true; 
  182. add_filter( 'locale', array( $this, 'get_locale' ), 20 ); // After the filter for the admin interface 
  183. $title = sanitize_title( $raw_title, '', $context ); 
  184. remove_filter( 'locale', array( $this, 'get_locale' ), 20 ); 
  185. $once = false; 
  186. return $title; 
  187.  
  188. /** 
  189. * Maybe fix the result of sanitize_user() in case the languages include German or Danish 
  190. * @since 2.0 
  191. * @param string $username Sanitized username. 
  192. * @param string $raw_username The username prior to sanitization. 
  193. * @param bool $strict Whether to limit the sanitization to specific characters. Default false. 
  194. * @return string 
  195. */ 
  196. public function sanitize_user( $username, $raw_username, $strict ) { 
  197. static $once = false; 
  198.  
  199. if ( ! $once && ! empty( $this->curlang ) ) { 
  200. $once = true; 
  201. add_filter( 'locale', array( $this, 'get_locale' ), 20 ); // After the filter for the admin interface 
  202. $username = sanitize_user( $raw_username, '', $strict ); 
  203. remove_filter( 'locale', array( $this, 'get_locale' ), 20 ); 
  204. $once = false; 
  205. return $username;