' . "\n";
}
// エントリ内の [rakuten]code[/rakuten] を置換する。
public function short_code( $atts, $content = null ){
$classcode = str_replace(":", "", $content);
if ( $this->is_mobile() ){
$rakutencode = '
' . $this->get_data(htmlspecialchars($content)) . '
';
} else {
$rakutencode = '' . $this->get_data(htmlspecialchars($content)) . '
';
}
return $rakutencode;
}
public function check_cache($itemcode){
$filename = dirname(__FILE__) . '/cache/' . str_replace(":", "_", htmlspecialchars($itemcode)) . '.xml';
if ( file_exists($filename) ) {
// キャッシュ作成日から24時間以上経過しているか
if ( time() <= filemtime($filename) + 86400) {
return date("Y/n/j H:i", filemtime($filename));
} else {
return false;
}
} else {
return false;
}
}
public function create_cache($itemcode, $str){
mb_internal_encoding("UTF-8");
if ( @file_put_contents(dirname(__FILE__) . '/cache/' . str_replace(":", "_", htmlspecialchars($itemcode)) . '.xml', $str) ){
return date("Y/n/j H:i");
} else {
return false;
}
}
public function is_mobile (){
$ua = $_SERVER['HTTP_USER_AGENT'];
if (preg_match("/^DoCoMo\//i", $ua)) {
return true;
} else if (preg_match("/^SoftBank/i", $ua)) { // SoftBank
return true;
} else if (preg_match("/^(Vodafone|MOT-)/i", $ua)) { // Vodafone 3G
return true;
} else if (preg_match("/^KDDI\-/i", $ua)) { // au (XHTML)
return true;
} else if (preg_match("/UP\.Browser/i", $ua)) { // au (HDML) TU-KA
return true;
} else if (preg_match("/WILLCOM/i", $ua)){ // WILLCOM Air EDGE
return true;
} else {
return false;
}
}
public function get_data($itemcode){
mb_internal_encoding("UTF-8");
$rakuten_options = get_option('wp_rakuten_options');
$itemcode = htmlspecialchars($itemcode);
$output = '';
// キャッシュの存在確認
if ( $update_time = $this->check_cache($itemcode) ){
if ( $xml = simplexml_load_string(@file_get_contents(dirname(__FILE__) . '/cache/' . str_replace(":", "_", $itemcode) . '.xml')) ){
$output = '';
} else {
return "Error: Failed to load cache file.";
}
// キャッシュがなければ取りに行く
} else {
if ( $rakuten_options['developer_id'] == '' ){
return "Error: Developers ID has not been set.";
}
if ( preg_match("/^(\d{3}):(\d+)/", $itemcode, $match) ){
if ( $match[1] == '001'){ // 001 = Books
$url =
"https://app.rakuten.co.jp/services/api/BooksTotal/Search/20130522?applicationId=" . $rakuten_options['developer_id'] .
"&affiliateId=" . $rakuten_options['affiliate_id'] .
"&format=xml" .
"&isbnjan=" . $match[2];
}
} else {
$url =
"https://app.rakuten.co.jp/services/api/IchibaItem/Search/20140222?applicationId=" . $rakuten_options['developer_id'] .
"&affiliateId=" . $rakuten_options['affiliate_id'] .
"&format=xml" .
"&itemCode=" . $itemcode;
}
$origin = @file_get_contents($url);
if ( !empty($origin) ) {
// http://d.hatena.ne.jp/ilo/20080101/1199199418 を参考にさせていただきつつ、処理
$origin = str_replace('header:Header', 'Header', $origin);
$origin = str_replace('itemCodeSearch:ItemCodeSearch', 'ItemCodeSearch', $origin);
$origin = str_replace('booksBookSearch:BooksBookSearch', 'BooksBookSearch', $origin);
$origin = str_replace($rakuten_options['developer_id'],'DELETED', $origin);
$xml = simplexml_load_string($origin);
} else {
return "Error: Failed to retrieve data for this request.";
}
}
// 表示処理部分
if ( $xml->count > 0 ){
if ( $match[1] != '001' ) {
$body = $xml->Items->Item;
$sImage = ($body->imageFlag == "1") ? '
' : '
';
$mImage = ($body->imageFlag == "1") ? '
' : '
';
$shop_name = $body->shopName;
$shop_url = $body->shopUrl;
} else {
$body = $xml->Items->Item;
if ( $api_category == 'BooksBookSearch' ) {
$sImage = '
';
$mImage = '
';
$shop_name = "楽天ブックス";
$shop_url = "http://books.rakuten.co.jp/";
}
}
$caution = "このサイトで掲載されている情報は、「" . htmlspecialchars(get_option('blogname')) . "」の作成者により運営されています。価格、販売可能情報は、変更される場合があります。購入時に楽天市場店舗(www.rakuten.co.jp)に表示されている価格が、その商品の販売に適用されます。";
$rak_words = array(
"#ItemName#" => ( isset($body->title) ) ? $body->title : attribute_escape($body->itemName),
"#CatchCopy#" => ( isset($body->catchcopy) ) ? attribute_escape($body->catchcopy) : "",
"#ItemPrice#" => number_format("$body->itemPrice", 0) . ' 円',
"#ItemCaption#" => (strlen($body->itemCaption) >= $rakuten_options['caption_char']) ? mb_substr($body->itemCaption, 0, $rakuten_options['caption_char'])."..." : htmlspecialchars($body->itemCaption),
"#Url#" => ($this->is_mobile()) ? 'http://hb.afl.rakuten.co.jp/hgc/'.$rakuten_options['affiliate_id'].'/?m=' . urlencode($body->itemUrl) : $body->affiliateUrl,
"#sImage#" => $sImage,
"#mImage#" => $mImage,
"#ItemStatus#" => ($body->availability == 0) ? "(販売不可)":"",
"#Tax#" => ($body->taxFlag == 0) ? "(税込)":"(税別)",
"#Postage#" => ($body->postageFlag == 0) ? "(送料込)":"(送料別)",
"#CCard#" => ($body->creditCardFlag == 0) ? "(カード不可)":"(カード可)",
"#AsuRaku#" => ($body->asurakuFlag == 0) ? "(翌日配送不可)":"(翌日配送可)",
"#RCount#" => $body->reviewCount,
"#RAvg#" => $body->reviewAverage,
"#ShopName#" => $shop_name,
"#ShopCode#" => $body->shopCode,
"#ShopUrl#" => '' . $shop_name . '',
"#Target#" => ($rakuten_options['target_window'] == 'blank') ? 'target="_blank"':'target="_self"',
"#LastDate#" => $update_time,
"#Caution#" => $caution,
"#CautionTips#" => '[ご利用にあたって]'.$caution.'',
);
// テンプレートが存在するか確認。
if ( $rakuten_options['item_template'] != '' ) {
$output = strtr(stripslashes($rakuten_options['item_template']), $rak_words);
} else {
$default_template =<<< TMP
#ItemName#
#ItemCaption#
販売価格: #ItemPrice# (#LastDate# 更新)
販売店舗: #ShopUrl#
TMP;
$output = strtr(stripslashes($default_template), $rak_words);
}
$credit_code =<<< CREDIT
Supported by 楽天ウェブサービス
CREDIT;
if ( $rakuten_options['show_credit'] != 'no' ) {
$output.= '' . $credit_code .'
';
}
return $output;
} else {
return "好評につき売切れです";
}
}
} // class RakutenLink end
// class Rakuten Admin Option Menus
$RakutenAdmin = new RakutenAdmin;
class RakutenAdmin {
public function __construct() {
add_action('admin_head', array($this, 'add_head'));
add_action('admin_menu', array($this, 'add_menu'));
}
public function add_head() {
echo '';
}
public function add_menu() {
add_options_page(__('WP Rakuten Link','rakuten_link'), __('WP Rakuten Link','rakuten_link'), 'manage_options', __FILE__, array($this, 'options_page'));
add_meta_box('wp-rakuten-link', '楽天市場 商品検索', array($this, 'itemsearchform'), 'post');
add_meta_box('wp-rakuten-link', '楽天市場 商品検索', array($this, 'itemsearchform'), 'page');
}
private function cache_delete() {
foreach (glob(WP_PLUGIN_DIR . '/' . basename(dirname(__FILE__)) . '/cache/*.xml') as $value) {
unlink($value);
}
}
private function update_options() {
$default_template =<<< TMP
#ItemName#
#ItemCaption#
販売価格: #ItemPrice# (#LastDate# 更新)
販売店舗: #ShopUrl#
TMP;
$item_template = $_POST['item_template'];
( $_POST['affiliate_id'] == '' ) ? $affiliate_id = "09e50163.3358f198.09e50164.effd7e09" : $affiliate_id = attribute_escape($_POST['affiliate_id']);
( $_POST['item_template'] == '' ) ? $item_template = $default_template : $item_template = $_POST['item_template'];
( (int) $_POST['result_num'] > 30 ) ? $result_num = 30 : $result_num = (int) $_POST['result_num'];
$rakuten_options = array(
'developer_id' => attribute_escape($_POST['developer_id']),
'affiliate_id' => $affiliate_id,
'target_window' => attribute_escape($_POST['target_window']),
'show_credit' => attribute_escape($_POST['show_credit']),
'caption_char' => (int) $_POST['caption_char'],
'result_num' => $result_num,
'item_template' => $item_template,
);
update_option('wp_rakuten_options', $rakuten_options);
}
public function itemsearchform(){
$rakuten_options = get_option('wp_rakuten_options');
?>
update_options(); ?>
cache_delete(); ?>
#mImage#
#CautionTips#
#ItemName#
#ItemCaption#
販売価格: #ItemPrice# (#LastDate# 更新)
販売店舗: #ShopUrl#
TMP;
?>