<?php
namespace WooSwatches;

/**
 * Protect direct access
 */
if ( ! defined( 'ABSPATH' ) ) exit;

function is_pro_active() {
    require_once ABSPATH . 'wp-admin/includes/plugin.php';
    return defined('GS_WOO_SWATCHES_PRO_VERSION') && is_plugin_active( GS_WOO_SWATCHES_PRO_PLUGIN );
}

function minimize_css_simple($css) {
    // https://datayze.com/howto/minify-css-with-php
    $css = preg_replace('/\/\*((?!\*\/).)*\*\//', '', $css); // negative look ahead
    $css = preg_replace('/\s{2,}/', ' ', $css);
    $css = preg_replace('/\s*([:;{}])\s*/', '$1', $css);
    $css = preg_replace('/;}/', '}', $css);
    return $css;
}

function gs_update_plugin_version() {
    if ( GS_WOO_SWATCHES_VERSION !==  get_option('gs_swatches_plugin_version') ) {
        update_option( 'gs_swatches_plugin_version', GS_WOO_SWATCHES_VERSION );
        return true;
    }
    return false;
}

/**
 * Initialize the plugin tracker
 *
 * @return void
 */
function gs_appsero_init() {

    if ( ! class_exists('WooSwatches\Insights') ) {
        require_once GS_WOO_SWATCHES_PLUGIN_DIR . 'includes/appsero/Client.php';
    }

    $client = new \WooSwatches\Client('448ef52e-6101-45e4-94b0-f570ac3b10bd', 'Variation Swatches', __FILE__);
    // Active insights
    $client->insights()->init();
}

/**
 * Upgrade notice if compatibility fails
 */
function pro_compatibility_notice() {

    $screen = get_current_screen();
    
    if ( isset( $screen->parent_file ) && 'plugins.php' === $screen->parent_file && 'update' === $screen->id ) return;
    if ( 'update' === $screen->base && 'update' === $screen->id ) return;

    if ( ! current_user_can( 'update_plugins' ) ) return;

    $upgrade_url = wp_nonce_url( self_admin_url( 'update.php?action=upgrade-plugin&plugin=' . GS_WOO_SWATCHES_PRO_PLUGIN ), 'upgrade-plugin_' . GS_WOO_SWATCHES_PRO_PLUGIN );
    $message = '<p>' . sprintf( __( '<strong>%1$s</strong> is not working because you need to upgrade the <strong>%2$s</strong> to latest version.', 'gs-woo-variation' ), 'Variation Swatches', 'Variation Swatches Pro' ) . '</p>';
    $message .= '<p>' . sprintf( '<a href="%1$s" class="button-primary">%2$s</a>', $upgrade_url, __( 'Upgrade Variation Swatches Pro Now', 'gs-woo-variation' ) ) . '</p>';

    echo '<div class="error"><p>' . wp_kses_post( $message ) . '</p></div>';
}

/**
 * WooCommerce compatibility notice
 */
function woo_compatibility_notice() {

    $screen = get_current_screen();
    
    if ( isset( $screen->parent_file ) && 'plugins.php' === $screen->parent_file && 'update' === $screen->id ) return;
    if ( 'update' === $screen->base && 'update' === $screen->id ) return;

    if ( ! current_user_can( 'update_plugins' ) ) return;

    $upgrade_url = wp_nonce_url( self_admin_url( 'update.php?action=upgrade-plugin&plugin=' . GS_WOOCOMMERCE_PLUGIN ), 'upgrade-plugin_' . GS_WOOCOMMERCE_PLUGIN );
    $message = '<p>' . sprintf( __( '<strong>%1$s</strong> is not working because you need to upgrade the <strong>%2$s</strong> to latest version.', 'gs-woo-variation' ), 'Variation Swatches', 'WooCommerce' ) . '</p>';
    $message .= '<p>' . sprintf( '<a href="%1$s" class="button-primary">%2$s</a>', $upgrade_url, __( 'Upgrade WooCommerce Now', 'gs-woo-variation' ) ) . '</p>';

    echo '<div class="error"><p>' . wp_kses_post( $message ) . '</p></div>';
}

/**
 * WooCommerce compatibility notice
 */
function woo_install_active_notice() {

    $screen = get_current_screen();
    
    if ( isset( $screen->parent_file ) && 'plugins.php' === $screen->parent_file && 'update' === $screen->id ) return;
    if ( 'update' === $screen->base && 'update' === $screen->id ) return;

    if ( ! current_user_can( 'update_plugins' ) ) return;



    if ( file_exists( WP_PLUGIN_DIR . '/' . GS_WOOCOMMERCE_PLUGIN ) ) {
        $install_url = wp_nonce_url( self_admin_url( 'plugins.php?action=activate&plugin=' . GS_WOOCOMMERCE_PLUGIN ), 'activate-plugin_' . GS_WOOCOMMERCE_PLUGIN );
        $message = '<p>' . sprintf( __( '<strong>%1$s</strong> is not working because you need to activate the <strong>%2$s</strong> plugin.', 'gs-woo-variation' ), 'Variation Swatches', 'WooCommerce' ) . '</p>';
        $message .= '<p>' . sprintf( '<a href="%1$s" class="button-primary">%2$s</a>', $install_url, __( 'Activate WooCommerce Now', 'gs-woo-variation' ) ) . '</p>';
    } else {
        $install_url = wp_nonce_url( self_admin_url( 'update.php?action=install-plugin&plugin=' . dirname(GS_WOOCOMMERCE_PLUGIN) ), 'install-plugin_' . dirname(GS_WOOCOMMERCE_PLUGIN) );
        $message = '<p>' . sprintf( __( '<strong>%1$s</strong> is not working because you need to install the <strong>%2$s</strong> plugin.', 'gs-woo-variation' ), 'Variation Swatches', 'WooCommerce' ) . '</p>';
        $message .= '<p>' . sprintf( '<a href="%1$s" class="button-primary">%2$s</a>', $install_url, __( 'Install WooCommerce Now', 'gs-woo-variation' ) ) . '</p>';
    }

    echo '<div class="error"><p>' . wp_kses_post( $message ) . '</p></div>';
}

