WP PLUGIN アーカイブ毎に投稿表示順を設定する

・ファイル名、クラス名の変更
・タクソノミーの有効化で、カテゴリー、タグ、カスタム分類のメタキーを分割
・表示用クラスの追加
・readmeの追加等
This commit is contained in:
2021-05-04 15:57:30 +09:00
parent 2ed2b25194
commit 7722f53f10
12 changed files with 409 additions and 211 deletions
+41
View File
@@ -0,0 +1,41 @@
<?php
/*
Plugin Name: Archive Post Order Plus
Plugin URI: https://www.n-k-y.net/wp_plugin_apop/
Author: Nobuhiro Kimura
Author URI: https://www.n-k-y.net
Description: アーカイブのタクソノミー毎に投稿の表示順を設定するプラグイン
Version: 1.0.0
License: GPLv2
*/
/* Copyright 2021 Nobuhiro Kimura (email : big-me@n-k-y.net)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
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.ui.php';
$APOP = new APOP;
//CSS, JSの読み込み
add_action( 'admin_enqueue_scripts', 'register_my_styles' );
function register_my_styles() {
$plugin_url = plugin_dir_url( __FILE__ );
wp_enqueue_style( 'hrc_post_style', $plugin_url . 'css/apop-style.css' );
wp_enqueue_script( 'jquery-ui-sortable' );
wp_enqueue_script( 'post-sort-cat-order_js', $plugin_url . 'js/apop-style.js' );
}
@@ -1,6 +1,6 @@
<?php <?php
class activate { class apop_activate {
public static function add_sort_posts() { public static function add_sort_posts() {
@@ -16,7 +16,6 @@ class activate {
//カテゴリーの投稿を取得する //カテゴリーの投稿を取得する
public static function get_category_posts( $cat_id ) { public static function get_category_posts( $cat_id ) {
$args = array( $args = array(
'post_type' => 'post',
'post_status' => array( 'publish', 'draft' ), 'post_status' => array( 'publish', 'draft' ),
'posts_per_page' => - 1, 'posts_per_page' => - 1,
'category' => $cat_id, 'category' => $cat_id,
@@ -32,7 +31,7 @@ class activate {
* @param $cat_id * @param $cat_id
*/ */
public static function create_sort_field( $post_id, $cat_id ) { public static function create_sort_field( $post_id, $cat_id ) {
$sort_key = 'nb_post_sort_' . $cat_id; $sort_key = '_apop_post_sort_' . $cat_id;
//カスタムフィールドが存在しなければ追加する //カスタムフィールドが存在しなければ追加する
$sort_filed = get_post_meta( $post_id, $sort_key, true ); $sort_filed = get_post_meta( $post_id, $sort_key, true );
if ( empty( $sort_filed ) ) { if ( empty( $sort_filed ) ) {
+94
View File
@@ -0,0 +1,94 @@
<?php
/**
* TODO:設定対象にカスタムタクソノミーの投稿リストを作成する
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
} // Exit if accessed directly
if ( ! class_exists( 'APOP' ) ) {
class APOP {
const TEMPLATE_DIR = __DIR__ . '/../template/';
public function __construct() {
add_action( 'admin_menu', array( $this, 'add_pages' ) );
}
public function add_pages() {
add_menu_page(
'APO +',
'APO +',
'level_8',
'apop_post_sort',
array( $this, 'show_option_page' ),
'',
50
);
add_submenu_page(
'apop_post_sort', // parent_slug
'Select taxonomy', // page_title
'タクソノミー選択', // menu_title
'administrator', // capability
'apop_post_sort_setting', // menu_slug
array( $this, 'display_setting_page' ) // function
);
}
public function display_setting_page() {
if ( isset( $_POST['_apop_cat_order'] )
|| isset( $_POST['_apop_tag_order'] )
|| isset( $_POST['_apop_tax_order'] ) ) {
check_admin_referer( 'sh_options' );
$settings = array(
'_apop_cat_order', //カテゴリー設定
'_apop_tag_order', //タグ設定
'_apop_tax_order', //カスタムタクソノミー設定
);
foreach ( $settings as $setting ) {
$opt = $_POST[ $setting ];
update_option( $setting, $opt );
}
require_once self::TEMPLATE_DIR . 'success.php';
}
require_once self::TEMPLATE_DIR . 'setting.php';
}
public function show_option_page() {
if ( isset( $_POST['_apop_post_category'] ) || isset( $_POST['_apop_post_post_tag'] ) ) {
check_admin_referer( 'sh_options' );
//投稿表示順の設定
$this->update_post_sort( 'category' );
$this->update_post_sort( 'post_tag' );
require_once self::TEMPLATE_DIR . 'success.php';
}
require_once self::TEMPLATE_DIR . 'order.php';
}
//投稿表示順の設定
private function update_post_sort( $target ) {
$target = '_apop_post_' . $target;
$posts_sort = $_POST[ $target ]['post_sort'];
foreach ( $posts_sort as $cat_id => $posts ) {
$sort_key = $target . '_' . $cat_id;
foreach ( $posts as $post_id => $sort ) {
update_post_meta( $post_id, $sort_key, $sort );
}
}
}
}
}
+73
View File
@@ -0,0 +1,73 @@
<?php
class APOP_UI {
public static function get_all_taxonomies( $key ) {
if($key !== 'taxonomy'){
return get_terms( array( 'taxonomy' => $key, 'get' => 'all' ) );
}
return get_taxonomies( array( 'public' => true, '_builtin' => false ) );
}
public static function get_tax_name( $tax_key ): string {
$tax_names = array(
'category' => 'カテゴリー',
'tag_id' => 'タグ',
'custom_tax' => 'カスタム分類',
);
return $tax_names[ $tax_key ];
}
public static function get_cat_tag_list( $target, $key ): array {
$opt = get_option( '_apop_' . $target . '_order' );
$include = array();
if ( isset( $opt['target_cat'] ) ) {
foreach ( $opt['target_cat'] as $tax_id => $check ) {
if ( $check ) {
$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();
}
public static function get_sort_post_list( $tax_id, $search_param, $tax_key ) {
$args = [
'post_status' => array( 'publish', 'draft' ),
'posts_per_page' => - 1,
$search_param => $tax_id,
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_key' => '_apop_post_' . $tax_key . '_' . $tax_id,
];
return get_posts( $args );
}
public static function get_none_sort_post_list( $tax_id, $key, $exclude_posts ) {
$exclude = implode( ',', $exclude_posts );
$args = [
'post_status' => array( 'publish', 'draft' ),
'posts_per_page' => - 1,
$key => $tax_id,
];
if ( ! empty( $exclude ) ) {
$args['exclude'] = $exclude;
}
return get_posts( $args );
}
}
+1
View File
@@ -40,6 +40,7 @@ h3 {
.list-orders-outer { .list-orders-outer {
width: 100%; width: 100%;
margin-bottom: 1em;
display: flex; display: flex;
justify-content: flex-start; justify-content: flex-start;
flex-wrap: wrap; flex-wrap: wrap;
-125
View File
@@ -1,125 +0,0 @@
<?php
/**
* TODO:設定対象にカスタムタクソノミー、カスタム投稿を含める
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
} // Exit if accessed directly
if ( ! class_exists( 'post_sort_cat_order' ) ) {
class post_sort_cat_order {
const TEMPLATE_DIR = __DIR__ . '/../template/';
public function __construct() {
add_action( 'admin_menu', array( $this, 'add_pages' ) );
// add_action( 'transition_post_status', array( $this, 'add_cat_post_order' ), 10, 3 );
}
public function add_pages() {
add_menu_page(
'PSBC',
'PSBC',
'level_8',
'nb_post_sort',
array( $this, 'show_option_page' ),
'',
50
);
add_submenu_page(
'nb_post_sort', // parent_slug
'Archive Setting', // page_title
'カテゴリー選択', // menu_title
'administrator', // capability
'nb_post_sort_setting', // menu_slug
array( $this, 'display_setting_page' ) // function
);
}
public function display_setting_page() {
if ( isset( $_POST['post_cat_order'] ) ) {
check_admin_referer( 'sh_options' );
$opt = $_POST['post_cat_order'];
//ソート対象カテゴリーの登録
update_option( 'post_cat_order', $opt );
require_once self::TEMPLATE_DIR . 'success.php';
}
require_once self::TEMPLATE_DIR . 'setting.php';
}
public function show_option_page() {
if ( isset( $_POST['nb_post_cat'] ) ) {
check_admin_referer( 'sh_options' );
//投稿表示順の設定
$this->update_post_sort();
require_once self::TEMPLATE_DIR . 'success.php';
}
require_once self::TEMPLATE_DIR . 'sort.php';
}
//投稿表示順の設定
private function update_post_sort() {
$posts_sort = $_POST['nb_post_cat']['post_sort'];
foreach ( $posts_sort as $cat_id => $posts ) {
$sort_key = 'nb_post_sort_' . $cat_id;
foreach ( $posts as $post_id => $sort ) {
update_post_meta( $post_id, $sort_key, $sort );
}
}
}
/**
* 対象カテゴリー
*
* @param $opt
*
* @return array
*/
public static function get_category_list( $opt ): array {
if ( isset( $opt['target_cat'] ) ) {
$include_cat = implode( ',', $opt['target_cat'] );
if ( ! empty( $include_cat ) ) {
$category_args = array(
'hide_empty' => 0,
'include' => $include_cat,
);
return get_categories( $category_args );
}
}
return array();
}
// public function add_cat_post_order( $new_status, $old_status, $post ) {
// //リビジョンはパスする
// if ( wp_is_post_revision( $post->ID ) ) {
// return;
// }
//
// }
public static function get_sort_post_list( $cat_id ) {
$product_args = [
'post_type' => 'post',
'post_status' => array( 'publish', 'draft' ),
'posts_per_page' => - 1,
'category' => $cat_id,
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_key' => 'nb_post_sort_' . $cat_id,
];
return get_posts( $product_args );
}
}
}
-27
View File
@@ -1,27 +0,0 @@
<?php
/*
Plugin Name: Post Sort By Categories
Plugin URI:
Author: Nob Kim
Description: カテゴリー毎に投稿の表示順を設定するプラグイン
Version: 1.0.0
License: GPLv2
*/
//初期化
require_once __DIR__ . '/lib/activate.php';
register_activation_hook( __FILE__, array( 'activate', 'add_sort_posts' ) );
//メイン処理のクラスをインスタンス化
require_once __DIR__ . '/lib/post-sort-cat-order.php';
$post_sort_cat_order = new post_sort_cat_order;
//CSS, JSの読み込み
add_action( 'admin_enqueue_scripts', 'register_my_styles' );
function register_my_styles() {
$plugin_url = plugin_dir_url( __FILE__ );
wp_enqueue_style( 'hrc_post_style', $plugin_url . 'css/base.css' );
wp_enqueue_script( 'jquery-ui-sortable' );
wp_enqueue_script( 'post-sort-cat-order_js', $plugin_url . 'js/post-sort-cat-order.js' );
}
+56
View File
@@ -0,0 +1,56 @@
=== Archive POst Order Plus ===
Contributors: nbk45
Tags: 投稿,表示順,投稿表示順,カテゴリー,タグ,カスタム分類,post,archive,category,tag,custom taxonomy,order
Requires at least: 4.9
Tested up to: 5.7
Requires PHP: 7.0
Stable tag: 1.0.0
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html
アーカイブページの投稿表示順をタクソノミー毎に設定するプラグイン。
== Description ==
このプラグインはアーカイブページの投稿表示順をタクソノミー毎に設定するプラグインです。
= 仕様 =
投稿表示順をカスタマイズしたいタクソノミーを選択します。
選択した各タクソノミー内の投稿位置をドラッグ&ドロップで変え、保存することでタクソノミー毎の表示順が変更できます。
【専用コード】
表示したい箇所のPHPファイルに下記のコードを記述することで表示順を変更することが可能です。
<pre><code>
<?php
?>
</code></pre>
== Installation ==
= 自動インストール =
1. プラグインの検索フィールドより「Archive Post Order Plus」や「投稿表示順」と入力し、"プラグインの検索"をクリックします。
1. 当プラグインを見つけたら、"今すぐインストール"をクリックしてインストールし、プラグインを有効化してください。
= 手動インストール =
1. プラグインをダウンロードします。
1. プラグインフォルダ内にアップロードし、管理画面よりプラグインを有効化してください。
== Frequently Asked Questions ==
== Screenshots ==
1. /assets/screenshot-1.png
2. /assets/screenshot-2.png
3. /assets/screenshot-3.png
4. /assets/screenshot-4.png
5. /assets/screenshot-5.png
6. /assets/screenshot-6.png
== Changelog ==
= 1.0.0 =
Initial working version.
== Upgrade Notice ==
No information
+53
View File
@@ -0,0 +1,53 @@
<?php
$order_list = array(
'category' => APOP_UI::get_cat_tag_list( 'cat', 'category' ),
'tag_id' => APOP_UI::get_cat_tag_list( 'tag', 'post_tag' ),
);
?>
<div class="post-order-box">
<form action="" method="post">
<?php wp_nonce_field( 'sh_options' ); ?>
<?php foreach ( $order_list as $tax_key => $tax_list ) : ?>
<div class="list-orders-outer">
<?php if ( count( $tax_list ) == 0 ): ?>
<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 ): ?>
<div class="list-orders-inner">
<h3><?php echo $tax_data->name; ?></h3>
<?php $target_posts = APOP_UI::get_sort_post_list( $tax_data->term_id, $tax_key, $tax_data->taxonomy ); ?>
<ul class="post-order-list">
<?php foreach ( $target_posts as $key => $target_post ): $sort_num = $key + 1;
$exclude_posts[] = $target_post->ID; ?>
<li class="product-list">
<span class="sort-num-label"><?php echo $sort_num; ?></span>
<?php echo get_the_title( $target_post->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->ID; ?>]"
value="<?php echo $sort_num; ?>">
</li>
<?php endforeach; ?>
<?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>
<?php endforeach; ?>
<?php endif; ?>
</div>
<?php endforeach; ?>
<p class="submit"><input type="submit" name="Submit" class="button-primary" value="変更を保存"/></p>
</form>
</div>
+89 -22
View File
@@ -1,33 +1,100 @@
<?php <?php
//各種パラメータ $all_categories = APOP_UI::get_all_taxonomies( 'category' );
$all_categories = get_categories(); $all_tags = APOP_UI::get_all_taxonomies( 'post_tag' );
$all_tax = APOP_UI::get_all_taxonomies( 'taxonomy' );
?> ?>
<div class="post-order-box"> <div class="post-order-box">
<form action="" method="post"> <form action="" method="post">
<?php <?php
wp_nonce_field( 'sh_options' ); wp_nonce_field( 'sh_options' );
$opt = get_option( 'post_cat_order' ); $opt_cat = get_option( '_apop_cat_order' );
$opt_tag = get_option( '_apop_tag_order' );
$opt_tax = get_option( '_apop_tax_order' );
?> ?>
<h2>対象カテゴリー選択</h2> <h2>対象タクソノミー選択</h2>
<ul class="order_setting_list"> <table>
<?php foreach ( $all_categories as $category ): ?> <?php if ( count( $all_categories ) > 0 ): ?>
<li> <tr>
<div class="select_cat"> <th>カテゴリー</th>
<label> <td>
<input type="hidden" <ul class="order_setting_list">
name="post_cat_order[target_cat][<?php echo $category->slug; ?>]" <?php foreach ( $all_categories as $category ): ?>
value="0"> <?php $check_slug = $opt_cat['target_cat'][ $category->term_id ] ?? ''; ?>
<input type="checkbox" <li>
name="post_cat_order[target_cat][<?php echo $category->slug; ?>]" <div class="select_cat">
<?php echo checked( $opt['target_cat'][ $category->slug ], 1 ); ?> <label>
value="1"> <input type="hidden"
<?php echo $category->name; ?> name="_apop_cat_order[target_cat][<?php echo $category->term_id; ?>]"
</label> value="0">
</div> <input type="checkbox"
</li> name="_apop_cat_order[target_cat][<?php echo $category->term_id; ?>]"
<?php endforeach; ?> <?php echo checked( $check_slug, 1 ); ?>
</ul> value="1">
<?php echo $category->name; ?>
</label>
</div>
</li>
<?php endforeach; ?>
</ul>
</td>
</tr>
<?php endif; ?>
<?php if ( count( $all_tags ) > 0 ): ?>
<tr>
<th>タグ</th>
<td>
<ul class="order_setting_list">
<?php foreach ( $all_tags as $tag ): ?>
<?php $check_slug = $opt_tag['target_cat'][ $tag->term_id ] ?? ''; ?>
<li>
<div class="select_cat">
<label>
<input type="hidden"
name="_apop_tag_order[target_cat][<?php echo $tag->term_id; ?>]"
value="0">
<input type="checkbox"
name="_apop_tag_order[target_cat][<?php echo $tag->term_id; ?>]"
<?php echo checked( $check_slug, 1 ); ?>
value="1">
<?php echo $tag->name; ?>
</label>
</div>
</li>
<?php endforeach; ?>
</ul>
</td>
</tr>
<?php endif; ?>
<?php if ( count( $all_tax ) > 0 ): ?>
<tr>
<th>カスタム分類</th>
<td>
<ul class="order_setting_list">
<?php foreach ( $all_tax as $tax ): $all_taxs = APOP_UI::get_all_taxonomies( $tax ) ?>
<?php foreach ( $all_taxs as $tax ): ?>
<?php $check_slug = $opt_tax['target_cat'][ $tax->term_id ] ?? ''; ?>
<li>
<div class="select_cat">
<label>
<input type="hidden"
name="_apop_tax_order[target_cat][<?php echo $tax->term_id; ?>]"
value="0">
<input type="checkbox"
name="_apop_tax_order[target_cat][<?php echo $tax->term_id; ?>]"
<?php echo checked( $check_slug, 1 ); ?>
value="1">
<?php echo $tax->name; ?>
</label>
</div>
</li>
<?php endforeach; ?>
<?php endforeach; ?>
</ul>
</td>
</tr>
<?php endif; ?>
</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>
-34
View File
@@ -1,34 +0,0 @@
<?php
$opt = get_option( 'post_cat_order' );
$category_list = post_sort_cat_order::get_category_list( $opt );
?>
<div class="post-order-box">
<form action="" method="post">
<?php wp_nonce_field( 'sh_options' ); ?>
<h2>投稿表示順設定</h2>
<div class="list-orders-outer">
<?php if ( count( $category_list ) == 0 ): ?>
<p>対象カテゴリーを選択してください。[ <a href="./admin.php?page=nb_post_sort_setting">カテゴリー選択</a> </p>
<?php else: ?>
<?php foreach ( $category_list as $category ): ?>
<div class="list-orders-inner">
<h3><?php echo $category->name; ?></h3>
<?php $target_posts = post_sort_cat_order::get_sort_post_list( $category->term_id ); ?>
<ul class="post-order-list">
<?php foreach ( $target_posts as $key => $target_post ): $sort_num = $key + 1; ?>
<li class="product-list">
<span class="sort-num-label"><?php echo $sort_num; ?></span>
<?php echo get_the_title( $target_post->ID ); ?>
<input type="hidden" class="list_order"
name="nb_post_cat[post_sort][<?php echo $category->term_id; ?>][<?php echo $target_post->ID; ?>]"
value="<?php echo $sort_num; ?>">
</li>
<?php endforeach; ?>
</ul>
</div>
<?php endforeach; ?>
<?php endif; ?>
</div>
<p class="submit"><input type="submit" name="Submit" class="button-primary" value="変更を保存"/></p>
</form>
</div>