Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
96.00% |
72 / 75 |
|
83.33% |
5 / 6 |
CRAP | |
0.00% |
0 / 1 |
| Debug_REST_Controller | |
98.63% |
72 / 73 |
|
83.33% |
5 / 6 |
8 | |
0.00% |
0 / 1 |
| register_routes | |
100.00% |
57 / 57 |
|
100.00% |
1 / 1 |
2 | |||
| get_entries | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| get_entry | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
2.06 | |||
| get_text | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| clear | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| filters | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * REST routes for the debug log. |
| 4 | * |
| 5 | * @package automattic/jetpack-send |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\Send; |
| 9 | |
| 10 | use WP_Error; |
| 11 | use WP_REST_Request; |
| 12 | use WP_REST_Server; |
| 13 | |
| 14 | if ( ! defined( 'ABSPATH' ) ) { |
| 15 | exit( 0 ); |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * `jetpack-send/v1/debug-log` routes; all require `manage_options`. There's no public link to the entries. |
| 20 | */ |
| 21 | class Debug_REST_Controller { |
| 22 | |
| 23 | /** |
| 24 | * Register the routes. |
| 25 | * |
| 26 | * @return void |
| 27 | */ |
| 28 | public static function register_routes() { |
| 29 | $filters = array( |
| 30 | 'search' => array( |
| 31 | 'type' => 'string', |
| 32 | 'maxLength' => 200, |
| 33 | 'default' => '', |
| 34 | ), |
| 35 | 'level' => REST_Controller::list_arg( Debug_Log::LEVELS ), |
| 36 | 'area' => REST_Controller::list_arg( Debug_Log::AREAS ), |
| 37 | 'email' => array( |
| 38 | 'type' => 'integer', |
| 39 | 'minimum' => 0, |
| 40 | 'default' => 0, |
| 41 | ), |
| 42 | ); |
| 43 | $routes = array( |
| 44 | '/debug-log' => array( |
| 45 | WP_REST_Server::READABLE, |
| 46 | 'get_entries', |
| 47 | $filters + array( |
| 48 | 'page' => array( |
| 49 | 'type' => 'integer', |
| 50 | 'minimum' => 1, |
| 51 | 'default' => 1, |
| 52 | ), |
| 53 | 'per_page' => array( |
| 54 | 'type' => 'integer', |
| 55 | 'minimum' => 1, |
| 56 | 'maximum' => 100, |
| 57 | 'default' => 20, |
| 58 | ), |
| 59 | ), |
| 60 | ), |
| 61 | '/debug-log/(?P<id>\d+)' => array( |
| 62 | WP_REST_Server::READABLE, |
| 63 | 'get_entry', |
| 64 | array( |
| 65 | 'id' => array( |
| 66 | 'type' => 'integer', |
| 67 | 'minimum' => 1, |
| 68 | ), |
| 69 | ), |
| 70 | ), |
| 71 | '/debug-log/text' => array( WP_REST_Server::READABLE, 'get_text', $filters ), |
| 72 | '/debug-log/clear' => array( WP_REST_Server::CREATABLE, 'clear', array() ), |
| 73 | ); |
| 74 | foreach ( $routes as $route => $definition ) { |
| 75 | list( $methods, $callback, $args ) = $definition; |
| 76 | register_rest_route( |
| 77 | REST_Controller::API_NAMESPACE, |
| 78 | $route, |
| 79 | array( |
| 80 | 'methods' => $methods, |
| 81 | 'callback' => array( __CLASS__, $callback ), |
| 82 | 'permission_callback' => array( REST_Controller::class, 'can_manage' ), |
| 83 | 'args' => $args, |
| 84 | ) |
| 85 | ); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * GET /debug-log: a page of entries, newest first. |
| 91 | * |
| 92 | * @param WP_REST_Request $request Request. |
| 93 | * @return \WP_REST_Response |
| 94 | */ |
| 95 | public static function get_entries( WP_REST_Request $request ) { |
| 96 | $args = self::filters( $request ); |
| 97 | $args['page'] = (int) $request->get_param( 'page' ); |
| 98 | $args['per_page'] = (int) $request->get_param( 'per_page' ); |
| 99 | return rest_ensure_response( Debug_Log::query( $args ) ); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * GET /debug-log/{id}. |
| 104 | * |
| 105 | * @param WP_REST_Request $request Request. |
| 106 | * @return \WP_REST_Response|WP_Error |
| 107 | */ |
| 108 | public static function get_entry( WP_REST_Request $request ) { |
| 109 | $entry = Debug_Log::get( (int) $request->get_param( 'id' ) ); |
| 110 | if ( ! $entry ) { |
| 111 | return new WP_Error( 'jetpack_send_debug_entry_not_found', __( 'This entry is no longer in the debug log.', 'jetpack-send-pkg' ), array( 'status' => 404 ) ); |
| 112 | } |
| 113 | return rest_ensure_response( $entry ); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * GET /debug-log/text: the newest 200 matching entries as plain text, for support. |
| 118 | * |
| 119 | * @param WP_REST_Request $request Request. |
| 120 | * @return \WP_REST_Response |
| 121 | */ |
| 122 | public static function get_text( WP_REST_Request $request ) { |
| 123 | return rest_ensure_response( Debug_Log::text( self::filters( $request ) ) ); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * POST /debug-log/clear: delete every entry. |
| 128 | * |
| 129 | * @return \WP_REST_Response |
| 130 | */ |
| 131 | public static function clear() { |
| 132 | return rest_ensure_response( array( 'deleted' => Debug_Log::clear() ) ); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * The list's filters from a request. |
| 137 | * |
| 138 | * @param WP_REST_Request $request Request. |
| 139 | * @return array |
| 140 | */ |
| 141 | private static function filters( WP_REST_Request $request ) { |
| 142 | return array( |
| 143 | 'search' => (string) $request->get_param( 'search' ), |
| 144 | 'level' => (array) $request->get_param( 'level' ), |
| 145 | 'area' => (array) $request->get_param( 'area' ), |
| 146 | 'email' => (int) $request->get_param( 'email' ), |
| 147 | ); |
| 148 | } |
| 149 | } |