/**
 * Compatibility check with Pro plugin
 */
function is_pro_compatible() {
    require_once ABSPATH . 'wp-admin/includes/plugin.php';
    if ( defined('GS_WOO_SWATCHES_PRO_VERSION') && is_plugin_active( GS_WOO_SWATCHES_PRO_PLUGIN ) ) {
        if ( version_compare( GS_WOO_SWATCHES_PRO_VERSION, GS_WOO_SWATCHES_MIN_PRO_VERSION, '<' ) ) {
            add_action( 'admin_notices', 'WooSwatches\pro_compatibility_notice' );
            return false;
        }
    }
    return true;
}

/**
 * Check if WooCommerce is active
 */
function is_woo_compatible() {
    require_once ABSPATH . 'wp-admin/includes/plugin.php';
    // Check if WooCommerce is active
    if ( is_plugin_active( GS_WOOCOMMERCE_PLUGIN ) ) {
        if ( defined('WC_VERSION') && version_compare( WC_VERSION, GS_WOO_SWATCHES_MIN_WOO_VERSION, '<' ) ) {
            add_action( 'admin_notices', 'WooSwatches\woo_compatibility_notice' );
            return false;
        }
        return true;
    }
    add_action( 'admin_notices', 'WooSwatches\woo_install_active_notice' );
    return false;
}

/**
 * Activation redirects
 */
function on_activation( $plugin ) {
    if ( $plugin === GS_WOO_SWATCHES_BASE_NAME ) {
        wp_redirect( admin_url( 'admin.php?page=gs-woo-swatches-help' ) );
        exit;
    }
}

/**
 * Remove Reviews Metadata on plugin Deactivation.
 */
function on_deactivation() {
}

/**
 * Plugins action links
 */
function add_pro_link( $links ) {
    if ( ! is_pro_active() ) {
        $links[] = '<a style="color: red; font-weight: bold;" class="gs-pro-link" href="https://www.gsplugins.com/product/woocommerce-variation-swatches/" target="_blank">Go Pro!</a>';
    }
    $links[] = '<a href="https://www.gsplugins.com/wordpress-plugins" target="_blank">GS Plugins</a>';
    return $links;
}

/**
 * Org Review Link
 */
function plugin_org_review_link( $meta_fields, $file ) {
    
    if ( $file != GS_WOO_SWATCHES_BASE_NAME ) return $meta_fields;
    
    echo "<style>.gs-woo-variation-pro-rate-stars { display: inline-block; color: #ffb900; position: relative; top: 3px; }.gs-woo-variation-pro-rate-stars svg{ fill:#ffb900; } .gs-woo-variation-pro-rate-stars svg:hover{ fill:#ffb900 } .gs-woo-variation-pro-rate-stars svg:hover ~ svg{ fill:none; } </style>";

    $plugin_rate   = "https://wordpress.org/support/plugin/gs-woo-variation-swatches/reviews/?filter=5#new-post";
    $plugin_filter = "https://wordpress.org/support/plugin/gs-woo-variation-swatches/reviews/";
    $svg_xmlns     = "https://www.w3.org/2000/svg";
    $svg_icon      = '';

    for ( $i = 0; $i < 5; $i++ ) {
        $svg_icon .= "<svg xmlns='" . esc_url( $svg_xmlns ) . "' width='15' height='15' viewBox='0 0 24 24' fill='none' stroke='currentColor' stroke-width='2' stroke-linecap='round' stroke-linejoin='round' class='feather feather-star'><polygon points='12 2 15.09 8.26 22 9.27 17 14.14 18.18 21.02 12 17.77 5.82 21.02 7 14.14 2 9.27 8.91 8.26 12 2'/></svg>";
    }

    // Set icon for thumbsup.
    $meta_fields[] = '<a href="' . esc_url( $plugin_filter ) . '" target="_blank"><span class="dashicons dashicons-thumbs-up"></span>' . __( 'Vote!', 'gs-woo-variation' ) . '</a>';

    // Set icon for 5-star reviews. v1.1.22
    $meta_fields[] = "<a href='" . esc_url( $plugin_rate ) . "' target='_blank' title='" . esc_html__( 'Rate', 'gs-woo-variation' ) . "'><i class='gs-woo-variation-pro-rate-stars'>" . $svg_icon . "</i></a>";

    return $meta_fields;

}

/**
 * Plugins Load Text Domain
 */
function gs_load_textdomain() {
    load_plugin_textdomain( 'gs-woo-variation', false, dirname( GS_WOO_SWATCHES_BASE_NAME ) . '/languages' );
}