商品情報・グッズメモ アフィリエイト

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

ImageItem.php


namespace goodsmemo\item;

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

    private $imageURL = "";
    private $imageWidth = 0;
    private $imageHeight = 0;

    public function getImageURL() {
	return $this->imageURL;
    }

    public function getImageWidth() {
	return $this->imageWidth;
    }

    public function getImageHeight() {
	return $this->imageHeight;
    }

    public function setImageURL($imageURL) {
	$this->imageURL = $imageURL;
    }

    public function setImageWidth($imageWidth) {
	$this->imageWidth = $imageWidth;
    }

    public function setImageHeight($imageHeight) {
	$this->imageHeight = $imageHeight;
    }

}

Item.php

商品情報を保管します。


namespace goodsmemo\item;

use goodsmemo\item\ImageItem;
use goodsmemo\item\PriceItem;
use goodsmemo\item\PointItem;
use goodsmemo\item\ProductionItem;
use goodsmemo\item\ReviewItem;

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/ProductionItem.php";
require_once GOODS_MEMO_DIR . "item/ReviewItem.php";

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

	private $pageURL = "";
	private $imageItem;
	private $title = "";
	private $priceItem;
	private $pointItem;
	private $productionItem;
	private $preferentialMember = ""; //優待会員
	private $shop = "";
	private $reviewItem;

	function __construct() {
		$this->imageItem = new ImageItem();
		$this->priceItem = new PriceItem();
		$this->pointItem = new PointItem();
		$this->productionItem = new ProductionItem(); //例:楽天で設定しない場合、空のProductionItemオブジェクトを使う。nullエラーを防げる。
		$this->reviewItem = new ReviewItem();
	}

	public function getPageURL() {
		return $this->pageURL;
	}

	public function setPageURL($pageURL) {
		$this->pageURL = $pageURL;
	}

	public function getImageItem(): ImageItem {
		return $this->imageItem;
	}

	public function setImageItem(ImageItem $imageItem) {
		$this->imageItem = $imageItem;
	}

	public function getTitle() {
		return $this->title;
	}

	public function setTitle($title) {
		$this->title = $title;
	}

	public function getPriceItem(): PriceItem {
		return $this->priceItem;
	}

	public function setPriceItem(PriceItem $priceItem) {
		$this->priceItem = $priceItem;
	}

	public function getPointItem(): PointItem {
		return $this->pointItem;
	}

	public function setPointItem(PointItem $pointItem) {
		$this->pointItem = $pointItem;
	}

	public function getProductionItem(): ProductionItem {
		return $this->productionItem;
	}

	public function setProductionItem(ProductionItem $productionItem) {
		$this->productionItem = $productionItem;
	}

	public function getPreferentialMember() {
		return $this->preferentialMember;
	}

	public function setPreferentialMember($preferentialMember) {
		$this->preferentialMember = $preferentialMember;
	}

	public function getShop() {
		return $this->shop;
	}

	public function setShop($shop) {
		$this->shop = $shop;
	}

	public function getReviewItem(): ReviewItem {
		return $this->reviewItem;
	}

	public function setReviewItem(ReviewItem $reviewItem) {
		$this->reviewItem = $reviewItem;
	}

}

ItemSearchOperation.php


namespace goodsmemo\item;

use goodsmemo\network\URLInfo;
use goodsmemo\item\html\ItemHTMLOption;
use goodsmemo\item\ItemsHTMLInfoMaker;
use goodsmemo\item\html\ItemArrayHTMLMaking;

require_once GOODS_MEMO_DIR . "network/URLInfo.php";
require_once GOODS_MEMO_DIR . "item/html/ItemHTMLOption.php";
require_once GOODS_MEMO_DIR . "item/ItemsHTMLInfoMaker.php";
require_once GOODS_MEMO_DIR . "item/html/ItemArrayHTMLMaking.php";

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

	public static function makeItemsHTML(
	URLInfo $urlInfo, ItemHTMLOption $itemHTMLOption, ItemsHTMLInfoMaker $itemsHTMLInfoMaker) {

		$cacheExpirationInSeconds = $itemHTMLOption->getCacheExpirationInSeconds();

		if ($cacheExpirationInSeconds <= 0) {

			$response = $itemsHTMLInfoMaker->requestItemSearch($urlInfo, $itemHTMLOption->getNumberToDisplay());
			$itemArray = $itemsHTMLInfoMaker->makeItemArray($response, $itemHTMLOption); // var_dump($itemArray);
			$itemsHtml = ItemArrayHTMLMaking::makeItemArrayHTML($itemArray, $itemHTMLOption);
			return $itemsHtml;
		}

		$itemArray = NULL;

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

		$transientItemArrayCache = get_transient($transientID);
		if ($transientItemArrayCache !== FALSE) {

			$binaryItemArray = base64_decode($transientItemArrayCache); //var_dump($binaryItemArray);
			if ($binaryItemArray !== FALSE) {

				$itemArray = @unserialize($binaryItemArray); //エラー抑制演算子:E_WARNINGが画面に表示されるのを防ぐ。//var_dump($itemArray);
				if ($itemArray === FALSE) {
					$itemArray = NULL;
				}
			}
		}

		if (is_null($itemArray)) {

			$response = $itemsHTMLInfoMaker->requestItemSearch($urlInfo, $itemHTMLOption->getNumberToDisplay());
			$itemArray = $itemsHTMLInfoMaker->makeItemArray($response, $itemHTMLOption); // var_dump($itemArray);

			/**
			 * レンタルサーバーのMySQLにおいて
			 * set_transient()に配列変数$itemArrayを設定する場合、失敗している場合があった。set_transient returns false.
			 * よって _transient_goodsmemoXXXX が作成されないため、429:Too Many Requestsエラーが起きていた。
			 * 設定する値を、64種類の英数字の文字列にした。
			 * テーブル: wp_options:option_value列 longtext型
			 */
			$binaryItemArray = serialize($itemArray); //バイナリ文字列。データベースのBLOB型に保存できる。longtext型に保存できない場合があった。//var_dump($binaryItemArray);
			$transientItemArray = base64_encode($binaryItemArray); //64種類の印字可能な英数字の文字列。データベースのlongtext型に保存できる。
			//一時的にデータベースに商品情報を保存する。有効期限後、削除される。
			set_transient($transientID, $transientItemArray, $cacheExpirationInSeconds); //失敗したら、falseを返した。
		}

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

}

