@charset "UTF-8";
/*!
Theme Name: Cocoon Child
Description: Cocoon専用の子テーマ
Theme URI: https://wp-cocoon.com/
Author: わいひら
Author URI: https://nelog.jp/
Template:   cocoon-master
Version:    1.1.3
*/
<?php//==========ここからAmazon 300x300広告ウィジェットのコード==========function amazon_ad_300x300_shortcode($atts){//ショートコードの属性（パラメータ）を設定 $attributes=shortcode_atts(['category_id'=>'2127209051',//デフォルトカテゴリ:パソコン・周辺機器 'count'=>1,//デフォルト表示件数:1件 'sort'=>'NewestArrivals'//デフォルトソート:新着順],$atts);//==========設定項目==========//あなたのPA-API認証情報とアソシエイトタグに書き換えてください $accessKey='AKPAX4QBGC1750111232';//★★★ あなたのアクセスキー ★★★ $secretKey='XO2C69X8i5JtNjQ/xxHKK0ap8FP5oJVcCHuS+Ckq';//★★★ あなたのシークレットキー ★★★ $partnerTag='anotherfuture-22';//★★★ あなたのアソシエイトタグ ★★★ $host='webservices.amazon.co.jp';$region='us-west-2';//============================//キャッシュ機能（毎回APIを叩かないようにするため） $cache_key='amazon_ad_' . md5(serialize($attributes));$cached_html=get_transient($cache_key);if ($cached_html){return $cached_html}$payload=json_encode(['BrowseNodeId'=>$attributes['category_id'],'LanguagesOfPreference'=>['ja_JP'],'Resources'=>['Images.Primary.Large','ItemInfo.Title','DetailPageURL'],'PartnerTag'=>$partnerTag,'PartnerType'=>'Associates','SortBy'=>$attributes['sort'],'ItemCount'=>$attributes['count']>10 ? 10 :$attributes['count']//APIの上限は10件]);//PA-API v5 署名生成とリクエスト (前回と同様のロジック) $service='ProductAdvertisingAPI';$timestamp=gmdate('Ymd\THis\Z');$headers=['x-amz-date'=>$timestamp,'x-amz-target'=>'com.amazon.paapi5.v1.ProductAdvertisingAPIv1.GetItems','host'=>$host,'content-encoding'=>'amz-1.0','content-type'=>'application/json; charset=utf-8'];ksort($headers);$canonicalHeaders='';$signedHeaders='';foreach ($headers as $key=>$value){$canonicalHeaders .=$key . ':' . $value . "\n";$signedHeaders .=$key . ';'}$signedHeaders=rtrim($signedHeaders,';');$canonicalRequest="POST\n/paapi5/getitems\n\n" . $canonicalHeaders . "\n" . $signedHeaders . "\n" . hash('sha256',$payload);$stringToSign="AWS4-HMAC-SHA256\n" . $timestamp . "\n" . substr($timestamp,0,8) . "/$region/$service/aws4_request\n" . hash('sha256',$canonicalRequest);$dateKey=hash_hmac('sha256',substr($timestamp,0,8),'AWS4' . $secretKey,true);$dateRegionKey=hash_hmac('sha256',$region,$dateKey,true);$dateRegionServiceKey=hash_hmac('sha256',$service,$dateRegionKey,true);$signingKey=hash_hmac('sha256','aws4_request',$dateRegionServiceKey,true);$signature=hash_hmac('sha256',$stringToSign,$signingKey);$curlHeaders=[];foreach ($headers as $key=>$value){$curlHeaders[]="$key: $value"}$curlHeaders[]="Authorization: AWS4-HMAC-SHA256 Credential=$accessKey/" . substr($timestamp,0,8) . "/$region/$service/aws4_request, SignedHeaders=$signedHeaders, Signature=$signature";$response=wp_remote_post("https://$host/paapi5/getitems",['headers'=>$curlHeaders,'body'=>$payload,'timeout'=>15]);$output='';//表示するHTMLを格納する変数 if (!is_wp_error($response)){$body=wp_remote_retrieve_body($response);$data=json_decode($body,true);if (isset($data['ItemsResult']['Items'])){$output .='<div class="amazon-ad-container-300">';foreach ($data['ItemsResult']['Items'] as $item){if (!empty($item['Images']['Primary']['Large']['URL'])){$output .=sprintf('<div class="amazon-ad-item-300"><a href="%s" target="_blank" rel="noopener sponsored"><img src="%s" alt="%s" width="300" height="300" loading="lazy"></a></div>',esc_url(//myaomoriguide.com/wp-content/themes/cocoon-child-master/$item['DetailPageURL']),esc_url(//myaomoriguide.com/wp-content/themes/cocoon-child-master/$item['Images']['Primary']['Large']['URL']),esc_attr($item['ItemInfo']['Title']['DisplayValue']))}}$output .='</div>';}}//結果を1時間キャッシュする if (!empty($output)){set_transient($cache_key,$output,1*HOUR_IN_SECONDS)}return $output;}add_shortcode('amazon_ad_300x300','amazon_ad_300x300_shortcode');//==========ここまでAmazon 300x300広告ウィジェットのコード==========