<?php

namespace Fab\Controller;

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

use Fab\Metabox\FABMetaboxSetting;
use Fab\Interfaces\JIT_Interface;

/**
 * Single Post.
 *
 * @package    Fab
 * @subpackage Fab/Module
 */
class Single_Post_Type_Frontend extends Single_Post_Type implements JIT_Interface {

    /**
     * Add link data to FAB item
     *
     * @frontend
     *
     * @param object $instance FAB item instance.
     * @return void
     */
    public function add_fab_item_data( $instance ) {
        if ( $instance->getType() !== $this->type ) {
            return;
        }

        // Set Link.
        $post_id = get_post_meta( $instance->getID(), $this->post_metas['post_id']['meta_key'], true );
        $post    = $post_id ? get_post( $post_id ) : null;
        if ( $post ) {
            $instance->setPost( $post );
            $instance->setLink( get_the_permalink( $post_id ) );
        } else {
            $instance->setLink( '#' );
        }

        // Set Link Behavior.
        $link_behavior = get_post_meta( $instance->getID(), FABMetaboxSetting::$post_metas['link_behavior']['meta_key'], true );
        $instance->setLinkBehavior( $link_behavior );

        // Set Link Href.
        $instance->setLinkHref( true );
    }

    /*
    |--------------------------------------------------------------------------
    | Fulfill implemented interface contracts
    |--------------------------------------------------------------------------
     */

    /**
     * JIT Method.
     *
     * @return void
     */
    public static function jit() {
        // Add fab item data.
        add_action( 'fab_item_data', array( __CLASS__, 'jit_add_fab_item_data' ), 10, 1 );
    }
}
