\WP_REST_Server::READABLE, 'callback' => array( $this, 'index' ), 'permission_callback' => $can_manage, 'args' => array( // A URL on this site or a path from its root, to check // for a redirect plugin's conflict before a merge. 'path' => array( 'type' => 'string' ), ), ), array( 'methods' => \WP_REST_Server::CREATABLE, 'callback' => array( $this, 'create' ), 'permission_callback' => $can_manage, 'args' => array( 'from' => array( 'type' => 'string', 'required' => true, ), 'to_post_id' => array( 'type' => 'integer', 'required' => true, ), 'op_key' => OpKeys::schema( true ), ), ), array( 'methods' => \WP_REST_Server::DELETABLE, 'callback' => array( $this, 'delete' ), 'permission_callback' => $can_manage, 'args' => array( 'id' => array( 'type' => 'integer', 'required' => true, ), 'op_key' => OpKeys::schema( true ), ), ), ) ); register_rest_route( Plugin::REST_NAMESPACE, '/redirects/(?P\d+)', array( 'methods' => \WP_REST_Server::DELETABLE, 'callback' => array( $this, 'delete' ), 'permission_callback' => $can_manage, 'args' => array( 'id' => array( 'type' => 'integer' ), 'op_key' => OpKeys::schema( true ), ), ) ); } /** * Lists the connector's redirects, and the redirect plugins on the site. * * With a `path`, it also says whether a redirect plugin already redirects * that address, so Blaze can refuse a merge before it copies anything. * `POST /redirects` still refuses a conflict, as a backstop. * * @param \WP_REST_Request $request Request. * @return \WP_REST_Response|\WP_Error */ public function index( \WP_REST_Request $request ) { $answer = array( 'redirects' => Redirects::all(), 'redirect_plugins' => Redirects::active_plugins(), ); if ( null !== $request['path'] ) { $path = Redirects::path_on_site( (string) $request['path'] ); if ( ! is_string( $path ) ) { return new \WP_Error( 'searchsmith_invalid_path', __( 'The path is not an address on this site other than its home page.', 'searchsmith' ), array( 'status' => 400 ) ); } $plugin = Redirects::conflict( $path ); $answer['path'] = $path; $answer['conflict'] = $plugin ? array( 'plugin' => $plugin ) : null; } return rest_ensure_response( $answer ); } /** * Adds a redirect. * * @param \WP_REST_Request $request Request. * @return \WP_REST_Response|\WP_Error */ public function create( \WP_REST_Request $request ) { $target = Posts::supported( (int) $request['to_post_id'] ); if ( is_wp_error( $target ) ) { return $target; } $replay = OpKeys::replay( $request['op_key'], 'add_redirect', $target->ID ); if ( null !== $replay ) { return Posts::respond( $replay ); } if ( ! current_user_can( 'edit_post', $target->ID ) ) { return new \WP_Error( 'rest_forbidden', __( 'Sorry, you are not allowed to do that.', 'searchsmith' ), array( 'status' => rest_authorization_required_code() ) ); } return Posts::respond( Redirects::add( (string) $request['from'], $target, (string) $request['op_key'] ) ); } /** * Removes a redirect. * * @param \WP_REST_Request $request Request. * @return \WP_REST_Response|\WP_Error */ public function delete( \WP_REST_Request $request ) { $replay = OpKeys::replay( $request['op_key'], 'remove_redirect', 0 ); if ( null !== $replay ) { return Posts::respond( $replay ); } $redirect = get_post( (int) $request['id'] ); if ( ! $redirect || Redirects::POST_TYPE !== $redirect->post_type ) { return new \WP_Error( 'searchsmith_not_found', __( 'No such redirect.', 'searchsmith' ), array( 'status' => 404 ) ); } return Posts::respond( Redirects::remove( $redirect, (string) $request['op_key'] ) ); } }