<?php

/**
 * Freemius SDK bootstrap.
 *
 * Global namespace on purpose: readjutsu_fs() must be reachable from the
 * ReadJutsu\ classes and from the admin partials alike.
 */
defined( 'ABSPATH' ) or die( 'Something went wrong.' );
if ( !function_exists( 'readjutsu_fs' ) ) {
    /**
     * Freemius instance accessor (singleton).
     *
     * @return Freemius
     */
    function readjutsu_fs() {
        global $readjutsu_fs;
        if ( !isset( $readjutsu_fs ) ) {
            require_once READJUTSU_PLUGIN_PATH . 'vendor/freemius/start.php';
            $readjutsu_fs = fs_dynamic_init( array(
                'id'               => '31399',
                'slug'             => 'readjutsu',
                'premium_slug'     => 'readjutsu-pro',
                'type'             => 'plugin',
                'public_key'       => 'pk_e9ae3aed413c2348cc994ab231da4',
                'is_premium'       => false,
                'premium_suffix'   => 'Pro',
                'has_addons'       => false,
                'has_paid_plans'   => true,
                'is_org_compliant' => true,
                'menu'             => array(
                    'slug'       => 'readjutsu-options',
                    'first-path' => 'admin.php?page=readjutsu-options',
                    'account'    => true,
                    'pricing'    => true,
                    'contact'    => false,
                    'support'    => false,
                ),
                'is_live'          => true,
            ) );
        }
        return $readjutsu_fs;
    }

    readjutsu_fs();
    do_action( 'readjutsu_fs_loaded' );
    readjutsu_fs()->add_action( 'after_uninstall', 'readjutsu_fs_uninstall_cleanup' );
}
if ( !function_exists( 'readjutsu_can_use_premium' ) ) {
    /**
     * Whether premium code is allowed to run (licence, trial, or the
     * READJUTSU_DEV_PREMIUM constant for local testing).
     *
     * @return bool
     */
    function readjutsu_can_use_premium() {
        if ( defined( 'READJUTSU_DEV_PREMIUM' ) && READJUTSU_DEV_PREMIUM ) {
            return true;
        }
        return function_exists( 'readjutsu_fs' ) && readjutsu_fs()->can_use_premium_code();
    }

}
if ( !function_exists( 'readjutsu_upgrade_url' ) ) {
    /**
     * Upgrade link used by all upsell UI.
     *
     * @return string
     */
    function readjutsu_upgrade_url() {
        return readjutsu_fs()->get_upgrade_url();
    }

}
if ( !function_exists( 'readjutsu_show_upsell' ) ) {
    /**
     * Whether upsell UI should render. Checks both conditions: a trial or a
     * dev override can run premium code while still "not paying".
     *
     * @return bool
     */
    function readjutsu_show_upsell() {
        return function_exists( 'readjutsu_fs' ) && readjutsu_fs()->is_not_paying() && !readjutsu_can_use_premium();
    }

}
if ( !function_exists( 'readjutsu_fs_uninstall_cleanup' ) ) {
    /**
     * Uninstall cleanup, hooked on the Freemius after_uninstall event
     * (the SDK replaces WP's uninstall.php flow).
     */
    function readjutsu_fs_uninstall_cleanup() {
        $known_options = array(
            'readjutsu_activate',
            'readjutsu_wpm',
            'readjutsu_post_types',
            // Legacy (pre-1.1, shared post types).
            'readjutsu_include_images',
            'readjutsu_badge_activate',
            'readjutsu_badge_display',
            'readjutsu_badge_post_types',
            'readjutsu_badge_position',
            'readjutsu_badge_style',
            'readjutsu_badge_float_position',
            'readjutsu_badge_icon',
            'readjutsu_badge_prefix',
            'readjutsu_badge_postfix',
            'readjutsu_badge_color',
            'readjutsu_badge_text_color',
            'readjutsu_progress_activate',
            'readjutsu_progress_display',
            'readjutsu_progress_post_types',
            'readjutsu_progress_scope',
            'readjutsu_progress_position',
            'readjutsu_progress_donut_position',
            'readjutsu_progress_donut_size',
            'readjutsu_progress_style',
            'readjutsu_progress_color',
            'readjutsu_progress_color2',
            'readjutsu_progress_bg',
            'readjutsu_progress_height',
            'readjutsu_analytics_activate',
            'readjutsu_analytics_retention',
            'readjutsu_stats_db_version',
        );
        foreach ( $known_options as $option_name ) {
            delete_option( $option_name );
        }
        foreach ( wp_load_alloptions() as $option_name => $option_value ) {
            if ( strpos( $option_name, 'readjutsu_' ) === 0 ) {
                delete_option( $option_name );
            }
        }
        delete_post_meta_by_key( '_readjutsu_override' );
        global $wpdb;
        $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}readjutsu_stats" );
        // phpcs:ignore WordPress.DB
        wp_clear_scheduled_hook( 'readjutsu_analytics_purge' );
    }

}