楽天用グッズメモ アフィリエイト

グッズメモ アフィリエイト・WordPressプラグインのソースコード記事の目次WordPress

CommonRESTParameter.php

namespace goodsmemo\rakuten;

/**
 * Description of CommonRESTParameter
 *
 * @author Goods Memo
 */
class CommonRESTParameter {

    private $applicationId;
    private $affiliateId;

    public function getApplicationId() {
	return $this->applicationId;
    }

    public function getAffiliateId() {
	return $this->affiliateId;
    }

    public function setApplicationId($applicationId) {
	$this->applicationId = $applicationId;
    }

    public function setAffiliateId($affiliateId) {
	$this->affiliateId = $affiliateId;
    }

}

ItemHTMLUtils.php

namespace goodsmemo\rakuten;

use goodsmemo\item\html\ItemHTMLOption;
use goodsmemo\item\html\ImageItemHTMLOption;
use goodsmemo\item\html\PriceItemHTMLOption;
use goodsmemo\item\html\ReviewItemHTMLOption;
use goodsmemo\item\html\ReviewItemHTMLUtils;
use goodsmemo\option\rakuten\RakutenSettingSection;
use goodsmemo\option\rakuten\ItemHTMLFieldUtils;
use goodsmemo\option\rakuten\ImageFieldUtils;
use goodsmemo\option\rakuten\PriceFieldUtils;
use goodsmemo\option\rakuten\ReviewFieldUtils;

require_once GOODS_MEMO_DIR . "item/html/ItemHTMLOption.php";
require_once GOODS_MEMO_DIR . "item/html/ImageItemHTMLOption.php";
require_once GOODS_MEMO_DIR . "item/html/PriceItemHTMLOption.php";
require_once GOODS_MEMO_DIR . "item/html/ReviewItemHTMLOption.php";
require_once GOODS_MEMO_DIR . "item/html/ReviewItemHTMLUtils.php";
require_once GOODS_MEMO_DIR . "option/rakuten/RakutenSettingSection.php";
require_once GOODS_MEMO_DIR . "option/rakuten/ItemHTMLFieldUtils.php";
require_once GOODS_MEMO_DIR . "option/rakuten/ImageFieldUtils.php";
require_once GOODS_MEMO_DIR . "option/rakuten/PriceFieldUtils.php";
require_once GOODS_MEMO_DIR . "option/rakuten/ReviewFieldUtils.php";

/**
 * Description of ItemHTMLOptionUtils
 *
 * @author Goods Memo
 */
class ItemHTMLUtils {

    public static function makeItemHTMLOption($optionMap, int $number): ItemHTMLOption {

	$option = new ItemHTMLOption();

	//TODO get_option()から、値を取得する。

	$option->setIdPrefix(RakutenSettingSection::ID_PREFIX);

	if (is_numeric($number)) {//ゼロも有効とした。
	    $option->setNumberToDisplay($number);
	} else {
	    $option->setNumberToDisplay(ItemHTMLFieldUtils::DEFAULT_NUMBER_TO_DISPLAY);
	}

	$titleLength = $optionMap[ItemHTMLFieldUtils::TITLE_LENGTH_ID];
	$option->setTitleLength($titleLength);

	$imageItemHTMLOption = ItemHTMLUtils::makeImageItemHTMLOption($optionMap);
	$option->setImageItemHTMLOption($imageItemHTMLOption);

	$priceItemHTMLOption = ItemHTMLUtils::makePriceItemHTMLOption($optionMap);
	$option->setPriceItemHTMLOption($priceItemHTMLOption);

	$reviewItemHTMLOption = ReviewItemHTMLUtils::makeReviewItemHTMLOption(
			$optionMap, ReviewFieldUtils::EDITORIAL_REVIEW_LENGTH_ID, ReviewFieldUtils::ARRAY_OF_STRING_TO_DELETE_ID, ReviewFieldUtils::ARRAY_OF_STRING_TO_BREAK_ID);
	$option->setReviewItemHTMLOption($reviewItemHTMLOption);

	$cacheExpirationInSeconds = $optionMap[ItemHTMLFieldUtils::CACHE_EXPIRATION_IN_SECONDS_ID];
	$option->setCacheExpirationInSeconds($cacheExpirationInSeconds);

	return $option;
    }

    private static function makeImageItemHTMLOption($optionMap): ImageItemHTMLOption {

	$imageItemHTMLOption = new ImageItemHTMLOption();

	//TODO get_option()から、値を取得する。
	$imageItemHTMLOption->setImageWidth(ImageFieldUtils::DEFAULT_MEDIUM_IMAGE_WIDTH);
	$imageItemHTMLOption->setImageHeight(ImageFieldUtils::DEFAULT_MEDIUM_IMAGE_HEIGHT);

	return $imageItemHTMLOption;
    }

    private static function makePriceItemHTMLOption($optionMap): PriceItemHTMLOption {

	$priceItemHTMLOption = new PriceItemHTMLOption();

	//TODO get_option()から、値を取得する。
	//PriceItemHTMLUtils::makePriceItemHTMLOptionを作成して、共通化する。

	$priceItemHTMLOption->setShowStateOfPriceTimeLink(PriceFieldUtils::DEFAULT_SHOW_STATE_OF_PRICE_TIME_LINK);
	$priceItemHTMLOption->setPriceFooterText(PriceFieldUtils::DEFAULT_PRICE_TIME_FOOTER_TEXT);

	return $priceItemHTMLOption;
    }

}

ItemSearchOperation.php

namespace goodsmemo\rakuten;

use goodsmemo\network\URLInfo;
use goodsmemo\rakuten\CommonRESTParameter;
use goodsmemo\rakuten\RESTParameter;
use goodsmemo\rakuten\SearchOption;
use goodsmemo\rakuten\RakutenRequest;
use goodsmemo\rakuten\RakutenResponse;
use goodsmemo\item\html\ItemArrayHTMLMaking;
use goodsmemo\item\html\ItemHTMLOption;
use goodsmemo\option\rakuten\RakutenSettingSection;
use goodsmemo\exception\HttpRequestException;

require_once GOODS_MEMO_DIR . "network/URLInfo.php";
require_once GOODS_MEMO_DIR . "rakuten/CommonRESTParameter.php";
require_once GOODS_MEMO_DIR . "rakuten/RESTParameter.php";
require_once GOODS_MEMO_DIR . "rakuten/SearchOption.php";
require_once GOODS_MEMO_DIR . "rakuten/RakutenRequest.php";
require_once GOODS_MEMO_DIR . "rakuten/RakutenResponse.php";
require_once GOODS_MEMO_DIR . "item/html/ItemArrayHTMLMaking.php";
require_once GOODS_MEMO_DIR . "item/html/ItemHTMLOption.php";
require_once GOODS_MEMO_DIR . "option/rakuten/RakutenSettingSection.php";
require_once GOODS_MEMO_DIR . "exception/HttpRequestException.php";

/**
 * Description of ItemSearchOperation
 *
 * @author Goods Memo
 */
class ItemSearchOperation {

    public static function makeHTMLOfIchibaItemSearch(
    URLInfo $urlInfo, CommonRESTParameter $commonParameter, RESTParameter $restParameter, SearchOption $searchOption, ItemHTMLOption $itemHTMLOption) {

	$API_ERROR_MESSAGE = "楽天商品検索APIの呼び出しに失敗しました。";

	$cacheExpirationInSeconds = $itemHTMLOption->getCacheExpirationInSeconds();
	if ($cacheExpirationInSeconds <= 0) {

	    $response = RakutenRequest::requestIchibaItemSearch($urlInfo, $commonParameter, $restParameter);
	    if ($response === false) {
		throw new HttpRequestException($API_ERROR_MESSAGE);
	    }

	    $itemArray = RakutenResponse::makeItemArray($response, $itemHTMLOption); // var_dump($itemArray);
	    $itemsHtml = ItemArrayHTMLMaking::makeItemArrayHTML($itemArray, $itemHTMLOption);
	    return $itemsHtml;
	}

	$itemArray;

	$operation = $searchOption->getOperation();
	$keyword = $restParameter->getKeyword();
	$numberToDisplay = $itemHTMLOption->getNumberToDisplay();
	//キャッシュされるデータにつけるユニークな識別子。長さ 45 文字以下であること。
	//md5() :32 文字の 16 進数からなるハッシュを返します。
	$uniqueText = RakutenSettingSection::ID_PREFIX . $operation . $keyword . $numberToDisplay . $cacheExpirationInSeconds;
	$transientID = GOODS_MEMO_PREFIX . md5($uniqueText);
	//var_dump($transientID);

	$cachedItemArray = get_transient($transientID);
	if ($cachedItemArray !== false) {

	    $itemArray = $cachedItemArray;
	} else {

	    $response = RakutenRequest::requestIchibaItemSearch($urlInfo, $commonParameter, $restParameter);
	    if ($response === false) {
		throw new HttpRequestException($API_ERROR_MESSAGE);
	    }

	    $itemArray = RakutenResponse::makeItemArray($response, $itemHTMLOption); // var_dump($itemArray);
	    //一時的に商品情報をデータベースに保存する。有効期限後、削除される。
	    set_transient($transientID, $itemArray, $cacheExpirationInSeconds);
	}

	$itemsHtml = ItemArrayHTMLMaking::makeItemArrayHTML($itemArray, $itemHTMLOption);

	return $itemsHtml;
    }

}

