| 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 |
// ugrades 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 |
|
| 38 |
/* |
| 39 |
* modifies the widgets forms to add our language dropdwown list |
| 40 |
* |
| 41 |
* @since 0.3 |
| 42 |
* |
| 43 |
* @param object $widget |
| 44 |
*/ |
| 45 |
public function in_widget_form( $widget, $return, $instance ) { |
| 46 |
$dropdown = new PLL_Walker_Dropdown(); |
| 47 |
printf( '<p><label for="%1$s">%2$s %3$s</label></p>', |
| 48 |
esc_attr( $widget->id.'_lang_choice' ), |
| 49 |
__( 'The widget is displayed for:', 'polylang' ), |
| 50 |
$dropdown->walk( |
| 51 |
array_merge( |
| 52 |
array( (object) array( 'slug' => 0, 'name' => __( 'All languages', 'polylang' ) ) ), |
| 53 |
$this->model->get_languages_list() |
| 54 |
), |
| 55 |
array( |
| 56 |
'name' => $widget->id.'_lang_choice', |
| 57 |
'class' => 'tags-input', |
| 58 |
'selected' => empty( $instance['pll_lang'] ) ? '' : $instance['pll_lang'], |
| 59 |
) |
| 60 |
) |
| 61 |
); |
| 62 |
} |
| 63 |
|
| 64 |
/* |
| 65 |
* called when widget options are saved |
| 66 |
* saves the language associated to the widget |
| 67 |
* |
| 68 |
* @since 0.3 |
| 69 |
* |
| 70 |
* @param array $instance widget options |
| 71 |
* @param array $new_instance not used |
| 72 |
* @param array $old_instance not used |
| 73 |
* @param object $widget WP_Widget object |
| 74 |
* @return array widget options |
| 75 |
*/ |
| 76 |
public function widget_update_callback( $instance, $new_instance, $old_instance, $widget ) { |
| 77 |
if ( ! empty( $_POST[ $key = $widget->id.'_lang_choice' ] ) && in_array( $_POST[ $key ], $this->model->get_languages_list( array( 'fields' => 'slug' ) ) ) ) { |
| 78 |
$instance['pll_lang'] = $_POST[ $key ]; |
| 79 |
} |
| 80 |
else { |
| 81 |
unset( $instance['pll_lang'] ); |
| 82 |
} |
| 83 |
|
| 84 |
return $instance; |
| 85 |
} |
| 86 |
|
| 87 |
/* |
| 88 |
* updates language user preference set in user profile |
| 89 |
* |
| 90 |
* @since 0.4 |
| 91 |
* |
| 92 |
* @param int $user_id |
| 93 |
*/ |
| 94 |
public function personal_options_update( $user_id ) { |
| 95 |
// admin language |
| 96 |
$user_lang = in_array( $_POST['user_lang'], $this->model->get_languages_list( array( 'fields' => 'locale' ) ) ) ? $_POST['user_lang'] : 0; |
| 97 |
update_user_meta( $user_id, 'user_lang', $user_lang ); |
| 98 |
|
| 99 |
// biography translations |
| 100 |
foreach ( $this->model->get_languages_list() as $lang ) { |
| 101 |
$meta = $lang->slug == $this->options['default_lang'] ? 'description' : 'description_' . $lang->slug; |
| 102 |
$description = empty( $_POST[ 'description_' . $lang->slug ] ) ? '' : trim( $_POST[ 'description_' . $lang->slug ] ); |
| 103 |
$description = apply_filters( 'pre_user_description', $description ); // applies WP default filter wp_filter_kses |
| 104 |
update_user_meta( $user_id, $meta, $description ); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
/* |
| 109 |
* form for language user preference in user profile |
| 110 |
* |
| 111 |
* @since 0.4 |
| 112 |
* |
| 113 |
* @param object $profileuser |
| 114 |
*/ |
| 115 |
public function personal_options( $profileuser ) { |
| 116 |
$dropdown = new PLL_Walker_Dropdown(); |
| 117 |
printf( ' |
| 118 |
<tr> |
| 119 |
<th><label for="user_lang">%s</label></th> |
| 120 |
<td>%s</td> |
| 121 |
</tr>', |
| 122 |
__( 'Admin language', 'polylang' ), |
| 123 |
$dropdown->walk( |
| 124 |
array_merge( |
| 125 |
array( (object) array( 'locale' => 0, 'name' => __( 'WordPress default', 'polylang' ) ) ), |
| 126 |
$this->model->get_languages_list() |
| 127 |
), |
| 128 |
array( |
| 129 |
'name' => 'user_lang', |
| 130 |
'value' => 'locale', |
| 131 |
'selected' => get_user_meta( $profileuser->ID, 'user_lang', true ), |
| 132 |
) |
| 133 |
) |
| 134 |
); |
| 135 |
|
| 136 |
// hidden informations to modify the biography form with js |
| 137 |
foreach ( $this->model->get_languages_list() as $lang ) { |
| 138 |
$meta = $lang->slug == $this->options['default_lang'] ? 'description' : 'description_' . $lang->slug; |
| 139 |
$description = apply_filters( 'user_description', get_user_meta( $profileuser->ID, $meta, true ) ); // applies WP default filter wp_kses_data |
| 140 |
|
| 141 |
printf( '<input type="hidden" class="biography" name="%s___%s" value="%s" />', |
| 142 |
esc_attr( $lang->slug ), |
| 143 |
esc_attr( $lang->name ), |
| 144 |
esc_attr( $description ) |
| 145 |
); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
/* |
| 150 |
* ugprades languages files after a core upgrade |
| 151 |
* only for backward compatibility WP < 4.0 *AND* Polylang < 1.6 |
| 152 |
* |
| 153 |
* @since 0.6 |
| 154 |
* |
| 155 |
* @param string $version new WP version |
| 156 |
*/ |
| 157 |
public function upgrade_languages( $version ) { |
| 158 |
// $GLOBALS['wp_version'] is the old WP version |
| 159 |
if ( version_compare( $version, '4.0', '>=' ) && version_compare( $GLOBALS['wp_version'], '4.0', '<' ) ) { |
| 160 |
apply_filters( 'update_feedback', __( 'Upgrading language files…', 'polylang' ) ); |
| 161 |
PLL_Upgrade::download_language_packs(); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
/* |
| 166 |
* allows to update translations files for plugins and themes |
| 167 |
* |
| 168 |
* @since 1.6 |
| 169 |
* |
| 170 |
* @param array $locale not used |
| 171 |
* @return array list of locales to update |
| 172 |
*/ |
| 173 |
function update_check_locales( $locales ) { |
| 174 |
return $this->model->get_languages_list( array( 'fields' => 'locale' ) ); |
| 175 |
} |
| 176 |
} |
| 177 |
|