<?php
/**
 * Plugin Name: Steve's Gallery Helper
 * Plugin URI: https://stevegibbs.info/wordpress-plugins/gallery-helper
 * Description: Adds lightbox add-on to the default WordPress gallery block (no jQuery).
 * Version: 1.7.3
 * Author: Steve Gibbs
 * Author URI: https://stevegibbs.info
 * License: GPL v2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: steves-gallery-helper
 * Tags: gallery, slideshow, lightbox, lightweight, steve
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * Register with the EDD SL SDK for updates. No personal data is transmitted without consent.
 */
add_action(
	'edd_sl_sdk_registry',
	function ( $init ) {
		$license_key = get_option( 'steves_gallery_helper_license_key', '' );
		$server_url  = 'https://wp.stevegibbs.info'; // Fallback URL

		if ( function_exists( 'pvd_edd_get_licensing_server_url' ) ) {
            $server_url = pvd_edd_get_licensing_server_url();
        }

		$init->register(
			array(
				'id'          => 'steves-gallery-helper',
				'url'         => $server_url,
				'item_id'     => '1099',
				'version'     => '1.7.3',
				'file'        => __FILE__,
				'license_key' => $license_key,
			)
		);
	}
);

if ( file_exists( __DIR__ . '/vendor/easy-digital-downloads/edd-sl-sdk/edd-sl-sdk.php' ) ) {
	require_once __DIR__ . '/vendor/easy-digital-downloads/edd-sl-sdk/edd-sl-sdk.php';
}

class GalleryHelper {
    public function __construct() {
        add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_editor_assets' ) );
        add_filter( 'render_block', array( $this, 'render_block' ), 10, 2 );
    }

    public function enqueue_editor_assets() {
        wp_enqueue_script(
            'gallery-helper-editor',
            plugin_dir_url( __FILE__ ) . 'js/editor.js',
            array( 'wp-blocks', 'wp-element', 'wp-block-editor', 'wp-components', 'wp-i18n', 'wp-data' ),
            '1.0.0',
            true
        );
    }

    public function enqueue_frontend_assets() {
        wp_enqueue_script( 'gallery-helper-frontend', plugin_dir_url( __FILE__ ) . 'js/frontend.js', array(), '1.7.3', true );
        wp_enqueue_style( 'gallery-helper-style', plugin_dir_url( __FILE__ ) . 'css/style.css', array(), '1.7.3' );
    }

    public function render_block( $block_content, $block ) {
        if ( $block['blockName'] === 'core/gallery' && isset( $block['attrs']['enableLightbox'] ) && $block['attrs']['enableLightbox'] ) {
            $this->enqueue_frontend_assets();
            $allowed_modes = array('manual', 'auto');
            $mode = isset($block['attrs']['slideshowMode']) && in_array($block['attrs']['slideshowMode'], $allowed_modes) ? $block['attrs']['slideshowMode'] : 'manual';
            $block_content = preg_replace( '/(class="[^"]*?wp-block-gallery)/', '$1 gallery-helper-lightbox', $block_content );
            $block_content = preg_replace( '/(<figure[^>]*)(>)/', '$1 data-slideshow-mode="' . esc_attr( $mode ) . '"$2', $block_content );
        }
        return $block_content;
    }
}

new GalleryHelper();

// Check license status and enable premium features
$license_status = get_option( 'steves_gallery_helper_license_status', 'inactive' );
if ( $license_status === 'active' ) {
    // Enable premium features here
    // Show success notice only on the plugin's license page
    add_action( 'admin_notices', function() {
        if ( isset( $_GET['page'] ) && $_GET['page'] === 'steves-gallery-helper-license' && ( get_option( 'steves_gallery_helper_license_status', 'inactive' ) === 'active' || isset($_GET['activated']) ) ) {
            echo '<div class="notice notice-success"><p>Gallery Helper Premium: Advanced slideshow modes enabled!</p></div>';
        }
    } );
    // Example: Enable premium slideshow mode
    add_filter( 'gallery_helper_premium_modes', '__return_true' );
} else {
    // Show upgrade prompt only on the plugin's license page
    add_action( 'admin_notices', function() {
        if ( isset( $_GET['page'] ) && $_GET['page'] === 'steves-gallery-helper-license' && get_option( 'steves_gallery_helper_license_status', 'inactive' ) !== 'active' ) {
            echo '<div class="notice notice-info"><p>Gallery Helper: Upgrade to premium for advanced slideshow modes. <a href="https://wp.stevegibbs.info" target="_blank">Learn more</a></p></div>';
        }
    } );
}


