<?php

namespace Fab\Controller;

defined( 'WPINC' ) || die;

use Fab\Interfaces\JIT_Interface;

/**
 * Plugin hooks for post actions in the backend
 *
 * @package    Fab
 * @subpackage Fab/Controller/Backend
 */
class Posts extends Base implements JIT_Interface {

    /**
     * Allowed post types.
     *
     * @var array
     */
    public $allowed_post_types = array( 'page', 'post' );

    /**
     * Add 'Turn into FAB' action to page post type row actions
     *
     * @param array   $actions Array of row action links.
     * @param WP_Post $post    The post object.
     * @return array
     */
    public function add_turn_into_fab_action( $actions, $post ) {
        if ( in_array( $post->post_type, $this->allowed_post_types, true ) ) {
            $actions['turn_into_fab'] = sprintf(
                '<a href="%s">%s</a>',
                wp_nonce_url(
                    add_query_arg(
                        array(
                            'action'  => 'turn_into_fab',
                            'post_id' => $post->ID,
                        ),
                        admin_url( 'admin.php' )
                    ),
                    'turn_into_fab_' . $post->ID
                ),
                __( 'Turn into FAB', 'floating-awesome-button' )
            );
        }
        return $actions;
    }

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

        // Check if post is allowed.
        $post = get_post( $post_id );
        if ( ! $post || ! in_array( $post->post_type, $this->allowed_post_types, true ) ) {
            wp_die( __( 'Invalid post type', 'floating-awesome-button' ) );
        }

        // Verify nonce.
        if ( ! wp_verify_nonce( $_GET['_wpnonce'], 'turn_into_fab_' . $post_id ) ) {
            wp_die( __( 'Security check failed', 'floating-awesome-button' ) );
        }

        // Add new fab.
        $result = \Fab\Helper\FAB_Template::getInstance()->add_new_fab(
            (object) array(
                'id'      => sprintf( 'single-%s', $post->post_type ),
                'post_id' => $post_id,
            )
        );

        // Check if there's an error.
        if ( is_wp_error( $result ) ) {
            wp_die( esc_html( $result->get_error_message() ) );
        }

        // Redirect to FAB edit page.
        wp_safe_redirect( admin_url( 'post.php?post=' . $result . '&action=edit' ) );
        exit;
    }

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

    /**
     * JIT method.
     *
     * @return void
     */
    public static function jit() {
        // Add 'Turn into FAB' Action to Page Post Type.
        add_filter( 'post_row_actions', array( __CLASS__, 'jit_add_turn_into_fab_action' ), 10, 2 );
        add_filter( 'page_row_actions', array( __CLASS__, 'jit_add_turn_into_fab_action' ), 10, 2 );
        add_action( 'admin_action_turn_into_fab', array( __CLASS__, 'jit_turn_page_into_fab' ) );
    }
}
