<?php

namespace Fab\Controller;

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

use Fab\Interfaces\JIT_Interface;

/**
 * Plugin hooks in a backend
 * setComponent
 *
 * @package    Fab
 * @subpackage Fab/Controller
 */
class Utils extends Base implements JIT_Interface {

    /**
     * Add preview action to FAB post type row actions
     *
     * @param array   $actions Array of row action links.
     * @param WP_Post $post    The post object.
     * @return array
     */
    public function add_fab_preview_action( $actions, $post ) {
        if ( 'fab' === $post->post_type ) {
            $actions['fab_preview'] = sprintf(
                '<a href="%s" target="_blank">%s</a>',
                \FAB()->Helper->get_preview_url(),
                __( 'Preview', 'floating-awesome-button' )
            );
        }
        return $actions;
    }

    /**
     * Add clone action to FAB post type row actions
     *
     * @param array   $actions Array of row action links.
     * @param WP_Post $post    The post object.
     * @return array
     */
    public function add_fab_clone_action( $actions, $post ) {
        if ( 'fab' === $post->post_type ) {
            $actions['fab_clone'] = sprintf(
                '<a href="%s">%s</a>',
                wp_nonce_url(
                    admin_url( 'admin.php?action=fab_clone&post=' . $post->ID ),
                    'clone_fab_' . $post->ID
                ),
                __( 'Clone', 'floating-awesome-button' )
            );
        }
        return $actions;
    }

    /**
     * Handle the cloning of FAB post
     *
     * @return void
     */
    public function clone_fab_post() {
        // Check if post ID is provided.
        $post_id = isset( $_GET['post'] ) ? intval( $_GET['post'] ) : 0;

        // Verify nonce.
        if ( ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'clone_fab_' . $post_id ) ) {
            wp_die( esc_html__( 'Security check failed', 'floating-awesome-button' ) );
        }

        // Get the post to clone.
        $post = get_post( $post_id );

        // Verify post exists and is FAB type.
        if ( null === $post || 'fab' !== $post->post_type ) {
            wp_die( esc_html__( 'FAB post not found', 'floating-awesome-button' ) );
        }

        // Create new post data array.
        $new_post_args = array(
            'post_type'    => $post->post_type,
            'post_title'   => $post->post_title . ' ' . __( '(Clone)', 'floating-awesome-button' ),
            'post_content' => $post->post_content,
            'post_status'  => 'draft',
            'post_author'  => get_current_user_id(),
        );

        // Insert the new post.
        $new_post_id = wp_insert_post( $new_post_args );

        // Copy post meta.
        $post_meta = get_post_meta( $post_id );
        if ( $post_meta ) {
            foreach ( $post_meta as $meta_key => $meta_values ) {
                foreach ( $meta_values as $meta_value ) {
                    add_post_meta( $new_post_id, $meta_key, maybe_unserialize( $meta_value ) );
                }
            }
        }

        // Redirect to the edit screen of the new post.
        wp_safe_redirect( admin_url( 'post.php?action=edit&post=' . $new_post_id ) );
        exit;
    }

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

    /**
     * JIT method.
     *
     * @return void
     */
    public static function jit() {
        // Add preview action to FAB post type row actions.
        add_filter( 'post_row_actions', array( __CLASS__, 'jit_add_fab_preview_action' ), 10, 2 );

        // Add clone action to FAB post type row actions.
        add_filter( 'post_row_actions', array( __CLASS__, 'jit_add_fab_clone_action' ), 10, 2 );
        add_action( 'admin_action_fab_clone', array( __CLASS__, 'jit_clone_fab_post' ) );
    }
}
