<?php

namespace Fab\Controller;

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

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

/**
 * Social Sharing Editor.
 *
 * @package    Fab
 * @subpackage Fab/Module
 */
class Social_Sharing_Editor extends Social_Sharing_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 social media.
        $social_media = $params['fab_setting_social_media'];
        foreach ( $social_media as $key => &$value ) {
            $value = \Fab\Plugin\Helper::getInstance()->transform_yes_no_value( $value );
        }
        update_post_meta( $post_id, $this->post_meta['social_media']['meta_key'], $social_media );
    }

    /**
     * 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 ) {
        // Social media platforms.
        $social_media = apply_filters( 'fab_list_of_social_buttons', array(), array() );
        $social_media = array_keys( $social_media );
        ksort( $social_media );

        // Add social sharing button fields.
        $fields = array();
        foreach ( $social_media as $platform ) {
            $fields[ $platform ] = array(
                'type'       => 'switch',
                'name'       => sprintf( 'fab_setting_social_media[%s]', $platform ),
                'label'      => $platform,
                'value'      => isset( $fab['extraOptions']['social_media'][ $platform ] ) ? $fab['extraOptions']['social_media'][ $platform ] : true,
                'inputClass' => 'flex',
            );
        }

        // Add social sharing button fields.
        $tabs['setting']['fields']['social_media'] = array(
            'name'       => 'fab_setting_social_media',
            'label'      => __( 'Social Media', 'floating-awesome-button' ),
            'tooltip'    => __( 'Select the social media to display', 'floating-awesome-button' ),
            'info'       => ! \Fab\Plugin\Helper::getInstance()->isPremiumPlan() ?
                sprintf(
                    // translators: %s: Upgrade URL.
                    __( 'Unlock all social media buttons, <a href="%s">upgrade to pro now!</a>', 'floating-awesome-button' ),
                    \Fab\Plugin\Helper::getInstance()->getUpgradeURL()
                )
                : '',
            'fields'     => $fields,
            '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 );
    }
}
