<?php
/**
 * Plugin Name:       CodeDE Table Import/Export for Excel & CSV
 * Plugin URI:        https://wp-code.de/codede-table-import-export
 * Description:       Upload Excel (.xlsx, .xls) and CSV files and display them as clean, responsive tables anywhere on your WordPress site using a simple shortcode.
 * Version:           1.8.0
 * Requires at least: 6.0
 * Requires PHP:      8.0
 * Author:            wpcodede
 * Author URI:        https://wp-code.de
 * License:           GPL-2.0-or-later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       codede-table-viewer
 * Domain Path:       /languages
 */

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

// Plugin constants
define( 'CDTV_VERSION',         '1.8.0' );
define( 'CDTV_PLUGIN_FILE',     __FILE__ );
define( 'CDTV_PLUGIN_DIR',      plugin_dir_path( __FILE__ ) );
define( 'CDTV_PLUGIN_URL',      plugin_dir_url( __FILE__ ) );
define( 'CDTV_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
define( 'CDTV_UPLOAD_DIR',      'cdtv' );

/* ==========================================================================
   Freemius SDK – License & Checkout
   ========================================================================== */

if ( ! function_exists( 'cdtv_freemius' ) ) {
    function cdtv_freemius() {
        global $cdtv_freemius;

        if ( ! isset( $cdtv_freemius ) ) {
            $sdk_start = CDTV_PLUGIN_DIR . 'vendor/freemius/start.php';

            if ( ! file_exists( $sdk_start ) ) {
                $cdtv_freemius = null;
                return null;
            }

            require_once $sdk_start;

            $cdtv_freemius = fs_dynamic_init( array(
                'id'                  => '27424',
                'slug'                => 'codede-table-viewer',
                'type'                => 'plugin',
                'public_key'          => 'pk_e7678291dc14cda61fed0c184de85',
                'is_premium'          => true,
                'has_premium_version' => false,
                'has_addons'          => false,
                'has_paid_plans'      => true,
                'menu'                => array(
                    'slug'    => 'cdtv-tables',
                    'account' => true,
                    'pricing' => false,
                    'contact' => false,
                    'support' => false,
                ),
                'is_live'             => true,
            ) );
        }

        return $cdtv_freemius;
    }

    cdtv_freemius();
}

/**
 * Check whether a valid Pro license is activated on this site.
 *
 * @return bool
 */
function cdtv_is_licensed(): bool {
    $fs = cdtv_freemius();
    if ( null === $fs ) {
        return false;
    }
    return $fs->can_use_premium_code();
}

/* ==========================================================================
   Core includes
   ========================================================================== */

require_once CDTV_PLUGIN_DIR . 'includes/class-cdtv-loader.php';
require_once CDTV_PLUGIN_DIR . 'includes/class-cdtv-activator.php';
require_once CDTV_PLUGIN_DIR . 'includes/class-cdtv-deactivator.php';
require_once CDTV_PLUGIN_DIR . 'includes/class-cdtv-parser.php';
require_once CDTV_PLUGIN_DIR . 'includes/class-cdtv-core.php';

// Activation / Deactivation hooks
register_activation_hook( __FILE__, array( 'CDTV_Activator', 'activate' ) );
register_deactivation_hook( __FILE__, array( 'CDTV_Deactivator', 'deactivate' ) );

// Uninstall cleanup via hook (no uninstall.php)
function cdtv_fs_uninstall_cleanup() {
    $tables = get_posts( array(
        'post_type'      => 'cdtv_table',
        'posts_per_page' => -1,
        'post_status'    => 'any',
    ) );
    foreach ( $tables as $table ) {
        $file_path = get_post_meta( $table->ID, '_cdtv_file_path', true );
        if ( $file_path && file_exists( $file_path ) ) {
            wp_delete_file( $file_path );
        }
        wp_delete_post( $table->ID, true );
    }
    $options = array( 'cdtv_version', 'cdtv_language', 'cdtv_decimal_separator' );
    foreach ( $options as $option ) {
        delete_option( $option );
    }
    global $wp_filesystem;
    if ( empty( $wp_filesystem ) ) {
        require_once ABSPATH . 'wp-admin/includes/file.php';
        WP_Filesystem();
    }
    $upload_dir = wp_upload_dir();
    $cdtv_dir   = $upload_dir['basedir'] . '/cdtv';
    if ( is_dir( $cdtv_dir ) ) {
        foreach ( glob( $cdtv_dir . '/*' ) ?: array() as $file ) {
            if ( is_file( $file ) ) wp_delete_file( $file );
        }
        $wp_filesystem->rmdir( $cdtv_dir );
    }
}
register_uninstall_hook( __FILE__, 'cdtv_fs_uninstall_cleanup' );

/**
 * Begin plugin execution.
 */
function cdtv_run() {
    $plugin = new CDTV_Core();
    $plugin->run();
}
cdtv_run();
