<?php

/**
 * Freemius SDK initialization for Yatoon Social Feed.
 *
 * IMPORTANT: this codebase represents the PREMIUM/full source. Do not
 * maintain a separate "free" copy by hand - instead, zip this single
 * folder and upload it via Freemius Dashboard > Deployment > Add New
 * Version. Freemius's deployment preprocessor automatically generates
 * the Free version by:
 *   1. Flipping `is_premium` to false.
 *   2. Stripping any `if` block that calls a `*__premium_only()` method
 *      (see includes/class-shortcode.php and admin/class-admin.php for
 *      where the "by Yatoon.com" credit / upgrade prompt are gated).
 *
 * @see https://freemius.com/help/documentation/getting-started/integration-with-sdk/
 *
 * @package Yatoon_Social_Feed
 */
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
if ( !function_exists( 'yatoon_sf_fs' ) ) {
    // Create a helper function for easy SDK access.
    function yatoon_sf_fs() {
        global $yatoon_sf_fs;
        if ( !isset( $yatoon_sf_fs ) ) {
            // Include Freemius SDK.
            require_once dirname( __FILE__ ) . '/vendor/freemius/start.php';
            $yatoon_sf_fs = fs_dynamic_init( array(
                'id'               => '32962',
                'slug'             => 'yatoon-social-feed',
                'premium_slug'     => 'yatoon-social-feed-premium',
                'type'             => 'plugin',
                'public_key'       => 'pk_b44fa0d52538523a662976fd1edab',
                'is_premium'       => false,
                'has_addons'       => false,
                'has_paid_plans'   => true,
                'is_org_compliant' => true,
                'menu'             => array(
                    'slug'    => 'ysf-social-feed-settings',
                    'account' => true,
                    'support' => false,
                ),
                'is_live'          => true,
            ) );
        }
        return $yatoon_sf_fs;
    }

    // Init Freemius.
    yatoon_sf_fs();
    // Signal that SDK was initiated.
    do_action( 'yatoon_sf_fs_loaded' );
    /**
     * Uninstall cleanup, run via Freemius' `after_uninstall` action instead
     * of a standalone uninstall.php file. Freemius does not allow an
     * uninstall.php file in the plugin root because, due to a WordPress
     * limitation, its presence prevents Freemius from reporting the
     * uninstall event (including user feedback) back to the dashboard.
     *
     * Freemius already checks internally whether the sibling Free/Premium
     * version is still active before firing this action, so we don't need
     * to repeat that check ourselves.
     *
     * @see https://freemius.com/help/documentation/release-management/deployment/?context=wordpress#uninstallphp-file
     */
    function yatoon_sf_after_uninstall_cleanup() {
        if ( is_multisite() ) {
            global $wpdb;
            // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
            $site_ids = $wpdb->get_col( "SELECT blog_id FROM {$wpdb->blogs}" );
            foreach ( $site_ids as $site_id ) {
                switch_to_blog( $site_id );
                yatoon_sf_after_uninstall_cleanup_single_site();
                restore_current_blog();
            }
        } else {
            yatoon_sf_after_uninstall_cleanup_single_site();
        }
    }

    function yatoon_sf_after_uninstall_cleanup_single_site() {
        global $wpdb;
        // phpcs:disable WordPress.DB.DirectDatabaseQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
        $option_names = $wpdb->get_col( $wpdb->prepare( "SELECT option_name FROM {$wpdb->options} WHERE option_name LIKE %s", $wpdb->esc_like( 'yatoon_sf_' ) . '%' ) );
        // phpcs:enable
        foreach ( $option_names as $option_name ) {
            delete_option( $option_name );
            delete_site_option( $option_name );
        }
        $table = $wpdb->prefix . 'yatoon_sf_photos';
        // phpcs:disable WordPress.DB.DirectDatabaseQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, WordPress.DB.PreparedSQL.NotPrepared
        $wpdb->query( "DROP TABLE IF EXISTS " . esc_sql( $table ) );
        // phpcs:enable
        wp_clear_scheduled_hook( 'yatoon_sf_auto_sync' );

        // Remove images this plugin cached in the uploads folder while
        // syncing (see Yatoon_SF_Sync::cache_remote_image()).
        $upload = wp_upload_dir();
        if ( empty( $upload['error'] ) && ! empty( $upload['basedir'] ) ) {
            $cache_dir = trailingslashit( $upload['basedir'] ) . 'yatoon-social-feed';
            if ( is_dir( $cache_dir ) ) {
                if ( ! function_exists( 'WP_Filesystem' ) ) {
                    require_once ABSPATH . 'wp-admin/includes/file.php';
                }
                WP_Filesystem();
                global $wp_filesystem;
                if ( $wp_filesystem ) {
                    $wp_filesystem->rmdir( $cache_dir, true );
                }
            }
        }
    }

    yatoon_sf_fs()->add_action( 'after_uninstall', 'yatoon_sf_after_uninstall_cleanup' );
}
