/* Plugin Name: Eboli Price Analyzer Description: Analisi differenziale dei prezzi tramite Google Content API for Shopping. Version: 1.1 Author: Vito */ if (!defined('ABSPATH')) { exit; } // Percorso assoluto del JSON Service Account define('GOOGLE_SA_PATH', '/home/vito/outletscarpeonline.it/NOME_DEL_TUO_FILE.json'); class Eboli_Price_Analyzer { private $merchant_id = '121246158'; public function __construct() { add_action('admin_menu', array($this, 'add_admin_menu')); } public function add_admin_menu() { add_submenu_page( 'woocommerce', 'Price Analyzer', 'Analisi Prezzi', 'manage_options', 'eboli-price-analyzer', array($this, 'render_page') ); } /** * Base64 URL-safe (obbligatorio per JWT Google) */ private function base64url_encode($data) { return rtrim(strtr(base64_encode($data), '+/', '-_'), '='); } /** * Genera Access Token OAuth2 firmando JWT RS256 */ private function get_access_token() { if (!file_exists(GOOGLE_SA_PATH)) { return false; } $json = json_decode(file_get_contents(GOOGLE_SA_PATH), true); if (!isset($json['client_email']) || !isset($json['private_key'])) { return false; } $header = $this->base64url_encode(json_encode([ 'alg' => 'RS256', 'typ' => 'JWT' ])); $payload = $this->base64url_encode(json_encode([ 'iss' => $json['client_email'], 'scope' => 'https://www.googleapis.com/auth/content', 'aud' => 'https://oauth2.googleapis.com/token', 'exp' => time() + 3600, 'iat' => time() ])); $signature = ''; openssl_sign($header . '.' . $payload, $signature, $json['private_key'], OPENSSL_ALGO_SHA256); $jwt = $header . '.' . $payload . '.' . $this->base64url_encode($signature); $ch = curl_init('https://oauth2.googleapis.com/token'); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([ 'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer', 'assertion' => $jwt ])); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, 10); $res = json_decode(curl_exec($ch), true); curl_close($ch); return $res['access_token'] ?? false; } /** * Pagina admin */ public function render_page() { $paged = isset($_GET['paged']) ? intval($_GET['paged']) : 1; $limit = 20; $offset = ($paged - 1) * $limit; // Query WooCommerce corretta con offset $products = wc_get_products([ 'orderby' => 'meta_value_num', 'meta_key' => 'total_sales', 'order' => 'DESC', 'limit' => $limit, 'offset' => $offset ]); $token = $this->get_access_token(); echo '
Prodotti ordinati per vendite totali.
'; if (!$token) { echo 'Errore autenticazione Google. Controlla il file JSON.
| Prodotto | '; echo 'Tuo Prezzo | '; echo 'Benchmark Concorrenza | '; echo 'Stato Mercato | '; echo '
|---|---|---|---|
| Nessun prodotto trovato. | |||
| ' . esc_html($product->get_name()) . ' (SKU: ' . esc_html($sku) . ') | '; echo '' . number_format($price, 2) . ' € | '; echo ''; echo ($comp_price !== 'N/A') ? number_format($comp_price, 2) . ' €' : 'Non rilevato'; echo ' | '; echo ''; if ($comp_price !== 'N/A') { if ($price > $comp_price) { echo 'Fuori Mercato'; } else { echo 'In Linea'; } } else { echo 'Nessun dato'; } echo ' | '; echo '
'; echo 'Carica altri 20 prodotti'; echo '
'; echo '