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

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

FooterHTMLInfo.php

namespace goodsmemo\item\html;

/**
 * Description of FooterHTML
 *
 * @author Goods Memo
 */
class FooterHTMLInfo {

    private $footerID;
    private $footerDiv;

    public function getFooterID() {
	return $this->footerID;
    }

    public function getFooterDiv() {
	return $this->footerDiv;
    }

    public function setFooterID($footerID) {
	$this->footerID = $footerID;
    }

    public function setFooterDiv($footerDiv) {
	$this->footerDiv = $footerDiv;
    }

}

FooterHTMLUtils.php

namespace goodsmemo\item\html;

use goodsmemo\date\DateTextMaking;
use goodsmemo\item\html\FooterHTMLInfo;
use goodsmemo\item\html\ItemHTMLOption;
use goodsmemo\item\html\PriceItemHTMLOption;

require_once GOODS_MEMO_DIR . "date/DateTextMaking.php";
require_once GOODS_MEMO_DIR . "item/html/FooterHTMLInfo.php";
require_once GOODS_MEMO_DIR . "item/html/ItemHTMLOption.php";
require_once GOODS_MEMO_DIR . "item/html/PriceItemHTMLOption.php";

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

    public static function makeFooterHTMLInfo(ItemHTMLOption $itemHTMLOption): FooterHTMLInfo {

	$footerID;
	$footerDiv; //今の所、価格詳細専用のフッターとなっている。

	$priceItemHTMLOption = $itemHTMLOption->getPriceItemHTMLOption();
	$priceFooterText = $priceItemHTMLOption->getPriceFooterText();

	if ($priceFooterText) {

	    $idPrefix = $itemHTMLOption->getIdPrefix();
	    $footerDisplayTime = DateTextMaking::getUnixTimeMillSecond(); //フッターが表示された時間
	    $footerTimeText = DateTextMaking::makeUnixTimeIDText(DateTextMaking::UNIX_TIME_ID_FORMAT,
			    $footerDisplayTime);

	    $footerID = GOODS_MEMO_PREFIX . "-" . $idPrefix . "-items-footer-" . $footerTimeText; //念のため、ページ内にフッターが2個以上ある場合に対応
	    $footerDiv = <<< EOD
<div id="{$footerID}" class="footer">{$priceFooterText}</div>
EOD;

	    $showState = $priceItemHTMLOption->getShowStateOfPriceTimeLink();
	    if ($showState === false) {
		$footerID = ""; //価格時刻の「詳細はこちら」リンクを非表示にする。
	    }
	} else {

	    $footerID = "";
	    $footerDiv = "";
	}

	$footerHTMLInfo = new FooterHTMLInfo();
	$footerHTMLInfo->setFooterID($footerID);
	$footerHTMLInfo->setFooterDiv($footerDiv);

	return $footerHTMLInfo;
    }

}

ImageItemHTMLOption.php

namespace goodsmemo\item\html;

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

    private $imageWidth = 0;
    private $imageHeight = 0;

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

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

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

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

}

ItemArrayHTMLMaking.php

商品を表示するHTMLを、作成します。

namespace goodsmemo\item\html;

use goodsmemo\item\Item;
use goodsmemo\item\ImageItem;
use goodsmemo\item\PriceItem;
use goodsmemo\item\PointItem;
use goodsmemo\item\ProductionItem;
use goodsmemo\item\ReviewItem;
use goodsmemo\item\html\ItemHTMLOption;
use goodsmemo\item\html\ReviewItemHTMLOption;
use goodsmemo\item\html\ReviewItemHTMLUtils;
use goodsmemo\item\html\FooterHTMLInfo;
use goodsmemo\item\html\FooterHTMLUtils;
use goodsmemo\date\DateTextMaking;

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/ProductionItem.php";
require_once GOODS_MEMO_DIR . "item/ReviewItem.php";
require_once GOODS_MEMO_DIR . "item/html/ItemHTMLOption.php";
require_once GOODS_MEMO_DIR . "item/html/ReviewItemHTMLOption.php";
require_once GOODS_MEMO_DIR . "item/html/ReviewItemHTMLUtils.php";
require_once GOODS_MEMO_DIR . "item/html/FooterHTMLInfo.php";
require_once GOODS_MEMO_DIR . "item/html/FooterHTMLUtils.php";
require_once GOODS_MEMO_DIR . "date/DateTextMaking.php";

/**
 * Description of HTMLMaking
 *
 * @author Goods Memo
 */
