array( 'type' => 'string' ), 'description' => array( 'type' => 'string' ), 'color' => array( 'type' => 'string' ), ); register_rest_route( $this->namespace, '/topics', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_items' ), 'permission_callback' => array( $this, 'permission_community' ), ), array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'create_item' ), 'permission_callback' => array( $this, 'permission_manage' ), 'args' => $fields, ), ) ); register_rest_route( $this->namespace, '/topics/(?P\d+)', array( array( 'methods' => WP_REST_Server::EDITABLE, 'callback' => array( $this, 'update_item' ), 'permission_callback' => array( $this, 'permission_manage' ), 'args' => $fields, ), array( 'methods' => WP_REST_Server::DELETABLE, 'callback' => array( $this, 'delete_item' ), 'permission_callback' => array( $this, 'permission_manage' ), ), ) ); } /** * Lists topics. * * @param WP_REST_Request $request Request. * @return WP_REST_Response */ public function get_items( $request ) { unset( $request ); return rest_ensure_response( Terms::topics() ); } /** * Creates a topic. * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public function create_item( $request ) { $topic = Terms::save_topic( $request->get_params() ); if ( is_wp_error( $topic ) ) { return $topic; } $response = rest_ensure_response( $topic ); $response->set_status( 201 ); return $response; } /** * Updates a topic. * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public function update_item( $request ) { $current = Terms::topic( $request['id'] ); if ( ! $current ) { return new WP_Error( 'spacebay_topic_not_found', __( 'Topic not found.', 'spacebay' ), array( 'status' => 404 ) ); } $topic = Terms::save_topic( array_merge( $current, $request->get_params() ), $current['id'] ); return is_wp_error( $topic ) ? $topic : rest_ensure_response( $topic ); } /** * Deletes a topic. * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public function delete_item( $request ) { if ( ! Terms::delete_topic( $request['id'] ) ) { return new WP_Error( 'spacebay_topic_not_found', __( 'Topic not found.', 'spacebay' ), array( 'status' => 404 ) ); } return rest_ensure_response( array( 'deleted' => true, 'id' => absint( $request['id'] ), ) ); } }