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

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

AbstractSettingSection.php


namespace goodsmemo\option;

use goodsmemo\option\SettingSection;
use goodsmemo\option\PageInfo;
use goodsmemo\option\paragraph\AbstractParagraph;

require_once GOODS_MEMO_DIR . "option/SettingSection.php";
require_once GOODS_MEMO_DIR . "option/PageInfo.php";
require_once GOODS_MEMO_DIR . "option/paragraph/AbstractParagraph.php";

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

    private $paragraphArray = array();

    abstract public function initSection(PageInfo $pageInfo);

    protected function addParagraph(AbstractParagraph $paragraph) {

	array_push($this->paragraphArray, $paragraph);
    }

    public function setOptionMap($optionMap) {

	foreach ($this->paragraphArray as $paragraph) {
	    $paragraph->setOptionMap($optionMap);
	}
    }

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

	foreach ($this->paragraphArray as $paragraph) {
	    $paragraph->sanitizeParagraphValue($inputedValueMap, $sanitizedValueMap);
	}
    }

}

AffiliateOptionUtils.php


namespace goodsmemo\option;

use goodsmemo\option\AffiliateSettingPage;
use goodsmemo\exception\OptionException;

require_once GOODS_MEMO_DIR . "option/AffiliateSettingPage.php";
require_once GOODS_MEMO_DIR . "exception/OptionException.php";

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

    public static function getAffiliateOption() {

	$optionMap = get_option(AffiliateSettingPage::OPTION_NAME_OF_DATABASE); //var_dump($optionMap);
	if ($optionMap === false) {
	    throw new OptionException("データベースに[" . AffiliateSettingPage::OPTION_NAME_OF_DATABASE . "]オプションが、存在しません。");
	}

	$filteredArray = array_filter($optionMap); //コールバック関数を省略した場合、配列のfalseに相当する値が全て削除される。
	if (empty($filteredArray)) {
	    throw new OptionException("アフィリエイトの設定が、ありません。");
	}

	return $optionMap;
    }

}

AffiliateSettingPage.php


namespace goodsmemo\option;

use goodsmemo\option\PageInfo;
use goodsmemo\option\AffiliateOptionUtils;
use goodsmemo\option\amazon\AmazonSettingSection;
use goodsmemo\option\rakuten\RakutenSettingSection;
use goodsmemo\exception\OptionException;

require_once GOODS_MEMO_DIR . "option/PageInfo.php";
require_once GOODS_MEMO_DIR . "option/AffiliateOptionUtils.php";
require_once GOODS_MEMO_DIR . "option/amazon/AmazonSettingSection.php";
require_once GOODS_MEMO_DIR . "option/rakuten/RakutenSettingSection.php";
require_once GOODS_MEMO_DIR . "exception/OptionException.php";

/**
 * Description of AffiliateSettingPage
 *
 * @author Goods Memo
 */
class AffiliateSettingPage {
    /*
     * 参照:設定ページの作成
     * https://wpdocs.osdn.jp/%E8%A8%AD%E5%AE%9A%E3%83%9A%E3%83%BC%E3%82%B8%E3%81%AE%E4%BD%9C%E6%88%90
     * 名前の付け方など、この解説を真似した。
     */

    const SETTING_MENU_SLUG = "goodsmemo-affiliate-setting";
    const OPTION_GROUP = "goodsmemo_option_group";
    const OPTION_NAME_OF_DATABASE = "goodsmemo_option_name"; //注意:goodsmemo-affiliate.phpにも記述した。

    private $sectionArray = array();

    /**
     * Start up
     */
    public function __construct() {

	// 管理画面でプラグインを削除した時に呼び出されるメソッドを登録(このメソッドはスタティックであること)
	//register_uninstall_hook(__FILE__, '\goodsmemo\option\AffiliateSettingPage::uninstall');//クラスのメソッドはエラーとなるらしい。

	$amazonSettingSelection = new AmazonSettingSection();
	array_push($this->sectionArray, $amazonSettingSelection);

	$rakutenSettingSelection = new RakutenSettingSection();
	array_push($this->sectionArray, $rakutenSettingSelection);

	add_action('admin_menu', array($this, 'add_plugin_page'));
	add_action('admin_init', array($this, 'init_page'));
    }

    /**
     * Add options page
     */
    public function add_plugin_page() {
	// This page will be under "Settings" 設定のサブメニューとしてメニューを追加する
	add_options_page(
		'アフィリエイトの設定', //メニューで選択したページのタイトルタグに表示されるテキスト
		'グッズメモ アフィリエイト', //メニューに使用されるテキスト
		'manage_options', //権限 ( 'manage_options' や 'administrator' など)
		AffiliateSettingPage::SETTING_MENU_SLUG, //スラッグ名
		array($this, 'output_affiliate_page')//The function to be called to output the content for this page.
	);
    }

