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

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

CommonRESTFieldParagraph.php

namespace goodsmemo\option\rakuten;

use goodsmemo\option\FieldParagraph;
use goodsmemo\option\AbstractFieldParagraph;
use goodsmemo\option\PageInfo;
use goodsmemo\option\SectionInfo;
use goodsmemo\option\FieldInfo;
use goodsmemo\option\FieldUtils;

require_once GOODS_MEMO_DIR . "option/FieldParagraph.php";
require_once GOODS_MEMO_DIR . "option/AbstractFieldParagraph.php";
require_once GOODS_MEMO_DIR . "option/PageInfo.php";
require_once GOODS_MEMO_DIR . "option/SectionInfo.php";
require_once GOODS_MEMO_DIR . "option/FieldInfo.php";
require_once GOODS_MEMO_DIR . "option/FieldUtils.php";

/**
 * Description of CommonRESTFieldParagraph
 *
 * @author Goods Memo
 */
class CommonRESTFieldParagraph extends AbstractFieldParagraph implements FieldParagraph {

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

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

	$applicationIDFieldInfo = $fieldInfoArray[0];
	add_settings_field(
		$applicationIDFieldInfo->getFieldID(), //入力項目のID
		$applicationIDFieldInfo->getFieldLabel(), //入力項目名
		array($this, 'outputApplicationIDID'), //入力項目のHTMLを出力する関数
		$pageInfo->getSettingMenuSlug(), //設定ページのslug。メニューのslugと同じもの。
		$sectionInfo->getSectionID()
	);

	$affiliateIDFieldInfo = $fieldInfoArray[1];
	add_settings_field(
		$affiliateIDFieldInfo->getFieldID(), //入力項目のID
		$affiliateIDFieldInfo->getFieldLabel(), //入力項目名
		array($this, 'outputAffiliateIDID'), //入力項目のHTMLを出力する関数
		$pageInfo->getSettingMenuSlug(), //設定ページのslug。メニューのslugと同じもの。
		$sectionInfo->getSectionID()
	);
    }

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

	$fieldInfoArray = parent::getFieldInfoArray();

	parent::validateExistence($input, $fieldInfoArray);
	FieldUtils::sanitizeTextOptions($input, $new_input, $fieldInfoArray);
    }

    public function outputApplicationIDID() {

	$fieldInfoArray = parent::getFieldInfoArray();

	$applicationIDFieldInfo = $fieldInfoArray[0];
	parent::printTextField($applicationIDFieldInfo);
    }

    public function outputAffiliateIDID() {

	$fieldInfoArray = parent::getFieldInfoArray();

	$affiliateIDFieldInfo = $fieldInfoArray[1];
	parent::printTextField($affiliateIDFieldInfo);
    }

}

CommonRESTFieldUtils.php

namespace goodsmemo\option\rakuten;

use goodsmemo\option\SectionInfo;
use goodsmemo\option\FieldInfo;
use goodsmemo\option\rakuten\RakutenSettingSection;

require_once GOODS_MEMO_DIR . "option/SectionInfo.php";
require_once GOODS_MEMO_DIR . "option/FieldInfo.php";
require_once GOODS_MEMO_DIR . "option/rakuten/RakutenSettingSection.php";

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

    const APPLICATION_ID_ID = RakutenSettingSection::ID_PREFIX . "_application_id_id";
    const AFFILIATE_ID_ID = RakutenSettingSection::ID_PREFIX . "_affiliate_id_id";

    public static function makeFieldInfoArray(SectionInfo $sectionInfo) {

	$fieldInfoArray = array();

	$applicationIDFieldInfo = new FieldInfo();
	$applicationIDFieldInfo->setFieldID(CommonRESTFieldUtils::APPLICATION_ID_ID);
	$applicationIDFieldInfo->setFieldLabel('楽天アプリID/デベロッパーID');
	$applicationIDFieldInfo->setDefaultFieldValue("");
	array_push($fieldInfoArray, $applicationIDFieldInfo);

	$affiliateIDFieldInfo = new FieldInfo();
	$affiliateIDFieldInfo->setFieldID(CommonRESTFieldUtils::AFFILIATE_ID_ID);
	$affiliateIDFieldInfo->setFieldLabel('楽天アフィリエイトID');
	$affiliateIDFieldInfo->setDefaultFieldValue("");
	array_push($fieldInfoArray, $affiliateIDFieldInfo);

	return $fieldInfoArray;
    }

}

