設定画面の段落・グッズメモ アフィリエイト

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

AbstractParagraph.php

namespace goodsmemo\option\paragraph;

use goodsmemo\option\PageInfo;
use goodsmemo\option\SectionInfo;
use goodsmemo\option\paragraph\ParagraphUtils;
use goodsmemo\option\field\AbstractField;
use goodsmemo\option\field\FieldPrinter;

require_once GOODS_MEMO_DIR . "option/PageInfo.php";
require_once GOODS_MEMO_DIR . "option/SectionInfo.php";
require_once GOODS_MEMO_DIR . "option/paragraph/ParagraphUtils.php";
require_once GOODS_MEMO_DIR . "option/field/AbstractField.php";
require_once GOODS_MEMO_DIR . "option/field/FieldPrinter.php";

/**
 * Description of AbstractParagraph
 *
 * @author Goods Memo
 */
abstract class AbstractParagraph {

    private $optionGroup;
    private $sectionTitle;
    private $fieldInfoArray;
    //
    private $fieldArray = array();

    abstract public function initParagraph(PageInfo $pageInfo, SectionInfo $sectionInfo, $fieldInfoArray);

    protected function addField(PageInfo $pageInfo, SectionInfo $sectionInfo, AbstractField $field) {

	$fieldInfo = $field->getFieldInfo();

	add_settings_field(
		$fieldInfo->getFieldID(), //入力項目のID
		$fieldInfo->getFieldLabel(), //入力項目名
		array($field, FieldPrinter::PRINT_FIELD_METHOD_NAME), //入力項目のHTMLを出力する関数
		$pageInfo->getSettingMenuSlug(), //設定ページのslug。メニューのslugと同じもの。
		$sectionInfo->getSectionID()
	);

	array_push($this->fieldArray, $field);
    }

    public function setOptionMap($optionMap) {

	foreach ($this->fieldArray as $field) {

	    $field->setOptionMap($optionMap);
	}
    }

    public function sanitizeParagraphValue($inputedValueMap, &$sanitizedValueMap) {
	//sanitizedValueMap:変更するため、配列の参照渡しとする。
	//入力値の検証なし
	ParagraphUtils::sanitizeParagraphValue($inputedValueMap, $sanitizedValueMap, $this->fieldInfoArray);
    }

    public function getOptionGroup() {
	return $this->optionGroup;
    }

    public function setOptionGroup($optionGroup) {
	$this->optionGroup = $optionGroup;
    }

    public function getSectionTitle() {
	return $this->sectionTitle;
    }

    public function setSectionTitle($sectionTitle) {
	$this->sectionTitle = $sectionTitle;
    }

    public function getFieldInfoArray() {
	return $this->fieldInfoArray;
    }

    public function setFieldInfoArray($fieldInfoArray) {
	$this->fieldInfoArray = $fieldInfoArray;
    }

}

AbstractTextParagraph.php

namespace goodsmemo\option\paragraph;

use goodsmemo\option\paragraph\ParagraphUtils;

require_once GOODS_MEMO_DIR . "option/paragraph/ParagraphUtils.php";

/**
 * Description of AbstractTextParagraph
 *
 * @author Goods Memo
 */
trait AbstractTextParagraph {

    //AbstractParagraphクラスから、テキストフィールドの検証処理を取り出した。
    //AbstractTextParagraph extends AbstractParagraph をやめた。
    //AbstractTextParagraphの子クラスで、input type="text"以外の部品(チェックボックスやセレクトのタグ)を使う場合があるため。
    //
    //このトレイト使うなら、以下のメソッドを実装していること
    abstract public function getFieldInfoArray();

    abstract public function getOptionGroup();

    abstract public function getSectionTitle();

    public function validateMoreThanZero($inputedValueMap) {

	$fieldInfoArray = parent::getFieldInfoArray();
	$optionGroup = parent::getOptionGroup();
	$sectionTitle = parent::getSectionTitle();

	//入力値の検証処理は、段落「画面クラス」の処理でないと判断しました。
	ParagraphUtils::validateMoreThanZero($inputedValueMap, $fieldInfoArray, $optionGroup, $sectionTitle);
    }

    public function validateExistence($inputedValueMap) {

	$fieldInfoArray = parent::getFieldInfoArray();
	$optionGroup = parent::getOptionGroup();
	$sectionTitle = parent::getSectionTitle();

	//入力値の検証処理は、段落「画面クラス」の処理でないと判断しました。
	ParagraphUtils::validateExistence($inputedValueMap, $fieldInfoArray, $optionGroup, $sectionTitle);
    }

}

ItemHTMLParagraph.php

namespace goodsmemo\option\paragraph;

use goodsmemo\option\paragraph\AbstractParagraph;
use goodsmemo\option\paragraph\AbstractTextParagraph;
use goodsmemo\option\PageInfo;
use goodsmemo\option\SectionInfo;
use goodsmemo\option\field\TextField;

