| @@ -7,18 +7,23 @@ | ||
| 7 | 7 | function bogo_admin_bar_menu( $wp_admin_bar ) { |
| 8 | 8 | $current_locale = bogo_get_user_locale(); |
| 9 | 9 | $current_language = bogo_get_language( $current_locale ); |
| 10 | 10 | |
| 11 | - if ( ! $current_language ) | |
| 11 | + if ( ! $current_language ) { | |
| 12 | 12 | $current_language = $current_locale; |
| 13 | + } | |
| 13 | 14 | |
| 14 | 15 | $wp_admin_bar->add_menu( array( |
| 15 | 16 | 'parent' => 'top-secondary', |
| 16 | 17 | 'id' => 'bogo-user-locale', |
| 17 | - 'title' => '✔ ' . $current_language ) ); | |
| 18 | + 'title' => sprintf( | |
| 19 | + '<span class="ab-icon"></span><span class="ab-label">%s</span>', | |
| 20 | + esc_html( $current_language ) ) ) ); | |
| 18 | 21 | |
| 19 | - $available_languages = bogo_available_languages( | |
| 20 | - array( 'exclude' => array( $current_locale ) ) ); | |
| 22 | + $available_languages = bogo_available_languages( array( | |
| 23 | + 'exclude' => array( $current_locale ), | |
| 24 | + 'exclude_enus_if_inactive' => true, | |
| 25 | + 'current_user_can_access' => true ) ); | |
| 21 | 26 | |
| 22 | 27 | foreach ( $available_languages as $locale => $lang ) { |
| 23 | 28 | $url = admin_url( 'profile.php?action=bogo-switch-locale&locale=' . $locale ); |
| 24 | 29 | |
| @@ -38,17 +43,21 @@ | ||
| 38 | 43 | |
| 39 | 44 | add_action( 'admin_init', 'bogo_switch_user_locale' ); |
| 40 | 45 | |
| 41 | 46 | function bogo_switch_user_locale() { |
| 42 | - if ( empty( $_REQUEST['action'] ) || 'bogo-switch-locale' != $_REQUEST['action'] ) | |
| 47 | + if ( empty( $_REQUEST['action'] ) | |
| 48 | + || 'bogo-switch-locale' != $_REQUEST['action'] ) { | |
| 43 | 49 | return; |
| 50 | + } | |
| 44 | 51 | |
| 45 | 52 | check_admin_referer( 'bogo-switch-locale' ); |
| 46 | 53 | |
| 47 | 54 | $locale = isset( $_REQUEST['locale'] ) ? $_REQUEST['locale'] : ''; |
| 48 | 55 | |
| 49 | - if ( ! bogo_is_available_locale( $locale ) || $locale == bogo_get_user_locale() ) | |
| 56 | + if ( ! bogo_is_available_locale( $locale ) | |
| 57 | + || $locale == bogo_get_user_locale() ) { | |
| 50 | 58 | return; |
| 59 | + } | |
| 51 | 60 | |
| 52 | 61 | update_user_option( get_current_user_id(), 'locale', $locale, true ); |
| 53 | 62 | |
| 54 | 63 | if ( ! empty( $_REQUEST['redirect_to'] ) ) { |
| @@ -57,13 +66,55 @@ | ||
| 57 | 66 | } |
| 58 | 67 | } |
| 59 | 68 | |
| 60 | 69 | function bogo_get_user_locale( $user_id = 0 ) { |
| 70 | + global $current_user; | |
| 71 | + | |
| 72 | + $default_locale = bogo_get_default_locale(); | |
| 73 | + $user_id = absint( $user_id ); | |
| 74 | + | |
| 75 | + if ( ! $user_id ) { | |
| 76 | + if ( function_exists( 'wp_get_current_user' ) && ! empty( $current_user ) ) { | |
| 77 | + $user_id = get_current_user_id(); | |
| 78 | + } elseif ( ! $user_id = apply_filters( 'determine_current_user', false ) ) { | |
| 79 | + return $default_locale; | |
| 80 | + } | |
| 81 | + } | |
| 82 | + | |
| 61 | 83 | $locale = get_user_option( 'locale', $user_id ); |
| 62 | 84 | |
| 63 | - if ( empty( $locale ) ) | |
| 64 | - $locale = bogo_get_default_locale(); | |
| 85 | + if ( bogo_is_available_locale( $locale ) | |
| 86 | + && ( 'en_US' != $locale || ! bogo_get_prop( 'enus_deactivated' ) ) | |
| 87 | + && user_can( $user_id, 'bogo_access_locale', $locale ) ) { | |
| 88 | + return $locale; | |
| 89 | + } | |
| 65 | 90 | |
| 66 | - return $locale; | |
| 91 | + if ( user_can( $user_id, 'bogo_access_locale', $default_locale ) ) { | |
| 92 | + return $default_locale; | |
| 93 | + } | |
| 94 | + | |
| 95 | + $available_locales = bogo_available_locales( array( | |
| 96 | + 'exclude_enus_if_inactive' => true ) ); | |
| 97 | + | |
| 98 | + foreach ( $available_locales as $locale ) { | |
| 99 | + if ( user_can( $user_id, 'bogo_access_locale', $locale ) ) { | |
| 100 | + return $locale; | |
| 101 | + } | |
| 102 | + } | |
| 103 | + | |
| 104 | + return $default_locale; | |
| 67 | 105 | } |
| 68 | 106 | |
| 69 | -?> | |
| 107 | +function bogo_get_user_accessible_locales( $user_id ) { | |
| 108 | + global $wpdb; | |
| 109 | + | |
| 110 | + $user_id = absint( $user_id ); | |
| 111 | + $meta_key = $wpdb->get_blog_prefix() . 'accessible_locale'; | |
| 112 | + | |
| 113 | + $locales = get_user_meta( $user_id, $meta_key ); | |
| 114 | + | |
| 115 | + if ( bogo_get_prop( 'enus_deactivated' ) ) { | |
| 116 | + $locales = array_diff( $locales, array( 'en_US' ) ); | |
| 117 | + } | |
| 118 | + | |
| 119 | + return $locales; | |
| 120 | +} | |