'integer' ); register_rest_route( Plugin::REST_NAMESPACE, '/content/(?P\d+)/blocks', array( 'methods' => \WP_REST_Server::READABLE, 'callback' => array( $this, 'blocks' ), 'permission_callback' => array( Posts::class, 'can_edit' ), 'args' => array( 'id' => $id ), ) ); register_rest_route( Plugin::REST_NAMESPACE, '/content/(?P\d+)/operations', array( 'methods' => \WP_REST_Server::CREATABLE, 'callback' => array( $this, 'operations' ), 'permission_callback' => array( Posts::class, 'can_edit' ), 'args' => array( 'id' => $id, // The snapshot's modified_gmt. `modified_gmt` is accepted // as another name for it. 'expected_modified_gmt' => array( 'type' => 'string' ), 'modified_gmt' => array( 'type' => 'string' ), 'change_id' => self::change_id_schema(), 'op_key' => OpKeys::schema( true ), 'operations' => array( 'type' => 'array', 'required' => true, 'items' => array( 'type' => 'object' ), ), ), ) ); register_rest_route( Plugin::REST_NAMESPACE, '/content/(?P\d+)/copy-blocks', array( 'methods' => \WP_REST_Server::CREATABLE, 'callback' => array( $this, 'copy_blocks' ), 'permission_callback' => array( Posts::class, 'can_edit' ), 'args' => array( 'id' => $id, 'expected_modified_gmt' => array( 'type' => 'string', 'required' => true, ), 'source_id' => array( 'type' => 'integer', 'required' => true, ), 'source_expected_modified_gmt' => array( 'type' => 'string', 'required' => true, ), 'after' => array( 'type' => 'string', 'required' => true, ), 'after_hash' => array( 'type' => 'string', 'required' => true, ), 'blocks' => array( 'type' => 'array', 'required' => true, 'items' => array( 'type' => 'object' ), ), 'change_id' => self::change_id_schema(), 'op_key' => OpKeys::schema( true ), ), ) ); register_rest_route( Plugin::REST_NAMESPACE, '/content/(?P\d+)/restore', array( 'methods' => \WP_REST_Server::CREATABLE, 'callback' => array( $this, 'restore' ), 'permission_callback' => array( Posts::class, 'can_edit' ), 'args' => array( 'id' => $id, 'expected_modified_gmt' => array( 'type' => 'string', 'required' => true, ), // The op key of the write to undo; without it, the whole // change named by change_id is undone. 'undoes' => OpKeys::schema( false ), 'change_id' => array( 'type' => 'string', 'pattern' => '^[A-Za-z0-9_-]{1,64}$', ), 'op_key' => OpKeys::schema( true ), ), ) ); } /** * The schema of a `change_id` argument, as the edit route has it. * * @return array */ public static function change_id_schema() { return array( 'type' => 'string', 'required' => true, 'pattern' => '^[A-Za-z0-9_-]{1,64}$', ); } /** * Returns the post's snapshot. * * @param \WP_REST_Request $request Request. * @return \WP_REST_Response|\WP_Error */ public function blocks( \WP_REST_Request $request ) { $post = Posts::supported( (int) $request['id'] ); if ( is_wp_error( $post ) ) { return $post; } return rest_ensure_response( Snapshot::of( $post ) ); } /** * Applies operations. * * @param \WP_REST_Request $request Request. * @return \WP_REST_Response|\WP_Error */ public function operations( \WP_REST_Request $request ) { $post = Posts::supported( (int) $request['id'] ); if ( is_wp_error( $post ) ) { return $post; } $replay = OpKeys::replay( $request['op_key'], 'operations', $post->ID ); if ( null !== $replay ) { return Posts::respond( $replay ); } $expected = null !== $request['expected_modified_gmt'] ? $request['expected_modified_gmt'] : $request['modified_gmt']; return Posts::respond( ContentOperations::apply( $post, array( 'expected_modified_gmt' => (string) $expected, 'change_id' => (string) $request['change_id'], 'op_key' => (string) $request['op_key'], 'operations' => $request['operations'], ) ) ); } /** * Copies blocks from another post. * * @param \WP_REST_Request $request Request. * @return \WP_REST_Response|\WP_Error */ public function copy_blocks( \WP_REST_Request $request ) { $post = Posts::supported( (int) $request['id'] ); if ( is_wp_error( $post ) ) { return $post; } $replay = OpKeys::replay( $request['op_key'], 'copy_blocks', $post->ID ); if ( null !== $replay ) { return Posts::respond( $replay ); } $source = Posts::supported( (int) $request['source_id'] ); if ( is_wp_error( $source ) ) { return $source; } if ( ! current_user_can( 'edit_post', $source->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( BlockCopier::copy( $post, $source, array( 'expected_modified_gmt' => (string) $request['expected_modified_gmt'], 'source_expected_modified_gmt' => (string) $request['source_expected_modified_gmt'], 'after' => (string) $request['after'], 'after_hash' => (string) $request['after_hash'], 'blocks' => $request['blocks'], 'change_id' => (string) $request['change_id'], 'op_key' => (string) $request['op_key'], ) ) ); } /** * Restores the post as it was before a change. * * @param \WP_REST_Request $request Request. * @return \WP_REST_Response|\WP_Error */ public function restore( \WP_REST_Request $request ) { $post = Posts::supported( (int) $request['id'] ); if ( is_wp_error( $post ) ) { return $post; } $replay = OpKeys::replay( $request['op_key'], 'restore', $post->ID ); if ( null !== $replay ) { return Posts::respond( $replay ); } if ( null === $request['undoes'] && null === $request['change_id'] ) { return new \WP_Error( 'rest_missing_callback_param', __( 'Either undoes or change_id is required.', 'searchsmith' ), array( 'status' => 400 ) ); } return Posts::respond( RevisionRestorer::restore( $post, array( 'undoes' => (string) $request['undoes'], 'expected_modified_gmt' => (string) $request['expected_modified_gmt'], 'change_id' => (string) $request['change_id'], 'op_key' => (string) $request['op_key'], ) ) ); } }