require_once GOODS_MEMO_DIR . "option/paragraph/AbstractParagraph.php";
require_once GOODS_MEMO_DIR . "option/paragraph/AbstractTextParagraph.php";
require_once GOODS_MEMO_DIR . "option/PageInfo.php";
require_once GOODS_MEMO_DIR . "option/SectionInfo.php";
require_once GOODS_MEMO_DIR . "option/field/TextField.php";

/**
 * Description of ItemHTMLParagraph
 *
 * @author Goods Memo
 */
class ItemHTMLParagraph extends AbstractParagraph {

    use AbstractTextParagraph;

    const DEFAULT_TITLE_LENGTH_LABEL = "商品名の表示文字数(目安の文字数)";
    const DEFAULT_TITLE_LENGTH_VALUE = 80;
    //
    const DEFAULT_CACHE_EXPIRATION_IN_SECONDS_LABEL = "商品情報のキャッシュ時間(秒)";
    const DEFAULT_CACHE_EXPIRATION_IN_SECONDS_VALUE = 3600; //例:3600秒

    public function initParagraph(PageInfo $pageInfo, SectionInfo $sectionInfo, $fieldInfoArray) {

	parent::setOptionGroup($pageInfo->getOptionGroup());
	parent::setSectionTitle($sectionInfo->getSectionTitle());
	parent::setFieldInfoArray($fieldInfoArray);

	$titleLengthTextField = new TextField($pageInfo->getOptionNameOfDatabase(), $fieldInfoArray[0]);
	parent::addField($pageInfo, $sectionInfo, $titleLengthTextField);

	$cacheExpirationInSecondsTextField = new TextField($pageInfo->getOptionNameOfDatabase(), $fieldInfoArray[1]);
	parent::addField($pageInfo, $sectionInfo, $cacheExpirationInSecondsTextField);
    }

    public function sanitizeParagraphValue($inputedValueMap, &$sanitizedValueMap) {
	//sanitizedValueMap:変更するため、配列の参照渡しとする。

	$this->validateMoreThanZero($inputedValueMap);
	parent::sanitizeParagraphValue($inputedValueMap, $sanitizedValueMap);
    }

}

ParagraphUtils.php

namespace goodsmemo\option\paragraph;

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

    public static function sanitizeParagraphValue($inputedValueMap, &$sanitizedValueMap, $fieldInfoArray) {
	//sanitizedValueMap:変更するため、配列の参照渡しとする。

	foreach ($fieldInfoArray as $fieldInfo) {

	    $inputFieldID = $fieldInfo->getFieldID();
	    if (isset($inputedValueMap[$inputFieldID])) {

		$sanitizedValueMap[$inputFieldID] = sanitize_text_field($inputedValueMap[$inputFieldID]);
	    }
	}
    }

    public static function validateMoreThanZero($inputedValueMap, $fieldInfoArray, $optionGroup, $sectionTitle) {

	foreach ($fieldInfoArray as $fieldInfo) {

	    if ($fieldInfo->getNumericalVerificationEnabled() === false) {
		continue;
	    }

	    $inputFieldID = $fieldInfo->getFieldID();

	    if (isset($inputedValueMap[$inputFieldID])) {

		$value = $inputedValueMap[$inputFieldID];
		if (is_numeric($value) && $value >= 0) {
		    ;
		} else {
		    $fieldLabel = $fieldInfo->getFieldLabel();
		    add_settings_error(
			    $optionGroup, $inputFieldID, //エラー出力時にHTMLで「id=」の形式で「setting-error-」の後に追加される文字列を設定します。任意の文字列で良いです。
			    $sectionTitle . ':' . $fieldLabel . ':無効な値 ' . $value . ' です。0以上の値を入力してください。'
		    );
		    //ここで処理を中断したい場合、自作の例外を通知する。
		}
	    }
	}
    }

    public static function validateExistence($inputedValueMap, $fieldInfoArray, $optionGroup, $sectionTitle) {

	foreach ($fieldInfoArray as $fieldInfo) {

	    if ($fieldInfo->getNumericalVerificationEnabled()) {
		continue;
	    }

	    $inputFieldID = $fieldInfo->getFieldID();
	    $valueExists;
	    if (isset($inputedValueMap[$inputFieldID])) {
		$value = $inputedValueMap[$inputFieldID];
		if (trim($value)) {
		    $valueExists = true;
		} else {
		    $valueExists = false;
		}
	    } else {
		$valueExists = false;
	    }

	    if ($valueExists === false) {
		$fieldLabel = $fieldInfo->getFieldLabel();
		add_settings_error(
			$optionGroup, $inputFieldID, //エラー出力時にHTMLで「id=」の形式で「setting-error-」の後に追加される文字列を設定します。任意の文字列で良いです。
			$sectionTitle . ':' . $fieldLabel . ':未入力です。有効な値を入力してください。'
		);
		//ここで処理を中断したい場合、自作の例外を通知する。
	    }
	}
    }

}

ReviewParagraph.php

namespace goodsmemo\option\paragraph;

use goodsmemo\option\paragraph\AbstractParagraph;
use goodsmemo\option\paragraph\AbstractTextParagraph;
use goodsmemo\option\PageInfo;
use goodsmemo\option\SectionInfo;
use goodsmemo\option\field\TextField;
use goodsmemo\option\field\TextareaField;

