<?php

/**
 * Plugin Name: Buy With Digikala
 * Plugin URI: https://digify.shop/
 * Description: امکان خرید از سایت توسط دیجی کالا
 * Version: 1.2.2
 * Author: دیجیفای
 * Author URI: https://digify.shop/
 * License: GPL2
 * Text Domain: bwdk
 * Domain Path: /languages
 * Requires Plugins: woocommerce
 *  Requires at least: 5.6
 *  Tested up to: 6.8
 *  Requires PHP: 7.4
 */
use BWDK\Support\Activate;
use BWDK\Support\BWDK_Updater;
use BWDK\Support\Deactive;


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

if (!function_exists('dd')) {
    function dd($data)
    {
        $trace      = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS , 2);
        $selfFile   = isset($trace[0]['file']) ? $trace[0]['file'] : '';
        $selfLine   = isset($trace[0]['line']) ? $trace[0]['line'] : '';
        $callerFile = isset($trace[1]['file']) ? $trace[1]['file'] : '';
        $callerLine = isset($trace[1]['line']) ? $trace[1]['line'] : '';
        $class      = isset($trace[1]['class']) ? $trace[1]['class'] : '';
        $function   = isset($trace[1]['function']) ? $trace[1]['function'] : '';
        echo "<pre style='display: block; text-align: left; direction: ltr; background-color: #fff; border: 1px solid #9e4e00; border-radius: 5px; padding: 7px; border-left-width: 7px; margin: 10px; font-size: 13px;'>";
        echo "Called from file: {$callerFile}\n";
        echo "Called from line: {$callerLine}\n";
        if ($class) {
            echo "Class: {$class}\n";
        }
        if ($function) {
            echo "Function/Method: {$function}\n";
            echo "Line (dd): {$selfLine}\n";
        }
        echo "-----------------------------\n";
        var_dump($data);
        echo '</pre>';
        exit();
    }
}



define('BWDK_PLUGIN_FILE', __FILE__);


require_once __DIR__ . '/vendor/autoload.php';


(new Activate())->register();
(new Deactive())->register();



add_action('init', function () {
    (new \BWDK\Core\Plugin())->run();
});



add_action( 'template_redirect', function () {
    if ( ! function_exists( 'is_order_received_page' ) || ! is_order_received_page() ) {
        return;
    }

    if ( WC()->cart && WC()->cart->get_cart_contents_count() > 0 ) {
        return;
    }

    $order_id = absint( get_query_var( 'order-received' ) );

    if ( ! $order_id ) {
        return;
    }

    $order = wc_get_order( $order_id );
    if ( ! $order ) {
        return;
    }


    $bad_statuses = array( 'failed', 'cancelled', 'pending' );
    if ( ! in_array( $order->get_status(), $bad_statuses, true ) ) {
        return;
    }


    foreach ( $order->get_items() as $item ) {
        if ( ! $item instanceof WC_Order_Item_Product ) {
            continue;
        }

        $qty      = (int) $item->get_quantity();
        $product  = $item->get_product();

        if ( ! $product || ! $product->is_purchasable() ) {
            continue;
        }

        $product_id   = $product->is_type( 'variation' ) ? $product->get_parent_id() : $product->get_id();
        $variation_id = $product->is_type( 'variation' ) ? $product->get_id() : (int) $item->get_variation_id();
        $variations   = array();


        if ( $variation_id ) {
            $variation_product = wc_get_product( $variation_id );
            if ( $variation_product ) {
                $variations = $variation_product->get_attributes();
            }
        }

        WC()->cart->add_to_cart( $product_id, $qty, $variation_id, $variations );
    }

    foreach ( $order->get_coupon_codes() as $code ) {
        WC()->cart->apply_coupon( $code );
    }

} ,100);




add_action('plugins_loaded', function () {
    $plugin_file = 'buy-with-digikala/buy-with-digikala.php';
    if ( ! function_exists('get_plugin_data') ) {
        require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
    }
    $plugin_data = get_plugin_data(WP_PLUGIN_DIR . '/' . $plugin_file);
    $current_version = $plugin_data['Version'];

    $metadata_url = 'https://plugins.digifyprime.net/bwdk/metadata.json';
    new BWDK_Updater([
        'plugin_file'     => $plugin_file,
        'current_version' => $current_version,
        'metadata_url'    => $metadata_url,
    ]);
});
