WP PLUGIN バグ修正 ソートのカスタムフィールドのメタボックス追加

・ソートのカスタムフィールドのメタボックスを追加
・共通nameタグが更新されないバグ対策としてフォームタグを共通化
・ファイル名をクラス名に合わせるよう変更
This commit is contained in:
2021-06-13 10:11:17 +09:00
parent 2c72904078
commit 9cc82988e2
8 changed files with 326 additions and 149 deletions
+10 -7
View File
@@ -23,12 +23,13 @@ License: GPLv2
*/ */
/** /**
* 指定したキーが無ければ、投稿の管理画面表示時にメタボックスを追加する。 * TODO:
* メタキーは下記のオプションテーブルから取得する 1)カテゴリーのソート用カスタムフィールド取得と設定
* _apop_normal_order_param ・保存しているタームIDと投稿が属するタームIDが一致する場合、管理画面に表示する
* _apop_search_order_param class.apop.apop_post.php、setting_post_custom_field.php
* _apop_tax_order_param * 2uninstall.php
* ラベルは「APO+カスタムフィールド:XXXX」とする * 投稿とタクソノミーのカスタムフィールド削除方法修正
* ・delete_post_meta_by_key()を廃止し、WP_QUERYに変更する
*/ */
define( 'APOP_PLUGIN_URL', plugin_dir_url( __FILE__ ) ); define( 'APOP_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
@@ -36,9 +37,11 @@ define( 'APOP_PLUGIN_PATH', plugin_dir_path( __FILE__ ) );
//メイン処理のクラスをインスタンス化 //メイン処理のクラスをインスタンス化
require_once __DIR__ . '/class/class.apop.order.php'; require_once __DIR__ . '/class/class.apop.order.php';
require_once __DIR__ . '/class/class.apop.ui.php'; require_once __DIR__ . '/class/class.apop.apop_ui.php';
require_once __DIR__ . '/class/class.apop.apop_post.php';
$APOP = new APOP; $APOP = new APOP;
new APOP_POST;
//CSS, JSの読み込み //CSS, JSの読み込み
add_action( 'admin_enqueue_scripts', 'register_my_styles' ); add_action( 'admin_enqueue_scripts', 'register_my_styles' );
+137
View File
@@ -0,0 +1,137 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
} // Exit if accessed directly
if ( ! class_exists( 'APOP_POST' ) ) {
class APOP_POST {
const TEMPLATE_DIR = __DIR__ . '/../template/';
private $order_field = array();
private $name_keys = array();
private $order_param_keys = array(
'custom_field',
'custom_field_2',
'custom_field_3',
'custom_field_4',
);
private $labels;
public function __construct() {
add_action( 'admin_menu', array( $this, 'add_meta_fields' ) );
add_action( 'save_post', array( $this, 'save_meta_fields' ) );
}
//メタボックスを追加
public function add_meta_fields() {
$target_posts = $this->set_post_types();
foreach ( $target_posts as $target_post ) {
add_meta_box( 'apo_custom_fields', 'APO+カスタムフィールド', array(
$this,
'insert_meta_fields'
), $target_post, 'normal' );
}
}
public function insert_meta_fields() {
//通常ソート
$this->create_order_field_data( get_option( '_apop_normal_order_param' ), 'normal' );
//検索ソート
$this->create_order_field_data( get_option( '_apop_search_order_param' ), 'search' );
//カテゴリー、タグ、カスタム分類
$this->create_order_tax_field_data( get_option( '_apop_tax_order_param' ) );
$this->labels = array(
'normal' => '通常',
'search' => '検索',
'tax' => 'カテゴリー、タグ、カスタム分類',
);
require_once self::TEMPLATE_DIR . 'setting_post_custom_field.php';
}
private function set_post_types() {
$custom_posts = array_values( get_post_types( array( 'public' => true, '_builtin' => false ) ) );
return array_merge( array( 'page', 'post', ), $custom_posts );
}
private function create_order_field_data( $param, $key, $update = false ) {
foreach ( $this->order_param_keys as $order_param_key ) {
if ( isset( $param[ $order_param_key ]['field']['meta_key'] ) &&
! empty( $param[ $order_param_key ]['field']['meta_key'] ) ) {
if ( $update ) {
$this->name_keys[] = $param[ $order_param_key ]['field']['meta_key'];
} else {
$this->order_field[ $key ][] = $param[ $order_param_key ]['field']['meta_key'];
}
}
}
}
private function create_order_tax_field_data( $param, $update = false ) {
$term_ids = $this->create_post_term_ids();
foreach ( $term_ids as $term_id ) {
foreach ( $this->order_param_keys as $order_param_key ) {
if ( isset( $param[ $term_id ][ $order_param_key ]['field']['meta_key'] ) &&
! empty( $param[ $term_id ][ $order_param_key ]['field']['meta_key'] ) ) {
if ( $update ) {
$this->name_keys[] = $param[ $term_id ][ $order_param_key ]['field']['meta_key'];
} else {
$this->order_field['tax'][] = $param[ $term_id ][ $order_param_key ]['field']['meta_key'];
}
}
}
}
}
private function create_post_term_ids() {
global $post;
$terms = array();
$term_ids = array();
$taxonomy_slugs = array_keys( get_the_taxonomies() );
foreach ( $taxonomy_slugs as $taxonomy_slug ) {
$terms[] = get_the_terms( $post->ID, $taxonomy_slug );
}
foreach ( $terms as $term ) {
foreach ( $term as $tax ) {
$term_ids[] = $tax->term_id;
}
}
return $term_ids;
}
private function get_custom_field_data( $custom_filed_name ) {
global $post;
return get_post_meta( $post->ID, $custom_filed_name, true );
}
// カスタムフィールドの値を保存
public function save_meta_fields( $post_id ) {
$this->get_update_meta_fields();
if ( count( $this->name_keys ) > 0 ) {
foreach ( $this->name_keys as $name_key ) {
update_post_meta( $post_id, $name_key, $_POST[ $name_key ] ?? '' );
update_post_meta( $post_id, $name_key, $_POST[ $name_key ] ?? '' );
}
}
}
private function get_update_meta_fields() {
//通常ソート
$this->create_order_field_data( get_option( '_apop_normal_order_param' ), 'normal', true );
//検索ソート
$this->create_order_field_data( get_option( '_apop_search_order_param' ), 'search', true );
//カテゴリー、タグ、カスタム分類
$this->create_order_tax_field_data( get_option( '_apop_tax_order_param' ), true );
}
}
}
+38 -5
View File
@@ -42,7 +42,8 @@ h3 {
.post-order-box-outer { .post-order-box-outer {
width: 100%; width: 100%;
margin: -1px 0 2em; margin: -1px 0 2em;
border-top: 1px solid #999; border: 1px solid #999;
background: #fff;
} }
.post-order-box:not(:first-child) { .post-order-box:not(:first-child) {
@@ -56,9 +57,7 @@ h3 {
.list-orders-inner { .list-orders-inner {
width: 50%; width: 50%;
margin: 1.5em 1em 0 0;
padding: .5em; padding: .5em;
border: 1px solid #999;
position: relative; position: relative;
} }
@@ -75,7 +74,8 @@ p.submit.post-order .button-primary {
bottom: 1em; bottom: 1em;
} }
dl.apop_setting_list { dl.apop_setting_list,
dl.apop_setting_list_dd {
width: 100%; width: 100%;
margin-top: .5em; margin-top: .5em;
} }
@@ -92,6 +92,29 @@ dl.apop_setting_list dd:first-of-type {
margin-bottom: 2em; margin-bottom: 2em;
} }
dl.apop_setting_list_dd dt {
margin-top: 1em;
font-weight: bold;
}
dl.apop_setting_list_dd dd {
margin: .5em 0;
border-bottom: 1px solid #c3c4c7;
}
dl.apop_setting_list_dd dd ul {
margin-bottom: 1em;
}
dl.apop_setting_list_dd dd label {
display: block;
margin-bottom: .3em;
}
dl.apop_setting_list_dd dd input {
width: 100%;
}
.form-table tr { .form-table tr {
border-bottom: 1px solid #ccc; border-bottom: 1px solid #ccc;
} }
@@ -145,7 +168,7 @@ dl.apop_setting_list dd:first-of-type {
.no_registered_exp { .no_registered_exp {
width: 100%; width: 100%;
margin-top: 1em; margin: 1em 0 0 .5em;
} }
.order_setting_list > li { .order_setting_list > li {
@@ -197,6 +220,16 @@ dl.apop_setting_list dd:first-of-type {
cursor: default; cursor: default;
} }
.apop-submit{
margin:1em .5em;
}
.apop-form_table{
background: #fff;
}
.apop-form_table th{
padding-left:.5em;
}
@media only screen and (max-width: 1264px) { @media only screen and (max-width: 1264px) {
.search_normal_sort .product-list { .search_normal_sort .product-list {
+39 -48
View File
@@ -13,12 +13,12 @@ $submit_type = APOP_UI::input_post_filter( 'submit_type', 'str' );
</ul> </ul>
</nav> </nav>
<div class="post-order-box-outer" data-submit_type="<?php echo $submit_type; ?>"> <div class="post-order-box-outer" data-submit_type="<?php echo $submit_type; ?>">
<div class="post-order-box"> <form action="" method="post">
<div class="list-orders-outer"> <?php wp_nonce_field( 'sh_options' ); ?>
<div class="list-orders-inner"> <div class="post-order-box">
<form action="" method="post"> <div class="list-orders-outer">
<div class="list-orders-inner">
<h3>通常</h3> <h3>通常</h3>
<?php wp_nonce_field( 'sh_options' ); ?>
<?php <?php
$order_target_type = '_apop_normal_order'; $order_target_type = '_apop_normal_order';
$order_target = APOP_UI::get_order_type( $order_target_type ); $order_target = APOP_UI::get_order_type( $order_target_type );
@@ -43,20 +43,13 @@ $submit_type = APOP_UI::input_post_filter( 'submit_type', 'str' );
<?php echo APOP_UI::get_all_search_normal_posts( 'normal' ); ?> <?php echo APOP_UI::get_all_search_normal_posts( 'normal' ); ?>
</ul> </ul>
</div> </div>
<input type="hidden" name="submit_type" value="0"> </div>
<p class="submit post-order"><input type="submit" name="Submit" class="button-primary"
value="変更を保存"/></p>
</form>
</div> </div>
</div> </div>
</div> <div class="post-order-box">
<div class="post-order-box"> <div class="list-orders-outer">
<div class="list-orders-outer"> <div class="list-orders-inner">
<div class="list-orders-inner">
<form action="" method="post">
<h3>検索</h3> <h3>検索</h3>
<?php wp_nonce_field( 'sh_options' ); ?>
<?php <?php
$order_target_type = '_apop_search_order'; $order_target_type = '_apop_search_order';
$order_target = APOP_UI::get_order_type( $order_target_type ); $order_target = APOP_UI::get_order_type( $order_target_type );
@@ -81,42 +74,40 @@ $submit_type = APOP_UI::input_post_filter( 'submit_type', 'str' );
<?php echo APOP_UI::get_all_search_normal_posts( 'search' ); ?> <?php echo APOP_UI::get_all_search_normal_posts( 'search' ); ?>
</ul> </ul>
</div> </div>
<input type="hidden" name="submit_type" value="1"> </div>
<p class="submit post-order"><input type="submit" name="Submit" class="button-primary"
value="変更を保存"/></p>
</form>
</div> </div>
</div> </div>
</div> <div class="post-order-box">
<div class="post-order-box"> <div class="tax_sort_box">
<div class="tax_sort_box"> <?php
<?php $tax_lists = array( 'category' => APOP_UI::get_cat_tag_list( 'cat', 'category' ) );
$tax_lists = array( 'category' => APOP_UI::get_cat_tag_list( 'cat', 'category' ) ); $tax_title_text = 'カテゴリー';
$tax_title_text = 'カテゴリー'; $submit_type_number = 2;
$submit_type_number = 2; ?>
?> <?php include APOP_PLUGIN_PATH . 'template/order_parts_taxonomy.php'; ?>
<?php include APOP_PLUGIN_PATH . 'template/order_parts_taxonomy.php'; ?> </div>
</div> </div>
</div> <div class="post-order-box">
<div class="post-order-box"> <div class="tax_sort_box">
<div class="tax_sort_box"> <?php
<?php $tax_lists = array( 'tag_id' => APOP_UI::get_cat_tag_list( 'tag', 'post_tag' ) );
$tax_lists = array( 'tag_id' => APOP_UI::get_cat_tag_list( 'tag', 'post_tag' ) ); $tax_title_text = 'タグ';
$tax_title_text = 'タグ'; $submit_type_number = 3;
$submit_type_number = 3; ?>
?> <?php include APOP_PLUGIN_PATH . 'template/order_parts_taxonomy.php'; ?>
<?php include APOP_PLUGIN_PATH . 'template/order_parts_taxonomy.php'; ?> </div>
</div> </div>
</div> <div class="post-order-box">
<div class="post-order-box"> <div class="tax_sort_box">
<div class="tax_sort_box"> <?php
<?php $tax_lists = array( 'taxonomy' => APOP_UI::get_cat_tag_list( 'tax', 'taxonomy' ) );
$tax_lists = array( 'taxonomy' => APOP_UI::get_cat_tag_list( 'tax', 'taxonomy' ) ); $tax_title_text = 'カスタム分類';
$tax_title_text = 'カスタム分類'; $submit_type_number = 4;
$submit_type_number = 4; ?>
?> <?php include APOP_PLUGIN_PATH . 'template/order_parts_taxonomy.php'; ?>
<?php include APOP_PLUGIN_PATH . 'template/order_parts_taxonomy.php'; ?> </div>
</div> </div>
</div> <p class="apop-submit"><input type="submit" name="submit" class="button-primary" value="変更を保存"/></p>
</form>
</div> </div>
</div> </div>
+40 -48
View File
@@ -5,56 +5,48 @@
<?php echo APOP_UI::none_registered_alert_msg(); ?> <?php echo APOP_UI::none_registered_alert_msg(); ?>
<?php foreach ( $tax_list as $tax_data ): ?> <?php foreach ( $tax_list as $tax_data ): ?>
<div class="list-orders-inner"> <div class="list-orders-inner">
<form action="" method="post"> <h3><?php echo $tax_data->name; ?></h3>
<?php wp_nonce_field( 'sh_options' ); ?> <?php
<h3><?php echo $tax_data->name; ?></h3> $order_target_data = get_option( '_apop_tax_sort_type' );
<?php if ( isset( $order_target_data[ $tax_data->term_id ] ) ) {
$order_target_data = get_option( '_apop_tax_sort_type' ); $order_target = $order_target_data[ $tax_data->term_id ];
if ( isset( $order_target_data[ $tax_data->term_id ] ) ) { } else {
$order_target = $order_target_data[ $tax_data->term_id ]; $order_target = 1;
} else { }
$order_target = 1; ?>
} <ul class="sort_menu_list"
?> data-order_target="<?php echo $order_target; ?>">
<ul class="sort_menu_list" <li>
data-order_target="<?php echo $order_target; ?>"> <label>
<li> <input class="sort_menu" type="radio"
<label> name="_apop_tax_sort_type[<?php echo $tax_data->term_id; ?>]"
<input class="sort_menu" type="radio" value="1"<?php checked( $order_target, 1 ); ?>>ドラッグソート</label>
name="_apop_tax_sort_type[<?php echo $tax_data->term_id; ?>]" </li>
value="1"<?php checked( $order_target, 1 ); ?>>ドラッグソート</label> <li>
</li> <label>
<li> <input class="sort_menu" type="radio"
<label> name="_apop_tax_sort_type[<?php echo $tax_data->term_id; ?>]"
<input class="sort_menu" type="radio" value="2"<?php checked( $order_target, 2 ); ?>>標準+カスタムフィールドソート</label>
name="_apop_tax_sort_type[<?php echo $tax_data->term_id; ?>]" </li>
value="2"<?php checked( $order_target, 2 ); ?>>標準+カスタムフィールドソート</label> </ul>
</li>
</ul>
<div class="sort_box"> <div class="sort_box">
<ul class="post-order-list"> <ul class="post-order-list">
<?php echo APOP_UI::create_order_list( $tax_data, $tax_key ); ?> <?php echo APOP_UI::create_order_list( $tax_data, $tax_key ); ?>
</ul> </ul>
</div>
<div class="sort_box">
<?php list( $list, $alert ) = APOP_UI::create_search_normal_list( 'tax', $tax_data->term_id ); ?>
<?php echo $alert; ?>
<h4>有効</h4>
<ul class="post-order-list search_normal_sort">
<?php echo $list; ?>
</ul>
<div class="disable_box">
<h4>無効</h4>
<ul class="disable_normal_list"></ul>
</div> </div>
<div class="sort_box"> </div>
<?php list( $list, $alert ) = APOP_UI::create_search_normal_list( 'tax', $tax_data->term_id ); ?>
<?php echo $alert; ?>
<h4>有効</h4>
<ul class="post-order-list search_normal_sort">
<?php echo $list; ?>
</ul>
<div class="disable_box">
<h4>無効</h4>
<ul class="disable_normal_list"></ul>
</div>
</div>
<input type="hidden" name="submit_type" value="<?php echo $submit_type_number; ?>">
<p class="submit post-order"><input type="submit" name="Submit"
class="button-primary"
value="変更を保存"/></p>
<input type="hidden" name="sort_type[cat]" value="1">
</form>
</div> </div>
<?php endforeach; ?> <?php endforeach; ?>
<?php else: ?> <?php else: ?>
+41 -41
View File
@@ -1,47 +1,47 @@
<div class="post-setting-box"> <div class="post-setting-box">
<form action="" method="post"> <form action="" method="post">
<?php <?php
wp_nonce_field( 'sh_options' ); wp_nonce_field( 'sh_options' );
$opt_per_page = get_option( '_apop_per_page' ); $opt_per_page = get_option( '_apop_per_page' );
$default_per_page = get_option( 'posts_per_page' ); $default_per_page = get_option( 'posts_per_page' );
?> ?>
<h2>設定</h2> <h2>設定</h2>
<table class="form-table"> <table class="form-table apop-form_table">
<tr> <tr>
<th scope="row">全体設定</th> <th scope="row">全体設定</th>
<td> <td>
<dl class="apop_setting_list"> <dl class="apop_setting_list">
<dt>1ページ表示件数</dt> <dt>1ページ表示件数</dt>
<dd><?php $per_page_data = APOP_UI::create_cat_per_page( $opt_per_page, 'search' ); ?> <dd><?php $per_page_data = APOP_UI::create_cat_per_page( $opt_per_page, 'search' ); ?>
<ul> <ul>
<li> <li>
<label> <label>
<input class="per_page_search" type="radio" name="_apop_per_page[search]" <input class="per_page_search" type="radio" name="_apop_per_page[search]"
value="default"<?php checked( $per_page_data['_per_page'], 'default' ); ?>>表示設定に従う(<?php echo $default_per_page; ?> value="default"<?php checked( $per_page_data['_per_page'], 'default' ); ?>>表示設定に従う(<?php echo $default_per_page; ?>
件)</label> 件)</label>
</li> </li>
<li><label> <li><label>
<input class="per_page_search" type="radio" name="_apop_per_page[search]" <input class="per_page_search" type="radio" name="_apop_per_page[search]"
value="-1"<?php checked( $per_page_data['_per_page'], '-1' ); ?>>全件</label> value="-1"<?php checked( $per_page_data['_per_page'], '-1' ); ?>>全件</label>
</li> </li>
<li> <li>
<label><input class="per_page_search" type="radio" <label><input class="per_page_search" type="radio"
name="_apop_per_page[search]" name="_apop_per_page[search]"
value=""<?php echo $per_page_data['_checked']; ?>>表示数設定 value=""<?php echo $per_page_data['_checked']; ?>>表示数設定
<input class="per_page_search_input" type="text" <input class="per_page_search_input" type="text"
name="_apop_per_page[search]" name="_apop_per_page[search]"
value="<?php echo $per_page_data['_per_page_num']; ?>" required> value="<?php echo $per_page_data['_per_page_num']; ?>" required>
</label> </label>
</li> </li>
</ul> </ul>
</dd> </dd>
</dl> </dl>
</td> </td>
</tr> </tr>
<?php APOP_UI::disp_tax_setting( 'category', 'カテゴリー', '_apop_cat_order' ); ?> <?php APOP_UI::disp_tax_setting( 'category', 'カテゴリー', '_apop_cat_order' ); ?>
<?php APOP_UI::disp_tax_setting( 'post_tag', 'タグ', '_apop_tag_order' ); ?> <?php APOP_UI::disp_tax_setting( 'post_tag', 'タグ', '_apop_tag_order' ); ?>
<?php APOP_UI::disp_tax_setting( 'taxonomy', 'カスタム分類', '_apop_tax_order' ); ?> <?php APOP_UI::disp_tax_setting( 'taxonomy', 'カスタム分類', '_apop_tax_order' ); ?>
</table> </table>
<p class="submit"><input type="submit" name="Submit" class="button-primary" value="変更を保存"/> <p class="submit"><input type="submit" name="Submit" class="button-primary" value="変更を保存"/>
</form> </form>
</div> </div>
+21
View File
@@ -0,0 +1,21 @@
<div class="order_setting_custom_field_box">
<?php if ( isset( $this->order_field ) && count( $this->order_field ) > 0 ): ?>
<dl class="apop_setting_list_dd">
<?php foreach ( $this->order_field as $type => $items ): ?>
<dt><?php echo $this->labels[ $type ]; ?></dt>
<dd>
<ul>
<?php foreach ( $items as $item ): ?>
<?php if ( ! empty( $item ) ): ?>
<li><label><?php echo $item; ?></label>
<input type="text" name="<?php echo $item; ?>"
value="<?php echo $this->get_custom_field_data( $item ); ?>"/>
</li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
</dd>
<?php endforeach; ?>
</dl>
<?php endif; ?>
</div>