<?php

namespace Fab\Controller;

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

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

/**
 * Related Post Editor.
 *
 * @package    Fab
 * @subpackage Fab/Module
 */
class Related_Post_Editor extends Related_Post_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 post recommendation, return.
        if ( $this->type !== $type ) {
            return;
        }

        // Save post recommedation display count.
        $post_meta     = $this->post_metas['display_count'];
        $display_count = isset( $params['fab_setting_related_post_display_count'] ) ? intval( $params['fab_setting_related_post_display_count'] ) : $post_meta['default'];
        update_post_meta( $post_id, $post_meta['meta_key'], $display_count );
    }

    /**
     * Add fab metabox setting tabs fields.
     *
     * @param array $tabs The tabs.
     * @param array $fab The fab.
     * @return array
     */
    public function add_fab_metabox_setting_tabs_fields( $tabs, $fab ) {
        // Add post recommendation display count field.
        $tabs['setting']['fields']['display_count'] = array(
            'type'       => 'text',
            'name'       => 'fab_setting_related_post_display_count',
            'label'      => __( 'Display Count', 'floating-awesome-button' ),
            'tooltip'    => __( 'Set how many related posts to show in the popup.', 'floating-awesome-button' ),
            'info'       => __( 'This controls how many related posts will be displayed when the Related Post popup is triggered. Posts are recommended based on the current post’s category or tag.', 'floating-awesome-button' ),
            'value'      => isset( $fab['extraOptions']['display_count'] ) ? $fab['extraOptions']['display_count'] : 3,
            'required'   => true,
            'visible_if' => array(
                array(
                    'tab'      => 'setting',
                    'field'    => 'type',
                    'operator' => '==',
                    'value'    => $this->type,
                ),
            ),
        );

        return $tabs;
    }

    /*
    |--------------------------------------------------------------------------
    | 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 );

        // Add fab metabox setting tabs fields.
        add_filter( 'fab_metabox_setting_tabs', array( __CLASS__, 'jit_add_fab_metabox_setting_tabs_fields' ), 11, 2 );
    }
}
