namespace = PAGECORE_REST_NAMESPACE; } /** * Attach route registration. * * @return void */ public function register() { add_action( 'rest_api_init', array( $this, 'register_routes' ) ); } /** * Capability a request must satisfy. * * Override in a controller that needs something other than plugin admin. * * @return string */ protected function get_required_capability() { return Capabilities::manage(); } /** * Standard permission callback for a private route. * * @param \WP_REST_Request $request Request. * @return true|\WP_Error */ public function check_permission( $request ) { unset( $request ); if ( ! is_user_logged_in() ) { return new \WP_Error( 'pagecore_not_logged_in', __( 'You must be signed in to do that.', 'pagecore' ), array( 'status' => rest_authorization_required_code() ) ); } if ( ! current_user_can( $this->get_required_capability() ) ) { return new \WP_Error( 'pagecore_forbidden', __( 'Your account does not have permission to do that.', 'pagecore' ), array( 'status' => rest_authorization_required_code() ) ); } return true; } /** * Build an error response. * * @param string $code Machine readable code, without the plugin prefix. * @param string $message Human readable message. * @param int $status HTTP status code. * @param array $data Extra data merged into the error payload. * @return \WP_Error */ protected function error( $code, $message, $status = 400, $data = array() ) { return new \WP_Error( 'pagecore_' . $code, $message, array_merge( array( 'status' => $status ), $data ) ); } /** * Build a success response. * * @param mixed $data Payload. * @return \WP_REST_Response */ protected function ok( $data = array() ) { return new \WP_REST_Response( $data, 200 ); } }