Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
27.08% covered (danger)
27.08%
13 / 48
33.33% covered (danger)
33.33%
2 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
WPCOM_REST_API_V2_Endpoint_Update_Schedules_Logs
27.08% covered (danger)
27.08%
13 / 48
33.33% covered (danger)
33.33%
2 / 6
57.91
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 register_routes
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 1
2
 create_item_permissions_check
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
3.33
 create_item
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
2
 get_items_permissions_check
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
3.33
 get_items
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * Endpoint to manage plugin and theme update schedules logs.
4 *
5 * Example: https://public-api.wordpress.com/wpcom/v2/update-schedules/$ID/logs
6 *
7 * @package automattic/scheduled-updates
8 */
9
10use Automattic\Jetpack\Scheduled_Updates_Logs;
11
12/**
13 * Class WPCOM_REST_API_V2_Endpoint_Update_Schedules_Logs
14 */
15class WPCOM_REST_API_V2_Endpoint_Update_Schedules_Logs extends WP_REST_Controller {
16    /**
17     * The namespace of this controller's route.
18     *
19     * @var string
20     */
21    public $namespace = 'wpcom/v2';
22
23    /**
24     * The base of this controller's route.
25     *
26     * @var string
27     */
28    public $rest_base = 'update-schedules';
29
30    /**
31     * WPCOM_REST_API_V2_Endpoint_Update_Schedules_Logs constructor.
32     */
33    public function __construct() {
34        add_action( 'rest_api_init', array( $this, 'register_routes' ) );
35    }
36
37    /**
38     * Register routes.
39     */
40    public function register_routes() {
41        register_rest_route(
42            $this->namespace,
43            '/' . $this->rest_base . '/(?P<schedule_id>[\w]+)/logs',
44            array(
45                array(
46                    'methods'             => WP_REST_Server::EDITABLE,
47                    'callback'            => array( $this, 'create_item' ),
48                    'permission_callback' => array( $this, 'create_item_permissions_check' ),
49                    'args'                => array(
50                        'action'  => array(
51                            'description' => 'The action to be logged',
52                            'type'        => 'string',
53                            'required'    => true,
54                            'enum'        => Scheduled_Updates_Logs::ENUM_ACTIONS,
55                        ),
56                        'message' => array(
57                            'description' => 'The message to be logged',
58                            'type'        => 'string',
59                        ),
60                        'context' => array(
61                            'description' => 'The context to be logged',
62                            'type'        => 'object',
63                        ),
64                    ),
65                ),
66                array(
67                    'methods'             => WP_REST_Server::READABLE,
68                    'callback'            => array( $this, 'get_items' ),
69                    'permission_callback' => array( $this, 'get_items_permissions_check' ),
70                ),
71            )
72        );
73    }
74
75    /**
76     * Permission check for adding a log entry
77     *
78     * @param WP_REST_Request $request Request object.
79     * @return bool|WP_Error
80     */
81    public function create_item_permissions_check( $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
82        if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
83            return new WP_Error( 'rest_forbidden', __( 'Sorry, you are not allowed to access this endpoint.', 'jetpack-scheduled-updates' ), array( 'status' => 403 ) );
84        }
85
86        return current_user_can( 'update_plugins' );
87    }
88
89    /**
90     * Adds a log entry to an update schedule.
91     *
92     * @param WP_REST_Request $request Request object.
93     * @return WP_REST_Response|WP_Error The updated event or a WP_Error if the schedule could not be found.
94     */
95    public function create_item( $request ) {
96        $schedule_id = $request['schedule_id'];
97        $action      = $request['action'];
98        $message     = $request['message'];
99        $context     = $request['context'];
100
101        $success = Scheduled_Updates_Logs::log( $schedule_id, $action, $message, $context );
102
103        if ( ! $success ) {
104            return new WP_Error( 'rest_invalid_schedule', __( 'The schedule could not be found.', 'jetpack-scheduled-updates' ), array( 'status' => 404 ) );
105        }
106
107        return rest_ensure_response( true );
108    }
109
110    /**
111     * Permission check for retrieving logs.
112     *
113     * @param WP_REST_Request $request Request object.
114     * @return bool|WP_Error
115     */
116    public function get_items_permissions_check( $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
117        if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
118            return new WP_Error( 'rest_forbidden', __( 'Sorry, you are not allowed to access this endpoint.', 'jetpack-scheduled-updates' ), array( 'status' => 403 ) );
119        }
120
121        return current_user_can( 'update_plugins' );
122    }
123
124    /**
125     * Retrieves logs for a specific schedule.
126     *
127     * @param WP_REST_Request $request Request object.
128     * @return WP_REST_Response|WP_Error The logs for the schedule or a WP_Error if the schedule could not be found.
129     */
130    public function get_items( $request ) {
131        return rest_ensure_response( Scheduled_Updates_Logs::get( $request['schedule_id'] ) );
132    }
133}