RakutenAffiliate.php

楽天アフィリエイト用の商品を表示するHTMLを、作成します。

namespace goodsmemo\rakuten;

use goodsmemo\rakuten\ItemSearchOperation;
use goodsmemo\rakuten\RakutenOptionUtils;
use goodsmemo\rakuten\ItemHTMLUtils;
use goodsmemo\network\URLUtils;
use goodsmemo\option\AffiliateSettingPage;
use goodsmemo\option\rakuten\URLFieldUtils;
use goodsmemo\option\rakuten\SearchFieldUtils;
use goodsmemo\exception\IllegalArgumentException;

require_once GOODS_MEMO_DIR . "rakuten/ItemSearchOperation.php";
require_once GOODS_MEMO_DIR . "rakuten/RakutenOptionUtils.php";
require_once GOODS_MEMO_DIR . "rakuten/ItemHTMLUtils.php";
require_once GOODS_MEMO_DIR . "network/URLUtils.php";
require_once GOODS_MEMO_DIR . "option/AffiliateSettingPage.php";
require_once GOODS_MEMO_DIR . "option/rakuten/URLFieldUtils.php";
require_once GOODS_MEMO_DIR . "option/rakuten/SearchFieldUtils.php";
require_once GOODS_MEMO_DIR . "exception/IllegalArgumentException.php";

/**
 * Description of RakutenAffiliate
 *
 * @author Goods Memo
 */
class RakutenAffiliate {

    public static function makeHTML($operationOfShortcode, $aKeyword, $number) {

	$keyword = trim($aKeyword);
	if (empty($keyword)) {
	    throw new IllegalArgumentException("検索キーワードが空です:[" . $keyword . "] number:[" . $number . "]");
	}

	if (is_numeric($number) && $number >= 0) {//ゼロ以上とした
; //NetBeansの「エディタ」「フォーマット」で左端に整形されてしまう。
	} else {
	    throw new IllegalArgumentException("無効な表示件数:" . $number);
	}

	$optionMap = get_option(AffiliateSettingPage::OPTION_NAME_OF_DATABASE); //ここで一回だけデータベースを読み込む。

	$urlInfo = URLUtils::makeURLInfo($optionMap, URLFieldUtils::HOSTNAME_ID, URLFieldUtils::PATH_ID);
	$commonParameter = RakutenOptionUtils::makeCommonRESTParameter($optionMap);
	$restParameter = RakutenOptionUtils::makeRESTParameter($optionMap, $keyword);
	$searchOption = RakutenOptionUtils::makeSearchOption($optionMap, $operationOfShortcode);
	$itemHTMLOption = ItemHTMLUtils::makeItemHTMLOption($optionMap, $number);

	$affiliateHTML;

	$operation = $searchOption->getOperation();
	switch ($operation) {
	    case SearchFieldUtils::ICHIBA_ITEM_SEARCH_OPERATION:

		//現時点では、Operation="ItemSearch"で、キーワード検索する処理だけ行なう。
		$affiliateHTML = ItemSearchOperation::makeHTMLOfIchibaItemSearch(
				$urlInfo, $commonParameter, $restParameter, $searchOption, $itemHTMLOption);
		break;

	    default:

		throw new IllegalArgumentException("無効なオペレーション:" . $operation);
	}

	return $affiliateHTML;
    }

}

RakutenOptionUtils.php

namespace goodsmemo\rakuten;

use goodsmemo\rakuten\CommonRESTParameter;
use goodsmemo\rakuten\RESTParameter;
use goodsmemo\rakuten\SearchOption;
use goodsmemo\option\rakuten\CommonRESTFieldUtils;
use goodsmemo\option\rakuten\RESTFieldUtils;
use goodsmemo\option\rakuten\SearchFieldUtils;

require_once GOODS_MEMO_DIR . "rakuten/CommonRESTParameter.php";
require_once GOODS_MEMO_DIR . "rakuten/RESTParameter.php";
require_once GOODS_MEMO_DIR . "rakuten/SearchOption.php";
require_once GOODS_MEMO_DIR . "option/rakuten/CommonRESTFieldUtils.php";
require_once GOODS_MEMO_DIR . "option/rakuten/RESTFieldUtils.php";
require_once GOODS_MEMO_DIR . "option/rakuten/SearchFieldUtils.php";

/**
 * Description of RakutenOptionUtils
 *
 * @author Goods Memo
 */
class RakutenOptionUtils {

    public static function makeCommonRESTParameter($optionMap): CommonRESTParameter {

	$parameter = new CommonRESTParameter();

	$applicationId = $optionMap[CommonRESTFieldUtils::APPLICATION_ID_ID]; //var_dump($accessKey);
	$parameter->setApplicationId($applicationId);

	$affiliateId = $optionMap[CommonRESTFieldUtils::AFFILIATE_ID_ID]; //var_dump($associateTag);
	$parameter->setAffiliateId($affiliateId);

	return $parameter;
    }

    public static function makeRESTParameter($optionMap, $keyword): RESTParameter {

	$restParameter = new RESTParameter();

	//TODO get_option()から、値を取得する。

	$restParameter->setImageFlag(RESTFieldUtils::EXISTENCE_IMAGE_FLAG);
	$restParameter->setKeyword($keyword);

	return $restParameter;
    }

    public static function makeSearchOption($optionMap, $operationOfShortcode): SearchOption {

	$searchOption = new SearchOption();

	//TODO get_option()から、値を取得する。

	if ($operationOfShortcode) {
	    $searchOption->setOperation($operationOfShortcode);
	} else {
	    $searchOption->setOperation(SearchFieldUtils::ICHIBA_ITEM_SEARCH_OPERATION);
	}

	return $searchOption;
    }

}

RakutenRequest.php

namespace goodsmemo\rakuten;

use goodsmemo\rakuten\CommonRESTParameter;
use goodsmemo\rakuten\RESTParameter;
use goodsmemo\network\HTTPRequest;
use goodsmemo\network\URLInfo;

require_once GOODS_MEMO_DIR . "rakuten/CommonRESTParameter.php";
require_once GOODS_MEMO_DIR . "rakuten/RESTParameter.php";
require_once GOODS_MEMO_DIR . "network/HTTPRequest.php";
require_once GOODS_MEMO_DIR . "network/URLInfo.php";

/**
 * Description of RakutenRequest
 *
 * @author Goods Memo
 */
class RakutenRequest {

    public static function requestIchibaItemSearch(URLInfo $urlInfo, CommonRESTParameter $commonParameter, RESTParameter $restParameter) {

	$parameterMap = array();

	$parameterMap["applicationId"] = $commonParameter->getApplicationId();
	$parameterMap["affiliateId"] = $commonParameter->getAffiliateId();

	$parameterMap["imageFlag"] = $restParameter->getImageFlag();
	$parameterMap["keyword"] = $restParameter->getKeyword();

	$queryString = HTTPRequest::makeQueryString($parameterMap);

	$hostname = $urlInfo->getHostname();
	$path = $urlInfo->getPath();

	$requestURL = 'https://' . $hostname . '/' . $path . '?' . $queryString; //var_dump($requestURL);
	$response = HTTPRequest::getContents($requestURL);

	return $response;
    }

}

RakutenResponse.php

namespace goodsmemo\rakuten;

use goodsmemo\rakuten\ReviewResponse;
use goodsmemo\item\Item;
use goodsmemo\item\ImageItem;
use goodsmemo\item\PriceItem;
use goodsmemo\item\PointItem;
use goodsmemo\item\html\ItemHTMLOption;
use goodsmemo\item\html\ImageItemHTMLOption;
use goodsmemo\item\html\ItemHTMLUtils;
use goodsmemo\item\html\PriceItemHTMLUtils;
use goodsmemo\date\DateTextMaking;
use goodsmemo\arrayutils\ArrayUtils;

require_once GOODS_MEMO_DIR . "rakuten/ReviewResponse.php";
require_once GOODS_MEMO_DIR . "item/Item.php";
require_once GOODS_MEMO_DIR . "item/ImageItem.php";
require_once GOODS_MEMO_DIR . "item/PriceItem.php";
require_once GOODS_MEMO_DIR . "item/PointItem.php";
require_once GOODS_MEMO_DIR . "item/html/ItemHTMLOption.php";
require_once GOODS_MEMO_DIR . "item/html/ImageItemHTMLOption.php";
require_once GOODS_MEMO_DIR . "item/html/ItemHTMLUtils.php";
require_once GOODS_MEMO_DIR . "item/html/PriceItemHTMLUtils.php";
require_once GOODS_MEMO_DIR . "date/DateTextMaking.php";
require_once GOODS_MEMO_DIR . "arrayutils/ArrayUtils.php";

/**
 * Description of RakutenResponse
 *
 * @author Goods Memo
 */
class RakutenResponse {

    public static function makeItemArray($response, ItemHTMLOption $itemHTMLOption) {

	$itemArray = array();

	$json = json_decode($response);
	$itemCount = intval($json->{'count'});
	if ($itemCount <= 0) {
	    return $itemArray; //商品情報なし
	}
	//var_dump($xml->Items->Item[0]);//print_r($xml->Items->Item[0]);

	$imageItemHTMLOption = $itemHTMLOption->getImageItemHTMLOption();
	$priceTime = DateTextMaking::getUnixTimeMillSecond();

	$numberToDisplay = $itemHTMLOption->getNumberToDisplay();
	$count = min($itemCount, $numberToDisplay);
	for ($i = 0; $i < $count; $i++) {

	    $node = $json->{'Items'}[$i]->{'Item'};

	    $item = RakutenResponse::makeItem($node, $imageItemHTMLOption, $priceTime);
	    array_push($itemArray, $item);
	}

	return $itemArray;
    }

    private static function makeItem($node, ImageItemHTMLOption $imageItemHTMLOption, float $priceTime) {

	$item = new Item();

	$item->setPageURL(esc_url($node->{'affiliateUrl'}));

	$imageItem = RakutenResponse::makeImageItem($node, $imageItemHTMLOption);
	$item->setImageItem($imageItem);

	$item->setTitle(ItemHTMLUtils::makePlainText($node->{'itemName'}));

	$priceItem = RakutenResponse::makePriceItem($node, $priceTime);
	$item->setPriceItem($priceItem);

	$pointItem = RakutenResponse::makePointItem($node);
	$item->setPointItem($pointItem);

	$item->setShop(ItemHTMLUtils::makePlainText($node->{'shopName'}));

	$reviewItem = ReviewResponse::makeReviewItem($node);
	$item->setReviewItem($reviewItem);

	return $item;
    }

    private static function makeImageItem($node, ImageItemHTMLOption $imageItemHTMLOption): ImageItem {

	$imageItem = new ImageItem();

	//最大3枚の画像(画像サイズ128px*128px)を imageUrl の配列で返却
	//httpsではじまる商品画像128x128のURL
	$mediumImageUrls = $node->{'mediumImageUrls'};

	$imageUrl = $mediumImageUrls['0']->{'imageUrl'};
	$imageItem->setImageURL(esc_url($imageUrl));

	$imageWidth = $imageItemHTMLOption->getImageWidth(); //楽天APIから値を取得できないため、データベースの値を使う。
	$imageItem->setImageWidth($imageWidth);

	$imageHeight = $imageItemHTMLOption->getImageHeight(); //楽天APIから値を取得できないため、データベースの値を使う。
	$imageItem->setImageHeight($imageHeight);

	return $imageItem;
    }