class ItemArrayHTMLMaking {

    public static function makeItemArrayHTML($itemArray, ItemHTMLOption $itemHTMLOption) {

	$_ = function($s) {
	    return $s;
	}; //展開用のラムダ関数。ヒアドキュメントで定数を展開できる。

	$idPrefix = $itemHTMLOption->getIdPrefix();
	$itemArrayHTML = <<< EOD
<div id="{$_(GOODS_MEMO_PREFIX)}-{$idPrefix}-items" class="{$_(GOODS_MEMO_PREFIX)}-items">
EOD;

	$itemCount = count($itemArray);
	if ($itemCount <= 0) {

	    $itemArrayHTML .= <<< EOD
<p>該当する商品は、ありませんでした。</p>
</div>
EOD;
	    return $itemArrayHTML;
	}

	$footerHTMLInfo = FooterHTMLUtils::makeFooterHTMLInfo($itemHTMLOption);
	$footerID = $footerHTMLInfo->getFooterID();

	for ($index = 0; $index < $itemCount; $index++) {

	    $item = $itemArray[$index];
	    $oneItemHTML = ItemArrayHTMLMaking::makeOneItemHTML($item, $index, $itemHTMLOption, $footerID);
	    $itemArrayHTML .= $oneItemHTML;
	}

	$footerDiv = $footerHTMLInfo->getFooterDiv();
	$itemArrayHTML .= $footerDiv;
	$itemArrayHTML .= "</div>";

	return $itemArrayHTML;
    }

    private static function makeOneItemHTML(
    Item $item, $index, ItemHTMLOption $itemHTMLOption, $footerID) {

	$idIndex = $index + 1; //1から始まる値にする。
	$idPrefix = $itemHTMLOption->getIdPrefix();

	$pageURL = $item->getPageURL();

	$imageItem = $item->getImageItem();
	$imageURL = $imageItem->getImageURL();
	$imageWidth = $imageItem->getImageWidth();
	$imageHeight = $imageItem->getImageHeight();

	$title = ItemArrayHTMLMaking::makeTitleOfOneItem($item, $itemHTMLOption);

	$priceItem = $item->getPriceItem();
	$priceLabel = $priceItem->getLabel();
	$price = $priceItem->getPrice();
	$priceAddition = $priceItem->getPriceAddition();
	$priceTimeText = ItemArrayHTMLMaking::makePriceTimeText($priceItem);
	$postageText = $priceItem->getPostageText();

	$pointItem = $item->getPointItem();
	$pointRate = $pointItem->getPointRate();
	$pointRateStartTime = $pointItem->getPointRateStartTime();
	$pointRateEndTime = $pointItem->getPointRateEndTime();

	$productionItem = $item->getProductionItem();
	$authorText = ItemArrayHTMLMaking::makeAuthorText($productionItem);
	$manufacturerLabel = $productionItem->getManufacturerLabel();
	$manufacturer = $productionItem->getManufacturer();

	$binding = $item->getBinding();
	$shop = $item->getShop();
	$reviewText = ItemArrayHTMLMaking::makeReviewText($item, $itemHTMLOption);

	$_ = function($s) {
	    return $s;
	}; //展開用のラムダ関数。ヒアドキュメントで定数を展開できる。
	//項目の有無によって、表示を制御する。
	$hideIfEmpty = function ($value) {
	    if (is_numeric($value)) {
		return "";
	    }
	    if (empty($value)) {
		return 'style="display: none;"';
	    } else {
		return "";
	    }
	};

	$oneItemHTML = <<< EOD
<div id="{$_(GOODS_MEMO_PREFIX)}-{$idPrefix}-item{$idIndex}" class="{$_(GOODS_MEMO_PREFIX)}-oneItem">
<p class="image" {$hideIfEmpty($imageURL)}>
    <a href="{$pageURL}">
    <img src="{$imageURL}" alt="{$title}" width="{$imageWidth}" height="{$imageHeight}" />
    </a>
</p>
<div class="areaNotToWrapAround">
<p class="title"  {$hideIfEmpty($title)}>
    <a href="{$pageURL}">{$title}</a>
</p>
<p class="price" {$hideIfEmpty($price)}>
    <span class="priceLabel">{$priceLabel}:</span><span class="priceValue">{$price}</span>{$priceAddition}<span class="priceTime">({$priceTimeText}時点<a href="#{$footerID}" class="priceTimeLink" {$hideIfEmpty($footerID)}>詳細はこちら</a>)</span>
    <span class="postage" {$hideIfEmpty($postageText)}><br /><span class="postageValue">{$postageText}</span></span>
</p>
<p class="point" {$hideIfEmpty($pointRate)}>
    <span class="pointRateLabel">ポイント:</span><span class="pointRateValue">{$pointRate} 倍</span><br />
    <span class="pointRateStartTimeLabel">&#10003;開始日時</span>{$pointRateStartTime}<br />
    <span class="pointRateEndTimeLabel">&#10003;終了日時</span>{$pointRateEndTime}
</p>
<p class="author" {$hideIfEmpty($authorText)}>
    <span class="authorLabel">著者:</span>{$authorText}
</p>
<p class="manufacturer" {$hideIfEmpty($manufacturer)}>
    <span class="manufacturerLabel">{$manufacturerLabel}:</span>{$manufacturer}
</p>
<p class="binding" {$hideIfEmpty($binding)}>
    <span class="bindingLabel">種別:</span>{$binding}
</p>
<p class="shop" {$hideIfEmpty($shop)}>
    <span class="shopLabel">販売店:</span>{$shop}
</p>
</div>
<p class="review" {$hideIfEmpty($reviewText)}>{$reviewText}</p>
</div>
EOD;

	return $oneItemHTML;
    }

    private static function makeTitleOfOneItem(Item $item, ItemHTMLOption $itemHTMLOption) {

	$title = $item->getTitle();
	$titleLength = $itemHTMLOption->getTitleLength();
	$trimmedTitle = mb_strimwidth($title, 0, $titleLength, "……", "UTF-8");
	return $trimmedTitle;
    }

    private static function makePriceTimeText(PriceItem $priceItem) {

	$priceTime = $priceItem->getPriceTime();
	$priceTimeText = DateTextMaking::makeTimeText(DateTextMaking::TIME_TEXT_FORMAT, $priceTime);
	return $priceTimeText;
    }

    private static function makeAuthorText(ProductionItem $productionItem) {

	$authorArray = $productionItem->getAuthorArray();
	$authorText = implode(", ", $authorArray);
	return $authorText;
    }

    private static function makeReviewText(Item $item, ItemHTMLOption $itemHTMLOption) {

	$reviewItem = $item->getReviewItem();
	$reviewItemHTMLOption = $itemHTMLOption->getReviewItemHTMLOption();
	$reviewText = ReviewItemHTMLUtils::makeFitReviewText($reviewItem, $reviewItemHTMLOption);
	return $reviewText;
    }

}

ItemHTMLOption.php

namespace goodsmemo\item\html;

use goodsmemo\item\html\ImageItemHTMLOption;
use goodsmemo\item\html\PriceItemHTMLOption;
use goodsmemo\item\html\ReviewItemHTMLOption;

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";

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

    private $idPrefix;
    private $numberToDisplay;
    private $titleLength;
    private $imageItemHTMLOption;
    private $priceItemHTMLOption;
    private $reviewItemHTMLOption;
    private $cacheExpirationInSeconds;

    public function getIdPrefix() {
	return $this->idPrefix;
    }

    public function setIdPrefix($idPrefix) {
	$this->idPrefix = $idPrefix;
    }

    public function getNumberToDisplay() {
	return $this->numberToDisplay;
    }

    public function setNumberToDisplay($numberToDisplay) {
	$this->numberToDisplay = $numberToDisplay;
    }

    public function getTitleLength() {
	return $this->titleLength;
    }

    public function setTitleLength($titleLength) {
	$this->titleLength = $titleLength;
    }

    public function getImageItemHTMLOption(): ImageItemHTMLOption {
	return $this->imageItemHTMLOption;
    }

    public function setImageItemHTMLOption(ImageItemHTMLOption $imageItemHTMLOption) {
	$this->imageItemHTMLOption = $imageItemHTMLOption;
    }

    public function getPriceItemHTMLOption(): PriceItemHTMLOption {
	return $this->priceItemHTMLOption;
    }

    public function setPriceItemHTMLOption(PriceItemHTMLOption $priceItemHTMLOption) {
	$this->priceItemHTMLOption = $priceItemHTMLOption;
    }

    public function getReviewItemHTMLOption(): ReviewItemHTMLOption {
	return $this->reviewItemHTMLOption;
    }

    public function setReviewItemHTMLOption(ReviewItemHTMLOption $reviewItemHTMLOption) {
	$this->reviewItemHTMLOption = $reviewItemHTMLOption;
    }

    public function getCacheExpirationInSeconds() {
	return $this->cacheExpirationInSeconds;
    }

    public function setCacheExpirationInSeconds($cacheExpirationInSeconds) {
	$this->cacheExpirationInSeconds = $cacheExpirationInSeconds;
    }

}

ItemHTMLUtils.php

namespace goodsmemo\item\html;

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

    public static function makePlainText($text) {

	$output = trim($text);
	$output = strip_tags($output); //HTML および PHP タグを取り除きます。
	$plainText = esc_html($output); //HTMLエスケープ。例:「<」を「<」、「&」を「&」に書き換える。

	return (string) $plainText;
    }

}

PriceItemHTMLOption.php

namespace goodsmemo\item\html;

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

    private $priceFooterText;
    private $showStateOfPriceTimeLink;

    public function getPriceFooterText() {
	return $this->priceFooterText;
    }

    public function getShowStateOfPriceTimeLink(): bool {
	return $this->showStateOfPriceTimeLink;
    }

    public function setPriceFooterText($priceFooterText) {
	$this->priceFooterText = $priceFooterText;
    }

    public function setShowStateOfPriceTimeLink(bool $showState) {
	$this->showStateOfPriceTimeLink = $showState;
    }

}

PriceItemHTMLUtils.php

namespace goodsmemo\item\html;

/**
 * Description of PriceHTMLUtils
 *
 * @author Goods Memo
 */
class PriceItemHTMLUtils {

    public static function makeFormattedPrice(float $price) {

	//千位毎にカンマ (",") を追加する。
	$priceText = number_format($price) . " 円"; //string number_format ( float $number , int $decimals = 0 , string $dec_point = "." , string $thousands_sep = "," )
	return $priceText;
    }

}

ReviewItemHTMLOption.php

namespace goodsmemo\item\html;

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

    private $reviewLength;
    private $arrayOfStringToDelete;
    private $arrayOfStringToBreak;

    public function getReviewLength() {
	return $this->reviewLength;
    }

    public function setReviewLength($reviewLength) {
	$this->reviewLength = $reviewLength;
    }

    public function getArrayOfStringToDelete() {
	return $this->arrayOfStringToDelete;
    }

    public function setArrayOfStringToDelete($arrayOfStringToDelete) {
	$this->arrayOfStringToDelete = $arrayOfStringToDelete;
    }

    public function getArrayOfStringToBreak() {
	return $this->arrayOfStringToBreak;
    }

    public function setArrayOfStringToBreak($arrayOfStringToBreak) {
	$this->arrayOfStringToBreak = $arrayOfStringToBreak;
    }

}

ReviewItemHTMLUtils.php

namespace goodsmemo\item\html;

use goodsmemo\item\ReviewItem;
use goodsmemo\item\html\ReviewItemHTMLOption;
use goodsmemo\text\TextUtils;

