<?php
/**
 * Plugin Name: ID Business Listing
 * Plugin URI:  https://insightdezign.com/plugin/id-business-listing
 * Description: Manage local business structured data (Schema.org JSON-LD) with optional WHMCS integration. Outputs schema markup on the homepage and contact pages.
 * Version:     1.6.1
 * Author:      Insight Dezign
 * Author URI:  https://insightdezign.com
 * License:     GPL-2.0-or-later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: id-business-listing
 * Domain Path: /languages
 * Requires at least: 5.6
 * Requires PHP: 7.4
 *
 * @package ID_Business_Listing
 * @author  Insight Dezign
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

define( 'BLM_VERSION', '1.6.1' );
define( 'BLM_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'BLM_PLUGIN_URL', plugin_dir_url( __FILE__ ) );

/* ------------------------------------------------------------------ */
/*  Freemius SDK integration                                           */
/* ------------------------------------------------------------------ */
if ( ! function_exists( 'idblm_fs' ) ) {
    /**
     * Create a helper function for easy SDK access.
     *
     * @return \Freemius
     */
    function idblm_fs() {
        global $idblm_fs;

        if ( ! isset( $idblm_fs ) ) {
            // Include Freemius SDK.
            require_once dirname( __FILE__ ) . '/vendor/freemius/wordpress-sdk/start.php';

            $idblm_fs = fs_dynamic_init( array(
                'id'                  => '24393',
                'slug'                => 'id-business-listing',
                'type'                => 'plugin',
                'public_key'          => 'pk_80f16311cb18a3730e7d759fcd272',
                'is_premium'          => false,
                'has_addons'          => false,
                'has_paid_plans'      => false,
                'menu'                => array(
                    'slug'           => 'blm-settings',
                    'account'        => false,
                    'support'        => false,
                    'parent'         => array(
                        'slug' => 'tools.php',
                    ),
                ),
            ) );
        }

        return $idblm_fs;
    }

    // Init Freemius.
    idblm_fs();
    // Signal that SDK was initiated.
    do_action( 'idblm_fs_loaded' );
}

// Include classes
require_once BLM_PLUGIN_DIR . 'includes/class-blm-activator.php';
require_once BLM_PLUGIN_DIR . 'includes/class-blm-deactivator.php';
require_once BLM_PLUGIN_DIR . 'includes/class-blm-business-types.php';
require_once BLM_PLUGIN_DIR . 'includes/class-blm-connection.php';
require_once BLM_PLUGIN_DIR . 'includes/class-blm-settings.php';
require_once BLM_PLUGIN_DIR . 'includes/class-blm-schema.php';
require_once BLM_PLUGIN_DIR . 'includes/class-blm-api.php';
require_once BLM_PLUGIN_DIR . 'includes/class-blm-hours-widget.php';
require_once BLM_PLUGIN_DIR . 'includes/class-blm-idve.php';
require_once BLM_PLUGIN_DIR . 'includes/class-blm-wpbakery.php';

// IDVE + WPBakery integrations (hooks are inert if editors are not active)
new BLM_IDVE();
new BLM_WPBakery();

// Activation / Deactivation
register_activation_hook( __FILE__, array( 'BLM_Activator', 'activate' ) );
register_deactivation_hook( __FILE__, array( 'BLM_Deactivator', 'deactivate' ) );

// Admin menu
add_action( 'admin_menu', array( 'BLM_Settings', 'add_menu' ) );

// Redirect to settings page on activation
add_action( 'admin_init', 'blm_activation_redirect' );

function blm_activation_redirect() {
    if ( ! get_transient( 'blm_activation_redirect' ) ) {
        return;
    }

    delete_transient( 'blm_activation_redirect' );

    // Don't redirect on multisite bulk activation.
    if ( isset( $_GET['activate-multi'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
        return;
    }

    wp_safe_redirect( admin_url( 'tools.php?page=blm-settings' ) );
    exit;
}

// Schema output on frontend
add_action( 'wp_head', array( 'BLM_Schema', 'output' ) );

// REST API
add_action( 'rest_api_init', array( 'BLM_API', 'register_routes' ) );

// Enqueue admin assets on settings page only
add_action( 'admin_enqueue_scripts', 'blm_enqueue_admin_assets' );

function blm_enqueue_admin_assets( $hook ) {
    if ( 'tools_page_blm-settings' !== $hook ) {
        return;
    }

    wp_enqueue_style(
        'blm-settings-css',
        BLM_PLUGIN_URL . 'assets/css/settings.css',
        array(),
        BLM_VERSION
    );

    wp_enqueue_media();

    wp_enqueue_script(
        'blm-settings-js',
        BLM_PLUGIN_URL . 'assets/js/settings.js',
        array(),
        BLM_VERSION,
        true
    );
}

// Enqueue frontend styles for widget/shortcode
add_action( 'wp_enqueue_scripts', 'blm_enqueue_frontend_assets' );

function blm_enqueue_frontend_assets() {
    wp_enqueue_style(
        'blm-hours-css',
        BLM_PLUGIN_URL . 'assets/css/hours.css',
        array(),
        BLM_VERSION
    );
}
