Skip to content

Commit

Permalink
Site Settings: Add support for the language setting to the API (#8568)
Browse files Browse the repository at this point in the history
* Add lang_id to the list of options available in the settings response
* Prevent modification & return an error code if the client cannot change the setting
* Download language packs if needed & available
* Let the `lang_id` param persist to the `WPLANG` option behind the scenes & control the setting
* Default to US english when option is empty
* Return an error code when the setting is "blocked" for the client.
  • Loading branch information
jblz authored Jan 22, 2018
1 parent 7fe2b35 commit f88694f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
7 changes: 7 additions & 0 deletions _inc/lib/class.core-rest-api-endpoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -1889,6 +1889,13 @@ public static function get_updateable_data_list( $selector = '' ) {
'jp_group' => 'settings',
),

'lang_id' => array(
'description' => esc_html__( 'Primary language for the site.', 'jetpack' ),
'type' => 'string',
'default' => 'en_US',
'jp_group' => 'settings',
),

'onboarding' => array(
'description' => '',
'type' => 'object',
Expand Down
38 changes: 38 additions & 0 deletions _inc/lib/core-api/class.jetpack-core-api-module-endpoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,23 @@ public function get_all_options() {

foreach ( $settings as $setting => $properties ) {
switch ( $setting ) {
case 'lang_id':
if ( defined( 'WPLANG' ) ) {
// We can't affect this setting, so warn the client
$response[ $setting ] = 'error_const';
break;
}

if ( ! current_user_can( 'install_languages' ) ) {
// The user doesn't have caps to install language packs, so warn the client
$response[ $setting ] = 'error_cap';
break;
}

$value = get_option( 'WPLANG' );
$response[ $setting ] = empty( $value ) ? 'en_US' : $value;
break;

case $holiday_snow_option_name:
$response[ $setting ] = get_option( $holiday_snow_option_name ) === 'letitsnow';
break;
Expand Down Expand Up @@ -607,6 +624,27 @@ public function update_data( $request ) {
$value = Jetpack_Core_Json_Api_Endpoints::cast_value( $value, $option_attrs );

switch ( $option ) {
case 'lang_id':
if ( defined( 'WPLANG' ) || ! current_user_can( 'install_languages' ) ) {
// We can't affect this setting
$updated = false;
break;
}

if ( $value === 'en_US' || empty( $value ) ) {
return delete_option( 'WPLANG' );
}

// `wp_download_language_pack` only tries to download packs if they're not already available
$language = wp_download_language_pack( $value );
if ( $language === false ) {
// The language pack download failed.
$updated = false;
break;
}
$updated = get_option( 'WPLANG' ) === $language ? true : update_option( 'WPLANG', $language );
break;

case 'monitor_receive_notifications':
$monitor = new Jetpack_Monitor();

Expand Down

0 comments on commit f88694f

Please sign in to comment.