<?php

namespace Blazemedia\Affiliation\AdminUserInterface\Dashboard;

use Blazemedia\DailyNewsWidget\AdminUserInterface\Template\Post;

class OfferExpires {

    public function __construct() {
        add_action('wp_dashboard_setup', [$this, 'blz_add_dashboard_offerexpire_widgets'],20);
    }

    public function blz_add_dashboard_offerexpire_widgets() {
        wp_add_dashboard_widget(
            'blz_dashboardexpire_widget',               // Widget slug.
            esc_html__('Articoli contententi offerte non disponibili (da rimuovere)', 'blz'), // Title.
            [$this, 'blz_dashboard_widget_render']
        );
    }

    public function blz_dashboard_widget_render() {
        //Check if transient is set
        $datas = get_transient('error_offer_not_found');
    
        if($datas === false){
            return;
        }

        $datas = json_decode($datas, true);

        $markup = <<<HTML
            <div class="blz_grid">
                <div class="blz_col blz_col-12">
                    <ul>
                        :articles
                    </ul>
                </div>
            </div>
        HTML;

        $hydra = [
            ':articles' => '',
        ];
        foreach ($datas as $data) {
            $post_id = $data['post_id'];
            $post_title = get_the_title($post_id);
            $post_link = get_edit_post_link($post_id);

            if ($post_link) {
                $link = '<a href="' . esc_url($post_link) . '" target="_blank">' . esc_html($post_title) . '</a>';
            } else {
                $link = esc_html($post_title);
            }

            $hydra[':articles'] .= '<li>' . $link . '</li>';
        }
        $markupReplaced = strtr($markup, $hydra);

        echo ($markupReplaced);
    }
}