require_once GOODS_MEMO_DIR . "item/ReviewItem.php";
require_once GOODS_MEMO_DIR . "item/html/ReviewItemHTMLOption.php";
require_once GOODS_MEMO_DIR . "text/TextUtils.php";

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

    public static function makeReviewItemHTMLOption(
    $optionMap, $editorialReviewLengthID, $arrayOfStringToDeleteID, $arrayOfStringToBreakID): ReviewItemHTMLOption {

	$reviewItemHTMLOption = new ReviewItemHTMLOption();

	$reviewLength = $optionMap[$editorialReviewLengthID]; //var_dump($reviewLength);
	$reviewItemHTMLOption->setReviewLength($reviewLength);

	$arrayOfStringToDelete = json_decode(
		$optionMap[$arrayOfStringToDeleteID], true); //true:連想配列に変換する
	$reviewItemHTMLOption->setArrayOfStringToDelete($arrayOfStringToDelete); //var_dump($arrayOfStringToDelete);

	$arrayOfStringToBreak = json_decode(
		$optionMap[$arrayOfStringToBreakID], true); //true:連想配列に変換する
	$reviewItemHTMLOption->setArrayOfStringToBreak($arrayOfStringToBreak);

	return $reviewItemHTMLOption;
    }

    public static function makeFitReviewText(ReviewItem $reviewItem, ReviewItemHTMLOption $reviewItemHTMLOption) {
	//「ちょうどいい」の単語で「fit」を選んだ。

	$reviewLength = $reviewItemHTMLOption->getReviewLength();

	$reviewText;

	$reviewLineArray = $reviewItem->getReviewLineArray();
	if (count($reviewLineArray) > 0) {

	    $reviewText = ReviewItemHTMLUtils::makeFitReviewLinesText($reviewLineArray, $reviewLength);
	} else {

	    $plainTextReview = $reviewItem->getPlainTextReview();
	    //HTML終了タグがないので、文字列を途中で切断できる。平文なので、HTML文法エラーとならない。
	    $reviewText = mb_strimwidth($plainTextReview, 0, $reviewLength, "……", "UTF-8"); //$reviewLengthは、文字幅(見た目の長さ)を示す。
	}

	$arrayOfStringToDelete = $reviewItemHTMLOption->getArrayOfStringToDelete();
	$reviewText = ReviewItemHTMLUtils::deleteStringFrom($reviewText, $arrayOfStringToDelete); //表示したくない文字列を削除する。

	$arrayOfStringToBreak = $reviewItemHTMLOption->getArrayOfStringToBreak();
	$reviewText = ReviewItemHTMLUtils::addLineBreakTo($reviewText, $arrayOfStringToBreak); //文字列の手前に、改行タグを追加する。

	return $reviewText;
    }

    private static function makeFitReviewLinesText($reviewLineArray, $reviewLength) {

	$fitReviewLinesText = "";

	$lineCount = count($reviewLineArray);
	$lastIndex = $lineCount - 1;

	for ($i = 0; $i < $lineCount; $i++) {

	    $fitReviewLinesText .= $reviewLineArray[$i];

	    $stringWidth = mb_strwidth($fitReviewLinesText, "UTF-8"); //文字幅(見た目の長さ) //var_dump($stringWidth);
	    if ($stringWidth >= $reviewLength) {

		$fitReviewLinesText = mb_strimwidth($fitReviewLinesText, 0, $reviewLength, "……", "UTF-8");
		break;
	    }

	    if ($i < $lastIndex) {
		$fitReviewLinesText .= "<br />";
	    }
	}

	return $fitReviewLinesText;
    }

    private static function deleteStringFrom($reviewText, $arrayOfStringToDelete) {

	//一行で処理できた。メソッドにする必要はなかった。
	$newReviewText = str_replace($arrayOfStringToDelete, "", $reviewText);
	return $newReviewText;
    }

    private static function addLineBreakTo($reviewText, $arrayOfStringToBreak) {

	$LINE_BREAK_TAG = "<br />";
	$NON_SENTENCE_CHARACTERS = '  ' . implode($arrayOfStringToBreak); //例:"「半角空白」「全角空白」●◆"    //「●箇条書き」の本文でない文字たち

	$newReviewText = $reviewText;

	$count = count($arrayOfStringToBreak);
	for ($i = 0; $i < $count; $i++) {

	    $stringToBreak = $arrayOfStringToBreak[$i]; //例:●
	    //例:'/●([^  ●◆]+?)/u'
	    //●の後に、「半角空白」または「全角空白」または「●」または「◆」でない文字列。この文字列は、「●箇条書き」の本文のこと。
	    //+? 最短一致 //UTF-8でpreg系を使う場合は、パターン修飾子として"u"を指定する。
	    $pattern = '/' . $stringToBreak . '([^' . $NON_SENTENCE_CHARACTERS . ']+?)/u';  //var_dump($pattern);

	    $replace = $LINE_BREAK_TAG . $stringToBreak . '\1'; //例:<br />●\1  \1は本文。// var_dump($replace);
	    $newReviewText = preg_replace($pattern, $replace, $newReviewText);
	}

	if (TextUtils::startsWith($newReviewText, $LINE_BREAK_TAG)) {//先頭の<br />を取り除く。
	    $startIndex = mb_strlen($LINE_BREAK_TAG, "UTF-8");
	    $newReviewText = mb_substr($newReviewText, $startIndex, NULL, "UTF-8"); //var_dump($newReviewText);
	}

	//例:'#<br />[\s ]*?<br />#u' // #区切り。<br />にスラッシュ文字があるため。
	$pattern = '#' . $LINE_BREAK_TAG . '[\s ]*?' . $LINE_BREAK_TAG . '#u'; //*? 最短一致  //var_dump($pattern);
	$newReviewText = preg_replace($pattern, $LINE_BREAK_TAG, $newReviewText); //例:<br /><br />を<br />に置換する。

	return $newReviewText;
    }

}