    /**
     * Register and add settings
     */
    public function init_page() {
	register_setting(
		AffiliateSettingPage::OPTION_GROUP, // option group
		AffiliateSettingPage::OPTION_NAME_OF_DATABASE, // option name データベースに保存するオプションの名前
		array($this, 'sanitize') // オプションの値を無害化するコールバック関数。
	);

	$pageInfo = new PageInfo();
	$pageInfo->setSettingMenuSlug(AffiliateSettingPage::SETTING_MENU_SLUG);
	$pageInfo->setOptionGroup(AffiliateSettingPage::OPTION_GROUP);
	$pageInfo->setOptionNameOfDatabase(AffiliateSettingPage::OPTION_NAME_OF_DATABASE);

	foreach ($this->sectionArray as $section) {
	    $section->initSection($pageInfo);
	}
    }

    /**
     * Options page callback
     *
     * Notice: screen_icon の使用はバージョン 3.8.0 から<strong>非推奨</strong>になりました。代替は用意されておりません。 in /app/public/wp-includes/functions.php on line 3856
     * Set class property
     * Holds the values to be used in the fields callbacks
     */
    public function output_affiliate_page() {

	$this->addGoodsMemoOptionStyles();

	try {
	    $optionMap = AffiliateOptionUtils::getAffiliateOption();

	    foreach ($this->sectionArray as $section) {
		$section->setOptionMap($optionMap);
	    }
	} catch (OptionException $ex) {//最初に「アフィリエイトの設定」画面を表示した際、OptionExceptionが通知される。
	    //print $ex;
	}
	?>
	<div class="wrap">
	    <h2>アフィリエイトの設定</h2>
	    <form method="post" action="options.php">
		<?php
		// This prints out all hidden setting fields
		settings_fields(AffiliateSettingPage::OPTION_GROUP);
		do_settings_sections(AffiliateSettingPage::SETTING_MENU_SLUG); //引数:設定セクションを表示したいページのスラッグ。
		submit_button();
		?>
	    </form>
	</div>
	<?php
    }

    /**
     * Sanitize each setting field as needed
     *
     * @param array $inputedValueMap Contains all settings fields as array keys
     */
    public function sanitize($inputedValueMap) {

	$sanitizedValueMap = array();

	foreach ($this->sectionArray as $section) {
	    $section->sanitizeSectionValue($inputedValueMap, $sanitizedValueMap);
	}

	return $sanitizedValueMap;
    }

    private function addGoodsMemoOptionStyles() {

	$pluginCssURL = plugins_url('gma-optionStyle.css', __FILE__);
	//var_dump($pluginCssURL);

	wp_enqueue_style(
		AffiliateSettingPage::SETTING_MENU_SLUG, //スラッグ名
		$pluginCssURL
	);
    }

    /*
      public static function uninstall() {
      // データベースからオプションを削除する
      delete_option(\goodsmemo\option\AffiliateSettingPage::OPTION_NAME_OF_DATABASE);
      }
     */
}

gma-optionStyle.css


/*
    Created on : 2018/11/16, 14:37:00
    Author     : Goods Memo
*/

.form-table{
    border-collapse: collapse;/*各セルの線を重ねて表示します。*/
}
.form-table th{
    padding-left: 10px;
}

/*
他の書き方(nth-of-typeを使わない書き方)があるか?考え中。
*/

/*アマゾンの設定*/

/*URLParagraph*/
.form-table:nth-of-type(1) tr:nth-of-type(1) {
    border-top: solid 1px darkgray;
    border-right: solid 1px darkgray;
    border-left: solid 1px darkgray;
}
.form-table:nth-of-type(1) tr:nth-of-type(2) {
    border-right: solid 1px darkgray;
    border-bottom: solid 1px darkgray;
    border-left: solid 1px darkgray;
}

/*CommonRESTParagraph*/
.form-table:nth-of-type(1) tr:nth-of-type(3) {
    border-top: solid 1px darkgray;
    border-right: solid 1px darkgray;
    border-left: solid 1px darkgray;
}
.form-table:nth-of-type(1) tr:nth-of-type(4) {
    border-right: solid 1px darkgray;
    border-left: solid 1px darkgray;
}
.form-table:nth-of-type(1) tr:nth-of-type(5) {
    border-right: solid 1px darkgray;
    border-bottom: solid 1px darkgray;
    border-left: solid 1px darkgray;
}

