<?php
/**
 * Sponsors > Sponsor Settings: drag to reorder tiers and set each tier's
 * heading and visibility on the front end.
 *
 * @package Kadence-Child
 */

defined( 'ABSPATH' ) || exit;

const PV_SPONSOR_SETTINGS_SLUG = 'pv-sponsor-settings';
const PV_SPONSOR_SETTINGS_CAP  = 'manage_categories';

add_action( 'admin_menu', 'pv_sponsor_settings_menu' );
function pv_sponsor_settings_menu() {
    $hook = add_submenu_page(
        'edit.php?post_type=sponsor',
        __( 'Sponsor Settings', 'kadence-child' ),
        __( 'Sponsor Settings', 'kadence-child' ),
        PV_SPONSOR_SETTINGS_CAP,
        PV_SPONSOR_SETTINGS_SLUG,
        'pv_render_sponsor_settings_page'
    );
    add_action( 'admin_print_styles-' . $hook, 'pv_sponsor_settings_assets' );
}

function pv_sponsor_settings_assets() {
    $uri     = get_stylesheet_directory_uri();
    $version = wp_get_theme()->get( 'Version' );
    wp_enqueue_style( 'pv-sponsor-settings', $uri . '/assets/css/sponsor-settings.css', array(), $version );
    wp_enqueue_script( 'pv-sponsor-settings', $uri . '/assets/js/sponsor-settings.js', array( 'jquery-ui-sortable' ), $version, true );
}

add_action( 'admin_post_pv_save_sponsor_settings', 'pv_handle_sponsor_settings_save' );
function pv_handle_sponsor_settings_save() {
    if ( ! current_user_can( PV_SPONSOR_SETTINGS_CAP ) ) {
        wp_die( esc_html__( 'You are not allowed to manage sponsor settings.', 'kadence-child' ), 403 );
    }
    check_admin_referer( 'pv_save_sponsor_settings' );

    $order    = isset( $_POST['pv_tier_order'] ) ? array_map( 'absint', (array) $_POST['pv_tier_order'] ) : array();
    $settings = isset( $_POST['pv_tiers'] ) ? (array) $_POST['pv_tiers'] : array(); // Sanitized per field in pv_save_tier_settings().
    pv_save_tier_settings( $order, $settings );

    wp_safe_redirect( add_query_arg(
        array( 'post_type' => 'sponsor', 'page' => PV_SPONSOR_SETTINGS_SLUG, 'updated' => '1' ),
        admin_url( 'edit.php' )
    ) );
    exit;
}

function pv_render_sponsor_settings_page() {
    $tiers     = pv_get_sponsor_tiers();
    $tiers_url = admin_url( 'edit-tags.php?taxonomy=' . PV_TIER_TAXONOMY . '&post_type=sponsor' );
    ?>
    <div class="wrap pv-sponsor-settings">
        <h1><?php esc_html_e( 'Sponsor Settings', 'kadence-child' ); ?></h1>

        <?php if ( isset( $_GET['updated'] ) ) : // phpcs:ignore WordPress.Security.NonceVerification ?>
            <div class="notice notice-success is-dismissible"><p><?php esc_html_e( 'Sponsor settings saved.', 'kadence-child' ); ?></p></div>
        <?php endif; ?>

        <p class="pv-sponsor-settings__intro">
            <?php esc_html_e( 'Drag tiers to set the order they appear in on the Sponsors block. The top tier gets the largest logos. Tiers with no published sponsors are skipped automatically.', 'kadence-child' ); ?>
            <a href="<?php echo esc_url( $tiers_url ); ?>"><?php esc_html_e( 'Add or rename tiers', 'kadence-child' ); ?></a>
        </p>

        <?php if ( ! $tiers ) : ?>
            <p><?php esc_html_e( 'No tiers yet.', 'kadence-child' ); ?></p>
        <?php else : ?>
        <form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
            <input type="hidden" name="action" value="pv_save_sponsor_settings">
            <?php wp_nonce_field( 'pv_save_sponsor_settings' ); ?>

            <table class="widefat striped pv-tier-table">
                <thead>
                    <tr>
                        <th class="pv-tier-table__handle-col"><span class="screen-reader-text"><?php esc_html_e( 'Reorder', 'kadence-child' ); ?></span></th>
                        <th><?php esc_html_e( 'Tier', 'kadence-child' ); ?></th>
                        <th><?php esc_html_e( 'Sponsors', 'kadence-child' ); ?></th>
                        <th><?php esc_html_e( 'Heading on page', 'kadence-child' ); ?></th>
                        <th><?php esc_html_e( 'Hide', 'kadence-child' ); ?></th>
                    </tr>
                </thead>
                <tbody class="pv-tier-table__body">
                <?php foreach ( $tiers as $term ) :
                    $s     = pv_get_tier_settings( $term );
                    $field = 'pv_tiers[' . $term->term_id . ']';
                    ?>
                    <tr class="pv-tier-table__row">
                        <td class="pv-tier-table__handle" title="<?php esc_attr_e( 'Drag to reorder', 'kadence-child' ); ?>">
                            <span class="dashicons dashicons-menu" aria-hidden="true"></span>
                            <input type="hidden" name="pv_tier_order[]" value="<?php echo esc_attr( $term->term_id ); ?>">
                        </td>
                        <td>
                            <strong><?php echo esc_html( $term->name ); ?></strong>
                            <div class="row-actions"><a href="<?php echo esc_url( get_edit_term_link( $term, PV_TIER_TAXONOMY, 'sponsor' ) ); ?>"><?php esc_html_e( 'Edit tier', 'kadence-child' ); ?></a></div>
                        </td>
                        <td>
                            <a href="<?php echo esc_url( admin_url( 'edit.php?post_type=sponsor&' . PV_TIER_TAXONOMY . '=' . $term->slug ) ); ?>"><?php echo esc_html( number_format_i18n( $term->count ) ); ?></a>
                        </td>
                        <td>
                            <input type="text" class="regular-text"
                                name="<?php echo esc_attr( $field . '[heading]' ); ?>"
                                value="<?php echo esc_attr( $s['heading_custom'] ); ?>"
                                placeholder="<?php echo esc_attr( pv_default_tier_heading( $term->name ) ); ?>">
                        </td>
                        <td>
                            <input type="checkbox" value="1" name="<?php echo esc_attr( $field . '[hidden]' ); ?>" <?php checked( $s['hidden'] ); ?>
                                aria-label="<?php echo esc_attr( sprintf( __( 'Hide %s on the front end', 'kadence-child' ), $term->name ) ); ?>">
                        </td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            </table>

            <?php submit_button( __( 'Save Settings', 'kadence-child' ) ); ?>
        </form>
        <?php endif; ?>
    </div>
    <?php
}

// Point editors on the Tiers screen to where order and headings are managed.
add_action( 'admin_notices', 'pv_tier_screen_notice' );
function pv_tier_screen_notice() {
    $screen = get_current_screen();
    if ( ! $screen || 'edit-' . PV_TIER_TAXONOMY !== $screen->id || ! current_user_can( PV_SPONSOR_SETTINGS_CAP ) ) {
        return;
    }
    printf(
        '<div class="notice notice-info"><p>%s <a href="%s">%s</a></p></div>',
        esc_html__( 'New tiers appear on the Sponsors page automatically. Set their order and heading in', 'kadence-child' ),
        esc_url( admin_url( 'edit.php?post_type=sponsor&page=' . PV_SPONSOR_SETTINGS_SLUG ) ),
        esc_html__( 'Sponsor Settings', 'kadence-child' )
    );
}
