Last active
June 3, 2024 18:45
-
-
Save geoffyuen/85aded92ca8b51dfd94abb5f7b6b6486 to your computer and use it in GitHub Desktop.
For REST API, when requesting a post, create an array to the blocks and also evaluate the ACF fields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// This doesn't really work just yet | |
add_action( | |
'rest_api_init', | |
function () { | |
if (!function_exists('use_block_editor_for_post_type')) { | |
require ABSPATH . 'wp-admin/includes/post.php'; | |
} | |
// Surface all Gutenberg blocks in the WordPress REST API | |
$post_types = get_post_types_by_support(['editor']); | |
foreach ($post_types as $post_type) { | |
if (use_block_editor_for_post_type($post_type)) { | |
// https://developer.wordpress.org/reference/functions/register_rest_field/ | |
register_rest_field($post_type, 'contentraw', ['get_callback' => function (array $post) { | |
return $post['content']['raw']; | |
}]); | |
register_rest_field( | |
$post_type, | |
'all_blocks', // don't call this 'blocks' or else you can't edit them. I don't get it either. | |
[ | |
'get_callback' => function (array $post) { | |
// get the blocks | |
$blocks = parse_blocks($post['content']['raw']); | |
// Remove the null blocks - why does wordpress have these??!? | |
// This breaks the WP editor - do not do this... | |
// Will have to filter this out on the frontend. | |
// $blocks = array_filter($blocks, function ($var) { | |
// return !empty($var['blockName']); | |
// }); | |
// run get_field on acf fields | |
foreach ($blocks as &$block) { | |
if (array_key_exists('data', $block['attrs'])) { | |
foreach ($block['attrs']['data'] as $fieldname => &$value) { | |
// $block['attrs']['data'][$fieldname] = wp_kses_post(get_field($value)); | |
// if _fieldname then do fieldname = get_field(value) | |
if (is_string($value) and str_starts_with($value, 'field_')) { | |
$realfield = substr($fieldname, 1); // remove the underscore to get the real fieldname | |
// get_field() always produces null | |
$block['attrs']['data'][$realfield] = wp_kses_post( get_field( $block['attrs']['data'][$realfield] ) ); | |
// $block['attrs']['data'][$realfield] = "this will work, above will = null"; | |
} | |
// Alternative take: using Wordpress functions work | |
// So now we have to enforce rules on field names so we know what to do: | |
// ACF Image field - Possible to cross-refence the fieldname to the acf-json/* files? | |
if (false) { | |
if (str_starts_with($fieldname, 'image')) { | |
$block['acf_image'] = get_field( $block['attrs']['data'][$fieldname] ); | |
$block['attrs']['data'][$fieldname] = hatch_get_image($value); | |
} | |
} | |
} | |
} | |
} | |
return $blocks; | |
}, | |
] | |
); | |
} | |
} | |
// If we want to have a flag existence of blocks like that other plugin... | |
// register_rest_field($post_type, 'has_blocks', ['get_callback' => function (array $post) { | |
// $blocks = parse_blocks($post['content']['raw']); | |
// $blocks = array_filter($blocks, function ($var) { | |
// return !empty($var['blockName']); | |
// }); | |
// return count($blocks) > 0; | |
// }]); | |
// Featured images for pages and posts | |
add_action('rest_api_init', function () { | |
register_rest_field('pages', 'featured_image_src', array( | |
'get_callback' => function ($post_arr) { | |
$image_src_arr = wp_get_attachment_image_src($post_arr['featured_media'], 'medium'); | |
return $image_src_arr[0]; | |
}, | |
'update_callback' => null, | |
'schema' => null | |
)); | |
}); | |
add_action('rest_api_init', function () { | |
register_rest_field('post', 'featured_image_src', array( | |
'get_callback' => function ($post_arr) { | |
$image_src_arr = wp_get_attachment_image_src($post_arr['featured_media'], 'medium'); | |
return $image_src_arr[0]; | |
}, | |
'update_callback' => null, | |
'schema' => null | |
)); | |
}); | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment