MOON
Server: Apache
System: Linux server.royaltuning.hu 4.18.0-425.13.1.el8_7.x86_64 #1 SMP Tue Feb 21 04:20:52 EST 2023 x86_64
User: royaltuning (1001)
PHP: 8.2.30
Disabled: exec,passthru,shell_exec,system
Upload Files
File: /home/royaltuning/public_html/public/wp-content/plugins/webshippy_tracking/webshippy_tracking.php
<?php
/**
 * Plugin Name: Webshippy WooCommerce Tracking
 * Description: Lekérdezi a Webshippy API-ból a szállításii státuszokat és megjeleníti a WooCommerce admin rendeléslistájában.
 * Version: 1.0
 * Author: Thomas Kemendi
 */

if (!defined('ABSPATH')) {
    exit; // Kilépés, ha közvetlenül próbálják megnyitni.
}
$secretID="G9Wvdm5Z4jLMi1DXAK6EPcosReJalBz3xTfQH7V2YnFOSUrukp";
define('WEBSHIPPY_API_KEY', $secretID);

// Webshippy API kulcs (Ide állítsd be az API kulcsodat)


// Új oszlop hozzáadása a WooCommerce rendeléslistához
add_filter('manage_edit-shop_order_columns', function ($columns) {
    $columns['webshippy_status'] = __('Webshippy Státusz', 'woocommerce');
    return $columns;
});

// Oszlop tartalmának feltöltése
add_action('manage_shop_order_posts_custom_column', function ($column, $post_id) {
    if ($column === 'webshippy_status') {
        $order = wc_get_order($post_id);
        if (!$order || $order->get_status() !== 'completed') {
            echo __('A rendelés még nincs teljesítve', 'woocommerce');
            return;
        }
        $order_id = get_webshippy_order_id($order->get_id());

        if (!$order_id) {
            echo __('Nincs Webshippy azonosító', 'woocommerce');
            return;
        }

        // Lekérdezés az API-ból
        $tracking_info = get_webshippy_tracking_status($order_id);

        if ($tracking_info) {
            echo '<strong>' . esc_html($tracking_info['status']) . '</strong><br/>';
            echo '<a href="' . esc_url($tracking_info['trackingUrl']) . '" target="_blank">Csomagkövetés</a>';
        } else {
            echo __('Nincs információ', 'woocommerce');
        }
    }
}, 10, 2);

// Webshippy API lekérdezés funkció
function get_webshippy_tracking_status($order_id) {
    $cache_key = 'webshippy_tracking_' . $order_id;
    $cached = get_transient($cache_key);
    if ($cached !== false) {
        return $cached;
    }

    $api_url = 'https://app.webshippy.com/wspyapi/getTrackInfo/json?apiKey=' . WEBSHIPPY_API_KEY . '&page=0&orderIds=' . $order_id;
    $response = wp_remote_get($api_url);

    if (is_wp_error($response)) {
        return false;
    }

    $body = wp_remote_retrieve_body($response);
    $data = json_decode($body, true);

    if (!isset($data['status']) || $data['status'] !== 'success' || empty($data['result'][$order_id])) {
        return false;
    }

    $result = $data['result'][$order_id];
    $tracking_data = [
        'status' => $result['shippingStatus'],
        'trackingUrl' => $result['trackingUrl']
    ];

    //set_transient($cache_key, $tracking_data, HOUR_IN_SECONDS);
    return $tracking_data;
}

// Webshippy API lekérdezés funkció rendelési azonosító alapján
function get_webshippy_order_id($reference_id) {
    $api_url = 'https://app.webshippy.com/wspyapi/GetOrder/json';
    $data = [
        'apiKey' => WEBSHIPPY_API_KEY,
        'limit' => 1,
        'filters' => ['referenceId' => (string) $reference_id]
    ];

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $api_url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(['request' => json_encode($data)]));
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Ezt állítsd true-ra éles környezetben
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    if (curl_errno($ch)) {
        return false;
    }
    curl_close($ch);

    $data = json_decode($response, true);

    if (!isset($data['status']) || $data['status'] !== 'success' || empty($data['result'][0]['wspyId'])) {
        return false;
    }

    return $data['result'][0]['wspyId'];
}