| 1 |
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
exit( 0 ); |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* Themes modify endpoint class. |
| 9 |
* POST /sites/%s/themes/%s |
| 10 |
* POST /sites/%s/themes |
| 11 |
* |
| 12 |
* @phan-constructor-used-for-side-effects |
| 13 |
*/ |
| 14 |
class Jetpack_JSON_API_Themes_Modify_Endpoint extends Jetpack_JSON_API_Themes_Endpoint { |
| 15 |
|
| 16 |
/** |
| 17 |
* Needed capabilities. |
| 18 |
* |
| 19 |
* @var string |
| 20 |
*/ |
| 21 |
protected $needed_capabilities = 'update_themes'; |
| 22 |
|
| 23 |
/** |
| 24 |
* The action. |
| 25 |
* |
| 26 |
* @var string |
| 27 |
*/ |
| 28 |
protected $action = 'default_action'; |
| 29 |
|
| 30 |
/** |
| 31 |
* Expected actions. |
| 32 |
* |
| 33 |
* @var array |
| 34 |
*/ |
| 35 |
protected $expected_actions = array( 'update', 'update_translations' ); |
| 36 |
|
| 37 |
/** |
| 38 |
* The default action. |
| 39 |
* |
| 40 |
* @return bool |
| 41 |
*/ |
| 42 |
public function default_action() { |
| 43 |
$args = $this->input(); |
| 44 |
if ( isset( $args['autoupdate'] ) && is_bool( $args['autoupdate'] ) ) { |
| 45 |
if ( $args['autoupdate'] ) { |
| 46 |
$this->autoupdate_on(); |
| 47 |
} else { |
| 48 |
$this->autoupdate_off(); |
| 49 |
} |
| 50 |
} |
| 51 |
if ( isset( $args['autoupdate_translations'] ) && is_bool( $args['autoupdate_translations'] ) ) { |
| 52 |
if ( $args['autoupdate_translations'] ) { |
| 53 |
$this->autoupdate_translations_on(); |
| 54 |
} else { |
| 55 |
$this->autoupdate_translations_off(); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
return true; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Turn autoupdate on. |
| 64 |
*/ |
| 65 |
public function autoupdate_on() { |
| 66 |
$autoupdate_themes = Jetpack_Options::get_option( 'autoupdate_themes', array() ); |
| 67 |
$autoupdate_themes = array_unique( array_merge( $autoupdate_themes, $this->themes ) ); |
| 68 |
Jetpack_Options::update_option( 'autoupdate_themes', $autoupdate_themes ); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Turn autoupdate off. |
| 73 |
*/ |
| 74 |
public function autoupdate_off() { |
| 75 |
$autoupdate_themes = Jetpack_Options::get_option( 'autoupdate_themes', array() ); |
| 76 |
$autoupdate_themes = array_diff( $autoupdate_themes, $this->themes ); |
| 77 |
Jetpack_Options::update_option( 'autoupdate_themes', $autoupdate_themes ); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Autoupdate translations on. |
| 82 |
*/ |
| 83 |
public function autoupdate_translations_on() { |
| 84 |
$autoupdate_themes_translations = Jetpack_Options::get_option( 'autoupdate_themes_translations', array() ); |
| 85 |
$autoupdate_themes_translations = array_unique( array_merge( $autoupdate_themes_translations, $this->themes ) ); |
| 86 |
Jetpack_Options::update_option( 'autoupdate_themes_translations', $autoupdate_themes_translations ); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Autoupdate translations off. |
| 91 |
*/ |
| 92 |
public function autoupdate_translations_off() { |
| 93 |
$autoupdate_themes_translations = Jetpack_Options::get_option( 'autoupdate_themes_translations', array() ); |
| 94 |
$autoupdate_themes_translations = array_diff( $autoupdate_themes_translations, $this->themes ); |
| 95 |
Jetpack_Options::update_option( 'autoupdate_themes_translations', $autoupdate_themes_translations ); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Update the theme. |
| 100 |
* |
| 101 |
* @return bool|WP_Error True on success, WP_Error on failure. |
| 102 |
*/ |
| 103 |
public function update() { |
| 104 |
include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 105 |
|
| 106 |
// Clear the cache. |
| 107 |
wp_update_themes(); |
| 108 |
|
| 109 |
$result = null; |
| 110 |
foreach ( $this->themes as $theme ) { |
| 111 |
/** |
| 112 |
* Pre-upgrade action |
| 113 |
* |
| 114 |
* @since 3.9.3 |
| 115 |
* |
| 116 |
* @param object $theme WP_Theme object |
| 117 |
* @param array $themes Array of theme objects |
| 118 |
*/ |
| 119 |
do_action( 'jetpack_pre_theme_upgrade', $theme, $this->themes ); |
| 120 |
// Objects created inside the for loop to clean the messages for each theme |
| 121 |
$skin = new Automatic_Upgrader_Skin(); |
| 122 |
$upgrader = new Theme_Upgrader( $skin ); |
| 123 |
$upgrader->init(); |
| 124 |
$result = $upgrader->upgrade( $theme ); |
| 125 |
$this->log[ $theme ][] = $upgrader->skin->get_upgrade_messages(); |
| 126 |
} |
| 127 |
|
| 128 |
if ( ! $this->bulk && ! $result ) { |
| 129 |
return new WP_Error( 'update_fail', __( 'There was an error updating your theme', 'jetpack' ), 400 ); |
| 130 |
} |
| 131 |
|
| 132 |
return true; |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Update translations. |
| 137 |
* |
| 138 |
* @return bool|WP_Error |
| 139 |
*/ |
| 140 |
public function update_translations() { |
| 141 |
include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 142 |
|
| 143 |
// Clear the cache. |
| 144 |
wp_update_themes(); |
| 145 |
|
| 146 |
$available_themes_updates = get_site_transient( 'update_themes' ); |
| 147 |
|
| 148 |
if ( ! isset( $available_themes_updates->translations ) || empty( $available_themes_updates->translations ) ) { |
| 149 |
return new WP_Error( 'nothing_to_translate' ); |
| 150 |
} |
| 151 |
|
| 152 |
$result = null; |
| 153 |
foreach ( $available_themes_updates->translations as $translation ) { |
| 154 |
$theme = $translation['slug']; |
| 155 |
if ( ! in_array( $translation['slug'], $this->themes, true ) ) { |
| 156 |
$this->log[ $theme ][] = __( 'No update needed', 'jetpack' ); |
| 157 |
continue; |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Pre-upgrade action |
| 162 |
* |
| 163 |
* @since 4.4.0 |
| 164 |
* |
| 165 |
* @param object $theme WP_Theme object |
| 166 |
* @param array $themes Array of theme objects |
| 167 |
*/ |
| 168 |
do_action( 'jetpack_pre_theme_upgrade_translations', $theme, $this->themes ); |
| 169 |
// Objects created inside the for loop to clean the messages for each theme |
| 170 |
$skin = new Automatic_Upgrader_Skin(); |
| 171 |
$upgrader = new Language_Pack_Upgrader( $skin ); |
| 172 |
$upgrader->init(); |
| 173 |
|
| 174 |
$result = $upgrader->upgrade( (object) $translation ); |
| 175 |
$this->log[ $theme ] = $upgrader->skin->get_upgrade_messages(); |
| 176 |
} |
| 177 |
|
| 178 |
if ( ! $this->bulk && ! $result ) { |
| 179 |
return new WP_Error( 'update_fail', __( 'There was an error updating your theme', 'jetpack' ), 400 ); |
| 180 |
} |
| 181 |
|
| 182 |
return true; |
| 183 |
} |
| 184 |
} |
| 185 |
|