<?php

/**
 * Plugin Name: Broken Image Fallback
 * Plugin URI:
 * Description: Replace broken or missing images in WordPress with a custom fallback image. Prevent broken images after migrations, protect WooCommerce product images, and optimize site performance.
 * Version: 1.0.0
 * Author: Nikunj Hatkar
 * Author URI: https://github.com/nikunj8866
 * License: GPL v2 or later
 * Text Domain: broken-image-fallback
 * Domain Path: /languages
 * Requires at least: 5.6
 * Tested up to: 6.8
 * Requires PHP: 7.4
 *
 */
// Prevent direct access
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
// Create a helper function for retrieving our Freemius instance
if ( function_exists( 'brokimfa_fs' ) ) {
    brokimfa_fs()->set_basename( false, __FILE__ );
} else {
    // Include Freemius SDK
    require_once dirname( __FILE__ ) . '/vendor/autoload.php';
    // Define plugin constants
    define( 'BROKIMFA_VERSION', '1.0.0' );
    define( 'BROKIMFA_PLUGIN_FILE', __FILE__ );
    define( 'BROKIMFA_PLUGIN_PATH', plugin_dir_path( __FILE__ ) );
    define( 'BROKIMFA_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
    define( 'BROKIMFA_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
    function brokimfa_fs() {
        global $brokimfa_fs;
        if ( !isset( $brokimfa_fs ) ) {
            $brokimfa_fs = fs_dynamic_init( array(
                'id'               => '20451',
                'slug'             => 'broken-image-fallback',
                'premium_slug'     => 'broken-image-fallback-pro',
                'type'             => 'plugin',
                'public_key'       => 'pk_d49861b94e87f501fc47e8ac664e1',
                'is_premium'       => false,
                'premium_suffix'   => 'PRO',
                'has_addons'       => false,
                'has_paid_plans'   => true,
                'menu'             => array(
                    'slug' => 'broken-image-fallback',
                ),
                'is_live'          => true,
                'is_org_compliant' => true,
            ) );
        }
        return $brokimfa_fs;
    }

    // Init Freemius
    brokimfa_fs();
    // Signal that SDK was initiated
    do_action( 'brokimfa_fs_loaded' );
    // Include required files
    require_once BROKIMFA_PLUGIN_PATH . 'includes/class-broken-image-fallback.php';
    // Initialize the plugin
    function brokimfa_init_plugin() {
        new Broken_Image_Fallback();
    }

    add_action( 'plugins_loaded', 'brokimfa_init_plugin' );
    // Activation hook
    register_activation_hook( __FILE__, 'brokimfa_activate_plugin' );
    function brokimfa_activate_plugin() {
        $default_options = array(
            'fallback_method'           => 'default_image',
            'default_image_url'         => BROKIMFA_PLUGIN_URL . 'assets/default-image.svg',
            'lazy_loading_compat'       => true,
            'alt_text_style'            => 'background: #f5f5f5; padding: 20px; text-align: center; color: #666; border: 1px dashed #ccc; display: inline-block; min-width: 200px; min-height: 100px; font-family: Arial, sans-serif;',
            'fade_duration'             => 300,
            'hide_completely'           => false,
            'enable_featured_image'     => false,
            'featured_image_url'        => '',
            'show_placeholder_featured' => false,
            'featured_placeholder_url'  => '',
            'enable_gutenberg'          => false,
        );
        add_option( 'brokimfa_options', $default_options );
        // Set a transient to redirect to settings page
        set_transient( 'brokimfa_activation_redirect', true, 30 );
    }

    // Deactivation hook
    register_deactivation_hook( __FILE__, 'brokimfa_deactivate_plugin' );
    function brokimfa_deactivate_plugin() {
        // Clean up transients
        delete_transient( 'brokimfa_activation_redirect' );
    }

    // Redirect to settings page after activation
    add_action( 'admin_init', 'brokimfa_activation_redirect' );
    function brokimfa_activation_redirect() {
        if ( get_transient( 'brokimfa_activation_redirect' ) ) {
            delete_transient( 'brokimfa_activation_redirect' );
            // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- This nonce verification is not needed here.
            if ( !isset( $_GET['activate-multi'] ) ) {
                wp_redirect( admin_url( 'admin.php?page=broken-image-fallback' ) );
                exit;
            }
        }
    }

    function brokimfa_uninstall_cleanup() {
        // Delete plugin options
        delete_option( 'brokimfa_options' );
        // Delete transients
        delete_transient( 'brokimfa_activation_redirect' );
        // Clean up any uploaded images
        $upload_dir = wp_upload_dir();
        $plugin_upload_dir = $upload_dir['basedir'] . '/broken-image-fallback/';
        if ( is_dir( $plugin_upload_dir ) ) {
            $files = glob( $plugin_upload_dir . '*' );
            foreach ( $files as $file ) {
                if ( is_file( $file ) ) {
                    wp_delete_file( $file );
                }
            }
            global $wp_filesystem;
            if ( !function_exists( 'WP_Filesystem' ) ) {
                require_once ABSPATH . 'wp-admin/includes/file.php';
            }
            WP_Filesystem();
            if ( $wp_filesystem->is_dir( $plugin_upload_dir ) ) {
                $wp_filesystem->rmdir( $plugin_upload_dir, true );
            }
        }
    }

    brokimfa_fs()->add_action( 'after_uninstall', 'brokimfa_uninstall_cleanup' );
}