| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Setup miscellaneous admin filters as well as filters common to admin and frontend |
| 5 |
* |
| 6 |
* @since 1.2 |
| 7 |
*/ |
| 8 |
class PLL_Admin_Filters extends PLL_Filters { |
| 9 |
|
| 10 |
/** |
| 11 |
* Constructor: setups filters and actions |
| 12 |
* |
| 13 |
* @since 1.2 |
| 14 |
* |
| 15 |
* @param object $polylang |
| 16 |
*/ |
| 17 |
public function __construct( &$polylang ) { |
| 18 |
parent::__construct( $polylang ); |
| 19 |
|
| 20 |
// Widgets languages filter |
| 21 |
add_action( 'in_widget_form', array( $this, 'in_widget_form' ), 10, 3 ); |
| 22 |
add_filter( 'widget_update_callback', array( $this, 'widget_update_callback' ), 10, 4 ); |
| 23 |
|
| 24 |
// Language management for users |
| 25 |
add_action( 'personal_options_update', array( $this, 'personal_options_update' ) ); |
| 26 |
add_action( 'edit_user_profile_update', array( $this, 'personal_options_update' ) ); |
| 27 |
add_action( 'personal_options', array( $this, 'personal_options' ) ); |
| 28 |
|
| 29 |
// Upgrades languages files after a core upgrade ( timing is important ) |
| 30 |
// Backward compatibility WP < 4.0 *AND* Polylang < 1.6 |
| 31 |
add_action( '_core_updated_successfully', array( $this, 'upgrade_languages' ), 1 ); // since WP 3.3 |
| 32 |
|
| 33 |
// Upgrades plugins and themes translations files |
| 34 |
add_filter( 'themes_update_check_locales', array( $this, 'update_check_locales' ) ); |
| 35 |
add_filter( 'plugins_update_check_locales', array( $this, 'update_check_locales' ) ); |
| 36 |
|
| 37 |
// We need specific filters for German and Danish |
| 38 |
$specific_locales = array( 'da_DK', 'de_DE', 'de_DE_formal', 'de_CH', 'de_CH_informal', 'ca', 'sr_RS', 'bs_BA' ); |
| 39 |
if ( array_intersect( $this->model->get_languages_list( array( 'fields' => 'locale' ) ), $specific_locales ) ) { |
| 40 |
add_filter( 'sanitize_title', array( $this, 'sanitize_title' ), 10, 3 ); |
| 41 |
add_filter( 'sanitize_user', array( $this, 'sanitize_user' ), 10, 3 ); |
| 42 |
} |
| 43 |
|
| 44 |
add_filter( 'admin_body_class', array( $this, 'admin_body_class' ) ); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Modifies the widgets forms to add our language dropdown list |
| 49 |
* |
| 50 |
* @since 0.3 |
| 51 |
* |
| 52 |
* @param object $widget Widget instance |
| 53 |
* @param null $return Not used |
| 54 |
* @param array $instance Widget settings |
| 55 |
*/ |
| 56 |
public function in_widget_form( $widget, $return, $instance ) { |
| 57 |
$screen = get_current_screen(); |
| 58 |
|
| 59 |
// Test the Widgets screen and the Customizer to avoid displaying the option in page builders |
| 60 |
// Saving the widget reloads the form. And curiously the action is in $_REQUEST but neither in $_POST, not in $_GET. |
| 61 |
if ( ( isset( $screen ) && 'widgets' === $screen->base ) || ( isset( $_REQUEST['action'] ) && 'save-widget' === $_REQUEST['action'] ) || isset( $GLOBALS['wp_customize'] ) ) { |
| 62 |
$dropdown = new PLL_Walker_Dropdown(); |
| 63 |
printf( |
| 64 |
'<p><label for="%1$s">%2$s %3$s</label></p>', |
| 65 |
esc_attr( $widget->id . '_lang_choice' ), |
| 66 |
esc_html__( 'The widget is displayed for:', 'polylang' ), |
| 67 |
$dropdown->walk( |
| 68 |
array_merge( |
| 69 |
array( (object) array( 'slug' => 0, 'name' => __( 'All languages', 'polylang' ) ) ), |
| 70 |
$this->model->get_languages_list() |
| 71 |
), |
| 72 |
array( |
| 73 |
'name' => $widget->id . '_lang_choice', |
| 74 |
'class' => 'tags-input pll-lang-choice', |
| 75 |
'selected' => empty( $instance['pll_lang'] ) ? '' : $instance['pll_lang'], |
| 76 |
) |
| 77 |
) |
| 78 |
); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Called when widget options are saved |
| 84 |
* saves the language associated to the widget |
| 85 |
* |
| 86 |
* @since 0.3 |
| 87 |
* |
| 88 |
* @param array $instance Widget options |
| 89 |
* @param array $new_instance Not used |
| 90 |
* @param array $old_instance Not used |
| 91 |
* @param object $widget WP_Widget object |
| 92 |
* @return array Widget options |
| 93 |
*/ |
| 94 |
public function widget_update_callback( $instance, $new_instance, $old_instance, $widget ) { |
| 95 |
if ( ! empty( $_POST[ $key = $widget->id . '_lang_choice' ] ) && in_array( $_POST[ $key ], $this->model->get_languages_list( array( 'fields' => 'slug' ) ) ) ) { |
| 96 |
$instance['pll_lang'] = $_POST[ $key ]; |
| 97 |
} else { |
| 98 |
unset( $instance['pll_lang'] ); |
| 99 |
} |
| 100 |
|
| 101 |
return $instance; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Updates language user preference set in user profile |
| 106 |
* |
| 107 |
* @since 0.4 |
| 108 |
* |
| 109 |
* @param int $user_id |
| 110 |
*/ |
| 111 |
public function personal_options_update( $user_id ) { |
| 112 |
// Biography translations |
| 113 |
foreach ( $this->model->get_languages_list() as $lang ) { |
| 114 |
$meta = $lang->slug == $this->options['default_lang'] ? 'description' : 'description_' . $lang->slug; |
| 115 |
$description = empty( $_POST[ 'description_' . $lang->slug ] ) ? '' : trim( $_POST[ 'description_' . $lang->slug ] ); |
| 116 |
|
| 117 |
/** This filter is documented in wp-includes/user.php */ |
| 118 |
$description = apply_filters( 'pre_user_description', $description ); // Applies WP default filter wp_filter_kses |
| 119 |
update_user_meta( $user_id, $meta, $description ); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Outputs hidden information to modify the biography form with js |
| 125 |
* |
| 126 |
* @since 0.4 |
| 127 |
* |
| 128 |
* @param object $profileuser |
| 129 |
*/ |
| 130 |
public function personal_options( $profileuser ) { |
| 131 |
foreach ( $this->model->get_languages_list() as $lang ) { |
| 132 |
$meta = $lang->slug == $this->options['default_lang'] ? 'description' : 'description_' . $lang->slug; |
| 133 |
|
| 134 |
/** This filter is documented in wp-includes/user.php */ |
| 135 |
$description = apply_filters( 'user_description', get_user_meta( $profileuser->ID, $meta, true ) ); // Applies WP default filter wp_kses_data |
| 136 |
|
| 137 |
printf( |
| 138 |
'<input type="hidden" class="biography" name="%s___%s" value="%s" />', |
| 139 |
esc_attr( $lang->slug ), |
| 140 |
esc_attr( $lang->name ), |
| 141 |
esc_attr( $description ) |
| 142 |
); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Upgrades languages files after a core upgrade |
| 148 |
* only for backward compatibility WP < 4.0 *AND* Polylang < 1.6 |
| 149 |
* |
| 150 |
* @since 0.6 |
| 151 |
* |
| 152 |
* @param string $version new WP version |
| 153 |
*/ |
| 154 |
public function upgrade_languages( $version ) { |
| 155 |
// $GLOBALS['wp_version'] is the old WP version |
| 156 |
if ( version_compare( $version, '4.0', '>=' ) && version_compare( $GLOBALS['wp_version'], '4.0', '<' ) ) { |
| 157 |
|
| 158 |
/** This filter is documented in wp-admin/includes/update-core.php */ |
| 159 |
apply_filters( 'update_feedback', __( 'Upgrading language files…', 'polylang' ) ); |
| 160 |
PLL_Upgrade::download_language_packs(); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Allows to update translations files for plugins and themes |
| 166 |
* |
| 167 |
* @since 1.6 |
| 168 |
* |
| 169 |
* @param array $locales Not used |
| 170 |
* @return array list of locales to update |
| 171 |
*/ |
| 172 |
public function update_check_locales( $locales ) { |
| 173 |
return $this->model->get_languages_list( array( 'fields' => 'locale' ) ); |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Filters the locale according to the current language instead of the language |
| 178 |
* of the admin interface |
| 179 |
* |
| 180 |
* @since 2.0 |
| 181 |
* |
| 182 |
* @param string $locale |
| 183 |
* @return string |
| 184 |
*/ |
| 185 |
public function get_locale( $locale ) { |
| 186 |
if ( isset( $_POST['post_lang_choice'] ) && $lang = $this->model->get_language( $_POST['post_lang_choice'] ) ) { |
| 187 |
$locale = $lang->locale; |
| 188 |
} elseif ( isset( $_POST['term_lang_choice'] ) && $lang = $this->model->get_language( $_POST['term_lang_choice'] ) ) { |
| 189 |
$locale = $lang->locale; |
| 190 |
} elseif ( isset( $_POST['inline_lang_choice'] ) && $lang = $this->model->get_language( $_POST['inline_lang_choice'] ) ) { |
| 191 |
$locale = $lang->locale; |
| 192 |
} elseif ( ! empty( $this->curlang ) ) { |
| 193 |
$locale = $this->curlang->locale; |
| 194 |
} |
| 195 |
|
| 196 |
return $locale; |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* Maybe fix the result of sanitize_title() in case the languages include German or Danish |
| 201 |
* Without this filter, if language of the title being sanitized is different from the language |
| 202 |
* used for the admin interface and if one this language is German or Danish, some specific |
| 203 |
* characters such as ä, ö, ü, ß are incorrectly sanitized. |
| 204 |
* |
| 205 |
* @since 2.0 |
| 206 |
* |
| 207 |
* @param string $title Sanitized title. |
| 208 |
* @param string $raw_title The title prior to sanitization. |
| 209 |
* @param string $context The context for which the title is being sanitized. |
| 210 |
* @return string |
| 211 |
*/ |
| 212 |
public function sanitize_title( $title, $raw_title, $context ) { |
| 213 |
static $once = false; |
| 214 |
|
| 215 |
if ( ! $once && 'save' == $context && ! empty( $this->curlang ) && ! empty( $title ) ) { |
| 216 |
$once = true; |
| 217 |
add_filter( 'locale', array( $this, 'get_locale' ), 20 ); // After the filter for the admin interface |
| 218 |
$title = sanitize_title( $raw_title, '', $context ); |
| 219 |
remove_filter( 'locale', array( $this, 'get_locale' ), 20 ); |
| 220 |
$once = false; |
| 221 |
} |
| 222 |
return $title; |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* Maybe fix the result of sanitize_user() in case the languages include German or Danish |
| 227 |
* |
| 228 |
* @since 2.0 |
| 229 |
* |
| 230 |
* @param string $username Sanitized username. |
| 231 |
* @param string $raw_username The username prior to sanitization. |
| 232 |
* @param bool $strict Whether to limit the sanitization to specific characters. Default false. |
| 233 |
* @return string |
| 234 |
*/ |
| 235 |
public function sanitize_user( $username, $raw_username, $strict ) { |
| 236 |
static $once = false; |
| 237 |
|
| 238 |
if ( ! $once && ! empty( $this->curlang ) ) { |
| 239 |
$once = true; |
| 240 |
add_filter( 'locale', array( $this, 'get_locale' ), 20 ); // After the filter for the admin interface |
| 241 |
$username = sanitize_user( $raw_username, '', $strict ); |
| 242 |
remove_filter( 'locale', array( $this, 'get_locale' ), 20 ); |
| 243 |
$once = false; |
| 244 |
} |
| 245 |
return $username; |
| 246 |
} |
| 247 |
|
| 248 |
/** |
| 249 |
* Adds a text direction dependent class to the body |
| 250 |
* |
| 251 |
* @since 2.2 |
| 252 |
* |
| 253 |
* @param string $classes Space-separated list of CSS classes. |
| 254 |
* @return string |
| 255 |
*/ |
| 256 |
public function admin_body_class( $classes ) { |
| 257 |
if ( ! empty( $this->curlang ) ) { |
| 258 |
$classes .= ' pll-dir-' . ( $this->curlang->is_rtl ? 'rtl' : 'ltr' ); |
| 259 |
} |
| 260 |
return $classes; |
| 261 |
} |
| 262 |
} |
| 263 |
|