Last active
July 28, 2017 08:35
-
-
Save y-krn/fa722621d551eb76c6302166681c10a2 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( 'ITUNES_AFFILIATE_TAG', '' ); | |
new Itunes_Link_Builder(); | |
/** | |
* Class Name: iTunes Link Builder | |
* @author @ottanxyz | |
*/ | |
class iTunes_Link_Builder { | |
function __construct() { | |
wp_embed_register_handler( | |
'itunes_link_builder', | |
'/https:\\/\\/itunes\\.apple\\.com\\/jp\\/(.*)$/i', | |
array( &$this, 'handler' ) | |
); | |
add_shortcode( 'itunes', array( &$this, 'display' ) ); | |
} | |
public function handler( $matches, $attr, $url, $rawattr ) { | |
preg_match( "/id(\d+)/", $matches[1], $id ); | |
return "[itunes id=\"{$id[1]}\"]"; | |
} | |
public function display( $p ) { | |
if ( !isset( $p['id'] ) ) { | |
return; | |
} | |
if ( false === ( $results = get_transient( 'itunes-' . $p['id'] ) ) ) { | |
$url = sprintf( "https://itunes.apple.com/lookup?id=%s&country=JP", $p['id'] ); | |
$ch = curl_init(); | |
curl_setopt( $ch, CURLOPT_URL, $url ); | |
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); | |
curl_setopt( $ch, CURLOPT_FRESH_CONNECT, true ); | |
$json = json_decode( curl_exec( $ch ) ); | |
curl_close( $ch ); | |
if ( isset( $json->error ) && $json->error ) { | |
return; | |
} | |
$results = $json->results[0]; | |
set_transient( 'itunes-' . $p['id'], $results, DAY_IN_SECONDS ); | |
} | |
if ( !isset( $results->kind ) || ( 'software' !== $results->kind && 'mac-software' !== $results->kind ) ) { | |
return; | |
} | |
$artistName = $results->artistName; | |
$artworkUrl60 = $results->artworkUrl60; | |
$trackName = $results->trackName; | |
$trackViewUrl = $results->trackViewUrl . "&at=". ITUNES_AFFILIATE_TAG; | |
$genres = implode( ', ', $results->genres ); | |
$formattedPrice = $results->formattedPrice; | |
$storeName = ( $results->kind === 'software' ) ? 'App Store' : 'Mac App Store'; | |
return <<< EOM | |
<div class="wpappbox"> | |
<div class="appicon"> | |
<a target="_blank" rel="nofollow" href="${trackViewUrl}" title="${trackName}" sl-processed="1"><img src="${artworkUrl60}" scale="0"></a> | |
</div> | |
<div class="applinks"> | |
</div> | |
<div class="appdetails"> | |
<div class="apptitle"> | |
<a target="_blank" rel="nofollow" href="${trackViewUrl}" title="${trackName}" class="apptitle" sl-processed="1">${trackName}</a></div> | |
<div class="developer">Developer: ${artistName}</div> | |
<div class="price">Price: ${formattedPrice}</div> | |
</div> | |
</div> | |
EOM; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment