<?php
/**
 * Wires the plugin into WordPress.
 *
 * @package SkyServers\Retragere
 */

declare(strict_types=1);

namespace SkyServers\Retragere\Wp;

/**
 * Registration only. No behaviour lives here.
 */
final class Plugin
{
    public function boot(): void
    {
        // Only the paid build carries languages/. On wordpress.org the
        // translations arrive from translate.wordpress.org and nothing has to
        // ask for them.
        if (file_exists(SSRC_PATH . 'src/Pro/BundledTranslations.php')) {
            \SkyServers\Retragere\Pro\BundledTranslations::register();
        }
        add_action('init', [PostType::class, 'register']);
        add_action('pre_get_posts', [PostType::class, 'filterAdminQuery']);
        add_action('init', [Privacy::class, 'register']);
        add_action(Retention::HOOK, [Retention::class, 'purge']);
        add_action('before_woocommerce_init', ['\SkyServers\Retragere\Wc\Bootstrap', 'declareCompatibility']);
        add_action('init', ['\SkyServers\Retragere\Wc\Bootstrap', 'register'], 20);
        add_action('init', [Shortcode::class, 'register']);
        add_action('init', [FormHandler::class, 'register']);
        // Priority 30 puts Settings after the Pro screens, which register at
        // 20 and 21. Configuration belongs at the bottom of a menu, not second.
        add_action('admin_menu', [Settings::class, 'registerPage'], 30);
        add_action('admin_init', [Settings::class, 'registerFields']);
        add_action('admin_init', [Admin\Registry::class, 'register']);
        add_action('admin_notices', [Activation::class, 'notices']);
        add_action(Mailer::HOOK_RETRY, [Mailer::class, 'onRetry']);
        add_action('init', [Settlement::class, 'register']);
        // src/Pro/ is marked @fs_premium_only, so Freemius removes it from the
        // free build. Whether the paid code is here is a question about files
        // on disk, not about a licence: class_exists() would route through
        // composer's optimised classmap and try to include a file that is gone.
        if (file_exists(SSRC_PATH . 'src/Pro/Gate.php')) {
            add_action('init', ['\SkyServers\Retragere\Pro\Gate', 'register'], 30);
        }
    }

    /**
     * The single place that decides whether Pro features are available.
     *
     * Only src/Pro/ asks this, and src/Pro/ is absent from the free build, so
     * nothing in the plugin published to wordpress.org ever calls it.
     */
    public static function isPremium(): bool
    {
        // Development override, so both tiers can be exercised on a test site
        // without issuing a licence.
        if (defined('SSRC_PREMIUM')) {
            return (bool) constant('SSRC_PREMIUM');
        }

        if (!function_exists('ssrc_fs')) {
            return (bool) apply_filters('ssrc_is_premium', false);
        }

        $fs = ssrc_fs();

        // Two questions, and both have to be answered yes.
        //
        // is_premium() asks which build this is. Freemius flips the flag to
        // false in the free build it publishes to wordpress.org, where src/Pro/
        // has been stripped. Without this, a paying customer who installed the
        // free ZIP would send the runtime looking for classes that are not on
        // disk, and the plugin would fatal on init rather than degrade.
        //
        // is_paying() or is_trial() asks about the licence, which keeps the
        // premium build inert once a licence lapses or is refunded.
        return $fs->is_premium() && ($fs->is_paying() || $fs->is_trial());
    }
}
