RECORDGOD/wp-bridge/includes/class-rg-api.php
type-two 5ad4de1d2c feat(bridge): RecordGod WP bridge plugin (WowPlatter successor) + thumb on browse/release
Thin WordPress/Woo bridge — RecordGod owns catalog/stock/pricing/shipping; the plugin is a storefront
skin + checkout adapter over /shop:
- storefront: SSR /records (browse) + /release/{id} inside the theme, with title/meta/JSON-LD MusicAlbum
- on-the-fly Woo product: /?rg_add=<sku> mints a hidden WC_Product_Simple from /shop/item (the WowPlatter
  trick — 25k catalog, only sold items become Woo products)
- shipping: WC_Shipping_Method quoting /shop/shipping/quote by cart item count (AusPost flat rate)
- orders: order_status_completed -> POST /shop/woo-order (X-Bridge-Key), idempotent, marks SKUs sold
- settings page: base URL + bridge key + store id
Backend: added thumb to /shop/browse + /shop/release so storefront pages have cover art.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-24 01:43:30 +10:00

40 lines
1.6 KiB
PHP

<?php
if (!defined('ABSPATH')) exit;
/** Thin HTTP client for the RecordGod /shop API — GET (transient-cached) + POST (bridge-key). */
class RG_API {
static function base() {
return rtrim(get_option('rg_base_url', 'https://recordgod.com'), '/');
}
/** GET /shop/<path>?args — decoded JSON or null. Cached in a transient for $ttl seconds (0 = no cache). */
static function get($path, $args = [], $ttl = 120) {
$url = self::base() . $path . ($args ? '?' . http_build_query($args) : '');
$key = 'rg_' . md5($url);
if ($ttl) {
$hit = get_transient($key);
if ($hit !== false) return $hit;
}
$r = wp_remote_get($url, ['timeout' => 12, 'headers' => ['Accept' => 'application/json']]);
if (is_wp_error($r) || wp_remote_retrieve_response_code($r) !== 200) return null;
$data = json_decode(wp_remote_retrieve_body($r), true);
if ($ttl && $data !== null) set_transient($key, $data, $ttl);
return $data;
}
/** POST JSON to /shop/<path> with the bridge key header. Returns [code, decoded-body]. */
static function post($path, $body) {
$r = wp_remote_post(self::base() . $path, [
'timeout' => 15,
'headers' => [
'Content-Type' => 'application/json',
'X-Bridge-Key' => get_option('rg_bridge_key', ''),
],
'body' => wp_json_encode($body),
]);
if (is_wp_error($r)) return [0, ['error' => $r->get_error_message()]];
return [wp_remote_retrieve_response_code($r), json_decode(wp_remote_retrieve_body($r), true)];
}
}