← All changes
|
json-endpoints/jetpack/class.jetpack-json-api-plugins-endpoint.php
+40
-4
12.7.3
→
16.3-a.1
View file →
| @@ -1,9 +1,14 @@ | ||
| 1 | 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 | 2 | |
| 3 | 3 | use Automattic\Jetpack\Constants; |
| 4 | +use Automattic\Jetpack\Current_Plan; | |
| 4 | 5 | use Automattic\Jetpack\Sync\Functions; |
| 5 | 6 | |
| 7 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 8 | + exit( 0 ); | |
| 9 | +} | |
| 10 | + | |
| 6 | 11 | /** |
| 7 | 12 | * Base class for working with plugins. |
| 8 | 13 | */ |
| 9 | 14 | abstract class Jetpack_JSON_API_Plugins_Endpoint extends Jetpack_JSON_API_Endpoint { |
| @@ -36,8 +41,15 @@ | ||
| 36 | 41 | */ |
| 37 | 42 | protected $log; |
| 38 | 43 | |
| 39 | 44 | /** |
| 45 | + * If the request is a scheduled update. | |
| 46 | + * | |
| 47 | + * @var boolean | |
| 48 | + */ | |
| 49 | + protected $scheduled_update = false; | |
| 50 | + | |
| 51 | + /** | |
| 40 | 52 | * Response format. |
| 41 | 53 | * |
| 42 | 54 | * @var array |
| 43 | 55 | */ |
| @@ -119,8 +131,13 @@ | ||
| 119 | 131 | if ( is_wp_error( $error ) ) { |
| 120 | 132 | return $error; |
| 121 | 133 | } |
| 122 | 134 | |
| 135 | + $error = $this->validate_scheduled_update(); | |
| 136 | + if ( is_wp_error( $error ) ) { | |
| 137 | + return $error; | |
| 138 | + } | |
| 139 | + | |
| 123 | 140 | $args = $this->input(); |
| 124 | 141 | // find out what plugin, or plugins we are dealing with |
| 125 | 142 | // validate the requested plugins |
| 126 | 143 | if ( ! isset( $plugin ) || empty( $plugin ) ) { |
| @@ -155,9 +172,9 @@ | ||
| 155 | 172 | return new WP_Error( 'missing_plugins', __( 'No plugins found.', 'jetpack' ) ); |
| 156 | 173 | } |
| 157 | 174 | foreach ( $this->plugins as $index => $plugin ) { |
| 158 | 175 | if ( ! preg_match( '/\.php$/', $plugin ) ) { |
| 159 | - $plugin = $plugin . '.php'; | |
| 176 | + $plugin .= '.php'; | |
| 160 | 177 | $this->plugins[ $index ] = $plugin; |
| 161 | 178 | } |
| 162 | 179 | $valid = $this->validate_plugin( urldecode( $plugin ) ); |
| 163 | 180 | if ( is_wp_error( $valid ) ) { |
| @@ -275,9 +292,9 @@ | ||
| 275 | 292 | * |
| 276 | 293 | * @return bool |
| 277 | 294 | */ |
| 278 | 295 | protected function plugin_has_translations_autoupdates_enabled( $plugin_file ) { |
| 279 | - return (bool) in_array( $plugin_file, Jetpack_Options::get_option( 'autoupdate_plugins_translations', array() ), true ); | |
| 296 | + return in_array( $plugin_file, Jetpack_Options::get_option( 'autoupdate_plugins_translations', array() ), true ); | |
| 280 | 297 | } |
| 281 | 298 | |
| 282 | 299 | /** |
| 283 | 300 | * Get file mod capabilities. |
| @@ -312,10 +329,10 @@ | ||
| 312 | 329 | } |
| 313 | 330 | } |
| 314 | 331 | |
| 315 | 332 | $file_mod_capabilities = array( |
| 316 | - 'modify_files' => (bool) empty( $reasons_can_not_modify_files ), // install, remove, update | |
| 317 | - 'autoupdate_files' => (bool) empty( $reasons_can_not_modify_files ) && empty( $reasons_can_not_autoupdate ), // enable autoupdates | |
| 333 | + 'modify_files' => empty( $reasons_can_not_modify_files ), // install, remove, update | |
| 334 | + 'autoupdate_files' => empty( $reasons_can_not_modify_files ) && empty( $reasons_can_not_autoupdate ), // enable autoupdates | |
| 318 | 335 | ); |
| 319 | 336 | |
| 320 | 337 | if ( ! empty( $reasons_can_not_modify_files ) ) { |
| 321 | 338 | $file_mod_capabilities['reasons_modify_files_unavailable'] = $reasons_can_not_modify_files; |
| @@ -414,8 +431,27 @@ | ||
| 414 | 431 | |
| 415 | 432 | $error = validate_plugin( $plugin ); |
| 416 | 433 | if ( is_wp_error( $error ) ) { |
| 417 | 434 | return new WP_Error( 'unknown_plugin', $error->get_error_messages(), 404 ); |
| 435 | + } | |
| 436 | + | |
| 437 | + return true; | |
| 438 | + } | |
| 439 | + | |
| 440 | + /** | |
| 441 | + * Validates if scheduled updates are allowed based on the current plan. | |
| 442 | + * | |
| 443 | + * @return bool|WP_Error True if scheduled updates are allowed or not provided, WP_Error otherwise. | |
| 444 | + */ | |
| 445 | + protected function validate_scheduled_update() { | |
| 446 | + $args = $this->input(); | |
| 447 | + | |
| 448 | + if ( isset( $args['scheduled_update'] ) && $args['scheduled_update'] ) { | |
| 449 | + if ( Current_Plan::supports( 'scheduled-updates' ) ) { | |
| 450 | + $this->scheduled_update = true; | |
| 451 | + } else { | |
| 452 | + return new WP_Error( 'unauthorized', __( 'Scheduled updates are not available on your current plan. Please upgrade to a plan that supports scheduled updates to use this feature.', 'jetpack' ), 403 ); | |
| 453 | + } | |
| 418 | 454 | } |
| 419 | 455 | |
| 420 | 456 | return true; |
| 421 | 457 | } |