<?php

/**
 * Plugin Name: Comment Image Boost
 * Plugin URI: https://www.audityseo.com/comment-image-boost
 * Description: Turn WordPress comments into visual social proof with secure image uploads, previews, moderation, and optimized comment galleries.
 * Author: AuditySEO
 * Author URI: https://www.audityseo.com/
 * Version: 1.0.16
 * Text Domain: comment-image-boost
 * Domain Path: /languages
 * Requires at least: 5.8
 * Tested up to: 7.0
 * Requires PHP: 7.4
 * License: GPLv3 or later
 * License URI: https://www.gnu.org/licenses/gpl-3.0.html
 *
 * @package CommentImageBoost
 */
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
define( 'CIBOOST_VERSION', '1.0.16' );
define( 'CIBOOST_PLUGIN_FILE', __FILE__ );
define( 'CIBOOST_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'CIBOOST_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
// ============================================
// FREEMIUS SDK INITIALIZATION
// ============================================
if ( function_exists( 'ca_fs' ) ) {
    ca_fs()->set_basename( false, __FILE__ );
} else {
    /**
     * DO NOT REMOVE THIS IF, IT IS ESSENTIAL FOR THE
     * `function_exists` CALL ABOVE TO PROPERLY WORK.
     */
    if ( !function_exists( 'ca_fs' ) ) {
        /**
         * Create a helper function for easy SDK access.
         *
         * @return \Freemius
         */
        function ca_fs() {
            global $ca_fs;
            if ( !isset( $ca_fs ) ) {
                // Include Freemius SDK.
                require_once dirname( __FILE__ ) . '/vendor/freemius/start.php';
                $ca_fs = fs_dynamic_init( array(
                    'id'               => '29339',
                    'slug'             => 'comment-image-boost',
                    'premium_slug'     => 'comment-attachment-premium',
                    'type'             => 'plugin',
                    'public_key'       => 'pk_7f6dbde37842a2ebcc3920f49e04d',
                    'is_premium'       => false,
                    'premium_suffix'   => 'PRO',
                    'has_addons'       => false,
                    'has_paid_plans'   => true,
                    'is_org_compliant' => true,
                    'menu'             => array(
                        'slug'    => 'comment-image-boost',
                        'account' => true,
                        'pricing' => true,
                        'support' => false,
                    ),
                    'is_live'          => true,
                ) );
            }
            return $ca_fs;
        }

        // Init Freemius.
        ca_fs();
        // Signal that SDK was initiated.
        do_action( 'ca_fs_loaded' );
    }
    // ============================================
    // UNINSTALL CLEANUP
    // ============================================
    /**
     * Uninstall cleanup — called by Freemius after_uninstall hook.
     * Replaces uninstall.php so Freemius can track uninstall events.
     */
    function ca_fs_uninstall_cleanup() {
        // Delete plugin options
        delete_option( 'ciboost_comment_images' );
        delete_option( 'ciboost_image_compression' );
        // Delete comment meta
        delete_metadata(
            'comment',
            0,
            'ciboost_comment_image_id',
            '',
            true
        );
        delete_metadata(
            'comment',
            0,
            'ciboost_comment_image_attachments',
            '',
            true
        );
        delete_metadata(
            'comment',
            0,
            'ciboost_comment_image_captions',
            '',
            true
        );
        delete_metadata(
            'comment',
            0,
            'ciboost_comment_image_status',
            '',
            true
        );
        // Clear cached data
        wp_cache_flush();
    }

    if ( function_exists( 'ca_fs' ) && ca_fs() ) {
        ca_fs()->add_action( 'after_uninstall', 'ca_fs_uninstall_cleanup' );
        ca_fs()->add_filter( 'plugin_icon', 'ca_fs_plugin_icon_path' );
    }
    // ============================================
    // HELPER FUNCTIONS
    // ============================================
    function ca_fs_plugin_icon_path(  $default_icon = false  ) {
        $icon_path = CIBOOST_PLUGIN_DIR . 'assets/logo.png';
        return ( file_exists( $icon_path ) ? $icon_path : $default_icon );
    }

    function ciboost_can_use_premium_features() {
        if ( defined( 'CIBOOST_UNLOCK_PREMIUM_FOR_DEV' ) && CIBOOST_UNLOCK_PREMIUM_FOR_DEV ) {
            return true;
        }
        $fs = ca_fs();
        return $fs && method_exists( $fs, 'can_use_premium_code' ) && $fs->can_use_premium_code();
    }

    function ciboost_get_comment_images_upload_dir() {
        $upload = wp_upload_dir();
        $dir = trailingslashit( $upload['basedir'] ) . 'comment-image-boost/comments-img/';
        if ( !file_exists( $dir ) ) {
            wp_mkdir_p( $dir );
        }
        return $dir;
    }

    function ciboost_get_comment_images_upload_url() {
        $upload = wp_upload_dir();
        return trailingslashit( $upload['baseurl'] ) . 'comment-image-boost/comments-img/';
    }

    function ciboost_log(  $message  ) {
        if ( !defined( 'WP_DEBUG' ) || !WP_DEBUG ) {
            return;
        }
        if ( !is_scalar( $message ) ) {
            $message = ( function_exists( 'wp_json_encode' ) ? wp_json_encode( $message ) : print_r( $message, true ) );
        }
        do_action( 'ciboost_debug_log', $message );
    }

    // ============================================
    // ACTIVATION HOOK
    // ============================================
    function ciboost_activate() {
        ciboost_get_comment_images_upload_dir();
        add_option( 'ciboost_comment_images', array(
            'enabled'               => true,
            'max_size_mb'           => 2,
            'image_size'            => 'medium',
            'required'              => false,
            'allow_multiple'        => false,
            'preview_size'          => 120,
            'display_layout'        => 'vertical',
            'allowed_formats'       => array('jpg', 'jpeg', 'png'),
            'lazy_loading'          => true,
            'enable_captions'       => false,
            'enable_lightbox'       => true,
            'image_position'        => 'after',
            'gallery_columns'       => 4,
            'enabled_post_types'    => array(),
            'enabled_categories'    => array(),
            'soft_moderation'       => true,
            'save_to_media_library' => false,
            'strip_metadata'        => true,
            'show_badge'            => true,
            'gallery_layout'        => 'grid',
        ) );
        add_option( 'ciboost_image_compression', array(
            'enabled'     => true,
            'quality'     => 85,
            'max_width'   => 767,
            'progressive' => true,
        ) );
    }

    register_activation_hook( __FILE__, 'ciboost_activate' );
    // ============================================
    // FREE: Core files (always loaded)
    // ============================================
    require_once CIBOOST_PLUGIN_DIR . 'includes/ciboost-image-utils.php';
    require_once CIBOOST_PLUGIN_DIR . 'includes/class-ciboost-comment-images.php';
    require_once CIBOOST_PLUGIN_DIR . 'includes/class-ciboost-moderation-tools.php';
    require_once CIBOOST_PLUGIN_DIR . 'includes/ciboost-upgrade-modal.php';
    require_once CIBOOST_PLUGIN_DIR . 'includes/class-ciboost-admin.php';
    // ============================================
    // PREMIUM: Advanced features (conditional)
    // Wrapped with is__premium_only() so Freemius PHP Processor
    // removes this entire block when generating the free version.
    // ============================================
    if ( function_exists( 'ca_fs' ) && ca_fs()->is__premium_only() ) {
        if ( ca_fs()->can_use_premium_code() ) {
            require_once CIBOOST_PLUGIN_DIR . 'includes/ciboost-image-compression__premium_only.php';
            require_once CIBOOST_PLUGIN_DIR . 'includes/ciboost-integrations__premium_only.php';
        }
    }
}
// End of else block (Freemius set_basename / plugin init)