<?php

namespace Blazemedia\Affiliation;

use Blazemedia\Affiliation\Rendering;
use Blazemedia\Affiliation\AdminUserInterface\Settings\PostTypes;
use Blazemedia\Affiliation\Utils\HttpRequest;

/**
 * Gather parameters firing the 
 * the_content filter over the post_content
 * of the current post
 * 
 * This action let the service to fill their caches
 * when the the_content filter is fired again
 * all the parameters should be already available 
 * in the transient
 */
class PostAttributesCacheBuilder {

    function __construct() {
        
        // add_action( 'save_post', [ $this, 'buildCacheOnSavePost' ], 100, 3 );
    }
    
    /**
     * @param integer $post_ID
     * @param WP_Post $post
     * @param boolean $update
     * @return void
     */
    function buildCacheOnSavePost( int $post_ID, $post, bool $update ) {

        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
        if ( 'auto-draft' === $post->post_status ) return;

        //var_dump($post->post_status, $update ); die;

        if( !PostTypes::isPostTypeEnabled( $post->post_type ) ) {
            return;
        }

        /// non dovrebbero servire ma non si sa mai
        remove_action( 'save_post', [ $this, 'buildCacheOnSavePost' ], 100, 3  );
        
        /// abilita tutti i formati 
        $this->enableFormatsRendering();

        /// effettua il rendering dei formati in pagina per caricare le cache
        apply_filters( 'the_content', $post->post_content );

        /// non dovrebbero servire ma non si sa mai
        add_action( 'save_post', [ $this, 'buildCacheOnSavePost' ], 100, 3 );
    }
    
    

    /**
     * Enable the formats rendering 
     * where the_content filter is fired
     *
     * @return void
     */
    function enableFormatsRendering() {
        
		/// effettua il rendering degli shortcode dei bottoni 
		/// di affiliazione
		new Rendering\Zone;
        new Rendering\Link;
		new Rendering\Button;
		new Rendering\Table;
		new Rendering\Parse;
		new Rendering\ParseButton;
        new Rendering\ParseCard;
        new Rendering\ParseImage;
		new Rendering\Price;
        new Rendering\Widget;
        new Rendering\Promo;
        new Rendering\BrandedContent;
    }


}
