Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Scheduled_Updates_Active
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 4
56
0.00% covered (danger)
0.00%
0 / 1
 get
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 update
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 clear
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 add_active_field
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Scheduled Updates Active class
4 *
5 * @package automattic/scheduled-updates
6 */
7
8namespace Automattic\Jetpack;
9
10/**
11 * Scheduled_Updates_Active class
12 *
13 * This class provides static methods to get/save active for scheduled updates.
14 */
15class Scheduled_Updates_Active {
16
17    /**
18     * The name of the WordPress option where the active option is stored.
19     */
20    const OPTION_NAME = 'jetpack_scheduled_update_active';
21
22    /**
23     * Get the active value for a scheduled update.
24     *
25     * @param string $schedule_id Request ID.
26     * @return bool Active value.
27     */
28    public static function get( $schedule_id ) {
29        $option = get_option( self::OPTION_NAME, array() );
30
31        return $option[ $schedule_id ] ?? true;
32    }
33
34    /**
35     * Update the active value for a scheduled update.
36     *
37     * @param string $schedule_id Request ID.
38     * @param bool   $active      Active value.
39     * @return bool
40     */
41    public static function update( $schedule_id, $active ) {
42        $option = get_option( self::OPTION_NAME, array() );
43
44        if ( ! is_array( $option ) ) {
45            $option = array();
46        }
47
48        $option[ $schedule_id ] = $active;
49
50        return update_option( self::OPTION_NAME, $option );
51    }
52
53    /**
54     * Clear the active value for a scheduled update.
55     *
56     * @param string|null $schedule_id Request ID.
57     * @return bool
58     */
59    public static function clear( $schedule_id ) {
60        $option = get_option( self::OPTION_NAME, array() );
61
62        if ( isset( $option[ $schedule_id ] ) ) {
63            unset( $option[ $schedule_id ] );
64        }
65
66        if ( count( $option ) ) {
67            return update_option( self::OPTION_NAME, $option );
68        } else {
69            return delete_option( self::OPTION_NAME );
70        }
71    }
72
73    /**
74     * Registers the active field for the update-schedule REST API.
75     */
76    public static function add_active_field() {
77        register_rest_field(
78            'update-schedule',
79            'active',
80            array(
81                /**
82                 * Populates the active field.
83                 *
84                 * @param array $item Prepared response array.
85                 * @return bool
86                 */
87                'get_callback' => function ( $item ) {
88                    return self::get( $item['schedule_id'] );
89                },
90                'schema'       => array(
91                    'description' => 'Whether the schedule is active or paused.',
92                    'type'        => 'boolean',
93                ),
94            )
95        );
96    }
97}