<?php
/**
 * Plugin Name: ElPlan Kuchikomi Wall
 * Plugin URI: https://www.el-plan.com/kuchikomi-wall/
 * Description: Display Google Reviews, X (Twitter) posts, and custom testimonials beautifully on your WordPress site. Manage all your social proof in one dashboard.
 * Version: 2.1.5
 * Author: EL-PLAN
 * Author URI: https://www.el-plan.com/
 * Text Domain: elplan-kuchikomi-wall
 * Requires at least: 6.0
 * Requires PHP: 7.4
 * License: GPL v2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 */

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

// ==================================================
// Freemius SDK 初期化
// ==================================================
if ( ! function_exists( 'kw_fs' ) ) {
    function kw_fs() {
        global $kw_fs;
        if ( ! isset( $kw_fs ) ) {
            require_once dirname( __FILE__ ) . '/freemius/start.php';
            $kw_fs = fs_dynamic_init( array(
                'id'                  => '26100',
                'slug'                => 'kuchikomi-wall',
                'type'                => 'plugin',
                'public_key'          => 'pk_21a37720e14df2112646e173c6fb0',
                'is_premium'          => false,
                'has_premium_version' => true,
                'has_addons'          => false,
                'has_paid_plans'      => true,
                'menu'                => array(
                    'slug' => 'tmnl-dashboard',
                ),
            ) );
        }
        return $kw_fs;
    }
    kw_fs();
    do_action( 'kw_fs_loaded' );
}

define( 'TMNL_VERSION', '2.1.5' );
define( 'TMNL_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );

// Pro判定ヘルパー: Freemius課金 OR 手動ライセンス有効化
function kw_is_pro() {
    if ( get_option( 'kw_manual_pro_license' ) === 'active' ) {
        return true;
    }
    return function_exists( 'kw_fs' ) && kw_fs()->is_paying();
}
define( 'TMNL_PLUGIN_URL', plugin_dir_url( __FILE__ ) );

// Load bundled translations from the plugin's own /languages directory.
// load_plugin_textdomain() is still required for plugins that bundle their own .mo files.
add_action( 'init', function() {
    load_plugin_textdomain( 'elplan-kuchikomi-wall', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
} );

// Load core components
require_once TMNL_PLUGIN_DIR . 'includes/class-tmnl-db.php';
require_once TMNL_PLUGIN_DIR . 'includes/class-tmnl-source-adapter.php';

// Load adapters
require_once TMNL_PLUGIN_DIR . 'includes/adapters/class-tmnl-adapter-google.php';
require_once TMNL_PLUGIN_DIR . 'includes/adapters/class-tmnl-adapter-manual.php';
require_once TMNL_PLUGIN_DIR . 'includes/adapters/class-tmnl-adapter-x-embed.php';

// Load UI components
require_once TMNL_PLUGIN_DIR . 'includes/class-tmnl-admin.php';
require_once TMNL_PLUGIN_DIR . 'includes/class-tmnl-shortcode.php';
require_once TMNL_PLUGIN_DIR . 'includes/class-tmnl-block.php';
require_once TMNL_PLUGIN_DIR . 'includes/class-tmnl-cron.php';

// Load Pro features
require_once TMNL_PLUGIN_DIR . 'includes/class-tmnl-jsonld.php';
require_once TMNL_PLUGIN_DIR . 'includes/class-tmnl-qr.php';
require_once TMNL_PLUGIN_DIR . 'includes/class-tmnl-sns-collector.php';

// Register adapters
$tmnl_adapters = array(
    'manual'  => new TMNL_Adapter_Manual(),
    'google'  => new TMNL_Adapter_Google(),
    'x_embed' => new TMNL_Adapter_X_Embed(),
);

/**
 * Allow third-party plugins to add adapters
 * Usage: add_filter('tmnl_source_adapters', function($adapters) {
 *     $adapters['my_source'] = new My_Adapter();
 *     return $adapters;
 * });
 */
$tmnl_adapters = apply_filters( 'tmnl_source_adapters', $tmnl_adapters );

// Activation: create/update DB tables + schedule cron
register_activation_hook( __FILE__, array( 'TMNL_DB', 'create_table' ) );
register_activation_hook( __FILE__, array( 'TMNL_Cron', 'activate' ) );
register_deactivation_hook( __FILE__, array( 'TMNL_Cron', 'deactivate' ) );

// Check for DB version upgrade
add_action( 'admin_init', function() {
    $current = get_option( 'tmnl_db_version', '0' );
    if ( version_compare( $current, TMNL_VERSION, '<' ) ) {
        TMNL_DB::create_table();
    }
});

// Init admin
if ( is_admin() ) {
    $tmnl_admin = new TMNL_Admin();
    $tmnl_admin->set_adapters( $tmnl_adapters );
    new TMNL_SNS_Collector( $tmnl_adapters['x_embed'] );
}

// Init shortcode
$tmnl_shortcode = new TMNL_Shortcode();
$tmnl_shortcode->set_adapters( $tmnl_adapters );

// Init Gutenberg block
new TMNL_Block( $tmnl_shortcode );

// Init WP-Cron auto cache refresh
new TMNL_Cron( $tmnl_adapters['google'] );

// Init Pro-2: JSON-LD structured data output
new TMNL_JsonLD( $tmnl_adapters['google'] );

// Init Pro-3: QR code generation
new TMNL_QR( $tmnl_adapters['google'] );
