<?php

namespace Fab\Controller;

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

use Fab\Interfaces\JIT_Interface;

/**
 * Latest Post.
 *
 * @package    Fab
 * @subpackage Fab/Module
 */
class Latest_Post_Link_Type extends Link_Group implements JIT_Interface {

    /**
     * Constructor.
     */
    public function __construct() {
        parent::__construct();
        $this->type = 'latest_post_link';
    }

    /**
     * Get labels type.
     *
     * @return array
     */
    public function get_labels_type() {
        return array(
            'name' => __( 'Latest Post', 'floating-awesome-button' ),
        );
    }

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

        // Get the link.
        $post = wp_get_recent_posts(
            array(
                'numberposts' => '1',
                'post_status' => 'publish',
            )
        );
        if ( isset( $post[0] ) ) {
            $instance->setLink(
                get_permalink( $post[0]['ID'] )
            );
        }

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

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

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

        // Add fab setting types.
        add_filter( 'fab_setting_types', array( __CLASS__, 'jit_add_fab_setting_types' ), 10, 1 );
    }
}
