如何实现WordPress插件收集多短码调用批量请求亚马逊API?
解决方案:实现可选批量请求的短码组
完全可以实现你说的方案,核心思路是通过组短码标记批量模式,收集内部所有单个短码的请求参数,统一执行批量API调用,再渲染每个短码的内容。同时保留原有单个短码的独立功能,不强制使用组短码。
实现步骤
1. 用类管理状态(避免全局变量污染)
用插件类维护批量模式状态、待收集的商品信息,既安全又便于维护。
2. 注册组短码和单个短码
- 组短码
mm-productlinking-group:负责开启批量模式、解析内部内容、收集请求参数、执行批量调用、渲染最终内容。 - 单个短码
mm-productlinking:根据当前是否处于批量模式,决定是收集参数还是直接执行独立请求。
3. 批量请求逻辑
- 收集所有商品ID后,先筛选出未在
transient缓存中的ID,仅对这些发起批量API请求。 - 将批量返回的结果逐个存入
transient,确保后续请求可直接复用缓存。
4. 替换短码内容
批量请求完成后,遍历收集的每个短码参数,按对应模板渲染商品信息,替换回原短码位置。
完整代码示例
class MM_Product_Linking { private $batch_mode = false; private $batch_items = []; private $batch_index = 0; public function __construct() { // 注册短码 add_shortcode('mm-productlinking-group', [$this, 'handle_group_shortcode']); add_shortcode('mm-productlinking', [$this, 'handle_single_shortcode']); } // 处理组短码 public function handle_group_shortcode($atts, $content = '') { // 开启批量模式 $this->batch_mode = true; $this->batch_items = []; $this->batch_index = 0; // 解析内部内容(单个短码会把参数存入batch_items,返回占位符) $parsed_content = do_shortcode($content); // 关闭批量模式 $this->batch_mode = false; // 分离已缓存和需要请求的商品ID $needed_asins = []; $cached_items = []; foreach ($this->batch_items as $item) { $transient_key = 'mm_productlinking_' . $item['id']; if (false !== ($data = get_transient($transient_key))) { $cached_items[$item['id']] = $data; } else { $needed_asins[] = $item['id']; } } // 批量请求亚马逊API(仅当有未缓存的ASIN时执行) if (!empty($needed_asins)) { // 替换为你的亚马逊批量API调用逻辑 $batch_results = $this->call_amazon_batch_api($needed_asins); // 将结果存入transient缓存 foreach ($batch_results as $asin => $data) { $transient_key = 'mm_productlinking_' . $asin; set_transient($transient_key, $data, DAY_IN_SECONDS); // 缓存时长可自定义 $cached_items[$asin] = $data; } } // 替换占位符为实际渲染内容 foreach ($this->batch_items as $index => $item) { $content = $cached_items[$item['id']] ?? null; if ($content) { // 替换为你的模板渲染逻辑 $rendered = $this->render_template($content, $item['template']); $parsed_content = str_replace("[mm-placehold-{$index}]", $rendered, $parsed_content); } else { $parsed_content = str_replace("[mm-placehold-{$index}]", '商品信息加载失败', $parsed_content); } } return $parsed_content; } // 处理单个短码 public function handle_single_shortcode($atts) { $atts = shortcode_atts([ 'id' => '', 'template' => 'image' ], $atts); if (empty($atts['id'])) { return ''; } // 批量模式下,收集参数并返回占位符 if ($this->batch_mode) { $this->batch_items[] = [ 'id' => $atts['id'], 'template' => $atts['template'] ]; $placeholder = "[mm-placehold-{$this->batch_index}]"; $this->batch_index++; return $placeholder; } // 非批量模式,正常独立请求 $transient_key = 'mm_productlinking_' . $atts['id']; if (false !== ($data = get_transient($transient_key))) { return $this->render_template($data, $atts['template']); } // 调用单个亚马逊API $data = $this->call_amazon_single_api($atts['id']); if ($data) { set_transient($transient_key, $data, DAY_IN_SECONDS); return $this->render_template($data, $atts['template']); } return '商品信息加载失败'; } // 批量调用亚马逊API的示例方法(替换为你的实际逻辑) private function call_amazon_batch_api($asins) { // 实现亚马逊Product Advertising API批量请求逻辑 // 返回格式:[ 'asin1' => 商品数据, 'asin2' => 商品数据 ] return []; } // 单个调用亚马逊API的示例方法(替换为你的实际逻辑) private function call_amazon_single_api($asin) { // 实现亚马逊Product Advertising API单个请求逻辑 return []; } // 模板渲染示例方法(替换为你的实际模板逻辑) private function render_template($data, $template) { if ($template === 'image') { return '<a href="' . esc_url($data['url']) . '"><img src="' . esc_url($data['image']) . '" alt="' . esc_attr($data['title']) . '"></a>'; } // 其他模板类型的渲染逻辑... return ''; } } // 初始化插件 new MM_Product_Linking();
关键细节说明
- 兼容性:原有单个短码
[mm-productlinking id="1" template="image"]可继续独立使用,不受组短码影响。 - 缓存复用:批量和单个请求都会优先读取
transient缓存,彻底避免重复请求。 - 占位符机制:组内解析单个短码时先返回唯一占位符,批量请求完成后再替换为实际内容,保证页面HTML结构不受干扰。
- 错误兜底:添加了商品加载失败的提示,避免页面出现空白或异常。
内容的提问来源于stack exchange,提问作者commadelimited
相关产品推荐
相关产品推荐

