商品情報の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->getPriceTimeLinkVisible();
	    if ($showState === false) {
		$footerID = ""; //価格時刻の「詳細はこちら」リンクを非表示にする。
	    }
	} else {

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

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

	return $footerHTMLInfo;
    }

}

HTMLUtils.php


namespace goodsmemo\item\html;

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

    public static function makePlainText($text) {

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

	return (string) $plainText;
    }

}

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\ProductionItem;
use goodsmemo\item\html\ItemHTMLOption;
use goodsmemo\item\html\ReviewItemHTMLUtils;
use goodsmemo\item\html\FooterHTMLUtils;
use goodsmemo\date\DateTextMaking;
use goodsmemo\text\TextUtils;
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/ProductionItem.php";
require_once GOODS_MEMO_DIR . "item/html/ItemHTMLOption.php";
require_once GOODS_MEMO_DIR . "item/html/ReviewItemHTMLUtils.php";
require_once GOODS_MEMO_DIR . "item/html/FooterHTMLUtils.php";
require_once GOODS_MEMO_DIR . "date/DateTextMaking.php";
require_once GOODS_MEMO_DIR . "text/TextUtils.php";
/**
* Description of HTMLMaking
*
* @author Goods Memo
*/
class ItemArrayHTMLMaking {
const NO_APPLICABLE_PRODUCT = "該当する商品は、ありませんでした。";
public static function makeItemArrayHTML($itemArray, ItemHTMLOption $itemHTMLOption) {
$_ = function($s) {
return $s;
}; //展開用のラムダ関数。ヒアドキュメントで定数を展開できる。
$idPrefix = $itemHTMLOption->getIdPrefix();
$itemArrayHTML = <<< EOD
<div class="{$_(GOODS_MEMO_PREFIX)}-{$idPrefix}-items {$_(GOODS_MEMO_PREFIX)}-items">
EOD;
$itemCount = count($itemArray);
if ($itemCount <= 0) {
$itemArrayHTML .= <<< EOD
<p>{$_(ItemArrayHTMLMaking::NO_APPLICABLE_PRODUCT)}</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();
ItemArrayHTMLMaking::setImageInfoIfURLIsEmpty($imageItem);
$imageURL = $imageItem->getImageURL();
$imageWidth = $imageItem->getImageWidth();
$imageHeight = $imageItem->getImageHeight();
$title = ItemArrayHTMLMaking::makeTitle($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();
$contributorText = ItemArrayHTMLMaking::makeContributorText($productionItem);
$manufacturerLabel = $productionItem->getManufacturerLabel();
$manufacturer = $productionItem->getManufacturer();
$binding = $productionItem->getBinding();
$preferentialMember = $item->getPreferentialMember();
$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 class="{$_(GOODS_MEMO_PREFIX)}-{$idPrefix}-item{$idIndex} {$_(GOODS_MEMO_PREFIX)}-oneItem">
<div class="imageArea" {$hideIfEmpty($imageURL)}>
<a href="{$pageURL}">
<img src="{$imageURL}" alt="{$title}" width="{$imageWidth}" height="{$imageHeight}" />
</a>
</div>
<p class="title"  {$hideIfEmpty($title)}>
<a href="{$pageURL}">{$title}</a>
</p>
<div class="itemDetailsTable">
<div class="tableRow" {$hideIfEmpty($price)}>
<p class="rowLabel">{$priceLabel}:</p>
<p class="price">
<span class="priceValue">{$price}</span>{$priceAddition}<span class="priceTime">({$priceTimeText}時点<a href="#{$footerID}" class="priceTimeLink" {$hideIfEmpty($footerID)}>詳細はこちら</a>)</span>
</p>
</div>
<div class="tableRow" {$hideIfEmpty($preferentialMember)}>
<p class="rowLabel"></p>
<p class="preferentialMember">{$preferentialMember}</p>
</div>
<div class="tableRow" {$hideIfEmpty($postageText)}>
<p class="rowLabel"></p>
<p class="postage">{$postageText}</p>
</div>
</div>
<div class="itemDetailsTable">
<div class="tableRow" {$hideIfEmpty($pointRate)}>
<p class="rowLabel">ポイント:</p>
<p class="pointRate">{$pointRate} 倍</p>
</div>
<div class="tableRow" {$hideIfEmpty($pointRateStartTime)}>
<p class="rowLabel"></p>
<p class="pointRateStartTime">&#10003;開始日時 {$pointRateStartTime}</p>
</div>
<div class="tableRow" {$hideIfEmpty($pointRateEndTime)}>
<p class="rowLabel"></p>
<p class="pointRateEndTime">&#10003;終了日時 {$pointRateEndTime}</p>
</div>
</div>
<div class="itemDetailsTable">
<div class="tableRow" {$hideIfEmpty($contributorText)}>
<p class="rowLabel"></p>
<p class="contributor">{$contributorText}</p>
</div>
<div class="tableRow" {$hideIfEmpty($manufacturer)}>
<p class="rowLabel">{$manufacturerLabel}:</p>
<p class="manufacturer">{$manufacturer}</p>
</div>
<div class="tableRow" {$hideIfEmpty($binding)}>
<p class="rowLabel">種別:</p>
<p class="binding">{$binding}</p>
</div>
<div class="tableRow" {$hideIfEmpty($shop)}>
<p class="rowLabel">販売店:</p>
<p class="shop">{$shop}</p>
</div>
</div>
<p class="review" {$hideIfEmpty($reviewText)}>{$reviewText}</p>
</div>
EOD;
return $oneItemHTML;
}
private static function makeTitle(Item $item, ItemHTMLOption $itemHTMLOption) {
$title = $item->getTitle();
$titleLength = $itemHTMLOption->getTitleLength();
$trimmedTitle = TextUtils::mb_strimwidth($title, 0, $titleLength, "……");
return $trimmedTitle;
}
private static function setImageInfoIfURLIsEmpty(ImageItem &$imageItem) {
$imageURL = $imageItem->getImageURL();
if (empty($imageURL)) {
$imageURL = plugins_url('images/no-image.png', __FILE__);
$imageItem->setImageURL($imageURL);
$imageItem->setImageWidth(300); //no-image.png の幅
$imageItem->setImageHeight(300); //no-image.png の高さ
}
}
private static function makePriceTimeText(PriceItem $priceItem) {
$priceTime = $priceItem->getPriceTime();
$priceTimeText = DateTextMaking::makeTimeText(DateTextMaking::TIME_TEXT_FORMAT, $priceTime);
return $priceTimeText;
}
private static function makeContributorText(ProductionItem $productionItem) {
$contributorArray = $productionItem->getContributorArray();
$contributorText = implode(", ", $contributorArray);
return $contributorText;
}
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;
}
}

PriceItemHTMLOption.php


namespace goodsmemo\item\html;
/**
* Description of PriceItemHTMLOption
*
* @author Goods Memo
*/
class PriceItemHTMLOption {
private $priceFooterText;
private $priceTimeLinkVisible;
public function getPriceFooterText() {
return $this->priceFooterText;
}
public function setPriceFooterText($priceFooterText) {
$this->priceFooterText = $priceFooterText;
}
public function getPriceTimeLinkVisible(): bool {
return $this->priceTimeLinkVisible;
}
public function setPriceTimeLinkVisible(bool $priceTimeLinkVisible) {
$this->priceTimeLinkVisible = $priceTimeLinkVisible;
}
}

PriceUtils.php


namespace goodsmemo\item\html;
/**
* Description of PriceHTMLUtils
*
* @author Goods Memo
*/
class PriceUtils {
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 = TextUtils::mb_strimwidth($plainTextReview, 0, $reviewLength, "……"); //$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 = "";
$BR_TAG = "<br />";
$lineCount = count($reviewLineArray);
$lastIndex = $lineCount - 1;
for ($i = 0; $i < $lineCount; $i++) {
$fitReviewLinesText .= $reviewLineArray[$i];
//$BR_TAGを加えた長さで判定する。例:mb_strimwidthの数え間違い例:<br />…… が <br /…… という風に切断されていた。
$stringWidth = mb_strwidth($fitReviewLinesText . $BR_TAG, "UTF-8"); //文字幅(見た目の長さ) //var_dump($stringWidth);
if ($stringWidth >= $reviewLength) {
$fitReviewLinesText = TextUtils::mb_strimwidth($fitReviewLinesText, 0, $reviewLength, "……");
break;
}
if ($i < $lastIndex) {
$fitReviewLinesText .= $BR_TAG;
}
}
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;
}
}
タイトルとURLをコピーしました