HEX
Server: Apache/2.4.41 (Ubuntu)
System: Linux wordpress-ubuntu-s-2vcpu-4gb-fra1-01 5.4.0-169-generic #187-Ubuntu SMP Thu Nov 23 14:52:28 UTC 2023 x86_64
User: root (0)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
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);


?>