<?php

/**
 * Plugin Name:       Donatico
 * Plugin URI:        https://wordpress.org/plugins/donatico/
 * Description:       Accept donations via Stripe. Clean, lightweight, and built for non-profits, content creators, podcasters, and churches.
 * Version:           1.6.0
 * Requires at least: 5.3
 * Requires PHP:      7.4
 * Author:            mamunitiw
 * Author URI:        https://profiles.wordpress.org/mamunitiw/
 * License:           GPL-2.0-or-later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       donatico
 *
 * @package Donatico
 *
 */
declare (strict_types = 1);
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
if ( !function_exists( 'don_fs' ) ) {
    // Create a helper function for easy SDK access.
    // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- Freemius SDK convention; renaming breaks the integration.
    function don_fs() {
        // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Freemius SDK convention; renaming breaks the integration.
        global $don_fs;
        if ( !isset( $don_fs ) ) {
            // Include Freemius SDK.
            require_once dirname( __FILE__ ) . '/vendor/freemius/start.php';
            // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Freemius SDK convention; renaming breaks the integration.
            $don_fs = fs_dynamic_init( array(
                'id'               => '30154',
                'slug'             => 'donatico',
                'type'             => 'plugin',
                'public_key'       => 'pk_bf92d92704c1722fff51836617e97',
                'is_premium'       => false,
                'has_addons'       => false,
                'has_paid_plans'   => true,
                'is_org_compliant' => true,
                'menu'             => array(
                    'support' => false,
                ),
                'is_live'          => true,
            ) );
        }
        return $don_fs;
    }

    // Init Freemius.
    don_fs();
    // Register uninstall cleanup on the Freemius after_uninstall hook.
    don_fs()->add_action( 'after_uninstall', 'donatico_uninstall_cleanup' );
    // Signal that SDK was initiated.
    // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Freemius SDK convention; renaming breaks the integration.
    do_action( 'don_fs_loaded' );
}
/**
 * Clean up all plugin data when the plugin is fully uninstalled.
 * Registered on the Freemius after_uninstall hook.
 *
 * @return void
 */
function donatico_uninstall_cleanup() : void {
    global $wpdb;
    // Drop the donations table.
    // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, WordPress.DB.PreparedSQL.NotPrepared
    $wpdb->query( 'DROP TABLE IF EXISTS `' . $wpdb->prefix . 'donatico_donations`' );
    // Delete all plugin options.
    // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
    $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s", $wpdb->esc_like( 'donatico_' ) . '%' ) );
    // Clear scheduled cron events.
    wp_clear_scheduled_hook( 'donatico_daily_cleanup' );
    // Delete transients.
    // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
    $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s OR option_name LIKE %s", $wpdb->esc_like( '_transient_donatico_' ) . '%', $wpdb->esc_like( '_transient_timeout_donatico_' ) . '%' ) );
}

// Plugin constants.
define( 'DONATICO_VERSION', '1.6.0' );
define( 'DONATICO_PLUGIN_FILE', __FILE__ );
define( 'DONATICO_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'DONATICO_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
define( 'DONATICO_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
// Require Composer autoloader.
if ( !file_exists( DONATICO_PLUGIN_DIR . 'vendor/autoload.php' ) ) {
    add_action( 'admin_notices', function () {
        echo '<div class="notice notice-error"><p>' . esc_html__( 'Donatico: Composer dependencies are missing. Please run composer install.', 'donatico' ) . '</p></div>';
    } );
    return;
}
require_once DONATICO_PLUGIN_DIR . 'vendor/autoload.php';
// Activation and deactivation hooks.
register_activation_hook( __FILE__, array('Donatico\\Activator', 'activate') );
register_deactivation_hook( __FILE__, array('Donatico\\Deactivator', 'deactivate') );
// Boot the plugin.
add_action( 'plugins_loaded', function () {
    Donatico\Plugin::getInstance()->run();
} );
// Schema upgrades for existing installs — Free has no reactivation-based
// upgrade path, so run the version-gated column check on admin_init.
add_action( 'admin_init', array('\\Donatico\\Database\\Schema', 'maybeUpgrade') );
/**
 * Whether premium features should be available (valid license or active trial).
 *
 * @return bool
 */
if ( !function_exists( 'donatico_can_use_premium' ) ) {
    function donatico_can_use_premium() : bool {
        return function_exists( 'don_fs' ) && don_fs()->can_use_premium_code();
    }

}