| 1 |
<?php |
| 2 |
|
| 3 |
add_action( 'personal_options_update', 'bogo_update_user_option', 10, 1 ); |
| 4 |
add_action( 'edit_user_profile_update', 'bogo_update_user_option', 10, 1 ); |
| 5 |
|
| 6 |
function bogo_update_user_option( $user_id ) { |
| 7 |
global $wpdb; |
| 8 |
|
| 9 |
$meta_key = $wpdb->get_blog_prefix() . 'accessible_locale'; |
| 10 |
|
| 11 |
if ( ! empty( $_POST['setting_bogo_accessible_locales'] ) ) { |
| 12 |
delete_user_meta( $user_id, $meta_key ); |
| 13 |
|
| 14 |
if ( isset( $_POST['bogo_accessible_locales'] ) ) { |
| 15 |
$locales = (array) $_POST['bogo_accessible_locales']; |
| 16 |
$locales = bogo_filter_locales( $locales ); |
| 17 |
|
| 18 |
foreach ( $locales as $locale ) { |
| 19 |
add_user_meta( $user_id, $meta_key, $locale ); |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
if ( ! metadata_exists( 'user', $user_id, $meta_key ) ) { |
| 24 |
add_user_meta( $user_id, $meta_key, 'zxx' ); |
| 25 |
// zxx is a special code in ISO 639-2 |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
if ( isset( $_POST['bogo_own_locale'] ) ) { |
| 30 |
$locale = trim( $_POST['bogo_own_locale'] ); |
| 31 |
|
| 32 |
if ( bogo_is_available_locale( $locale ) ) { |
| 33 |
update_user_option( $user_id, 'locale', $locale, true ); |
| 34 |
} |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
add_action( 'personal_options', 'bogo_set_locale_options', 10, 1 ); |
| 40 |
|
| 41 |
function bogo_set_locale_options( $profileuser ) { |
| 42 |
if ( is_network_admin() or IS_PROFILE_PAGE ) { |
| 43 |
return; |
| 44 |
} |
| 45 |
|
| 46 |
if ( ! user_can( $profileuser, 'bogo_access_all_locales' ) ) { |
| 47 |
bogo_set_accessible_locales( $profileuser ); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
function bogo_set_accessible_locales( $profileuser ) { |
| 53 |
$available_languages = bogo_available_languages( array( |
| 54 |
'orderby' => 'value', |
| 55 |
) ); |
| 56 |
|
| 57 |
$accessible_locales = bogo_get_user_accessible_locales( $profileuser->ID ); |
| 58 |
|
| 59 |
?> |
| 60 |
|
| 61 |
<!-- Bogo plugin --> |
| 62 |
<tr> |
| 63 |
<th scope="row"><?php echo esc_html( __( 'Locale', 'bogo' ) ); ?></th> |
| 64 |
<td> |
| 65 |
<input type="hidden" name="setting_bogo_accessible_locales" value="1" /> |
| 66 |
<span class="description"><?php echo esc_html( __( 'This user is allowed to access the following locales:', 'bogo' ) ); ?></span><br /> |
| 67 |
<fieldset class="bogo-locale-options"> |
| 68 |
|
| 69 |
<?php |
| 70 |
|
| 71 |
foreach ( $available_languages as $locale => $language ) { |
| 72 |
$checked = in_array( $locale, $accessible_locales ); |
| 73 |
$id_attr = sprintf( 'bogo_accessible_locale-%s', $locale ); |
| 74 |
|
| 75 |
$checkbox = sprintf( |
| 76 |
'<label %1$s><input %2$s />%3$s</label>', |
| 77 |
bogo_format_atts( array( |
| 78 |
'class' => 'bogo-locale-option' . ( $checked ? ' checked' : '' ), |
| 79 |
'for' => $id_attr, |
| 80 |
) ), |
| 81 |
bogo_format_atts( array( |
| 82 |
'type' => 'checkbox', |
| 83 |
'id' => $id_attr, |
| 84 |
'name' => 'bogo_accessible_locales[]', |
| 85 |
'value' => $locale, |
| 86 |
'checked' => $checked, |
| 87 |
) ), |
| 88 |
$language |
| 89 |
); |
| 90 |
|
| 91 |
echo wp_kses( $checkbox, 'bogo_form_inside' ) . "\n"; |
| 92 |
} |
| 93 |
|
| 94 |
?> |
| 95 |
</fieldset> |
| 96 |
</td> |
| 97 |
</tr> |
| 98 |
|
| 99 |
<?php |
| 100 |
} |
| 101 |
|