ImageFieldUtils.php

namespace goodsmemo\option\rakuten;

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

    const DEFAULT_MEDIUM_IMAGE_WIDTH = 128;
    const DEFAULT_MEDIUM_IMAGE_HEIGHT = 128;

}

ItemHTMLFieldUtils.php

namespace goodsmemo\option\rakuten;

use goodsmemo\option\SectionInfo;
use goodsmemo\option\FieldInfo;
use goodsmemo\option\rakuten\RakutenSettingSection;

require_once GOODS_MEMO_DIR . "option/SectionInfo.php";
require_once GOODS_MEMO_DIR . "option/FieldInfo.php";
require_once GOODS_MEMO_DIR . "option/rakuten/RakutenSettingSection.php";

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

    const DEFAULT_NUMBER_TO_DISPLAY = 1;
    //
    const TITLE_LENGTH_ID = RakutenSettingSection::ID_PREFIX . "_title_length_id";
    const CACHE_EXPIRATION_IN_SECONDS_ID = RakutenSettingSection::ID_PREFIX . "_cache_expiration_in_seconds_id";

    public static function makeFieldInfoArray(SectionInfo $sectionInfo) {

	$fieldInfoArray = array();

	$titleLengthFieldInfo = new FieldInfo();
	$titleLengthFieldInfo->setFieldID(ItemHTMLFieldUtils::TITLE_LENGTH_ID);
	$titleLengthFieldInfo->setFieldLabel('商品名の表示文字数(目安の文字数)');
	$titleLengthFieldInfo->setDefaultFieldValue("80");
	$titleLengthFieldInfo->setNumericalVerificationEnabled(true);
	array_push($fieldInfoArray, $titleLengthFieldInfo);

	$cacheExpirationInSecondsFieldInfo = new FieldInfo();
	$cacheExpirationInSecondsFieldInfo->setFieldID(ItemHTMLFieldUtils::CACHE_EXPIRATION_IN_SECONDS_ID);
	$cacheExpirationInSecondsFieldInfo->setFieldLabel('商品情報のキャッシュ時間(秒)');
	$cacheExpirationInSecondsFieldInfo->setDefaultFieldValue("3600"); //例:3600秒
	$cacheExpirationInSecondsFieldInfo->setNumericalVerificationEnabled(true);
	array_push($fieldInfoArray, $cacheExpirationInSecondsFieldInfo);

	return $fieldInfoArray;
    }

}

PriceFieldUtils.php

namespace goodsmemo\option\rakuten;

/**
 * Description of PriceFieldParagraph
 *
 * @author Goods Memo
 */
class PriceFieldUtils {//TODO  extends AbstractFieldParagraph implements FieldParagraph

    const DEFAULT_SHOW_STATE_OF_PRICE_TIME_LINK = false;
    const DEFAULT_PRICE_TIME_FOOTER_TEXT = "価格の詳細:本商品の購入においては、購入の時点で楽天市場に表示されている価格の情報が適用されます。";

}

RakutenSettingSection.php

楽天の設定画面を表示します。

namespace goodsmemo\option\rakuten;

use goodsmemo\option\PageInfo;
use goodsmemo\option\SettingSection;
use goodsmemo\option\SectionInfo;
use goodsmemo\option\URLFieldParagraph;
use goodsmemo\option\ItemHTMLFieldParagraph;
use goodsmemo\option\ReviewFieldParagraph;
use goodsmemo\option\rakuten\URLFieldUtils;
use goodsmemo\option\rakuten\CommonRESTFieldParagraph;
use goodsmemo\option\rakuten\CommonRESTFieldUtils;
use goodsmemo\option\rakuten\ItemHTMLFieldUtils;
use goodsmemo\option\rakuten\ReviewFieldUtils;

require_once GOODS_MEMO_DIR . "option/PageInfo.php";
require_once GOODS_MEMO_DIR . "option/SettingSection.php";
require_once GOODS_MEMO_DIR . "option/SectionInfo.php";
require_once GOODS_MEMO_DIR . "option/URLFieldParagraph.php";
require_once GOODS_MEMO_DIR . "option/ItemHTMLFieldParagraph.php";
require_once GOODS_MEMO_DIR . "option/ReviewFieldParagraph.php";
require_once GOODS_MEMO_DIR . "option/rakuten/URLFieldUtils.php";
require_once GOODS_MEMO_DIR . "option/rakuten/CommonRESTFieldParagraph.php";
require_once GOODS_MEMO_DIR . "option/rakuten/CommonRESTFieldUtils.php";
require_once GOODS_MEMO_DIR . "option/rakuten/ItemHTMLFieldUtils.php";
require_once GOODS_MEMO_DIR . "option/rakuten/ReviewFieldUtils.php";

/**
 * Description of RakutenSettingSection
 *
 * @author Goods Memo
 */
class RakutenSettingSection implements SettingSection {

    const ID_PREFIX = "rakuten";

    private $urlFieldParagraph;
    private $commonRESTFieldParagraph;
    private $itemHTMLFieldParagraph;
    private $reviewFieldParagraph;

    public function initSection(PageInfo $pageInfo) {

	$sectionInfo = new SectionInfo();
	$sectionInfo->setSectionID("rakuten_section_id");
	$sectionInfo->setSectionTitle("楽天の設定");

	add_settings_section(
		$sectionInfo->getSectionID(), // ID
		$sectionInfo->getSectionTitle(), // Title
		array($this, SettingSection::PRINT_SECTION_INFO_FUNCTION_NAME), // Callback
		$pageInfo->getSettingMenuSlug() //設定ページのslug。メニューのslugと同じもの。
	);

	$this->urlFieldParagraph = new URLFieldParagraph();
	$urlFieldInfoArray = URLFieldUtils::makeFieldInfoArray($sectionInfo);
	$this->urlFieldParagraph->initFieldParagraph($pageInfo, $sectionInfo, $urlFieldInfoArray);

	$this->commonRESTFieldParagraph = new CommonRESTFieldParagraph();
	$commonRESTFieldInfoArray = CommonRESTFieldUtils::makeFieldInfoArray($sectionInfo);
	$this->commonRESTFieldParagraph->initFieldParagraph($pageInfo, $sectionInfo, $commonRESTFieldInfoArray);

	$this->itemHTMLFieldParagraph = new ItemHTMLFieldParagraph();
	$itemHTMLFieldInfoArray = ItemHTMLFieldUtils::makeFieldInfoArray($sectionInfo);
	$this->itemHTMLFieldParagraph->initFieldParagraph($pageInfo, $sectionInfo, $itemHTMLFieldInfoArray);

	$this->reviewFieldParagraph = new ReviewFieldParagraph();
	$reviewFieldInfoArray = ReviewFieldUtils::makeFieldInfoArray($sectionInfo);
	$this->reviewFieldParagraph->initFieldParagraph($pageInfo, $sectionInfo, $reviewFieldInfoArray);
    }

    public function printSectionInfo() {
	print '楽天アフィリエイトの設定を入力してください。未入力の場合、初期値を表示します。';
    }

    public function setOptionMap($optionMap) {

	$this->urlFieldParagraph->setOptionMap($optionMap);
	$this->commonRESTFieldParagraph->setOptionMap($optionMap);
	$this->itemHTMLFieldParagraph->setOptionMap($optionMap);
	$this->reviewFieldParagraph->setOptionMap($optionMap);
    }

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

	$this->urlFieldParagraph->sanitizeTextOption($input, $new_input);
	$this->commonRESTFieldParagraph->sanitizeTextOption($input, $new_input);
	$this->itemHTMLFieldParagraph->sanitizeTextOption($input, $new_input);
	$this->reviewFieldParagraph->sanitizeTextOption($input, $new_input);
    }

}

RESTFieldUtils.php

namespace goodsmemo\option\rakuten;

/**
 * Description of RESTFieldParagraph
 *
 * @author Goods Memo
 */
class RESTFieldUtils {//TODO  extends AbstractFieldParagraph implements FieldParagraph

    const EXISTENCE_IMAGE_FLAG = 1; //商品画像ありの商品のみを検索対象とする//TODO Choice部品

}

ReviewFieldUtils.php

namespace goodsmemo\option\rakuten;

use goodsmemo\option\SectionInfo;
use goodsmemo\option\FieldInfo;
use goodsmemo\option\rakuten\RakutenSettingSection;