ItemsHTMLInfoMaker.php


namespace goodsmemo\item;

use goodsmemo\network\URLInfo;
use goodsmemo\item\html\ItemHTMLOption;

require_once GOODS_MEMO_DIR . "network/URLInfo.php";
require_once GOODS_MEMO_DIR . "item/html/ItemHTMLOption.php";

/**
 *
 * @author Goods Memo
 */
interface ItemsHTMLInfoMaker {

	public function requestItemSearch(URLInfo $urlInfo, int $itemCount);

	public function makeItemArray($response, ItemHTMLOption $itemHTMLOption);

	public function makeUniqueText(ItemHTMLOption $itemHTMLOption);
}

PointItem.php


namespace goodsmemo\item;

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

    //今の所、楽天だけで使うアイテムクラス。
    private $pointRate = "";
    private $pointRateStartTime = "";
    private $pointRateEndTime = "";

    public function getPointRate() {
	return $this->pointRate;
    }

    public function getPointRateStartTime() {
	return $this->pointRateStartTime;
    }

    public function getPointRateEndTime() {
	return $this->pointRateEndTime;
    }

    public function setPointRate($pointRate) {
	$this->pointRate = $pointRate;
    }

    public function setPointRateStartTime($pointRateStartTime) {
	$this->pointRateStartTime = $pointRateStartTime;
    }

    public function setPointRateEndTime($pointRateEndTime) {
	$this->pointRateEndTime = $pointRateEndTime;
    }

}

PriceItem.php


namespace goodsmemo\item;

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

    private $label = "";
    private $price = "";
    private $priceAddition = "";
    private $priceTime = 0;
    private $postageText = "";

    public function getLabel() {
	return $this->label;
    }

    public function setLabel($label) {
	$this->label = $label;
    }

    public function getPrice() {
	return $this->price;
    }

    public function setPrice($price) {
	$this->price = $price;
    }

    public function getPriceAddition() {
	return $this->priceAddition;
    }

    public function setPriceAddition($priceAddition) {
	$this->priceAddition = $priceAddition;
    }

    public function getPriceTime(): float {
	return $this->priceTime;
    }

    public function setPriceTime(float $priceTime) {
	$this->priceTime = $priceTime;
    }

    public function getPostageText() {
	return $this->postageText;
    }

    public function setPostageText($postageText) {
	$this->postageText = $postageText;
    }

}

ProductionItem.php


namespace goodsmemo\item;

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

	private $contributorArray = array(); //空のオブジェクト作成に対応するため、空の配列を設定する。
	//
	private $manufacturerLabel = "";
	private $manufacturer = ""; //製造元、メーカー
	//
	private $binding = ""; //装丁、形式、種別

	public function getContributorArray() {
		return $this->contributorArray;
	}

	public function getManufacturerLabel() {
		return $this->manufacturerLabel;
	}

	public function getManufacturer() {
		return $this->manufacturer;
	}

	public function getBinding() {
		return $this->binding;
	}

	public function setContributorArray($contributorArray) {
		$this->contributorArray = $contributorArray;
	}

	public function setManufacturerLabel($manufacturerLabel) {
		$this->manufacturerLabel = $manufacturerLabel;
	}

	public function setManufacturer($manufacturer) {
		$this->manufacturer = $manufacturer;
	}

	public function setBinding($binding) {
		$this->binding = $binding;
	}

}

ReviewItem.php


namespace goodsmemo\item;

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

    private $reviewLineArray = array();
    private $plainTextReview = "";

    public function getReviewLineArray() {
	return $this->reviewLineArray;
    }

    public function getPlainTextReview() {
	return $this->plainTextReview;
    }

    public function setReviewLineArray($reviewLineArray) {
	$this->reviewLineArray = $reviewLineArray;
    }

    public function setPlainTextReview($plainTextReview) {
	$this->plainTextReview = $plainTextReview;
    }

}