<?php
/**
 * Plugin Name: Freshworks Forms
 * Description: Freshworks Forms plugin allows you to create beautiful contact forms, feedback form, subscription forms, and other types of forms for your site with just a few clicks.
 * Author: Freshworks
 * Author URI: https://www.freshworks.com/
 * Version: 1.0.0
 * Description: Freshworks Forms
 * Text-Domain: fw-forms
 */

if( ! defined( 'ABSPATH' ) ) : exit(); endif; // No direct access allowed.

/**
* Define Plugins Contants
*/

// Plugin Folder Path.
if ( ! defined( 'FWFORMS_PATH' ) ) {
	define ( 'FWFORMS_PATH', trailingslashit( plugin_dir_path( __FILE__ ) ) );
}

// Plugin Folder URL.
if ( ! defined( 'FWFORMS_URL' ) ) {
    define ( 'FWFORMS_URL', trailingslashit( plugins_url( '/', __FILE__ ) ) );
}

/**
 * Loading Necessary Scripts
 */

function fwforms_load_scripts() {
    wp_enqueue_script( 'fw-forms', FWFORMS_URL . 'build/bundle.js', [ 'jquery', 'wp-element' ], wp_rand(), true );
    wp_enqueue_style( 'fw-forms', FWFORMS_URL . 'build/bundle.c89a1ef9739dc69c5797.css' );
    wp_localize_script( 'fw-forms', 'appLocalizer', [
        'apiUrl' => home_url( '/wp-json' ),
        'nonce' => wp_create_nonce( 'wp_rest' ),
    ]);
}

add_action( 'admin_enqueue_scripts', 'fwforms_load_scripts' );

/**
 * Registers a shortcode that simply displays a placeholder for our React App.
 */

function fwforms_shortcode( $atts ) {
    $id = uniqid( 'fw_forms' );
    $shortcodeId = str_replace("=","",implode($atts));
    $data = shortcode_atts(
        array (
            'templateId' => $shortcodeId,
            'apiUrl' => home_url( '/wp-json' ),
            'nonce' => wp_create_nonce( 'wp_rest' ),
        ),
        $atts
    );
    wp_enqueue_script( "fwforms-id", FWFORMS_URL . 'build/shortcode.js', array( 'wp-element' ), time(), true );
    wp_enqueue_style( 'fwforms-id', FWFORMS_URL . 'build/shortcode.649d64408b8aea13d457.css' );
    wp_localize_script(
        "fwforms-id",
        $id,
        $data
    );
    return '<div class="wp-reactivate-shortcode" data-object-id="' . $id. '" data-title="true"></div>';
}

add_shortcode( 'fwforms-id', 'fwforms_shortcode' );

function fw_render_callback($atts2,$content){
	if ($atts2['form']!=="No Forms Found" && $atts2['form']!=="Select a Form" && $atts2['form']!=="" && $atts2['form']!==null  ) { 

    $atts=["=".$atts2['form']];
        $id = uniqid( 'fw_forms' );
        $shortcodeId = str_replace("=","",implode($atts));
        $data = shortcode_atts(
            array (
                'templateId' => $shortcodeId,
                'apiUrl' => home_url( '/wp-json' ),
                'nonce' => wp_create_nonce( 'wp_rest' ),
            ),
            $atts
        );
        wp_enqueue_script( "fwforms-id", FWFORMS_URL . 'build/shortcode.js', array( 'wp-element' ), time(), true );
        wp_enqueue_style( 'fwforms-id', FWFORMS_URL . 'build/shortcode.649d64408b8aea13d457.css' );
        wp_localize_script(
            "fwforms-id",
            $id,
            $data
        );
        // var_dump($atts2['title']);
        return '<div class="wp-reactivate-shortcode" data-object-id="' . $id. '" data-title="'.$atts2['title'].'" " data-desc="'.$atts2['desc'].'" ></div>';
    };
}

function fwform_fwform_block_init2() {
    $asset_file = include( plugin_dir_path( __FILE__ ) . 'build/index.asset.php');

    wp_register_script(
        'fwform',
        plugins_url( 'build/index.js', __FILE__ ),
        $asset_file['dependencies'],
        $asset_file['version']
    );

    register_block_type( 'fwform/fwform', array(
        'title'=>'Freshworks Form Builder',
        'description'=>'Select and display one of your forms',
        'category'=>'widgets',
        'api_version' => 2,
        'attributes' => [
                'message'  => [
                    'type' => 'string', 
                ],
                'form' => [
                    'type' => 'string', 
                ],
                'title' => ['type'=>'bool',
                'default'=>true],
                'desc' => ['type'=>'bool',
                'default'=>true]
        ],
        'editor_script' => 'fwform',
        'render_callback' => 'fw_render_callback'
    ) );
}

add_action( 'init', 'fwform_fwform_block_init2' );

require_once FWFORMS_PATH . 'classes/class-create-admin-menu.php';
require_once FWFORMS_PATH . 'classes/class-create-settings-routes.php';