require_once GOODS_MEMO_DIR . "option/SectionInfo.php";
require_once GOODS_MEMO_DIR . "option/FieldInfo.php";
require_once GOODS_MEMO_DIR . "option/rakuten/RakutenSettingSection.php";

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

    const EDITORIAL_REVIEW_LENGTH_ID = RakutenSettingSection::ID_PREFIX . "_editorial_review_length_id";
    const ARRAY_OF_STRING_TO_DELETE_ID = RakutenSettingSection::ID_PREFIX . "_array_of_string_to_delete_id";
    const ARRAY_OF_STRING_TO_BREAK_ID = RakutenSettingSection::ID_PREFIX . "_array_of_string_to_break_id";

    public static function makeFieldInfoArray(SectionInfo $sectionInfo) {

	$fieldInfoArray = array();

	$editorialReviewLengthFieldInfo = new FieldInfo();
	$editorialReviewLengthFieldInfo->setFieldID(ReviewFieldUtils::EDITORIAL_REVIEW_LENGTH_ID);
	$editorialReviewLengthFieldInfo->setFieldLabel('商品説明の表示文字数(目安の文字数)');
	$editorialReviewLengthFieldInfo->setDefaultFieldValue("200");
	$editorialReviewLengthFieldInfo->setNumericalVerificationEnabled(true);
	array_push($fieldInfoArray, $editorialReviewLengthFieldInfo);

	$arrayOfStringToDeleteFieldInfo = new FieldInfo();
	$arrayOfStringToDeleteFieldInfo->setFieldID(ReviewFieldUtils::ARRAY_OF_STRING_TO_DELETE_ID);
	$arrayOfStringToDeleteFieldInfo->setFieldLabel('商品説明から削除する文字列の配列(JSON配列)');
	$arrayOfStringToDeleteFieldInfo->setDefaultFieldValue('["<p>"]'); //削除する文字列の配列
	array_push($fieldInfoArray, $arrayOfStringToDeleteFieldInfo);

	$arrayOfStringToBreakFieldInfo = new FieldInfo();
	$arrayOfStringToBreakFieldInfo->setFieldID(ReviewFieldUtils::ARRAY_OF_STRING_TO_BREAK_ID);
	$arrayOfStringToBreakFieldInfo->setFieldLabel('商品説明で改行する文字の配列(JSON配列)');
	$arrayOfStringToBreakFieldInfo->setDefaultFieldValue('["●","■","◆","★","【"]'); //改行する文字列の配列
	array_push($fieldInfoArray, $arrayOfStringToBreakFieldInfo);

	return $fieldInfoArray;
    }

}

SearchFieldUtils.php

namespace goodsmemo\option\rakuten;

/**
 * Description of SearchFieldParagraph
 *
 * @author Goods Memo
 */
class SearchFieldUtils {//TODO  extends AbstractFieldParagraph implements FieldParagraph

    const ICHIBA_ITEM_SEARCH_OPERATION = "IchibaItem"; //TODO Choice部品

}

URLFieldUtils.php

namespace goodsmemo\option\rakuten;

use goodsmemo\option\SectionInfo;
use goodsmemo\option\FieldInfo;
use goodsmemo\option\rakuten\RakutenSettingSection;

require_once GOODS_MEMO_DIR . "option/SectionInfo.php";
require_once GOODS_MEMO_DIR . "option/FieldInfo.php";
require_once GOODS_MEMO_DIR . "option/rakuten/RakutenSettingSection.php";

/**
 * Description of URLFieldParagraph
 *
 * @author Goods Memo
 */
class URLFieldUtils {

    const HOSTNAME_ID = RakutenSettingSection::ID_PREFIX . "_hostname_id";
    const PATH_ID = RakutenSettingSection::ID_PREFIX . "_path_id";

    public static function makeFieldInfoArray(SectionInfo $sectionInfo) {

	$fieldInfoArray = array();

	$hostnameFieldInfo = new FieldInfo();
	$hostnameFieldInfo->setFieldID(URLFieldUtils::HOSTNAME_ID);
	$hostnameFieldInfo->setFieldLabel('楽天商品検索API ホスト名');
	$hostnameFieldInfo->setDefaultFieldValue("app.rakuten.co.jp");
	array_push($fieldInfoArray, $hostnameFieldInfo);

	$pathFieldInfo = new FieldInfo();
	$pathFieldInfo->setFieldID(URLFieldUtils::PATH_ID);
	$pathFieldInfo->setFieldLabel('楽天商品検索API リクエストURLのパス');
	$pathFieldInfo->setDefaultFieldValue("services/api/IchibaItem/Search/20170706");
	array_push($fieldInfoArray, $pathFieldInfo);

	return $fieldInfoArray;
    }

}