<?php
/**
 * Functions for customizing the WordPress API.
 * @package MarComm-AUX
 * @link https://developer.wordpress.org/rest-api/
 * @link https://developer.wordpress.com/docs/api/
 *
 * This file is included by the AUX parent theme functions.php.
 * It can be appended to by a file of the same name in a child theme.
 *
 * @since 1.0.0
 */

// Exit if accessed directly
if (!defined("ABSPATH")) {
  exit();
}

// The PostService class provides the pageReadTime method.
use WP_AUX\Services\PostService;

/**
 * Add API fields for categories and tag names and slugs.
 * By default, the API only returns IDs for taxonomies.
 *
 * Access by adding category_names and tag_names to the REST API response. Example:
 * https://(site url).tamu.edu/wp-json/wp/v2/news?per_page=20&_fields=id,title,link,date,modified,category_names,tag_names,read_time,featured_img_lg,featured_img_md,featured_img_thumb,excerpt,content
 *
 * @since 1.0.0
 */
add_action('rest_api_init', function() {
  // Get all public post types
  $post_types = get_post_types(['public' => true]);

  // Loop through each post type and add fields if they support categories or tags
  foreach ($post_types as $post_type) {
    // Output category name and slug if the post type supports the category taxonomy
    if (is_object_in_taxonomy($post_type, 'category')) {
      register_rest_field($post_type, 'category_names', [
        'get_callback' => function($post) {
          $categories = wp_get_post_categories($post['id'], ['fields' => 'all']);
          return array_map(function($category) {
            return [
              'name' => $category->name,
              'slug' => $category->slug
            ];
          }, $categories);
        }
      ]);
    }

    // Output tag name and slug if the post type supports the tag taxonomy
    if (is_object_in_taxonomy($post_type, 'post_tag')) {
      register_rest_field($post_type, 'tag_names', [
        'get_callback' => function($post) {
          $tags = wp_get_post_tags($post['id'], ['fields' => 'all']);
          return array_map(function($tag) {
            return [
              'name' => $tag->name,
              'slug' => $tag->slug
            ];
          }, $tags);
        }
      ]);
    }

    // Output the URLs for of the featured image thumbnails if the post type supports thumbnails
    if (post_type_supports($post_type, 'thumbnail')) {
      // 1400 x 788
      register_rest_field($post_type, 'featured_img_lg', [
        'get_callback' => function($post) {
          return aux_get_featured_img_url($post, 'large');
        }
      ]);

      // 600 x 338
      register_rest_field($post_type, 'featured_img_md', [
        'get_callback' => function($post) {
          return aux_get_featured_img_url($post, 'medium');
        }
      ]);

      // 300 x 300
      register_rest_field($post_type, 'featured_img_thumb', [
        'get_callback' => function($post) {
          return aux_get_featured_img_url($post, 'thumbnail');
        }
      ]);
    }
  }

  // Add API fields for the News post type
  register_rest_field('news', 'read_time', [
    'get_callback' => function() {
      $read_time = PostService::pageReadTime();
      if ($read_time) {
        return $read_time;
      }
      return null;
    }
  ]);
});
