<?php

/**
 * Plugin Name:       Click to Copy
 * Description:       The Click To Copy Block plugin offers a seamless Gutenberg block for one-click content copying.
 * Version:           2.0.2
 * Author:            bPlugins
 * Author URI:        http://bplugins.com
 * License:           GPLv3
 * License URI:       https://www.gnu.org/licenses/gpl-3.0.txt
 * Plugin URI: https://wordpress.org/plugins/click-to-copy/
 * Text Domain:       clipboard
 */
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
if ( function_exists( 'ctc_fs' ) ) {
    ctc_fs()->set_basename( false, __FILE__ );
} else {
    // Defined Constant
    define( 'CTC_PLUGIN_VERSION', ( isset( $_SERVER['HTTP_HOST'] ) && 'localhost' === sanitize_text_field( $_SERVER['HTTP_HOST'] ) ? time() : '2.0.2' ) );
    define( 'CTC_DIR_URL', plugin_dir_url( __FILE__ ) );
    define( 'CTC_ASSETS_DIR', plugin_dir_url( __FILE__ ) . 'assets/' );
    define( 'CTC_DIR_PATH', plugin_dir_path( __FILE__ ) );
    define( 'CTC_HAS_FREE', 'click-to-copy/click-to-copy-block.php' === plugin_basename( __FILE__ ) );
    define( 'CTC_HAS_PRO', 'click-to-copy-pro/click-to-copy-block.php' === plugin_basename( __FILE__ ) );
    // Freemius Code
    if ( !function_exists( 'ctc_fs' ) ) {
        // Create a helper function for easy SDK access.
        function ctc_fs() {
            global $ctc_fs;
            if ( !isset( $ctc_fs ) ) {
                $fsStartPath = dirname( __FILE__ ) . '/freemius/start.php';
                $bSDKInitPath = dirname( __FILE__ ) . '/freemius-lite/start.php';
                if ( CTC_HAS_PRO && file_exists( $fsStartPath ) ) {
                    require_once $fsStartPath;
                } else {
                    if ( CTC_HAS_FREE && file_exists( $bSDKInitPath ) ) {
                        require_once $bSDKInitPath;
                    }
                }
                $bptbConfig = [
                    'id'                  => '20412',
                    'slug'                => 'click-to-copy',
                    'premium_slug'        => 'click-to-copy-pro',
                    'type'                => 'plugin',
                    'public_key'          => 'pk_06363f5cbf7f2bdba1337af3d5f8d',
                    'is_premium'          => true,
                    'premium_suffix'      => 'Pro',
                    'has_premium_version' => true,
                    'has_addons'          => false,
                    'has_paid_plans'      => true,
                    'trial'               => [
                        'days'               => 7,
                        'is_require_payment' => false,
                    ],
                    'menu'                => ( CTC_HAS_PRO ? array(
                        'slug'       => 'click-to-copy-dashboard',
                        'first-path' => 'admin.php?page=click-to-copy-dashboard#/welcome',
                        'support'    => false,
                    ) : array(
                        'slug'       => 'click-to-copy-dashboard',
                        'first-path' => 'tools.php?page=click-to-copy-dashboard#/welcome',
                        'support'    => false,
                        'parent'     => array(
                            'slug' => 'tools.php',
                        ),
                    ) ),
                ];
                $ctc_fs = ( CTC_HAS_PRO && file_exists( $fsStartPath ) ? fs_dynamic_init( $bptbConfig ) : fs_lite_dynamic_init( $bptbConfig ) );
            }
            return $ctc_fs;
        }

        // Init Freemius.
        ctc_fs();
        // Signal that SDK was initiated.
        do_action( 'ctc_fs_loaded' );
    }
    // Main Plugin Logic...
    function bpctcPremiumChecker() {
        return ( CTC_HAS_PRO ? ctc_fs()->can_use_premium_code() : false );
    }

    if ( CTC_HAS_PRO && bpctcPremiumChecker() ) {
        require_once CTC_DIR_PATH . '/inc/ShortCode.php';
        require_once CTC_DIR_PATH . '/inc/ProAdminMenu.php';
    } else {
        require_once CTC_DIR_PATH . '/inc/AdminMenu.php';
    }
    if ( !class_exists( "CTCBClickToCopy" ) ) {
        class CTCBClickToCopy {
            public function __construct() {
                add_action( 'init', [$this, 'onInit'] );
                add_action( 'admin_init', [$this, 'registerSettings'] );
                add_action( 'rest_api_init', [$this, 'registerSettings'] );
                add_action( 'enqueue_block_editor_assets', [$this, 'ctcEnqueueBlockEditorAssets'] );
            }

            // Initialize Gutenberg block & translations
            public function onInit() {
                register_block_type( __DIR__ . '/build' );
                wp_set_script_translations( 'ctcb-click-to-copy-editor-script', 'clipboard', plugin_dir_path( __FILE__ ) . 'languages' );
                wp_set_script_translations( 'ctcb-click-to-copy-view-script', 'clipboard', plugin_dir_path( __FILE__ ) . 'languages' );
            }

            function ctcEnqueueBlockEditorAssets() {
                wp_add_inline_script( 'ctcb-click-to-copy-editor-script', 'const ctcbpipecheck =  ' . wp_json_encode( bpctcPremiumChecker() ) . ';', 'before' );
            }

            // Register plugin settings
            public function registerSettings() {
                register_setting( 'bpctcUtils', 'bpctcUtils', [
                    'show_in_rest'      => [
                        'name'   => 'bpctcUtils',
                        'schema' => [
                            'type' => 'string',
                        ],
                    ],
                    'type'              => 'string',
                    'default'           => wp_json_encode( [
                        'nonce' => wp_create_nonce( 'wp_ajax' ),
                    ] ),
                    'sanitize_callback' => 'sanitize_text_field',
                ] );
            }

        }

        new CTCBClickToCopy();
    }
}