<?php

namespace ModernFields\FieldGroups\Hooks;

use ModernFields\WordPress\Interfaces\Hooks;

defined('ABSPATH') || exit;

/**
 * Redirects the raw field-group CPT edit screen back to the Modern Fields app.
 *
 * The admin page, menu and assets are owned by AdminPageHooks; field groups are
 * rendered as the default tab of the single React app.
 *
 * @since 0.7.3
 */
class FieldGroupAdminMenu implements Hooks
{
  /**
   * @since 0.7.3
   */
  public function registerHooks(): void
  {
    add_action('load-edit.php', [$this, 'redirectEditPage']);
  }

  /**
   * @since 0.7.3
   */
  public function redirectEditPage(): void
  {
    if (!is_admin()) {
      return;
    }

    $post_type = isset($_GET['post_type']) ? sanitize_text_field(wp_unslash($_GET['post_type'])) : '';

    if ($post_type === MODERN_FIELDS_CPT_SLUG) {
      wp_safe_redirect(admin_url('admin.php?page=modern-fields'));
      exit;
    }
  }
}
