render_iframe_embed($affirm_id, $size, $attr);
}
return $this->render_sdk_embed($affirm_id, $size, $attr);
}
/**
* Filter post content to replace aFFirmFirst images with SDK divs
*/
public function filter_content_images($content) {
if (!$content) return $content;
// Replace
with SDK div
$pattern = '/
]+data-affirm-id=["\']([^"\']+)["\'][^>]*>/i';
$content = preg_replace_callback($pattern, function($matches) {
$affirm_id = $matches[1];
$render_mode = get_option('affirm_render_mode', 'sdk');
if ($render_mode === 'iframe') {
return $this->render_iframe_embed($affirm_id, 'large', array());
}
return $this->render_sdk_embed($affirm_id, 'large', array());
}, $content);
return $content;
}
/**
* Render SDK canvas embed (maximum protection)
* The SDK script auto-discovers these divs and renders encrypted canvas.
*/
private function render_sdk_embed($affirm_id, $size, $attr) {
$dimensions = $this->get_dimensions($size, $attr);
return sprintf(
'
',
esc_attr($affirm_id),
esc_attr($dimensions['width']),
esc_attr($dimensions['height']),
esc_attr($dimensions['width']),
esc_attr($dimensions['height'])
);
}
/**
* Render iframe embed (high protection)
* The iframe loads the backend's /sdk/embed/:imageId page which includes the SDK.
*/
private function render_iframe_embed($affirm_id, $size, $attr) {
$dimensions = $this->get_dimensions($size, $attr);
$embed_url = affirm_get_backend_url() . '/sdk/embed/' . esc_attr($affirm_id);
return sprintf(
'',
esc_url($embed_url),
esc_attr($dimensions['width']),
esc_attr($dimensions['height'])
);
}
/**
* Output og:image meta tag using signed URL.
* Social crawlers (Facebook, Twitter, LinkedIn, Google) need a real image URL.
* This is the ONLY place signed URLs are used for rendering — everything else uses SDK.
*/
public function output_og_image() {
if (!is_singular()) return;
global $post;
if (!$post) return;
$affirm_id = get_post_meta($post->ID, 'affirm_featured_image_id', true);
if (!$affirm_id) return;
// 7-day TTL — long enough for social crawlers to cache
$signed_url = Affirm_Api::get_signed_url($affirm_id, 604800);
if (!$signed_url) return;
echo '' . "\n";
echo '' . "\n";
echo '' . "\n";
echo '' . "\n";
echo '' . "\n";
echo '' . "\n";
}
/**
* WooCommerce product image support
* Return the thumbnail_id as-is. The actual rendering override happens
* in woo_single_product_image_html filter.
*/
public function woo_product_image($thumbnail_id) {
return $thumbnail_id;
}
/**
* Replace the main product image HTML on single product pages.
* WooCommerce calls wc_get_gallery_image_html() which generates an
tag
* from a WP attachment ID. Since vault images have no attachment, we replace
* the entire HTML output with our SDK/iframe embed.
*
* @param string $html The default gallery image HTML
* @param int $attachment_id The WP attachment ID (first image = main product image)
*/
public function woo_single_product_image_html($html, $attachment_id) {
global $product;
if (!$product) return $html;
$post_id = $product->get_id();
// Check if this is the main product image (first call = main image)
// WooCommerce calls this filter for the main image with the product's thumbnail_id
$product_thumbnail_id = $product->get_image_id();
// Only replace the main image slot if it matches the product thumbnail
// AND we have a vault featured image set
if ($attachment_id == $product_thumbnail_id || !$product_thumbnail_id) {
$affirm_id = get_post_meta($post_id, 'affirm_featured_image_id', true);
if ($affirm_id) {
return $this->render_woo_image_slide($affirm_id);
}
}
return $html;
}
/**
* Replace product listing/archive thumbnails with vault image.
* This handles shop pages, related products, upsells, etc.
*/
public function woo_product_listing_image($image, $product, $size, $attr, $placeholder) {
if (!$product) return $image;
$post_id = $product->get_id();
$affirm_id = get_post_meta($post_id, 'affirm_featured_image_id', true);
if (!$affirm_id) return $image;
$render_mode = get_option('affirm_render_mode', 'sdk');
if ($render_mode === 'iframe') {
$embed_url = affirm_get_backend_url() . '/sdk/embed/' . esc_attr($affirm_id);
return sprintf(
'',
esc_url($embed_url)
);
}
return sprintf(
'',
esc_attr($affirm_id)
);
}
/**
* Render vault gallery images in WooCommerce product gallery
* Hooks into woocommerce_product_thumbnails to add vault images
* alongside any standard WP gallery images.
*/
public function render_woo_gallery_images() {
global $product;
if (!$product) return;
$post_id = $product->get_id();
$vault_gallery = get_post_meta($post_id, '_affirm_product_gallery', true);
if (!$vault_gallery || !is_array($vault_gallery) || empty($vault_gallery)) return;
foreach ($vault_gallery as $affirm_id) {
if (empty($affirm_id)) continue;
echo wp_kses($this->render_woo_image_slide(sanitize_text_field($affirm_id)), $this->allowed_embed_html());
}
}
/**
* Allowed HTML tags/attributes for protected image embeds (SDK div + iframe).
*/
private function allowed_embed_html() {
return array(
'div' => array(
'data-affirmfirst' => true,
'data-thumb' => true,
'data-af-width' => true,
'data-af-height' => true,
'class' => true,
'style' => true,
),
'iframe' => array(
'src' => true,
'frameborder' => true,
'style' => true,
'loading' => true,
'width' => true,
'height' => true,
),
);
}
/**
* Render a single WooCommerce gallery slide with a vault image
*/
private function render_woo_image_slide($affirm_id) {
$render_mode = get_option('affirm_render_mode', 'sdk');
if ($render_mode === 'iframe') {
$embed_url = affirm_get_backend_url() . '/sdk/embed/' . esc_attr($affirm_id);
$image_html = sprintf(
'',
esc_url($embed_url)
);
} else {
$image_html = sprintf(
'',
esc_attr($affirm_id)
);
}
return '' . $image_html . '
';
}
/**
* Enqueue CSS for vault images inside WooCommerce product gallery.
* Only loaded on single product pages that actually use a vault image.
*/
public function enqueue_woo_vault_styles() {
if (!is_singular('product')) return;
global $post;
if (!$post) return;
$affirm_id = get_post_meta($post->ID, 'affirm_featured_image_id', true);
$vault_gallery = get_post_meta($post->ID, '_affirm_product_gallery', true);
if (!$affirm_id && (!$vault_gallery || empty($vault_gallery))) return;
wp_enqueue_style(
'affirm-woo-vault',
AFFIRM_PLUGIN_URL . 'assets/css/woo-vault.css',
array(),
AFFIRM_VERSION
);
}
/**
* Normalize a dimension value for safe output in an inline style attribute.
* Delegates to the strict allow-list sanitizer in Affirm_Shortcode so that
* size attributes can never be used to inject arbitrary CSS.
*/
private function normalize_dimension($value, $default = 'auto') {
return Affirm_Shortcode::sanitize_dimension($value, $default);
}
/**
* Get dimensions from WP size name or attributes, normalized with CSS units.
* Attributes override size presets. Default: 100% width, auto height.
*/
private function get_dimensions($size, $attr) {
$width = '100%';
$height = 'auto';
// Size presets (only if no explicit attr override)
if (is_string($size)) {
switch ($size) {
case 'thumbnail':
$width = '150px';
$height = '150px';
break;
case 'medium':
$width = '300px';
$height = 'auto';
break;
case 'large':
$width = '1024px';
$height = 'auto';
break;
case 'full':
default:
$width = '100%';
$height = 'auto';
break;
}
}
// Explicit attributes override size presets
if (is_array($attr)) {
if (isset($attr['width'])) $width = $this->normalize_dimension($attr['width'], $width);
if (isset($attr['height'])) $height = $this->normalize_dimension($attr['height'], $height);
}
return array('width' => $width, 'height' => $height);
}
}