<?php
/**
 * Plugin Name: Skyquake LoginForge
 * Plugin URI: https://wordpress.org/plugins/skyquake-loginforge/
 * Description: Customize the WordPress login page with logo, redirect URL, themes, and colors.
 * Author: Pranav Mundre
 * Version: 1.0.5
 * License: GPL2
 * Text Domain: skyquake-loginforge
 */

// Prevent direct access
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

// Define constants
define( 'LG_FORGE_VERSION', '1.0.5' );
define( 'LG_FORGE_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'LG_FORGE_PLUGIN_URL', plugin_dir_url( __FILE__ ) );

// Include required files
require_once LG_FORGE_PLUGIN_DIR . 'admin/settings-page.php';
require_once LG_FORGE_PLUGIN_DIR . 'public/login-functions.php';

// Activation hook
register_activation_hook( __FILE__, 'lg_forge_activate' );

// Deactivation hook
register_deactivation_hook( __FILE__, 'lg_forge_deactivate' );

// Activation function
function lg_forge_activate() {
    // Set default options if not set
    $defaults = function_exists( 'lg_forge_get_default_options' )
        ? lg_forge_get_default_options()
        : array(
            'logo_url'     => '',
            'redirect_url' => home_url(),
            'logo_width'   => '84',
            'logo_height'  => '84',
            'active_theme' => 'default',
        );
    add_option( 'loginlook_options', $defaults );
}

// Deactivation function
function lg_forge_deactivate() {
    // Nothing to do on deactivation
}

// Enqueue admin scripts and styles
add_action( 'admin_enqueue_scripts', 'lg_forge_admin_enqueue_scripts' );
function lg_forge_admin_enqueue_scripts( $hook ) {
    // Load admin CSS globally so the menu icon sizing remains correct on all admin screens.
    wp_enqueue_style( 'loginlook-admin-css', LG_FORGE_PLUGIN_URL . 'admin/admin.css', array(), LG_FORGE_VERSION );

    $is_loginlook_page = ( false !== strpos( $hook, 'loginlook' ) );

    if ( $is_loginlook_page ) {
        wp_enqueue_media();
        wp_enqueue_script( 'loginlook-admin-js', LG_FORGE_PLUGIN_URL . 'admin/admin.js', array( 'jquery' ), LG_FORGE_VERSION, true );
        wp_localize_script( 'loginlook-admin-js', 'loginlook_ajax', array(
            'ajax_url' => admin_url( 'admin-ajax.php' ),
            'nonce'    => wp_create_nonce( 'loginlook_nonce' ),
        ) );
    }
}

// Add settings link to plugin list
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'lg_forge_add_settings_link' );
function lg_forge_add_settings_link( $links ) {
    $settings_url  = is_multisite() ? network_admin_url( 'admin.php?page=loginlook' ) : admin_url( 'admin.php?page=loginlook' );
    $settings_link = '<a href="' . esc_url( $settings_url ) . '">' . esc_html__( 'Settings', 'skyquake-loginforge' ) . '</a>';
    $feedback_link = '<a href="https://forms.gle/Tw7eHwccC2GW2ceW7" target="_blank" rel="noopener noreferrer">' . esc_html__( 'Feedback', 'skyquake-loginforge' ) . '</a>';
    array_unshift( $links, $settings_link );
    $links[] = $feedback_link;
    return $links;
}

// Add plugin row meta links for rating/reviews.
add_filter( 'plugin_row_meta', 'lg_forge_add_review_links', 10, 2 );
function lg_forge_add_review_links( $plugin_meta, $plugin_file ) {
    if ( plugin_basename( __FILE__ ) !== $plugin_file ) {
        return $plugin_meta;
    }

    $plugin_meta[] = '<a href="https://wordpress.org/support/plugin/skyquake-loginforge/reviews/?filter=5" target="_blank" rel="noopener noreferrer">' . esc_html__( 'Rate Plugin ★★★★★', 'skyquake-loginforge' ) . '</a>';
    return $plugin_meta;
}

// Open the default "View details" modal on the Reviews tab for this plugin.
add_action( 'admin_footer-plugins.php', 'lg_forge_set_view_details_reviews_tab' );
add_action( 'admin_footer-plugins-network.php', 'lg_forge_set_view_details_reviews_tab' );
function lg_forge_set_view_details_reviews_tab() {
    ?>
    <script>
    (function($){
        var pluginFile = 'skyquake-loginforge/skyquake-loginforge.php';
        var $row = $('tr[data-plugin="' + pluginFile + '"]');
        var $detailsLink = $row.find('a.thickbox.open-plugin-details-modal').first();

        if (!$detailsLink.length) {
            return;
        }

        var href = $detailsLink.attr('href') || '';
        if (!href) {
            return;
        }

        if (href.indexOf('section=reviews') === -1) {
            href += (href.indexOf('?') === -1 ? '?' : '&') + 'section=reviews';
            $detailsLink.attr('href', href);
        }
    })(jQuery);
    </script>
    <?php
}

?>
