Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
91.84% covered (success)
91.84%
45 / 49
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Retention_Bridge
95.74% covered (success)
95.74%
45 / 47
50.00% covered (danger)
50.00%
1 / 2
9
0.00% covered (danger)
0.00%
0 / 1
 register_routes
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
1
 update_retention
93.33% covered (success)
93.33%
28 / 30
0.00% covered (danger)
0.00%
0 / 1
8.02
1<?php
2/**
3 * Retention REST bridge — changes how many days of backups WordPress.com keeps.
4 *
5 * @package automattic/jetpack-backup-plugin
6 */
7
8namespace Automattic\Jetpack\Backup\V0005\REST;
9
10use Automattic\Jetpack\Backup\V0005\Jetpack_Backup;
11use Automattic\Jetpack\Connection\Client;
12use WP_Error;
13use WP_REST_Request;
14use WP_REST_Server;
15
16if ( ! defined( 'ABSPATH' ) ) {
17    exit( 0 );
18}
19
20/**
21 * Writes the retention period. The read is `retention_days` on the legacy `/site/backup/size`.
22 */
23class Retention_Bridge {
24
25    /**
26     * The periods Jetpack Cloud offers. WordPress.com also accepts 2, which only its own downgrade sets.
27     *
28     * @var int[]
29     */
30    const RETENTION_DAYS = array( 7, 30, 120, 365 );
31
32    /**
33     * Register the POST /jetpack/v4/site/backup/retention route.
34     *
35     * @return void
36     */
37    public static function register_routes() {
38        register_rest_route(
39            'jetpack/v4',
40            '/site/backup/retention',
41            array(
42                'methods'             => WP_REST_Server::CREATABLE,
43                'callback'            => array( __CLASS__, 'update_retention' ),
44                'permission_callback' => array( Rest_Controller::class, 'permission_check' ),
45                'args'                => array(
46                    'retention_days' => array(
47                        'description' => __( 'Days of backups to keep.', 'jetpack-backup-pkg' ),
48                        'type'        => 'integer',
49                        'required'    => true,
50                        'enum'        => self::RETENTION_DAYS,
51                    ),
52                ),
53            )
54        );
55    }
56
57    /**
58     * Proxy `POST /sites/{id}/backup/retention/update` (v2). It refuses blog tokens, so this signs as the user.
59     *
60     * @param WP_REST_Request $request The request.
61     * @return \WP_REST_Response|WP_Error The new retention, or WP_Error on failure.
62     */
63    public static function update_retention( WP_REST_Request $request ) {
64        $blog_id = Rest_Controller::get_blog_id_or_error();
65        if ( is_wp_error( $blog_id ) ) {
66            return $blog_id;
67        }
68
69        $days = (int) $request->get_param( 'retention_days' );
70
71        $response = Client::wpcom_json_api_request_as_user(
72            sprintf( '/sites/%d/backup/retention/update', $blog_id ),
73            'v2',
74            array( 'method' => 'POST' ),
75            array( 'retention_days' => $days ),
76            'wpcom'
77        );
78
79        // Whatever the outcome: the dashboard re-reads `/site/backup/size` for `retention_days` either way.
80        Jetpack_Backup::clear_storage_cache();
81
82        if ( is_wp_error( $response ) ) {
83            return Rest_Controller::transport_error( $response, 'retention_update_failed' );
84        }
85
86        $message = __( 'Could not change how long backups are kept.', 'jetpack-backup-pkg' );
87
88        if ( 200 !== (int) wp_remote_retrieve_response_code( $response ) ) {
89            return Rest_Controller::upstream_error( $response, 'retention_update_failed', $message );
90        }
91
92        $decoded = json_decode( wp_remote_retrieve_body( $response ), true );
93
94        // A VaultPress refusal arrives as a 200 with `success: false`.
95        if ( ! is_array( $decoded ) || empty( $decoded['success'] ) ) {
96            $data   = array( 'status' => 500 );
97            $reason = Rest_Controller::upstream_reason( is_array( $decoded ) ? $decoded : array() );
98            if ( ! empty( $reason ) ) {
99                $data['wpcom'] = $reason;
100            }
101
102            return new WP_Error( 'retention_update_failed', $message, $data );
103        }
104
105        return rest_ensure_response(
106            array(
107                'ok'             => true,
108                'retention_days' => $days,
109            )
110        );
111    }
112}