<?php
/**
 * Plugin Name:       PufferGo | 外贸独立站AI建站专家 | AI Website Builder for Export
 * Plugin URI:        https://puffergo.com/wp
 * Description:       AI website builder built for export businesses. Create high-converting B2B brand sites and landing pages optimized for Google SEO in minutes.
 * Version:           0.23.0
 * Requires at least: 6.7
 * Requires PHP:      7.4
 * Author:            深圳小河豚出海科技有限公司
 * Author URI:        https://PufferGo.com
 * License:           GPL-2.0-or-later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       puffergo
 * Domain Path:       /languages
 *
 * @package PufferGo
 */

if (! defined('ABSPATH')) {
    exit;
} define('PUFFERGO_PLUGIN_FILE', __FILE__);
function puffergo_log($message, $prefix = 'PufferGo')
{
    do_action('qm/debug', $prefix . ': ' . $message);

    if (defined('WP_DEBUG') && WP_DEBUG) {
        error_log($prefix . ': ' . $message);
    }
} define('PUFFERGO_PLUGIN_DIR', __DIR__);
define('PUFFERGO_CORE_VERSION', '0.23.0');
define('PUFFERGO_HAS_DOM', class_exists('DOMDocument'));

if (! defined('PUFFERGO_AI_PAGE_BUILDER_BASE_URL')) {
    define('PUFFERGO_AI_PAGE_BUILDER_BASE_URL', 'https://api.puffergo.com');
}

if (! defined('PUFFERGO_BASE_URL')) {
    define('PUFFERGO_BASE_URL', 'https://puffergo.com');
} require_once PUFFERGO_PLUGIN_DIR . '/php/class-head-purifier.php';
require_once PUFFERGO_PLUGIN_DIR . '/php/class-puffergo-runtime.php';
require_once PUFFERGO_PLUGIN_DIR . '/php/config/class-puffergo-unit-config.php';
require_once PUFFERGO_PLUGIN_DIR . '/php/class-puffergo-unit-formatter.php';
require_once PUFFERGO_PLUGIN_DIR . '/php/class-i18n-loader.php';
require_once PUFFERGO_PLUGIN_DIR . '/php/class-puffergo-frontend-i18n.php';
PufferGo_I18n_Loader::init();
PufferGo_Frontend_I18n::init();
require_once PUFFERGO_PLUGIN_DIR . '/php/class-design-system.php';
require_once PUFFERGO_PLUGIN_DIR . '/php/class-theme-bridge.php';
require_once PUFFERGO_PLUGIN_DIR . '/php/class-design-switcher.php';
new PufferGo_Design_Switcher();
require_once PUFFERGO_PLUGIN_DIR . '/php/class-puffergo-library-registry.php';
require_once PUFFERGO_PLUGIN_DIR . '/php/class-alpine-ssr.php';
require_once PUFFERGO_PLUGIN_DIR . '/php/class-tailwind-container.php';
require_once PUFFERGO_PLUGIN_DIR . '/php/class-puffergo-api-exception.php';
require_once PUFFERGO_PLUGIN_DIR . '/php/class-puffergo-json-object-codec.php';
require_once PUFFERGO_PLUGIN_DIR . '/php/class-puffergo-api-client.php';
require_once PUFFERGO_PLUGIN_DIR . '/php/class-puffergo-workflow-jobs-rest.php';
new PufferGo_Workflow_Jobs_REST();
require_once PUFFERGO_PLUGIN_DIR . '/php/class-puffergo-chat-rest.php';
new PufferGo_Chat_REST();
require_once PUFFERGO_PLUGIN_DIR . '/php/class-telemetry.php';
PufferGo_Telemetry::register_hooks();
require_once PUFFERGO_PLUGIN_DIR . '/php/ai-site-builder.php';
new PufferGo_AI_Site_Builder();
require_once PUFFERGO_PLUGIN_DIR . '/php/site-profile.php';
new PufferGo_Site_Profile();
require_once PUFFERGO_PLUGIN_DIR . '/php/class-puffergo-menu-icons.php';
require_once PUFFERGO_PLUGIN_DIR . '/php/class-puffergo-nav-menu-helper.php';
require_once PUFFERGO_PLUGIN_DIR . '/php/class-puffergo-header-context.php';
require_once PUFFERGO_PLUGIN_DIR . '/php/class-puffergo-footer-context.php';
require_once PUFFERGO_PLUGIN_DIR . '/php/class-puffergo-component-loader.php';
require_once PUFFERGO_PLUGIN_DIR . '/templates/class-puffergo-template-loader.php';
add_action('init', array('PufferGo_Template_Loader', 'init'), 1);
require_once PUFFERGO_PLUGIN_DIR . '/php/template-manager.php';
new PufferGo_Template_Manager();
require_once PUFFERGO_PLUGIN_DIR . '/php/class-puffergo-template-config.php';
$pattern_playground_bootstrap = PUFFERGO_PLUGIN_DIR . '/php/dev/bootstrap-pattern-playground.php';

if (is_readable($pattern_playground_bootstrap)) {
    require_once $pattern_playground_bootstrap;
} require_once PUFFERGO_PLUGIN_DIR . '/php/ai-page-builder.php';
new PufferGo_AI_Page_Builder();
require_once PUFFERGO_PLUGIN_DIR . '/php/puffergo-tool.php';
new PufferGo_Tool();

if (! is_admin()) {
    require_once PUFFERGO_PLUGIN_DIR . '/php/class-analytics.php';
    PufferGo_Analytics::init();
} require_once PUFFERGO_PLUGIN_DIR . '/php/class-contact-form-handler.php';
new PufferGo_Contact_Form_Handler();
require_once PUFFERGO_PLUGIN_DIR . '/php/class-puffergo-business-mode.php';
PufferGo_Business_Mode::init();
require_once PUFFERGO_PLUGIN_DIR . '/php/admin/class-puffergo-module-settings.php';
PufferGo_Module_Settings::init();
require_once PUFFERGO_PLUGIN_DIR . '/php/class-puffergo-global-widgets.php';
new PufferGo_Global_Widgets();
require_once PUFFERGO_PLUGIN_DIR . '/php/class-puffergo-widgets-admin.php';
new PufferGo_Widgets_Admin();
require_once PUFFERGO_PLUGIN_DIR . '/php/cpt/class-puffergo-product-cpt.php';
require_once PUFFERGO_PLUGIN_DIR . '/php/cpt/class-puffergo-solution-cpt.php';
require_once PUFFERGO_PLUGIN_DIR . '/php/cpt/class-puffergo-case-cpt.php';
require_once PUFFERGO_PLUGIN_DIR . '/php/cpt/class-puffergo-testimonial-cpt.php';
puffergo_pause_testimonial_module();
$puffergo_product_cpt = new PufferGo_Product_CPT();
$puffergo_solution_cpt = new PufferGo_Solution_CPT();
$puffergo_case_cpt = new PufferGo_Case_CPT();
$puffergo_testimonial_cpt = new PufferGo_Testimonial_CPT();
require_once PUFFERGO_PLUGIN_DIR . '/php/class-puffergo-product-settings.php';
new PufferGo_Product_Settings();
require_once PUFFERGO_PLUGIN_DIR . '/php/class-puffergo-product-admin.php';
new PufferGo_Product_Admin();
require_once PUFFERGO_PLUGIN_DIR . '/php/polylang-translate/class-puffergo-polylang.php';
PufferGo_Polylang::init();
require_once PUFFERGO_PLUGIN_DIR . '/php/polylang-translate/class-puffergo-shared-slug.php';
PufferGo_Shared_Slug::init();
require_once PUFFERGO_PLUGIN_DIR . '/php/polylang-translate/class-puffergo-language-redirect.php';
PufferGo_Language_Redirect::init();
require_once PUFFERGO_PLUGIN_DIR . '/php/polylang-translate/class-puffergo-polylang-setup.php';
PufferGo_Polylang_Setup::init();
require_once PUFFERGO_PLUGIN_DIR . '/php/class-puffergo-tailwind-block.php';
require_once PUFFERGO_PLUGIN_DIR . '/php/polylang-translate/class-puffergo-html-translatable.php';
require_once PUFFERGO_PLUGIN_DIR . '/php/polylang-translate/class-puffergo-block-translatable.php';
require_once PUFFERGO_PLUGIN_DIR . '/php/polylang-translate/class-puffergo-meta-translatable.php';
require_once PUFFERGO_PLUGIN_DIR . '/php/polylang-translate/class-puffergo-translation-client.php';
require_once PUFFERGO_PLUGIN_DIR . '/php/polylang-translate/class-puffergo-page-translator.php';
require_once PUFFERGO_PLUGIN_DIR . '/php/polylang-translate/class-puffergo-nav-menu-translator.php';
require_once PUFFERGO_PLUGIN_DIR . '/php/polylang-translate/class-puffergo-translation-rest.php';
new PufferGo_Translation_REST();
PufferGo_Nav_Menu_Translator::init();
require_once PUFFERGO_PLUGIN_DIR . '/php/polylang-translate/class-puffergo-string-translator.php';
PufferGo_String_Translator::init();
require_once PUFFERGO_PLUGIN_DIR . '/php/polylang-translate/class-puffergo-languages-admin.php';
PufferGo_Languages_Admin::init();
require_once PUFFERGO_PLUGIN_DIR . '/php/seo/class-puffergo-schema.php';
PufferGo_Schema::init();
require_once PUFFERGO_PLUGIN_DIR . '/php/seo/class-puffergo-hreflang.php';
PufferGo_Hreflang::init();
require_once PUFFERGO_PLUGIN_DIR . '/php/class-puffergo-slug-helper.php';
require_once PUFFERGO_PLUGIN_DIR . '/php/class-puffergo-content-importer.php';
require_once PUFFERGO_PLUGIN_DIR . '/php/class-puffergo-sample-data-seeder.php';
new PufferGo_Sample_Data_Seeder();
require_once PUFFERGO_PLUGIN_DIR . '/php/class-puffergo-product-import.php';
new PufferGo_Product_Import();
add_action('admin_init', 'puffergo_run_db_migrations');
function puffergo_run_db_migrations(): void
{
    $current = get_option('puffergo_db_version', '0');

    if ($current === '0' && get_option('puffergo_gallery_migrated_v2')) {
        update_option('puffergo_db_version', '1.1.0');

        return;
    }

if (version_compare($current, '1.1.0', '<')) {
        puffergo_migrate_gallery_v2();
        update_option('puffergo_db_version', '1.1.0');
    }

if (version_compare($current, '1.2.0', '<')) {
        puffergo_ensure_blog_page();
        update_option('puffergo_db_version', '1.2.0');
    }

if (version_compare($current, '1.3.0', '<')) {
        puffergo_pause_testimonial_module();
        update_option('puffergo_db_version', '1.3.0');
    }

if (version_compare($current, '1.4.0', '<')) {
        if (class_exists('PufferGo_Component_Loader')) {
            PufferGo_Component_Loader::invalidate_cache();
        } update_option('puffergo_db_version', '1.4.0');
    }
} function puffergo_pause_testimonial_module(): void
{
    $enabled = get_option('puffergo_enabled_modules', null);

    if (! is_array($enabled)) {
        return;
    } $next = array_values(array_filter($enabled, static function ($key) { return 'testimonial' !== $key; }));

    if ($next !== array_values($enabled)) {
        update_option('puffergo_enabled_modules', $next);
        update_option('puffergo_rewrite_hash', '');
    }
} function puffergo_migrate_gallery_v2(): void
{
    $products = get_posts(array('post_type' => 'puffergo_product', 'posts_per_page' => -1, 'post_status' => 'any', 'no_found_rows' => true, ));

    foreach ($products as $product) {
        $raw = get_post_meta($product->ID, '_puffergo_gallery', true);

        if (empty($raw)) {
            continue;
        } $items = json_decode($raw, true);

        if (! is_array($items)) {
            continue;
        } $needs_update = false;
        $converted = array();

        foreach ($items as $item) {
            if (is_numeric($item)) {
                $converted[] = array('type' => 'image', 'id' => intval($item));
                $needs_update = true;
            } elseif (is_array($item) && isset($item['type'])) {
                $converted[] = $item;
            }
        }

if ($needs_update) {
            update_post_meta($product->ID, '_puffergo_gallery', wp_json_encode($converted));
        }
    }
} add_action('init', 'puffergo_maybe_flush_rewrite_rules', 99);
function puffergo_maybe_flush_rewrite_rules(): void
{
    $enabled_modules = (array) get_option('puffergo_enabled_modules', PufferGo_Module_Settings::DEFAULT_ENABLED);
    sort($enabled_modules);
    $hash = md5(implode('|', array(get_option('puffergo_product_slug', 'products'), get_option('puffergo_product_category_slug', 'catalog'), get_option('puffergo_solution_slug', 'solutions'), get_option('puffergo_case_slug', 'case-studies'), get_option('puffergo_testimonial_slug', 'testimonials'), implode(',', $enabled_modules), )));

    if (get_option('puffergo_rewrite_hash') !== $hash) {
        flush_rewrite_rules();
        update_option('puffergo_rewrite_hash', $hash);
    }
} require_once PUFFERGO_PLUGIN_DIR . '/php/blog.php';
require_once PUFFERGO_PLUGIN_DIR . '/php/class-plugin-setup.php';
new PufferGo_Plugin_Setup();
require_once PUFFERGO_PLUGIN_DIR . '/php/class-theme-setup.php';
$puffergo_theme_setup = new PufferGo_Theme_Setup();
require_once PUFFERGO_PLUGIN_DIR . '/php/class-puffergo-coming-soon.php';
PufferGo_Coming_Soon::get_instance();

if (is_admin()) {
    require_once PUFFERGO_PLUGIN_DIR . '/php/class-admin-loader.php';
    PufferGo_Admin_Loader::get_instance(PUFFERGO_PLUGIN_DIR);
    require_once PUFFERGO_PLUGIN_DIR . '/php/page-ai-chat.php';
    require_once PUFFERGO_PLUGIN_DIR . '/php/class-puffergo-commands.php';
    PufferGo_Commands::init();
} add_action('template_redirect', function () {
    if (isset($_GET['puffergo_preview_mode']) && $_GET['puffergo_preview_mode'] === '1') {
        show_admin_bar(false);
    }
}, 1);
function puffergo_core_register_admin_blocks()
{
    $admin_blocks = array('dashboard-app', 'ai-site-builder-app', 'settings-app', 'site-profile-app', 'tailwind-container', 'contact-form', 'inquiries-app', );
    $blocks_dir = PUFFERGO_PLUGIN_DIR . '/build/blocks';

    foreach ($admin_blocks as $block_type) {
        $block_path = $blocks_dir . "/{$block_type}";

        if (file_exists($block_path)) {
            register_block_type($block_path);
        }
    }
} add_action('init', 'puffergo_core_register_admin_blocks');
function puffergo_register_nav_menu_locations()
{
    register_nav_menus(array('puffergo_primary' => __('PufferGo Header Menu', 'puffergo'), 'puffergo_footer' => __('PufferGo Footer', 'puffergo'), ));
} add_action('init', 'puffergo_register_nav_menu_locations');
function puffergo_register_block_category($categories)
{
    return array_merge(array(array('slug' => 'puffergo', 'title' => __('PufferGo', 'puffergo'), 'icon' => 'superhero-alt', ), ), $categories);
} add_filter('block_categories_all', 'puffergo_register_block_category', 1);
function puffergo_activate()
{
    require_once PUFFERGO_PLUGIN_DIR . '/php/class-contact-form-handler.php';
    $contact_handler = new PufferGo_Contact_Form_Handler();
    $contact_handler->create_table();

    if (defined('PUFFERGO_CORE_VERSION')) {
        update_option('puffergo_version', PUFFERGO_CORE_VERSION);
    } require_once PUFFERGO_PLUGIN_DIR . '/php/class-telemetry.php';

    if (! get_option('puffergo_download_sent', false)) {
        PufferGo_Telemetry::send_download_event();
        update_option('puffergo_download_sent', '1');
    } PufferGo_Telemetry::send_activate_event();
    global $puffergo_product_cpt, $puffergo_solution_cpt, $puffergo_case_cpt, $puffergo_testimonial_cpt;

    if (! $puffergo_product_cpt) {
        require_once PUFFERGO_PLUGIN_DIR . '/php/admin/class-puffergo-module-settings.php';
        PufferGo_Module_Settings::init();
        require_once PUFFERGO_PLUGIN_DIR . '/php/cpt/class-puffergo-product-cpt.php';
        require_once PUFFERGO_PLUGIN_DIR . '/php/cpt/class-puffergo-solution-cpt.php';
        require_once PUFFERGO_PLUGIN_DIR . '/php/cpt/class-puffergo-case-cpt.php';
        require_once PUFFERGO_PLUGIN_DIR . '/php/cpt/class-puffergo-testimonial-cpt.php';
        $puffergo_product_cpt = new PufferGo_Product_CPT();
        $puffergo_solution_cpt = new PufferGo_Solution_CPT();
        $puffergo_case_cpt = new PufferGo_Case_CPT();
        $puffergo_testimonial_cpt = new PufferGo_Testimonial_CPT();
    } $puffergo_product_cpt->_load_config();
    $puffergo_solution_cpt->_load_config();
    $puffergo_case_cpt->_load_config();
    $puffergo_product_cpt->register_cpt();
    $puffergo_product_cpt->register_taxonomy();
    $puffergo_product_cpt->register_tag_taxonomy();
    $puffergo_solution_cpt->register_cpt();
    $puffergo_solution_cpt->register_taxonomy();
    $puffergo_case_cpt->register_cpt();
    $puffergo_case_cpt->register_taxonomy();

    if (PufferGo_Base_CPT::is_enabled('testimonial')) {
        $puffergo_testimonial_cpt->_load_config();
        $puffergo_testimonial_cpt->register_cpt();
        $puffergo_testimonial_cpt->register_taxonomy();
    } puffergo_ensure_primary_menu();
    puffergo_ensure_footer_menu();
    puffergo_ensure_blog_page();
    delete_option('puffergo_template_appearance');
    delete_option('puffergo_header_appearance');
    flush_rewrite_rules();

    if (! PufferGo_Theme_Setup::is_generatepress_present()) {
        wp_schedule_single_event(time(), PufferGo_Theme_Setup::INSTALL_HOOK);
    }

if (! get_option(PufferGo_Design_System::OPTION_KEY)) {
        wp_schedule_single_event(time(), 'puffergo_init_design_system');
    }
} register_activation_hook(__FILE__, 'puffergo_activate');
add_action('puffergo_init_design_system', 'puffergo_run_init_design_system');
function puffergo_run_init_design_system(): void
{
    if (get_option(PufferGo_Design_System::OPTION_KEY)) {
        return;
    } $response = wp_remote_post(rtrim(PUFFERGO_AI_PAGE_BUILDER_BASE_URL, '/') . '/api/ai/generate-design-tokens', array('headers' => array('Content-Type' => 'application/json'), 'body' => '{}', 'timeout' => 15, ));

    if (is_wp_error($response) || wp_remote_retrieve_response_code($response) !== 200) {
        return;
    } $body = json_decode(wp_remote_retrieve_body($response), true);
    $schemes = isset($body['data']['schemes']) && is_array($body['data']['schemes']) ? $body['data']['schemes'] : array();

    if (empty($schemes)) {
        return;
    } $active = null;

    foreach ($schemes as $s) {
        if (isset($s['id']) && strpos($s['id'], 'apple') !== false) {
            $active = $s;
            break;
        }
    }

if (! $active) {
        $active = $schemes[0];
    } update_option(PufferGo_Design_System::OPTION_KEY, array('schemes' => $schemes, 'activeSchemeId' => $active['id'], 'compiledCss' => $active['compiledCss'] ?? '', 'updatedAt' => gmdate('c'), ));
} function puffergo_ensure_blog_page()
{
    if (get_option('page_for_posts')) {
        return;
    } $existing = get_posts(array('post_type' => 'page', 'post_status' => 'publish', 'name' => 'blog', 'posts_per_page' => 1, 'no_found_rows' => true, ));

    if (! empty($existing)) {
        update_option('page_for_posts', $existing[0]->ID);

        return;
    } $page_id = wp_insert_post(array('post_title' => 'Blog', 'post_name' => 'blog', 'post_status' => 'publish', 'post_type' => 'page', 'post_content' => '', 'comment_status' => 'closed', 'ping_status' => 'closed', ), true, false);

    if (! is_wp_error($page_id)) {
        update_option('page_for_posts', $page_id);
    }
} function puffergo_deactivate()
{
    flush_rewrite_rules();
} register_deactivation_hook(__FILE__, 'puffergo_deactivate');