    private static function makePriceItem($node, float $priceTime): PriceItem {

	$priceItem = new PriceItem();

	$priceItem->setLabel("価格");

	$itemPrice = ItemHTMLUtils::makePlainText($node->{'itemPrice'});
	$priceText = PriceItemHTMLUtils::makeFormattedPrice($itemPrice);
	$priceItem->setPrice($priceText);

	$TAG_FLAG_MAP = array('0' => '[税込]', '1' => '[税別]');
	$taxFlag = ItemHTMLUtils::makePlainText($node->{'taxFlag'});
	$taxText = ArrayUtils::getValueIfKeyExists($taxFlag, $TAG_FLAG_MAP);
	$priceItem->setPriceAddition($taxText);

	$priceItem->setPriceTime($priceTime);

	$POSTAGE_FLAG_MAP = array('0' => '送料込', '1' => '送料別');
	$postageFlag = ItemHTMLUtils::makePlainText($node->{'postageFlag'});
	$postageText = ArrayUtils::getValueIfKeyExists($postageFlag, $POSTAGE_FLAG_MAP);
	$priceItem->setPostageText($postageText);

	return $priceItem;
    }

    private static function makePointItem($node): PointItem {

	$pointItem = new PointItem();

	$pointRate = ItemHTMLUtils::makePlainText($node->{'pointRate'});
	if (is_numeric($pointRate)) {

	    $pointRateValue = intval($pointRate);
	    if ($pointRateValue >= 2) {

		$pointItem->setPointRate($pointRate);

		$pointRateStartTime = ItemHTMLUtils::makePlainText($node->{'pointRateStartTime'});
		$pointItem->setPointRateStartTime($pointRateStartTime);

		$pointRateEndTime = ItemHTMLUtils::makePlainText($node->{'pointRateEndTime'});
		$pointItem->setPointRateEndTime($pointRateEndTime);
	    }
	}

	return $pointItem;
    }

}

RESTParameter.php

namespace goodsmemo\rakuten;

use goodsmemo\network\DefaultRESTParameter;

require_once GOODS_MEMO_DIR . "network/DefaultRESTParameter.php";

/**
 * Description of RESTParameter
 *
 * @author Goods Memo
 */
class RESTParameter extends DefaultRESTParameter {

    private $imageFlag;

    public function getImageFlag() {
	return $this->imageFlag;
    }

    public function setImageFlag($imageFlag) {
	$this->imageFlag = $imageFlag;
    }

}

ReviewResponse.php

namespace goodsmemo\rakuten;

use goodsmemo\item\ReviewItem;
use goodsmemo\item\html\ItemHTMLUtils;

require_once GOODS_MEMO_DIR . "item/ReviewItem.php";
require_once GOODS_MEMO_DIR . "item/html/ItemHTMLUtils.php";

/**
 * Description of ReviewResponse
 *
 * @author Goods Memo
 */
class ReviewResponse {

    public static function makeReviewItem($node): ReviewItem {

	$reviewItem = new ReviewItem();

	$itemCaption = ItemHTMLUtils::makePlainText($node->{'itemCaption'});
	$reviewItem->setPlainTextReview($itemCaption);

	$reviewLineArray = ReviewResponse::makeReviewLineArray($node, $itemCaption);
	$reviewItem->setReviewLineArray($reviewLineArray);

	return $reviewItem;
    }

    private static function makeReviewLineArray($node, $itemCaption) {

	$reviewLineArray = array();

	if (empty($node->{'catchcopy'}) || empty($itemCaption)) {
	    return $reviewLineArray;
	}

	$catchcopy = ItemHTMLUtils::makePlainText($node->{'catchcopy'});
	array_push($reviewLineArray, $catchcopy);

	array_push($reviewLineArray, $itemCaption);

	return $reviewLineArray;
    }

}

SearchOption.php

namespace goodsmemo\rakuten;

/**
 * Description of SearchOperation
 *
 * @author Goods Memo
 */
class SearchOption {

    private $operation;

    public function getOperation() {
	return $this->operation;
    }

    public function setOperation($operation) {
	$this->operation = $operation;
    }

}