File: /var/www/linde/wp-content/plugins/lindevr-site-plugin/faqs/rest/posts.php
<?php
// This are modifications to the standard wordpress rest method for posts to extend it with FAQ features
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* Registers custom fields for the native Wordpress REST API response
*/
function register_custom_rest_fields() {
// Register attachments field used on FAQs
register_rest_field('linde_faq', 'attachments', [
'get_callback' => function($post) {
if (!have_rows('file_upload')) {
return array();
}
$attachments = array();
while (have_rows('file_upload')) {
the_row();
$file = get_sub_field('select_file');
$file_url = isset($file['url']) ? $file['url'] : '';
$file_extension = $file_url ? strtolower(pathinfo($file_url, PATHINFO_EXTENSION)) : '';
$file_name = get_sub_field('file_name');
$original_file_name = isset($file['filename']) ? $file['filename'] : '';
$attachments[] = array(
'file' => $file_url,
'file_name' => $file_name,
'original_file_name' => $original_file_name,
'file_extension' => $file_extension,
);
}
return $attachments;
},
'schema' => null,
]);
// Register post visbility acf field used on FAQs
register_rest_field('linde_faq', 'post_visibility', [
'get_callback' => function($post) {
$value = get_field('post_visibility', $post->ID);
if($value){
return $value["value"];
}else{
return null;
}
},
'schema' => null,
]);
// Register featured image renditions field used on posts in general
register_rest_field('linde_faq', 'featured_image', [
'get_callback' => function($post) {
return get_api_image_renditions($post->ID);
},
'schema' => null,
]);
}
add_action('rest_api_init', 'register_custom_rest_fields');
/**
* Modify the REST API response to return both tag names and tag IDs
*/
function modify_rest_post_tags($data, $post, $request) {
// Get the tags associated with the post
$tags = wp_get_post_tags($post->ID);
$results = [];
if ($tags) {
foreach ($tags as $tag) {
$results[] = [
"id" => $tag->term_id,
"name"=> $tag->name
];
}
}
// Replace the tags field in the response with tag names
$data->data['tags'] = $results;
return $data;
}
add_filter('rest_prepare_linde_faq', 'modify_rest_post_tags', 10, 3);
?>