<?php

namespace Inoby\Plugins\FilterAndPagination;

class RestCardField {
  public function init() {
    add_action("rest_api_init", [$this, "register_card_field"]);
  }

  public function register_card_field() {
    register_rest_field(get_post_types(), "card_html", [
      "get_callback" => [$this, "get_post_card"],
      "schema" => [
        "type" => "string",
      ],
    ]);
  }

  public function get_post_card($post) {
    $p = get_post($post["id"]);
    $pt = $p->post_type;
    inoby_setup_postdata($p);

    ob_start();
    get_template_part("post-types/{$pt}/parts/card", null, ["post" => $post]);
    $html = ob_get_clean();

    inoby_reset_post_data();
    return $html;
  }
}
