| @@ -22,9 +22,9 @@ | ||
| 22 | 22 | |
| 23 | 23 | // Language management for users |
| 24 | 24 | add_action( 'personal_options_update', array( $this, 'personal_options_update' ) ); |
| 25 | 25 | add_action( 'edit_user_profile_update', array( $this, 'personal_options_update' ) ); |
| 26 | - add_action( 'personal_options', array( $this, 'personal_options' ) ); | |
| 26 | + add_action( 'personal_options', array( $this, 'user_profile_enqueue_scripts' ) ); | |
| 27 | 27 | |
| 28 | 28 | // Upgrades plugins and themes translations files |
| 29 | 29 | add_filter( 'themes_update_check_locales', array( $this, 'update_check_locales' ) ); |
| 30 | 30 | add_filter( 'plugins_update_check_locales', array( $this, 'update_check_locales' ) ); |
| @@ -55,27 +55,48 @@ | ||
| 55 | 55 | } |
| 56 | 56 | } |
| 57 | 57 | |
| 58 | 58 | /** |
| 59 | - * Outputs hidden information to modify the biography form with js. | |
| 59 | + * Enqueues scripts for the multilingual biography on the user's profile admin page. | |
| 60 | 60 | * |
| 61 | - * @since 0.4 | |
| 61 | + * @since 3.8.4 | |
| 62 | 62 | * |
| 63 | 63 | * @param WP_User $profileuser The current WP_User object. |
| 64 | 64 | * @return void |
| 65 | 65 | */ |
| 66 | - public function personal_options( $profileuser ) { | |
| 67 | - foreach ( $this->model->get_languages_list() as $lang ) { | |
| 68 | - $meta = $lang->is_default ? 'description' : 'description_' . $lang->slug; | |
| 66 | + public function user_profile_enqueue_scripts( $profileuser ): void { | |
| 67 | + $screen = get_current_screen(); | |
| 68 | + | |
| 69 | + if ( empty( $screen ) || ! in_array( $screen->base, array( 'profile', 'user-edit' ), true ) ) { | |
| 70 | + return; | |
| 71 | + } | |
| 72 | + | |
| 73 | + if ( ! $this->model->has_languages() ) { | |
| 74 | + return; | |
| 75 | + } | |
| 76 | + | |
| 77 | + $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; | |
| 78 | + $data = array(); | |
| 79 | + | |
| 80 | + wp_enqueue_script( 'pll_user', plugins_url( "/js/build/user{$suffix}.js", POLYLANG_ROOT_FILE ), array(), POLYLANG_VERSION, array( 'in_footer' => true ) ); | |
| 81 | + | |
| 82 | + foreach ( $this->model->languages->get_list() as $lang ) { | |
| 83 | + $meta = $lang->is_default ? 'description' : "description_{$lang->slug}"; | |
| 69 | 84 | $description = get_user_meta( $profileuser->ID, $meta, true ); |
| 85 | + $description = is_string( $description ) ? $description : ''; | |
| 70 | 86 | |
| 71 | - printf( | |
| 72 | - '<input type="hidden" class="biography" name="%s___%s" value="%s" />', | |
| 73 | - esc_attr( $lang->slug ), | |
| 74 | - esc_attr( $lang->name ), | |
| 75 | - sanitize_user_field( 'description', $description, $profileuser->ID, 'edit' ) | |
| 87 | + $data[] = array( | |
| 88 | + 'slug' => esc_attr( $lang->slug ), | |
| 89 | + 'name' => esc_html( $lang->name ), | |
| 90 | + 'lang' => esc_attr( $lang->get_locale( 'display' ) ), | |
| 91 | + 'direction' => $lang->is_rtl ? 'rtl' : 'ltr', | |
| 92 | + 'flag' => PLL_Language::get_flag_information( $lang->flag_code ), | |
| 93 | + 'description' => sanitize_user_field( 'description', $description, $profileuser->ID, 'edit' ), | |
| 76 | 94 | ); |
| 77 | 95 | } |
| 96 | + | |
| 97 | + $script = sprintf( 'const pllDescriptionData = %s;', wp_json_encode( $data ) ); | |
| 98 | + wp_add_inline_script( 'pll_user', $script, 'before' ); | |
| 78 | 99 | } |
| 79 | 100 | |
| 80 | 101 | /** |
| 81 | 102 | * Allows to update translations files for plugins and themes. |