/* === RDA add-on: auto-insert live-price shortcodes in title & body === */ if (!function_exists('rda_amz_extract_asin_from_content')) { function rda_amz_extract_asin_from_content($content){ if (!is_string($content)) return ''; if (preg_match('/data-asin=["\\\']([A-Z0-9]{8,16})["\\\']/i', $content, $m)) { return $m[1]; } // Try common ASIN meta if present $post_id = get_the_ID(); if ($post_id){ $meta = get_post_meta($post_id, '_rda_amz_asin', true); if (!empty($meta)) return $meta; } return ''; } // Replace static price text inside our deal paragraph with shortcodes function rda_amz_autoshortcodes_content($html){ if (!is_string($html) || stripos($html, 'rda-amz') === false) return $html; $asin = rda_amz_extract_asin_from_content($html); if (!$asin) return $html; // Replace "only $123.45" and "(reg $123.45)" patterns inside the rda-amz-found paragraph $html = preg_replace_callback( '#(
]*class=["\\\']rda-amz-found["\\\'][^>]*>)(.*?)(
)#is', function($m) use ($asin){ $p1=$m[1]; $txt=$m[2]; $p3=$m[3]; // only $X → shortcode $txt = preg_replace('/only\\s*\\$[0-9\\.,]+/i', 'only [rd_amz_price asin="'.$asin+'"]', $txt, 1); // (reg $X) → shortcode $txt = preg_replace('/\\(\\s*reg\\s*\\$[0-9\\.,]+\\s*\\)/i', '(reg [rd_amz_list asin="'.$asin.'"])', $txt, 1); // If no reg portion exists, append live reg after price once if (strpos($txt, 'rd_amz_list') === false && strpos($txt, 'rd_amz_price') !== false){ $txt .= ' (reg [rd_amz_list asin="'.$asin.'"])'; } return $p1.$txt.$p3; }, $html ); return $html; } add_filter('the_content', 'rda_amz_autoshortcodes_content', 8); // Ensure shortcodes run in title add_filter('the_title', 'do_shortcode', 9); // Replace static price in title with shortcodes using the first ASIN found in content function rda_amz_autoshortcodes_title($title, $post_id){ if (!is_singular() && !is_admin()) return $title; $post = get_post($post_id); if (!$post) return $title; $asin = rda_amz_extract_asin_from_content($post->post_content); if (!$asin) return $title; $new = $title; // Replace first $price in title with shortcode $replaced = 0; $new = preg_replace('/\\$[0-9\\.,]+/', '[rd_amz_price asin="'.$asin.'"]', $new, 1, $replaced); if ($replaced === 0) { // If no $ found, append price $new .= ' — [rd_amz_price asin="'.$asin.'"]'; } // Handle (reg $...) if (preg_match('/\\(\\s*reg\\s*\\$[0-9\\.,]+\\s*\\)/i', $new)) { $new = preg_replace('/\\(\\s*reg\\s*\\$[0-9\\.,]+\\s*\\)/i', '(reg [rd_amz_list asin="'.$asin.'"])', $new, 1); } else { $new .= ' (reg [rd_amz_list asin="'.$asin.'"])'; } return $new; } add_filter('the_title', 'rda_amz_autoshortcodes_title', 8, 2); } /* === /RDA add-on === */