<?php

/**
 * @link              https://wingdevs.com
 * @since             1.0.0
 * @package           Wing_Popup
 *
 * @wordpress-plugin
 * Plugin Name:       Wing Popup
 * Plugin URI:        https://wingdevs.com/wing-popup
 * Description:       Lightweight and powerful popup builder plugin for WordPress.
 * Version:           2.0.0
 * Author:            WingDevs
 * Author URI:        https://wingdevs.com/
 * License:           GPL-2.0+
 * License URI:       http://www.gnu.org/licenses/gpl-2.0.txt
 * Text Domain:       wing-popup
 */

// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
	die;
}

// Define necessary constants
define( 'WDPOP_VER', '2.0.0' );
define( 'WDPOP_URL', plugin_dir_url( __FILE__ ) );
define( 'WDPOP_DIR', plugin_dir_path( __FILE__ ));
define( 'WDPOP_NAME', 'wing-popup');

// The code that runs during plugin activation.
function wdpop_activate() {
	require_once WDPOP_DIR . 'includes/class-wing-popup-activator.php';
	WDPOP_Activator::activate();
}

// The code that runs during plugin deactivation.
function wdpop_deactivate() {
	require_once WDPOP_DIR . 'includes/class-wing-popup-deactivator.php';
	WDPOP_Deactivator::deactivate();
}

register_activation_hook( __FILE__, 'wdpop_activate' );
register_deactivation_hook( __FILE__, 'wdpop_deactivate' );

add_action( 'in_plugin_update_message-wing-popup/wing-popup.php', 'wing_popup_major_update_notice', 10, 2 );

function wing_popup_major_update_notice( $plugin_data, $response ) {
    $current_version = isset( $plugin_data->Version ) ? $plugin_data->Version : '';
    $new_version     = isset( $response->new_version ) ? $response->new_version : '';

    if ( $current_version && $new_version && wing_popup_is_major_update( $current_version, $new_version ) ) {
        echo '<div style="margin-top:8px; padding:10px; background:#fff3cd; border-left:4px solid #ff9800;">';
        echo '<strong>' . esc_html__( 'Heads up!', 'wing-popup' ) . '</strong> ';
        echo esc_html__( 'This is a major Wing Popup update — please back up your site before updating.', 'wing-popup' );
        echo '</div>';
    }
}

function wing_popup_is_major_update( $current, $new ) {
    $current_parts = explode( '.', $current );
    $new_parts     = explode( '.', $new );

    // Compare only the major version number (first digit)
    return isset( $current_parts[0], $new_parts[0] ) && ( (int) $new_parts[0] > (int) $current_parts[0] );
}


// Begins execution of the plugin.
function wdpop_run() {
	// Load Core Framework for Wingdevs Plugin
	require WDPOP_DIR . 'includes/class-wing-popup.php';
	
	require_once WDPOP_DIR . 'includes/libs/wingcore/wingcore.php';
	new WDPOP();
}
add_action( 'plugins_loaded', 'wdpop_run' );
