Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block added to revisions endpoint #30

Open
adambrgmn opened this issue Jan 13, 2022 · 1 comment
Open

Block added to revisions endpoint #30

adambrgmn opened this issue Jan 13, 2022 · 1 comment

Comments

@adambrgmn
Copy link

Hey! First, thanks for this awesome little library! Real easy to follow along in the code as well 👏
I'm just wondering if you know if it is possible to also add the blocks data to the revisions endpoints (/wp/v2/<post_type>/revisions/<id>)?

@adambrgmn
Copy link
Author

Btw while testing this out I found a way to do it. But it is not as good as the one used for "normal" post types:

\add_filter(
    'rest_prepare_revision',
    function (\WP_REST_Response $response, \WP_Post $post) {
        $has_blocks = \has_blocks($post);
        $blocks = [];
        if ($has_blocks) {
            $blocks = \WP_REST_Blocks\Data\get_blocks(
                $post->post_content,
                $post->ID
            );
        }

        $response->set_data(
            array_merge($response->get_data(), [
                'has_blocks' => $has_blocks,
                'blocks' => $blocks,
            ])
        );

        return $response;
    },
    10,
    2
);

This doesn't add the field to the api schema or anything. But for now this works for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant