<?php
/**
 * Plugin Name: Help4 Builder Suite
 * Plugin URI:  https://help4wordpress.com/plugin-help4-builder-suite/
 * Description: Help4 all-in-one builder foundation (templates, slider, SEO, commerce engine, and vertical packs).
 * Version:     1.3.402
 * Author:      Help4 WordPress
 * Author URI:  https://help4wordpress.com/about-us/
 * Update URI:  https://help4wordpress.com/help4-builder-suite/
 * Requires at least: 6.5
 * Requires PHP: 8.1
 * Text Domain: help4-builder-suite
 * License:     GPLv2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 */

declare(strict_types=1);

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

define('H4BS_VERSION', '1.3.402');
define('H4BS_BUILD_HASH', '58bc7d45edacec7d6f9328434f83b0aa4f1cd6d8404d9c83a8ac10210e94a8c0');
define('H4BS_FILE', __FILE__);
define('H4BS_PATH', plugin_dir_path(__FILE__));
define(
    'H4BS_URL',
    set_url_scheme(
        plugin_dir_url(__FILE__),
        (string) (wp_parse_url(home_url('/'), PHP_URL_SCHEME) ?: (is_ssl() ? 'https' : 'http'))
    )
);

$h4bsCurrentHost = static function (): string {
    $host = '';
    $homeHost = wp_parse_url(home_url(), PHP_URL_HOST);
    if (is_string($homeHost)) {
        $host = strtolower($homeHost);
    }

    if ($host === '' && isset($_SERVER['HTTP_HOST'])) {
        $host = strtolower(sanitize_text_field((string) wp_unslash($_SERVER['HTTP_HOST'])));
        $host = (string) preg_replace('/:\d+$/', '', $host);
    }

    return $host;
};

$h4bsIsHelp4OwnedHost = static function () use ($h4bsCurrentHost): bool {
    return in_array($h4bsCurrentHost(), [
        'help4wordpress.com',
        'www.help4wordpress.com',
        'md.help4wordpress.com',
        'plugin.help4wordpress.com',
        'plugins.help4wordpress.com',
    ], true);
};

$h4bsIsHelp4MarketingHost = static function () use ($h4bsCurrentHost): bool {
    return in_array($h4bsCurrentHost(), [
        'help4wordpress.com',
        'www.help4wordpress.com',
        'md.help4wordpress.com',
    ], true);
};

add_filter('body_class', static function (array $classes) use ($h4bsIsHelp4MarketingHost): array {
    if ($h4bsIsHelp4MarketingHost()) {
        $classes[] = 'h4bs-help4wordpress-site';
    }

    return array_values(array_unique($classes));
}, 5);

add_action('wp_enqueue_scripts', static function () use ($h4bsIsHelp4MarketingHost): void {
    if (! $h4bsIsHelp4MarketingHost()) {
        return;
    }

    $skinPath = H4BS_PATH . 'assets/css/help4-site-skin.css';
    if (! is_readable($skinPath)) {
        return;
    }

    $dependencies = ['h4bs-frontend'];
    if (wp_style_is('help4-blank-style', 'enqueued') || wp_style_is('help4-blank-style', 'registered')) {
        array_unshift($dependencies, 'help4-blank-style');
    }

    wp_enqueue_style(
        'h4bs-help4-site-skin',
        H4BS_URL . 'assets/css/help4-site-skin.css',
        $dependencies,
        H4BS_VERSION . '-' . (string) filemtime($skinPath)
    );

    $navPath = H4BS_PATH . 'assets/js/help4-site-nav.js';
    if (is_readable($navPath)) {
        wp_enqueue_script(
            'h4bs-help4-site-nav',
            H4BS_URL . 'assets/js/help4-site-nav.js',
            [],
            H4BS_VERSION,
            true
        );
    }

    $tutorialSearchPath = H4BS_PATH . 'assets/js/tutorial-search.js';
    if (is_page('help4-builder-tutorials') && is_readable($tutorialSearchPath)) {
        wp_enqueue_script(
            'h4bs-tutorial-search',
            H4BS_URL . 'assets/js/tutorial-search.js',
            [],
            H4BS_VERSION,
            true
        );
    }
}, 130);

add_action('init', static function () use ($h4bsIsHelp4OwnedHost): void {
    if (! $h4bsIsHelp4OwnedHost()) {
        return;
    }

    // Help4-owned release sites use the plugin fallback icon when no site icon is set.
    remove_action('wp_head', 'wp_site_icon', 99);
    remove_action('admin_head', 'wp_site_icon', 99);
    remove_action('login_head', 'wp_site_icon', 10);
}, 30);

$h4bsRenderFavicon = static function () use ($h4bsIsHelp4OwnedHost): void {
    if (! $h4bsIsHelp4OwnedHost()) {
        return;
    }

    $assetVersion = H4BS_VERSION !== '' ? H4BS_VERSION : (string) time();
    $icon32 = (string) get_site_icon_url(32);
    $icon192 = (string) get_site_icon_url(192);
    $icon180 = (string) get_site_icon_url(180);
    $icon270 = (string) get_site_icon_url(270);

    $fallback = H4BS_URL . 'assets/images/help4-favicon.png';
    if ($icon32 === '') {
        $icon32 = $fallback;
    }
    if ($icon192 === '') {
        $icon192 = $fallback;
    }
    if ($icon180 === '') {
        $icon180 = $fallback;
    }
    if ($icon270 === '') {
        $icon270 = $fallback;
    }

    $icon32 = add_query_arg('v', $assetVersion, $icon32);
    $icon192 = add_query_arg('v', $assetVersion, $icon192);
    $icon180 = add_query_arg('v', $assetVersion, $icon180);
    $icon270 = add_query_arg('v', $assetVersion, $icon270);

    echo '<link rel="shortcut icon" href="' . esc_url($icon32) . '" />' . "\n";
    echo '<link rel="icon" href="' . esc_url($icon32) . '" sizes="32x32" />' . "\n";
    echo '<link rel="icon" href="' . esc_url($icon192) . '" sizes="192x192" />' . "\n";
    echo '<link rel="apple-touch-icon" href="' . esc_url($icon180) . '" />' . "\n";
    echo '<meta name="msapplication-TileImage" content="' . esc_url($icon270) . '" />' . "\n";
};

add_action('wp_head', $h4bsRenderFavicon, 1);
add_action('admin_head', $h4bsRenderFavicon, 1);
add_action('login_head', $h4bsRenderFavicon, 1);

require_once H4BS_PATH . 'includes/Autoloader.php';
\Help4BuilderSuite\Autoloader::register(H4BS_PATH . 'includes');

register_activation_hook(__FILE__, [\Help4BuilderSuite\Database\Installer::class, 'install']);

add_action('plugins_loaded', static function (): void {
    $dbVersion = (string) get_option('h4bs_db_version', '');
    if ($dbVersion !== H4BS_VERSION) {
        \Help4BuilderSuite\Database\Installer::install();
        \Help4BuilderSuite\Compatibility\BrandCleanup::maybeRun();
        update_option('h4bs_db_version', H4BS_VERSION);
    }

    (new \Help4BuilderSuite\Plugin())->boot();
});