/*ItemHTMLParagraph*/
.form-table:nth-of-type(1) tr:nth-of-type(6) {
    border-top: solid 1px darkgray;
    border-right: solid 1px darkgray;
    border-left: solid 1px darkgray;
}
.form-table:nth-of-type(1) tr:nth-of-type(7) {
    border-right: solid 1px darkgray;
    border-bottom: solid 1px darkgray;
    border-left: solid 1px darkgray;
}

/*ReviewParagraph*/
.form-table:nth-of-type(1) tr:nth-of-type(8) {
    border-top: solid 1px darkgray;
    border-right: solid 1px darkgray;
    border-left: solid 1px darkgray;
}
.form-table:nth-of-type(1) tr:nth-of-type(9) {
    border-right: solid 1px darkgray;
    border-left: solid 1px darkgray;
}
.form-table:nth-of-type(1) tr:nth-of-type(10) {
    border-right: solid 1px darkgray;
    border-bottom: solid 1px darkgray;
    border-left: solid 1px darkgray;
}

/*ProductTypeParagraph*/
.form-table:nth-of-type(1) tr:nth-of-type(11) {
    border-right: solid 1px darkgray;
    border-bottom: solid 1px darkgray;
    border-left: solid 1px darkgray;
}

/*RESTParagraph*/
.form-table:nth-of-type(1) tr:nth-of-type(12) {
    border-right: solid 1px darkgray;
    border-bottom: solid 1px darkgray;
    border-left: solid 1px darkgray;
}

/*SearchWidgetParagraph*/
.form-table:nth-of-type(1) tr:nth-of-type(13) {
    border-right: solid 1px darkgray;
    border-bottom: solid 1px darkgray;
    border-left: solid 1px darkgray;
}

/*楽天の設定*/

/*URLParagraph*/
.form-table:nth-of-type(2) tr:nth-of-type(1) {
    border-top: solid 1px darkgray;
    border-right: solid 1px darkgray;
    border-left: solid 1px darkgray;
}
.form-table:nth-of-type(2) tr:nth-of-type(2) {
    border-right: solid 1px darkgray;
    border-bottom: solid 1px darkgray;
    border-left: solid 1px darkgray;
}

/*CommonRESTParagraph*/
.form-table:nth-of-type(2) tr:nth-of-type(3) {
    border-top: solid 1px darkgray;
    border-right: solid 1px darkgray;
    border-left: solid 1px darkgray;
}
.form-table:nth-of-type(2) tr:nth-of-type(4) {
    border-right: solid 1px darkgray;
    border-bottom: solid 1px darkgray;
    border-left: solid 1px darkgray;
}

/*ItemHTMLParagraph*/
.form-table:nth-of-type(2) tr:nth-of-type(5) {
    border-top: solid 1px darkgray;
    border-right: solid 1px darkgray;
    border-left: solid 1px darkgray;
}
.form-table:nth-of-type(2) tr:nth-of-type(6) {
    border-right: solid 1px darkgray;
    border-bottom: solid 1px darkgray;
    border-left: solid 1px darkgray;
}

/*ReviewParagraph*/
.form-table:nth-of-type(2) tr:nth-of-type(7) {
    border-top: solid 1px darkgray;
    border-right: solid 1px darkgray;
    border-left: solid 1px darkgray;
}
.form-table:nth-of-type(2) tr:nth-of-type(8) {
    border-right: solid 1px darkgray;
    border-left: solid 1px darkgray;
}
.form-table:nth-of-type(2) tr:nth-of-type(9) {
    border-right: solid 1px darkgray;
    border-bottom: solid 1px darkgray;
    border-left: solid 1px darkgray;
}

PageInfo.php


namespace goodsmemo\option;

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

    private $settingMenuSlug;
    private $optionGroup;
    private $optionNameOfDatabase;

    public function getSettingMenuSlug() {
	return $this->settingMenuSlug;
    }

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

    public function getOptionNameOfDatabase() {
	return $this->optionNameOfDatabase;
    }

    public function setSettingMenuSlug($settingMenuSlug) {
	$this->settingMenuSlug = $settingMenuSlug;
    }

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

    public function setOptionNameOfDatabase($optionNameOfDatabase) {
	$this->optionNameOfDatabase = $optionNameOfDatabase;
    }

}

SectionInfo.php


namespace goodsmemo\option;

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

    private $sectionID;
    private $sectionTitle;

    public function getSectionID() {
	return $this->sectionID;
    }

    public function setSectionID($sectionID) {
	$this->sectionID = $sectionID;
    }

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

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

}

SettingSection.php


namespace goodsmemo\option;

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

    public function printSectionInfo();

    const PRINT_SECTION_INFO_FUNCTION_NAME = "printSectionInfo";

}

コンピューター
この記事をシェアする

グッズ・メモランダム