WP PLUGIN アーカイブページの投稿表示順設定

・不要な初期化を削除
・カスタムタクソノミー対応
・設定画面のタブ化
・設定画面各タクソノミーの投稿リスト表示をメソッド化
・name属性の閉じ忘れ修正
・不要なreturn削除
・APOP_UI::create_product_none_order_list() returnに変数が未設定の場合の条件を追加
・readmeに若干加筆
This commit is contained in:
2021-05-05 17:48:32 +09:00
parent 7722f53f10
commit 05b5fa91aa
12 changed files with 280 additions and 173 deletions
-8
View File
@@ -1,8 +0,0 @@
# デフォルトの無視ファイル
/shelf/
/workspace.xml
# データソースのローカルストレージがファイルを無視しました
/dataSources/
/dataSources.local.xml
# エディターベースの HTTP クライアントリクエスト
/httpRequests/
+1 -1
View File
@@ -2,7 +2,7 @@
<project version="4"> <project version="4">
<component name="ProjectModuleManager"> <component name="ProjectModuleManager">
<modules> <modules>
<module fileurl="file://$PROJECT_DIR$/.idea/nb-post-sort-cat-order.iml" filepath="$PROJECT_DIR$/.idea/nb-post-sort-cat-order.iml" /> <module fileurl="file://$PROJECT_DIR$/.idea/archive-post-oder-plus.iml" filepath="$PROJECT_DIR$/.idea/archive-post-oder-plus.iml" />
</modules> </modules>
</component> </component>
</project> </project>
-8
View File
@@ -1,8 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
-4
View File
@@ -22,10 +22,6 @@ License: GPLv2
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/ */
//初期化
require_once __DIR__ . '/class/class.apop.activate.php';
register_activation_hook( __FILE__, array( 'apop_activate', 'add_sort_posts' ) );
//メイン処理のクラスをインスタンス化 //メイン処理のクラスをインスタンス化
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.ui.php';
-42
View File
@@ -1,42 +0,0 @@
<?php
class apop_activate {
public static function add_sort_posts() {
$categories = get_categories();
foreach ( $categories as $category ) {
$posts = self::get_category_posts( $category->term_id );
foreach ( $posts as $post ) {
self::create_sort_field( $post->ID, $category->term_id );
}
}
}
//カテゴリーの投稿を取得する
public static function get_category_posts( $cat_id ) {
$args = array(
'post_status' => array( 'publish', 'draft' ),
'posts_per_page' => - 1,
'category' => $cat_id,
);
return get_posts( $args );
}
/**
* ソート用のカスタムフィールド作成
*
* @param $post_id
* @param $cat_id
*/
public static function create_sort_field( $post_id, $cat_id ) {
$sort_key = '_apop_post_sort_' . $cat_id;
//カスタムフィールドが存在しなければ追加する
$sort_filed = get_post_meta( $post_id, $sort_key, true );
if ( empty( $sort_filed ) ) {
update_post_meta( $post_id, $sort_key, 0 );
}
}
}
+16 -8
View File
@@ -1,8 +1,4 @@
<?php <?php
/**
* TODO:設定対象にカスタムタクソノミーの投稿リストを作成する
*/
if ( ! defined( 'ABSPATH' ) ) { if ( ! defined( 'ABSPATH' ) ) {
exit; exit;
} // Exit if accessed directly } // Exit if accessed directly
@@ -64,15 +60,27 @@ if ( ! class_exists( 'APOP' ) ) {
public function show_option_page() { public function show_option_page() {
//カテゴリーの投稿表示順設定
if ( isset( $_POST['_apop_post_category'] ) || isset( $_POST['_apop_post_post_tag'] ) ) { if ( isset( $_POST['_apop_post_category'] ) ) {
check_admin_referer( 'sh_options' ); check_admin_referer( 'sh_options' );
//投稿表示順の設定
$this->update_post_sort( 'category' ); $this->update_post_sort( 'category' );
require_once self::TEMPLATE_DIR . 'success.php';
}
//タグの投稿表示順設定
if ( isset( $_POST['_apop_post_post_tag'] ) ) {
check_admin_referer( 'sh_options' );
$this->update_post_sort( 'post_tag' ); $this->update_post_sort( 'post_tag' );
require_once self::TEMPLATE_DIR . 'success.php'; require_once self::TEMPLATE_DIR . 'success.php';
} }
//カスタム分類の投稿表示順設定
if ( isset( $_POST['_apop_post_tax'] ) ) {
check_admin_referer( 'sh_options' );
$this->update_post_sort( 'tax' );
require_once self::TEMPLATE_DIR . 'success.php';
}
require_once self::TEMPLATE_DIR . 'order.php'; require_once self::TEMPLATE_DIR . 'order.php';
} }
@@ -88,7 +96,7 @@ if ( ! class_exists( 'APOP' ) ) {
} }
} }
} }
} }
+126 -31
View File
@@ -3,71 +3,166 @@
class APOP_UI { class APOP_UI {
public static function get_all_taxonomies( $key ) { public static function get_all_taxonomies( $key ) {
if($key !== 'taxonomy'){ if ( $key !== 'taxonomy' ) {
return get_terms( array( 'taxonomy' => $key, 'get' => 'all' ) ); return get_terms( array( 'taxonomy' => $key, 'get' => 'all' ) );
} }
return get_taxonomies( array( 'public' => true, '_builtin' => false ) ); return get_taxonomies( array( 'public' => true, '_builtin' => false ) );
} }
public static function get_tax_name( $tax_key ): string { public static function get_cat_tag_list( $target, $key ): array {
$tax_names = array( $opt = get_option( '_apop_' . $target . '_order' );
'category' => 'カテゴリー',
'tag_id' => 'タグ',
'custom_tax' => 'カスタム分類',
);
return $tax_names[ $tax_key ]; if ( ! isset( $opt['target_cat'] ) ) {
return array();
}
if ( $key == 'taxonomy' ) {
return self::create_custom_tax_term( $opt );
} else {
return self::create_tax_term( $opt, $key );
}
} }
public static function get_cat_tag_list( $target, $key ): array { private static function create_custom_tax_term( $opt ): array {
$opt = get_option( '_apop_' . $target . '_order' ); $tax_data = [];
$include = array(); foreach ( $opt['target_cat'] as $tax_id => $status ) {
if ( isset( $opt['target_cat'] ) ) { //フラグが立っているカスタムタクソノミーは情報を取得する
foreach ( $opt['target_cat'] as $tax_id => $check ) { if ( $status ) {
if ( $check ) { $args = array(
$include[] = $tax_id; 'taxonomy' => get_term( $tax_id )->taxonomy,
}
}
$include_tax = implode( ',', $include );
if ( ! empty( $include_tax ) ) {
$args = array(
'taxonomy' => $key,
'hide_empty' => 0, 'hide_empty' => 0,
'include' => $include_tax, 'include' => $tax_id,
); );
$tax_data[] = get_terms( $args );
return get_terms( $args );
} }
} }
return $tax_data;
}
private static function create_tax_term( $opt, $key ) {
$include = array();
foreach ( $opt['target_cat'] as $tax_id => $status ) {
//フラグが立っているタクソノミーIDを取得
if ( $status ) {
$include[] = $tax_id;
}
}
$include_tax = implode( ',', $include );
if ( ! empty( $include_tax ) ) {
$args = array(
'taxonomy' => $key,
'hide_empty' => 0,
'include' => $include_tax,
);
return get_terms( $args );
}
return array(); return array();
} }
public static function get_sort_post_list( $tax_id, $search_param, $tax_key ) { public static function create_order_list( $tax_data, $tax_key, $exclude_posts ): array {
$target_posts = self::get_sort_post_list( $tax_data->term_id, $tax_key, $tax_data->taxonomy );
$list = [];
$sort_num = 0;
foreach ( $target_posts as $key => $target_post ) {
$sort_num = $key + 1;
$exclude_posts[] = $target_post->ID;
$list[] = '
<li class="product-list"><span class="sort-num-label">' . $sort_num . '</span>' . get_the_title( $target_post->ID ) . '
<input type="hidden" class="list_order"
name="_apop_post_' . self::create_post_sort_key( $tax_data->taxonomy, $tax_key ) . '[post_sort][' . $tax_data->term_id . '][' . $target_post->ID . ']"
value="' . $sort_num . '">
</li>';
}
return array(
implode( PHP_EOL, $list ),
$exclude_posts,
$sort_num,
);
}
private static function get_sort_post_list( $tax_id, $search_param, $tax_name ) {
$args = [ $args = [
'post_status' => array( 'publish', 'draft' ), 'post_status' => array( 'publish', 'draft' ),
'posts_per_page' => - 1, 'posts_per_page' => - 1,
$search_param => $tax_id,
'orderby' => 'meta_value_num', 'orderby' => 'meta_value_num',
'order' => 'ASC', 'order' => 'ASC',
'meta_key' => '_apop_post_' . $tax_key . '_' . $tax_id, 'meta_key' => '_apop_post_' . self::create_post_sort_key( $tax_name, $search_param ) . '_' . $tax_id,
]; ];
self::create_post_tax_query( $args, $search_param, $tax_name, $tax_id );
return get_posts( $args ); return get_posts( $args );
} }
public static function get_none_sort_post_list( $tax_id, $key, $exclude_posts ) { private static function create_post_sort_key( $tax_name, $tax_key ) {
$exclude = implode( ',', $exclude_posts ); if ( $tax_key == 'taxonomy' ) {
$args = [ return 'tax';
}
return $tax_name;
}
private static function create_post_tax_query( &$args, $key, $tax_name, $tax_id ) {
if ( $key == 'taxonomy' ) {
$args['tax_query'] = array(
array(
'taxonomy' => $tax_name,
'field' => 'term_id',
'terms' => $tax_id,
'include_children' => false
)
);
} else {
$args[ $key ] = $tax_id;
}
}
public static function create_product_none_order_list( $tax_data, $tax_key, $exclude_posts, $sort_num ): string {
$target_posts_no_order = self::get_none_sort_post_list( $tax_data->term_id, $tax_key, $exclude_posts, $tax_data->taxonomy );
if ( count( $target_posts_no_order ) > 0 ) {
$sort_num = $sort_num ?? 0;
foreach ( $target_posts_no_order as $key => $target_post_no_order ) {
$sort_num_no_order = $sort_num + $key + 1;
$list[] = '
<li class="product-list no_order">
<span class="sort-num-label">' . $sort_num_no_order . '</span>' . get_the_title( $target_post_no_order->ID ) . '
<input type="hidden" class="list_order"
name="_apop_post_' . self::create_post_sort_key( $tax_data->taxonomy, $tax_key ) . '[post_sort][' . $tax_data->term_id . '][' . $target_post_no_order->ID . ']"
value="' . $sort_num_no_order . '">
</li>';
}
if ( isset( $list ) ) {
return implode( PHP_EOL, $list );
}
}
return '';
}
private static function get_none_sort_post_list( $tax_id, $search_param, $exclude_posts, $tax_name ) {
$args = array(
'post_status' => array( 'publish', 'draft' ), 'post_status' => array( 'publish', 'draft' ),
'posts_per_page' => - 1, 'posts_per_page' => - 1,
$key => $tax_id, );
];
$exclude = implode( ',', $exclude_posts );
if ( ! empty( $exclude ) ) { if ( ! empty( $exclude ) ) {
$args['exclude'] = $exclude; $args['exclude'] = $exclude;
} }
self::create_post_tax_query( $args, $search_param, $tax_name, $tax_id );
return get_posts( $args ); return get_posts( $args );
} }
} }
+35 -20
View File
@@ -5,32 +5,35 @@
margin-bottom: 0; margin-bottom: 0;
} }
.post-order-box {
width: 95%;
margin: 2em 0;
}
.post-order-nav li { .post-order-nav li {
width: 20em; width: 20em;
margin-right: .5em;
margin-bottom: 0;
padding: .5em; padding: .5em;
margin-bottom: -1px;
font-size: 18px;
text-align: center; text-align: center;
background: #2c2c2e; background: #ababab;
color: #fff; border: 1px solid #999;
cursor: pointer; cursor: pointer;
border-top-right-radius: 6px;
border-top-left-radius: 6px;
opacity: .5;
} }
.post-order-nav li.en { .post-order-nav li.en {
background: #fff;
border-bottom: 1px solid #fff;
opacity: 1; opacity: 1;
} }
.post-order-nav li:first-child { .post-order-nav li:last-child {
margin-right: .3em; margin-right: 0;
}
.post-order-box-outer {
width: 95%;
margin: -1px 0 2em;
border-top: 1px solid #999;
}
.post-order-box:not(:first-child) {
display: none;
} }
h3 { h3 {
@@ -47,10 +50,17 @@ h3 {
} }
.list-orders-inner { .list-orders-inner {
width: calc((100% - 10px) / 3); width: calc((100% - 10px) / 5);
border: 1px solid #999; margin-right: .5em;
background: #fff;
padding: .5em; padding: .5em;
border: 1px solid #999;
position: relative;
}
p.submit.post-order .button-primary {
position: absolute;
bottom: 1em;
right: 1.5em;
} }
.post-order-list { .post-order-list {
@@ -60,9 +70,14 @@ h3 {
.product-list { .product-list {
padding: .5em; padding: .5em;
border-radius: 4px; border-radius: 4px;
background: #2271B1; background: #fff;
cursor: move; cursor: move;
color: #fff; color: #4b4b4b;
border: 1px solid #ababab;
}
.product-list.no_order {
background: #ababab;
} }
.list_order { .list_order {
+10
View File
@@ -18,5 +18,15 @@ jQuery(function ($) {
my_list.disableSelection(); my_list.disableSelection();
} }
$('.post-order-nav li').click(function () {
if (!$(this).hasClass('en')) {
let target_index = $(this).index();
$('.post-order-nav li').removeClass('en');
$(this).addClass('en');
$('.post-order-box').hide();
$('.post-order-box:eq(' + target_index + ')').show();
}
});
}); });
+4 -4
View File
@@ -12,14 +12,14 @@ License URI: https://www.gnu.org/licenses/gpl-2.0.html
== Description == == Description ==
このプラグインはアーカイブページの投稿表示順をタクソノミー毎に設定するプラグインです。 このプラグインはタクソノミー毎に投稿表示順を設定するプラグインです。
= 仕様 = = 仕様 =
投稿表示順をカスタマイズしたいタクソノミーを選択します。 投稿表示順をカスタマイズしたいタクソノミーを選択します。
選択した各タクソノミー内の投稿位置をドラッグ&ドロップで変え、保存することでタクソノミー毎の表示順が変更できます。 選択した各タクソノミー内の投稿をドラッグ&ドロップで変更し保存します。
専用コード】 サブクエリコード】
表示したい箇所のPHPファイルに下記のコードを記述することで表示順を変更することが可能です。 get_posts()などを利用する場合、下記のコードに並び順のパラメータを取得できます。
<pre><code> <pre><code>
<?php <?php
?> ?>
+83 -42
View File
@@ -1,53 +1,94 @@
<?php <?php
$order_list = array( $category_lists = array( 'category' => APOP_UI::get_cat_tag_list( 'cat', 'category' ) );
'category' => APOP_UI::get_cat_tag_list( 'cat', 'category' ), $tag_lists = array( 'tag_id' => APOP_UI::get_cat_tag_list( 'tag', 'post_tag' ) );
'tag_id' => APOP_UI::get_cat_tag_list( 'tag', 'post_tag' ), $taxonomy_lists = array( 'taxonomy' => APOP_UI::get_cat_tag_list( 'tax', 'taxonomy' ) );
);
?> ?>
<div class="post-order-box"> <nav class="post-order-nav">
<form action="" method="post"> <ul>
<?php wp_nonce_field( 'sh_options' ); ?> <li class="en">カテゴリー</li>
<?php foreach ( $order_list as $tax_key => $tax_list ) : ?> <li>タグ</li>
<li>カスタム分類</li>
</ul>
</nav>
<div class="post-order-box-outer">
<div class="post-order-box">
<h2>カテゴリー</h2>
<?php foreach ( $category_lists as $tax_key => $tax_list ) : ?>
<div class="list-orders-outer"> <div class="list-orders-outer">
<?php if ( count( $tax_list ) == 0 ): ?> <?php if ( count( $tax_list ) > 0 ): $exclude_posts = []; ?>
<p>対象<?php echo APOP_UI::get_tax_name($tax_key); ?>を選択してください。
<a href="./admin.php?page=apop_post_sort_setting"><?php echo APOP_UI::get_tax_name($tax_key); ?>選択</a>
</p>
<?php else: $exclude_posts = []; ?>
<?php foreach ( $tax_list as $tax_data ): ?> <?php foreach ( $tax_list as $tax_data ): ?>
<div class="list-orders-inner"> <div class="list-orders-inner">
<h3><?php echo $tax_data->name; ?></h3> <form action="" method="post">
<?php $target_posts = APOP_UI::get_sort_post_list( $tax_data->term_id, $tax_key, $tax_data->taxonomy ); ?> <?php wp_nonce_field( 'sh_options' ); ?>
<ul class="post-order-list"> <h3><?php echo $tax_data->name; ?></h3>
<?php foreach ( $target_posts as $key => $target_post ): $sort_num = $key + 1; <ul class="post-order-list">
$exclude_posts[] = $target_post->ID; ?> <?php
<li class="product-list"> list( $product_order_list, $exclude_posts, $sort_num ) = APOP_UI::create_order_list( $tax_data, $tax_key, $exclude_posts );
<span class="sort-num-label"><?php echo $sort_num; ?></span> echo $product_order_list;
<?php echo get_the_title( $target_post->ID ); ?> echo APOP_UI::create_product_none_order_list( $tax_data, $tax_key, $exclude_posts, $sort_num );
<input type="hidden" class="list_order" ?>
name="_apop_post_<?php echo $tax_data->taxonomy; ?>[post_sort][<?php echo $tax_data->term_id; ?>][<?php echo $target_post->ID; ?>]" </ul>
value="<?php echo $sort_num; ?>"> <p class="submit post-order"><input type="submit" name="Submit" class="button-primary"
</li> value="変更を保存"/></p>
<?php endforeach; ?> </form>
<?php $target_posts_no_order = APOP_UI::get_none_sort_post_list( $tax_data->term_id, $tax_key, $exclude_posts ); ?>
<?php if ( count( $target_posts_no_order ) > 0 ): ?>
<?php $sort_num = $sort_num ?? 0; ?>
<?php foreach ( $target_posts_no_order as $key => $target_post_no_order ): $sort_num_no_order = $sort_num + $key + 1; ?>
<li class="product-list">
<span class="sort-num-label"><?php echo $sort_num_no_order; ?></span>
<?php echo get_the_title( $target_post_no_order->ID ); ?>
<input type="hidden" class="list_order"
name="_apop_post_<?php echo $tax_data->taxonomy; ?>[post_sort][<?php echo $tax_data->term_id; ?>][<?php echo $target_post_no_order->ID; ?>]"
value="<?php echo $sort_num_no_order; ?>">
</li>
<?php endforeach; ?>
<?php endif; ?>
</ul>
</div> </div>
<?php endforeach; ?> <?php endforeach; ?>
<?php endif; ?> <?php endif; ?>
</div> </div>
<?php endforeach; ?> <?php endforeach; ?>
<p class="submit"><input type="submit" name="Submit" class="button-primary" value="変更を保存"/></p> </div>
</form> <div class="post-order-box">
<h2>タグ</h2>
<?php foreach ( $tag_lists as $tax_key => $tax_list ) : ?>
<div class="list-orders-outer">
<?php if ( count( $tax_list ) > 0 ): $exclude_posts = []; ?>
<?php foreach ( $tax_list as $tax_data ): ?>
<div class="list-orders-inner">
<form action="" method="post">
<?php wp_nonce_field( 'sh_options' ); ?>
<h3><?php echo $tax_data->name; ?></h3>
<ul class="post-order-list">
<?php
list( $product_order_list, $exclude_posts, $sort_num ) = APOP_UI::create_order_list( $tax_data, $tax_key, $exclude_posts );
echo $product_order_list;
echo APOP_UI::create_product_none_order_list( $tax_data, $tax_key, $exclude_posts, $sort_num );
?>
</ul>
<p class="submit post-order"><input type="submit" name="Submit" class="button-primary"
value="変更を保存"/></p>
</form>
</div>
<?php endforeach; ?>
<?php endif; ?>
</div>
<?php endforeach; ?>
</div>
<div class="post-order-box">
<h2>カスタム分類</h2>
<?php foreach ( $taxonomy_lists as $tax_key => $taxonomy_list ): ?>
<div class="list-orders-outer">
<?php foreach ( $taxonomy_list as $tax_list ) : ?>
<?php if ( count( $taxonomy_list ) > 0 ): $exclude_posts = []; ?>
<?php foreach ( $tax_list as $tax_data ): ?>
<div class="list-orders-inner">
<form action="" method="post">
<?php wp_nonce_field( 'sh_options' ); ?>
<h3><?php echo $tax_data->name; ?></h3>
<ul class="post-order-list">
<?php
list( $product_order_list, $exclude_posts, $sort_num ) = APOP_UI::create_order_list( $tax_data, $tax_key, $exclude_posts );
echo $product_order_list;
echo APOP_UI::create_product_none_order_list( $tax_data, $tax_key, $exclude_posts, $sort_num );
?>
</ul>
<p class="submit post-order"><input type="submit" name="Submit" class="button-primary"
value="変更を保存"/></p>
</form>
</div>
<?php endforeach; ?>
<?php endif; ?>
<?php endforeach; ?>
</div>
<?php endforeach; ?>
</div>
</div> </div>
+5 -5
View File
@@ -4,7 +4,7 @@ $all_tags = APOP_UI::get_all_taxonomies( 'post_tag' );
$all_tax = APOP_UI::get_all_taxonomies( 'taxonomy' ); $all_tax = APOP_UI::get_all_taxonomies( 'taxonomy' );
?> ?>
<div class="post-order-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' );
@@ -13,10 +13,10 @@ $all_tax = APOP_UI::get_all_taxonomies( 'taxonomy' );
$opt_tax = get_option( '_apop_tax_order' ); $opt_tax = get_option( '_apop_tax_order' );
?> ?>
<h2>対象タクソノミー選択</h2> <h2>対象タクソノミー選択</h2>
<table> <table class="form-table">
<?php if ( count( $all_categories ) > 0 ): ?> <?php if ( count( $all_categories ) > 0 ): ?>
<tr> <tr>
<th>カテゴリー</th> <th scope="row">カテゴリー</th>
<td> <td>
<ul class="order_setting_list"> <ul class="order_setting_list">
<?php foreach ( $all_categories as $category ): ?> <?php foreach ( $all_categories as $category ): ?>
@@ -42,7 +42,7 @@ $all_tax = APOP_UI::get_all_taxonomies( 'taxonomy' );
<?php endif; ?> <?php endif; ?>
<?php if ( count( $all_tags ) > 0 ): ?> <?php if ( count( $all_tags ) > 0 ): ?>
<tr> <tr>
<th>タグ</th> <th scope="row">タグ</th>
<td> <td>
<ul class="order_setting_list"> <ul class="order_setting_list">
<?php foreach ( $all_tags as $tag ): ?> <?php foreach ( $all_tags as $tag ): ?>
@@ -68,7 +68,7 @@ $all_tax = APOP_UI::get_all_taxonomies( 'taxonomy' );
<?php endif; ?> <?php endif; ?>
<?php if ( count( $all_tax ) > 0 ): ?> <?php if ( count( $all_tax ) > 0 ): ?>
<tr> <tr>
<th>カスタム分類</th> <th scope="row">カスタム分類</th>
<td> <td>
<ul class="order_setting_list"> <ul class="order_setting_list">
<?php foreach ( $all_tax as $tax ): $all_taxs = APOP_UI::get_all_taxonomies( $tax ) ?> <?php foreach ( $all_tax as $tax ): $all_taxs = APOP_UI::get_all_taxonomies( $tax ) ?>