require_once GOODS_MEMO_DIR . "option/paragraph/AbstractParagraph.php";
require_once GOODS_MEMO_DIR . "option/paragraph/AbstractTextParagraph.php";
require_once GOODS_MEMO_DIR . "option/PageInfo.php";
require_once GOODS_MEMO_DIR . "option/SectionInfo.php";
require_once GOODS_MEMO_DIR . "option/field/TextField.php";
require_once GOODS_MEMO_DIR . "option/field/TextareaField.php";

/**
 * Description of ReviewParagraph
 *
 * @author Goods Memo
 */
class ReviewParagraph extends AbstractParagraph {

    use AbstractTextParagraph;

    const DEFAULT_EDITORIAL_REVIEW_LENGTH_LABEL = "商品説明の表示文字数(目安の文字数)";
    const DEFAULT_EDITORIAL_REVIEW_LENGTH_VALUE = 200;
    //
    const DEFAULT_ARRAY_OF_STRING_TO_DELETE_LABEL = "商品説明から削除する文字列の配列(JSON配列。空の配列[])";
    const DEFAULT_ARRAY_OF_STRING_TO_DELETE_VALUE = '["<p>","<b>","<br />"]'; //削除する文字列の配列
    //
    const DEFAULT_ARRAY_OF_STRING_TO_BREAK_LABEL = "商品説明で改行する文字の配列(JSON配列。空の配列[])";
    const DEFAULT_ARRAY_OF_STRING_TO_BREAK_VALUE = '["●","■","◆","★","【"]'; //改行する文字列の配列

    public function initParagraph(PageInfo $pageInfo, SectionInfo $sectionInfo, $fieldInfoArray) {

	parent::setOptionGroup($pageInfo->getOptionGroup());
	parent::setSectionTitle($sectionInfo->getSectionTitle());
	parent::setFieldInfoArray($fieldInfoArray);

	$editorialReviewLengthTextField = new TextField($pageInfo->getOptionNameOfDatabase(), $fieldInfoArray[0]);
	parent::addField($pageInfo, $sectionInfo, $editorialReviewLengthTextField);

	$arrayOfStringToDeleteTextarea = new TextareaField($pageInfo->getOptionNameOfDatabase(), $fieldInfoArray[1]);
	parent::addField($pageInfo, $sectionInfo, $arrayOfStringToDeleteTextarea);

	$arrayOfStringToBreakTextarea = new TextareaField($pageInfo->getOptionNameOfDatabase(), $fieldInfoArray[2]);
	parent::addField($pageInfo, $sectionInfo, $arrayOfStringToBreakTextarea);
    }

    public function sanitizeParagraphValue($inputedValueMap, &$sanitizedValueMap) {
	//sanitizedValueMap:変更するため、配列の参照渡しとする。

	$this->validateMoreThanZero($inputedValueMap);
	$this->validateExistence($inputedValueMap);
	parent::sanitizeParagraphValue($inputedValueMap, $sanitizedValueMap);
    }

}

URLParagraph.php

namespace goodsmemo\option\paragraph;

use goodsmemo\option\paragraph\AbstractParagraph;
use goodsmemo\option\paragraph\AbstractTextParagraph;
use goodsmemo\option\PageInfo;
use goodsmemo\option\SectionInfo;
use goodsmemo\option\field\TextField;

require_once GOODS_MEMO_DIR . "option/paragraph/AbstractParagraph.php";
require_once GOODS_MEMO_DIR . "option/paragraph/AbstractTextParagraph.php";
require_once GOODS_MEMO_DIR . "option/PageInfo.php";
require_once GOODS_MEMO_DIR . "option/SectionInfo.php";
require_once GOODS_MEMO_DIR . "option/field/TextField.php";

/**
 * Description of URLParagraph
 *
 * @author Goods Memo
 */
class URLParagraph extends AbstractParagraph {

    use AbstractTextParagraph;

    public function initParagraph(PageInfo $pageInfo, SectionInfo $sectionInfo, $fieldInfoArray) {

	parent::setOptionGroup($pageInfo->getOptionGroup());
	parent::setSectionTitle($sectionInfo->getSectionTitle());
	parent::setFieldInfoArray($fieldInfoArray);

	$hostnameTextField = new TextField($pageInfo->getOptionNameOfDatabase(), $fieldInfoArray[0]);
	parent::addField($pageInfo, $sectionInfo, $hostnameTextField);

	$pathTextField = new TextField($pageInfo->getOptionNameOfDatabase(), $fieldInfoArray[1]);
	parent::addField($pageInfo, $sectionInfo, $pathTextField);
    }

    public function sanitizeParagraphValue($inputedValueMap, &$sanitizedValueMap) {
	//sanitizedValueMap:変更するため、配列の参照渡しとする。

	$this->validateExistence($inputedValueMap);
	parent::sanitizeParagraphValue($inputedValueMap, $sanitizedValueMap);
    }

}