<?php
/**
 * Plugin Name: Jalpi Page Builder
 * Description: A modern, lightweight WordPress visual page builder with clean HTML output and fast performance.
 * Version:     1.0.3
 * Requires at least: 6.0
 * Author:      jalpi
 * License:     GPL-2.0-or-later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: jalpi-page-builder
 * Domain Path: /languages
 * Requires PHP: 8.0
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 */

defined( 'ABSPATH' ) || exit;

define( 'JALPI_VERSION',     '1.0.3' );
define( 'JALPI_FILE',        __FILE__ );
define( 'JALPI_DIR',         plugin_dir_path( __FILE__ ) );
define( 'JALPI_URL',         plugin_dir_url( __FILE__ ) );
define( 'JALPI_ASSETS_URL',  JALPI_URL . 'assets/' );
define( 'JALPI_ADMIN_URL',   JALPI_URL . 'admin/' );

// ── Autoloader ───────────────────────────────────────────────────────────────
// Maps Jalpi_* classes to includes/class-jalpi-*.php and Jalpi_Element_* classes to
// includes/elements/class-jalpi-element-*.php.  Replaces the manual require_once
// list so new classes are loaded automatically.
spl_autoload_register( static function ( string $class ) {
    // Only handle Jalpi_ prefixed classes.
    if ( 0 !== strpos( $class, 'Jalpi_' ) ) {
        return;
    }

    // Jalpi_Element_Heading → element-heading, Jalpi_Theme_Builder → theme-builder, etc.
    $suffix = substr( $class, 6 ); // strip 'Jalpi_'
    $file   = 'class-jalpi-' . strtolower( str_replace( '_', '-', $suffix ) ) . '.php';

    // Element classes live in includes/elements/
    if ( 0 === strpos( $suffix, 'Element_' ) ) {
        $path = JALPI_DIR . 'includes/elements/' . $file;
    } else {
        $path = JALPI_DIR . 'includes/' . $file;
    }

    if ( file_exists( $path ) ) {
        require_once $path;
    }
} );

// Schema-driven widget definitions (plain functions, not a class — the
// autoloader only handles Jalpi_* classes). Loaded eagerly so the sanitizer
// and generic element can consult it. See includes/widgets-schema.php.
require_once JALPI_DIR . 'includes/widgets-schema.php';

// Shared Split Text config builder. A trait, so the autoloader (classes only)
// will not find it — required eagerly because element classes `use` it at
// class-definition time.
require_once JALPI_DIR . 'includes/trait-jalpi-split-text.php';

// Support Center — a self-contained module (PHP + CSS + JS in one folder).
// The autoloader only scans includes/ and includes/elements/, so this nested
// module is required explicitly. To remove the feature entirely: delete
// includes/support/ and this require plus the instance() call in jalpi_init().
require_once JALPI_DIR . 'includes/support/class-jalpi-support-center.php';

// TEMPORARY: header-template diagnostic (visit any page with ?jalpi_diag=1 as
// an admin). Delete this file and this require once the issue is resolved.
if ( file_exists( JALPI_DIR . 'jalpi-header-diagnostic.php' ) ) {
    require_once JALPI_DIR . 'jalpi-header-diagnostic.php';
}

register_activation_hook( JALPI_FILE, [ 'Jalpi_Activator', 'activate' ] );
register_deactivation_hook( JALPI_FILE, [ 'Jalpi_Activator', 'deactivate' ] );

// Multisite: create tables when a new blog is created
add_action( 'wp_initialize_site', function( $new_site ) {
    switch_to_blog( $new_site->blog_id );
    Jalpi_Activator::activate();
    restore_current_blog();
} );

add_action( 'plugins_loaded', 'jalpi_init' );

function jalpi_init() {
    Jalpi_Activator::maybe_upgrade();

    Jalpi_Settings::instance();
    Jalpi_API::instance();
    Jalpi_Editor::instance();
    Jalpi_Post_Support::instance();
    Jalpi_Renderer::instance();
    Jalpi_Templates::instance();
    Jalpi_Template_Preview::instance();
    Jalpi_Bundled_Templates::instance();
    Jalpi_Revisions::instance();
    Jalpi_Forms::instance();
    Jalpi_Theme_Builder::instance();
    Jalpi_Support_Center::instance();
    Jalpi_Theme_API::instance();
    Jalpi_Class_Manager::instance();
    Jalpi_Classes_API::instance();

    // WooCommerce storefront theming for imported e-commerce kits (no-op unless
    // WooCommerce is active). Autoloaded via the Jalpi_* autoloader.
    if ( class_exists( 'Jalpi_Woo' ) ) {
        Jalpi_Woo::init_frontend();
    }

    // Products Grid AJAX (pagination + add-to-cart). Registered unconditionally
    // so the grid still paginates its placeholder products when WooCommerce is
    // inactive; the cart endpoint self-guards on WC being loaded.
    Jalpi_Woo_Ajax::instance();

    if ( function_exists( 'wp_add_privacy_policy_content' ) ) {
        wp_add_privacy_policy_content(
            __( 'Jalpi Page Builder', 'jalpi-page-builder' ),
            '<p>' . esc_html__(
                'This plugin loads web fonts from Google Fonts (fonts.googleapis.com, fonts.gstatic.com) and icon libraries from Cloudflare CDN (cdnjs.cloudflare.com) and jsDelivr (cdn.jsdelivr.net) when they are used in your page designs. These services may log visitor IP addresses as part of standard CDN operation. No personal data is collected or transmitted by Jalpi Page Builder itself.',
                'jalpi-page-builder'
            ) . '</p>'
        );
    }
}
