<?php

/**
 * Plugin Name: WebP Image Paster
 * Description: Intercepts image paste, prompts for filename and width preset, converts to WebP, and uploads to Gutenberg.
 * Version: 1.0.3
 * Requires at least: 6.0
 * Requires PHP: 7.4
 * Author: Mia
 * License: GPLv2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: webp-image-paster
 * 
 */
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
// [수정] 상수명 접두사 변경 (WEBP_PASTER_VERSION -> WEBP_IMAGE_PASTER_VERSION)
if ( !defined( 'WEBP_IMAGE_PASTER_VERSION' ) ) {
    define( 'WEBP_IMAGE_PASTER_VERSION', '1.0.3' );
}
// 1. Freemius SDK 초기화 (독립적인 블록으로 격리)
if ( function_exists( 'webp_image_paster_fs' ) ) {
    webp_image_paster_fs()->set_basename( false, __FILE__ );
} else {
    if ( !function_exists( 'webp_image_paster_fs' ) ) {
        function webp_image_paster_fs() {
            global $webp_image_paster_fs;
            if ( !isset( $webp_image_paster_fs ) ) {
                require_once dirname( __FILE__ ) . '/vendor/freemius/start.php';
                $webp_image_paster_fs = fs_dynamic_init( array(
                    'id'               => '36907',
                    'slug'             => 'webp-image-paster',
                    'type'             => 'plugin',
                    'public_key'       => 'pk_6c38c7744aa83ee20a8728c34b0f9',
                    'is_premium'       => false,
                    'premium_suffix'   => 'Pro',
                    'has_addons'       => false,
                    'has_paid_plans'   => true,
                    'is_org_compliant' => true,
                    'menu'             => array(
                        'first-path' => 'plugins.php',
                        'support'    => false,
                    ),
                    'is_live'          => true,
                ) );
            }
            return $webp_image_paster_fs;
        }

        webp_image_paster_fs();
        do_action( 'webp_image_paster_fs_loaded' );
    }
}
// Freemius SDK 블록 종료
// 2. 에셋 앙큐 로직 (Freemius 블록 외부로 분리 및 function_exists 가드 추가)
if ( !function_exists( 'webp_image_paster_enqueue_editor_assets' ) ) {
    function webp_image_paster_enqueue_editor_assets() {
        wp_enqueue_style( 'wp-components' );
        // 1) 순수 유틸리티 모듈
        wp_enqueue_script(
            'webp-image-paster-utils',
            plugins_url( 'js/paster-utils.js', __FILE__ ),
            array(),
            WEBP_IMAGE_PASTER_VERSION,
            true
        );
        wp_localize_script( 'webp-image-paster-utils', 'WebpImagePasterSettings', array(
            'heic2anyUrl' => plugins_url( 'js/heic2any.min.js', __FILE__ ),
        ) );
        // 2) 이미지 변환 및 미리보기 프로세서 모듈
        wp_enqueue_script(
            'webp-image-paster-processor',
            plugins_url( 'js/paster-processor.js', __FILE__ ),
            array('webp-image-paster-utils', 'wp-i18n'),
            WEBP_IMAGE_PASTER_VERSION,
            true
        );
        // 3) 에디터 이벤트 및 미디어 업로드 핸들러 모듈
        wp_enqueue_script(
            'webp-image-paster-editor',
            plugins_url( 'js/paster-editor.js', __FILE__ ),
            array(
                'webp-image-paster-utils',
                'wp-blocks',
                'wp-editor',
                'wp-block-editor',
                'wp-api-fetch',
                'wp-i18n'
            ),
            WEBP_IMAGE_PASTER_VERSION,
            true
        );
        // 4) 공용 React 모달 컴포넌트 모듈
        wp_enqueue_script(
            'webp-image-paster-modal',
            plugins_url( 'js/paster-modal.js', __FILE__ ),
            array(
                'webp-image-paster-utils',
                'webp-image-paster-processor',
                'webp-image-paster-editor',
                'wp-element',
                'wp-components',
                'wp-i18n'
            ),
            WEBP_IMAGE_PASTER_VERSION,
            true
        );
        wp_set_script_translations( 'webp-image-paster-modal', 'webp-image-paster', plugin_dir_path( __FILE__ ) . 'languages' );
        wp_enqueue_script(
            'webp-image-paster-editor-free',
            plugins_url( 'js/paster-free.js', __FILE__ ),
            array('webp-image-paster-modal', 'wp-plugins'),
            WEBP_IMAGE_PASTER_VERSION,
            true
        );
        wp_set_script_translations( 'webp-image-paster-editor-free', 'webp-image-paster', plugin_dir_path( __FILE__ ) . 'languages' );
    }

    add_action( 'enqueue_block_editor_assets', 'webp_image_paster_enqueue_editor_assets' );
}
// 3. 플러그인 목록 액션 링크 (가드 추가)
if ( !function_exists( 'webp_image_paster_add_action_links' ) ) {
    function webp_image_paster_add_action_links(  $links  ) {
        if ( function_exists( 'webp_image_paster_fs' ) ) {
            if ( webp_image_paster_fs()->is_paying() ) {
                $account_url = webp_image_paster_fs()->get_account_url();
                if ( $account_url ) {
                    $account_link = sprintf( '<a href="%s">%s</a>', esc_url( $account_url ), esc_html__( 'Account', 'webp-image-paster' ) );
                    array_unshift( $links, $account_link );
                }
            } else {
                $upgrade_url = webp_image_paster_fs()->get_upgrade_url();
                if ( $upgrade_url ) {
                    $pro_link = sprintf( '<a href="%s" target="_blank" style="color: #5058E9; font-weight: bold;">%s</a>', esc_url( $upgrade_url ), esc_html__( 'Upgrade to Pro', 'webp-image-paster' ) );
                    array_unshift( $links, $pro_link );
                }
            }
        }
        return $links;
    }

    add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'webp_image_paster_add_action_links' );
}