<?php

namespace Fab\Controller;

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

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

/**
 * Link Type Editor.
 *
 * @package    Fab
 * @subpackage Fab/Module
 */
class Link_Type_Editor extends Link_Type implements JIT_Interface {

    /**
     * Save Metabox Data.
     *
     * @param int    $post_id Post ID.
     * @param object $post Post object.
     * @param bool   $update Whether the post is being updated.
     * @return void
     */
    public function save_metabox_data( $post_id, $post, $update ) {
        $params = $_POST; // phpcs:ignore WordPress.Security.NonceVerification.Missing
        $type   = isset( $params['fab_setting_type'] ) ? sanitize_text_field( wp_unslash( $params['fab_setting_type'] ) ) : '';

        // If type is not link, return.
        if ( $this->type !== $type ) {
            return;
        }

        // Save link behavior.
        $link_behavior = \Fab\Plugin\Helper::getInstance()->transform_yes_no_value( $params['fab_setting_link_behavior'] );
        update_post_meta( $post_id, 'fab_setting_link_behavior', $link_behavior );
    }

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

    /**
     * JIT Method.
     *
     * @return void
     */
    public static function jit() {
        // Save Metabox Data.
        add_action( 'fab_save_post', array( __CLASS__, 'jit_save_metabox_data' ), 10, 3 );
    }
}
