Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Autoupdate Translations #5953

Merged
merged 3 commits into from
Jan 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions class.jetpack-autoupdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ public function autoupdate_plugin( $update, $item ) {
}

public function autoupdate_translation( $update, $item ) {
// Autoupdate all translations
if ( Jetpack_Options::get_option( 'autoupdate_translations', false ) ) {
return true;
}

// Themes
$autoupdate_themes_translations = Jetpack_Options::get_option( 'autoupdate_themes_translations', array() );
$autoupdate_theme_list = Jetpack_Options::get_option( 'autoupdate_themes', array() );
Expand Down
1 change: 1 addition & 0 deletions class.jetpack-options.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public static function get_option_names( $type = 'compact' ) {
'autoupdate_themes', // (array) An array of theme ids ( eg. twentyfourteen ) that should be autoupdated
'autoupdate_themes_translations', // (array) An array of theme ids ( eg. twentyfourteen ) that should autoupdated translation files.
'autoupdate_core', // (bool) Whether or not to autoupdate core
'autoupdate_translations', // (bool) Whether or not to autoupdate all translations
'json_api_full_management', // (bool) Allow full management (eg. Activate, Upgrade plugins) of the site via the JSON API.
'sync_non_public_post_stati', // (bool) Allow synchronisation of posts and pages with non-public status.
'site_icon_url', // (string) url to the full site icon
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ protected function format_plugin( $plugin_file, $plugin_data ) {
$plugin['autoupdate'] = $autoupdate;

$autoupdate_translation = in_array( $plugin_file, Jetpack_Options::get_option( 'autoupdate_plugins_translations', array() ) );
$plugin['autoupdate_translation'] = $autoupdate || $autoupdate_translation;
$plugin['autoupdate_translation'] = $autoupdate || $autoupdate_translation || Jetpack_Options::get_option( 'autoupdate_translations', false );

$plugin['uninstallable'] = is_uninstallable_plugin( $plugin_file );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ protected function format_theme( $theme ) {
$formatted_theme['autoupdate'] = $autoupdate;

$autoupdate_translation = in_array( $id, Jetpack_Options::get_option( 'autoupdate_themes_translations', array() ) );
$formatted_theme['autoupdate_translation'] = $autoupdate || $autoupdate_translation;
$formatted_theme['autoupdate_translation'] = $autoupdate || $autoupdate_translation || Jetpack_Options::get_option( 'autoupdate_translations', false );

if ( isset( $this->log[ $id ] ) ) {
$formatted_theme['log'] = $this->log[ $id ];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

// Translations
class Jetpack_JSON_API_Translations_Endpoint extends Jetpack_JSON_API_Endpoint {
// GET /sites/%s/translations
// POST /sites/%s/translations
// POST /sites/%s/translations/update
protected $needed_capabilities = array( 'update_core', 'update_plugins', 'update_themes' );
protected $log;
protected $success;

public function result() {
return array(
'translations' => wp_get_translation_updates(),
'autoupdate' => Jetpack_Options::get_option( 'autoupdate_translations', false ),
'log' => $this->log,
'success' => $this->success,
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

class Jetpack_JSON_API_Translations_Modify_Endpoint extends Jetpack_JSON_API_Translations_Endpoint {
// POST /sites/%s/translations
// POST /sites/%s/translations/update
protected $action = 'default_action';
protected $new_version;
protected $log;

public function default_action() {
$args = $this->input();

if ( isset( $args['autoupdate'] ) && is_bool( $args['autoupdate'] ) ) {
Jetpack_Options::update_option( 'autoupdate_translations', $args['autoupdate'] );
}

return true;
}

protected function update() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this be result ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No result is defined in Jetpack_JSON_API_Translations_Endpoint class.

include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';

$upgrader = new Language_Pack_Upgrader( new Automatic_Upgrader_Skin() );
$result = $upgrader->bulk_upgrade();

$this->log = $upgrader->skin->get_upgrade_messages();
$this->success = ( ! is_wp_error( $result ) ) ? (bool) $result : false;
}
}
70 changes: 70 additions & 0 deletions json-endpoints/jetpack/json-api-jetpack-endpoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,76 @@

) );

require_once( $json_jetpack_endpoints_dir . 'class.jetpack-json-api-translations-endpoint.php' );
require_once( $json_jetpack_endpoints_dir . 'class.jetpack-json-api-translations-modify-endpoint.php' );

new Jetpack_JSON_API_Translations_Endpoint( array(
'description' => 'Gets info about a Jetpack blog\'s core installation',
'method' => 'GET',
'path' => '/sites/%s/translations',
'stat' => 'translations',
'path_labels' => array(
'$site' => '(int|string) The site ID, The site domain'
),
'response_format' => array(
'translations' => '(array) A list of translations that are available',
'autoupdate' => '(bool) Whether or not we automatically update translations',
'log' => '(array:safehtml) An array of log strings.',
),
'example_request_data' => array(
'headers' => array(
'authorization' => 'Bearer YOUR_API_TOKEN'
),
),
'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/example.wordpress.org/translations'
) );

new Jetpack_JSON_API_Translations_Modify_Endpoint( array(
'description' => 'Toggle automatic core updates for a Jetpack blog',
'method' => 'POST',
'path' => '/sites/%s/translations',
'stat' => 'translations',
'path_labels' => array(
'$site' => '(int|string) The site ID, The site domain'
),
'request_format' => array(
'autoupdate' => '(bool) Whether or not we automatically update translations',
),
'response_format' => array(
'translations' => '(array) A list of translations that are available',
'autoupdate' => '(bool) Whether or not we automatically update translations',
),
'example_request_data' => array(
'headers' => array(
'authorization' => 'Bearer YOUR_API_TOKEN'
),
'body' => array(
'autoupdate' => true,
),
),
'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/example.wordpress.org/translations'
) );

new Jetpack_JSON_API_Translations_Modify_Endpoint( array(
'description' => 'Update All Translations installation on a Jetpack blog',
'method' => 'POST',
'path' => '/sites/%s/translations/update',
'stat' => 'translations:update',
'path_labels' => array(
'$site' => '(int|string) The site ID, The site domain'
),
'response_format' => array(
'log' => '(array:safehtml) An array of log strings.',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we also return translations and autoupdate

'success' => '(bool) Was the operation successful'
),
'example_request_data' => array(
'headers' => array(
'authorization' => 'Bearer YOUR_API_TOKEN'
),
),
'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/example.wordpress.org/translations/update'
) );

// Options
require_once( $json_jetpack_endpoints_dir . 'class.wpcom-json-api-get-option-endpoint.php' );

Expand Down
1 change: 1 addition & 0 deletions sync/class.jetpack-sync-defaults.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class Jetpack_Sync_Defaults {
'jetpack_autoupdate_themes',
'jetpack_autoupdate_themes_translations',
'jetpack_autoupdate_core',
'jetpack_autoupdate_translations',
'carousel_background_color',
'carousel_display_exif',
'jetpack_portfolio',
Expand Down
1 change: 1 addition & 0 deletions tests/php/sync/test_class.jetpack-sync-options.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ public function test_sync_default_options() {
'jetpack_autoupdate_themes' => 'pineapple',
'jetpack_autoupdate_themes_translations' => 'pineapple',
'jetpack_autoupdate_core' => 'pineapple',
'jetpack_autoupdate_translations' => 'pineapple',
'carousel_background_color' => 'pineapple',
'carousel_display_exif' => 'pineapple',
'jetpack_portfolio' => 'pineapple',
Expand Down