\WP_REST_Server::READABLE, 'callback' => array( $this, 'get_status' ), 'permission_callback' => array( $this, 'can_view_status' ), ) ); register_rest_route( self::NS, '/report', array( 'methods' => \WP_REST_Server::READABLE, 'callback' => array( $this, 'get_report' ), 'permission_callback' => array( $this, 'can_view_report' ), 'args' => array( 'days' => array( 'default' => 30, 'sanitize_callback' => 'absint', 'validate_callback' => static function ( $value ) { return is_numeric( $value ) && (int) $value > 0 && (int) $value <= 365; }, ), ), ) ); register_rest_route( self::NS, '/check', array( 'methods' => \WP_REST_Server::CREATABLE, 'callback' => array( $this, 'post_check' ), 'permission_callback' => array( $this, 'can_manage' ), ) ); } /** * GET /status * * @since 0.1.0 * * @return \WP_REST_Response */ public function get_status() { $status = StatusLights::get_status(); $data = array( 'level' => $status['level'], 'label' => $status['label'], 'message' => $status['message'], 'last_check' => array( 'timestamp' => (int) $status['last_check'], 'human' => $status['last_check_human'], ), 'site' => array( 'name' => wp_specialchars_decode( (string) get_bloginfo( 'name' ), ENT_QUOTES ), 'url' => home_url( '/' ), ), 'agency' => array( 'name' => (string) Options::get( 'agency_name' ), 'url' => (string) Options::get( 'agency_url' ), ), ); // Only expose the plugin version to the agency, not to every logged-in user. if ( current_user_can( 'qa_manage_settings' ) ) { $data['version'] = QUIETADMIN_VERSION; $data['checks'] = isset( $status['checks'] ) ? $status['checks'] : array(); } return rest_ensure_response( $data ); } /** * GET /report * * @since 0.1.0 * * @param \WP_REST_Request $request Request. * @return \WP_REST_Response */ public function get_report( $request ) { $days = (int) $request->get_param( 'days' ); $range = $days ? Reports::recent_range( $days ) : Reports::last_month_range(); $summary = Reports::summary_for_period( $range ); unset( $summary['events'] ); return rest_ensure_response( $summary ); } /** * POST /check - trigger the local system check. * * @since 0.1.0 * * @return \WP_REST_Response */ public function post_check() { StatusLights::run_checks(); $status = StatusLights::get_status(); Tracker::log( Event_Log::TYPE_MAINTENANCE, 'external_check', __( 'System check performed (triggered via REST)', 'oms-quietadmin' ) ); return rest_ensure_response( array( 'updated' => true, 'last_check' => StatusLights::last_check(), 'level' => $status['level'], 'label' => $status['label'], ) ); } /** * Permission: read status. * * @since 0.1.0 * * @return bool|\WP_Error */ public function can_view_status() { if ( current_user_can( 'qa_view_dashboard' ) || current_user_can( 'read' ) ) { return true; } return new \WP_Error( 'quietadmin_forbidden', __( 'You are not allowed to read the status.', 'oms-quietadmin' ), array( 'status' => 403 ) ); } /** * Permission: read report. * * @since 0.1.0 * * @return bool|\WP_Error */ public function can_view_report() { if ( current_user_can( 'qa_view_reports' ) ) { return true; } return new \WP_Error( 'quietadmin_forbidden', __( 'You are not allowed to read the report.', 'oms-quietadmin' ), array( 'status' => 403 ) ); } /** * Permission: trigger settings/checks. * * @since 0.1.0 * * @return bool|\WP_Error */ public function can_manage() { if ( current_user_can( 'qa_manage_settings' ) ) { return true; } return new \WP_Error( 'quietadmin_forbidden', __( 'You are not allowed to perform this action.', 'oms-quietadmin' ), array( 'status' => 403 ) ); } }