Last active
August 13, 2016 03:40
-
-
Save y-krn/4a44a5094bf27606c12803590a92d131 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
define( 'AWS_API_KEY', '' ); | |
define( 'AWS_API_SECRET_KEY', '' ); | |
define( 'AWS_ASSOCIATE_TAG', '' ); | |
new Amazon_Link_Builder(); | |
use ApaiIO\Configuration\GenericConfiguration; | |
use ApaiIO\Operations\Lookup; | |
use ApaiIO\ApaiIO; | |
/** | |
* Class Name: Amazon Link Builder | |
*@author @ottanxyz | |
*/ | |
class Amazon_Link_Builder { | |
function __construct() { | |
wp_embed_register_handler( | |
'amazon_link_builder', | |
'/http:\\/\\/www\\.amazon\\.co\\.jp\\/dp\\/(.*)$/i', | |
array( &$this, 'handler' ) | |
); | |
add_shortcode( 'amazon', array( &$this, 'display' ) ); | |
} | |
public function handler( $matches, $attr, $url, $rawattr ) { | |
preg_match( "/([a-zA-Z0-9]+)/", $matches[1], $asin ); | |
return "[amazon asin=\"{$asin[1]}\"]"; | |
} | |
public function display( $p ) { | |
if ( !isset( $p['asin'] ) ) { | |
return; | |
} | |
if ( false === ( $results = get_transient( 'amazon-' . $p['asin'] ) ) ) { | |
require_once 'ApaiIO/Configuration/ConfigurationInterface.php'; | |
require_once 'ApaiIO/Configuration/Country.php'; | |
require_once 'ApaiIO/Configuration/GenericConfiguration.php'; | |
require_once 'ApaiIO/Operations/OperationInterface.php'; | |
require_once 'ApaiIO/Operations/AbstractOperation.php'; | |
require_once 'ApaiIO/Operations/Lookup.php'; | |
require_once 'ApaiIO/Request/Util.php'; | |
require_once 'ApaiIO/Request/RequestInterface.php'; | |
require_once 'ApaiIO/Request/Rest/Request.php'; | |
require_once 'ApaiIO/Request/RequestFactory.php'; | |
require_once 'ApaiIO/ApaiIO.php'; | |
$conf = new GenericConfiguration(); | |
$conf | |
->setCountry( 'co.jp' ) | |
->setAccessKey( AWS_API_KEY ) | |
->setSecretKey( AWS_API_SECRET_KEY ) | |
->setAssociateTag( AWS_ASSOCIATE_TAG ); | |
$apaiIO = new ApaiIO( $conf ); | |
$search = new Lookup(); | |
$search->setItemId( $p['asin'] ); | |
$search->setResponseGroup( array( 'Images', 'ItemAttributes', 'OfferSummary' ) ); | |
$formattedResponse = $apaiIO->runOperation( $search ); | |
$results = json_decode( json_encode( simplexml_load_string( $formattedResponse ) ), true ); | |
$results = $results['Items']['Item']; | |
unset( $results['ImageSets'] ); | |
set_transient( 'amazon-' . $p['asin'], $results, DAY_IN_SECONDS ); | |
} | |
$DetailPageURL = $results['DetailPageURL']; | |
$Title = $results['ItemAttributes']['Title']; | |
$Publisher = $results['ItemAttributes']['Publisher']; | |
$FormattedPrice = $results['OfferSummary']['LowestNewPrice']['FormattedPrice']; | |
$SmallImage = $results['SmallImage']['URL']; | |
return <<< EOM | |
<div class="wpappbox"> | |
<div class="appicon"> | |
<a target="_blank" rel="nofollow" href="${detail_page_url}" title="${title}" sl-processed="1"><img src="${small_image}" scale="0" width="60"></a> | |
</div> | |
<div class="applinks"> | |
</div> | |
<div class="appdetails"> | |
<div class="apptitle"> | |
<a target="_blank" rel="nofollow" href="${detail_page_url}" title="${title}" class="apptitle" sl-processed="1">${title}</a></div> | |
<div class="developer">Developer: ${publisher}</div> | |
<div class="price">Price: ${formatted_price}</div> | |
</div> | |
</div> | |
EOM; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment