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

・管理画面メニュー(サブメニュー)の修正
・1ページ表示件数の追加
・pre_get_postするクエリの追加
・検索用設定の追加
This commit is contained in:
2021-05-06 21:09:35 +09:00
parent 05b5fa91aa
commit 803c742a9f
8 changed files with 773 additions and 234 deletions
+3
View File
@@ -25,6 +25,7 @@ License: GPLv2
//メイン処理のクラスをインスタンス化 //メイン処理のクラスをインスタンス化
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';
$APOP = new APOP; $APOP = new APOP;
//CSS, JSの読み込み //CSS, JSの読み込み
@@ -35,3 +36,5 @@ function register_my_styles() {
wp_enqueue_script( 'jquery-ui-sortable' ); wp_enqueue_script( 'jquery-ui-sortable' );
wp_enqueue_script( 'post-sort-cat-order_js', $plugin_url . 'js/apop-style.js' ); wp_enqueue_script( 'post-sort-cat-order_js', $plugin_url . 'js/apop-style.js' );
} }
$APOP->set_query();
+36 -5
View File
@@ -4,9 +4,12 @@ if ( ! defined( 'ABSPATH' ) ) {
} // Exit if accessed directly } // Exit if accessed directly
if ( ! class_exists( 'APOP' ) ) { if ( ! class_exists( 'APOP' ) ) {
require_once __DIR__ . '/../util/apop-order-setting.php';
class APOP { class APOP {
use APOP_ORDER_SETTING;
const TEMPLATE_DIR = __DIR__ . '/../template/'; const TEMPLATE_DIR = __DIR__ . '/../template/';
public function __construct() { public function __construct() {
@@ -19,17 +22,25 @@ if ( ! class_exists( 'APOP' ) ) {
'APO +', 'APO +',
'level_8', 'level_8',
'apop_post_sort', 'apop_post_sort',
array( $this, 'show_option_page' ), array( $this, 'display_setting_page' ),
'', '',
50 50
); );
add_submenu_page(
'apop_post_sort', // parent_slug
'Sort taxonomy', // page_title
'設定', // menu_title
'administrator', // capability
'apop_post_sort', // menu_slug
array( $this, 'display_setting_page' ) // function
);
add_submenu_page( add_submenu_page(
'apop_post_sort', // parent_slug 'apop_post_sort', // parent_slug
'Select taxonomy', // page_title 'Select taxonomy', // page_title
'タクソノミー選択', // menu_title '並べ替え', // menu_title
'administrator', // capability 'administrator', // capability
'apop_post_sort_setting', // menu_slug 'apop_post_sort_setting', // menu_slug
array( $this, 'display_setting_page' ) // function array( $this, 'show_option_page' ) // function
); );
} }
@@ -42,15 +53,14 @@ if ( ! class_exists( 'APOP' ) ) {
$settings = array( $settings = array(
'_apop_cat_order', //カテゴリー設定 '_apop_cat_order', //カテゴリー設定
'_apop_per_page', //1ページ表示件数
'_apop_tag_order', //タグ設定 '_apop_tag_order', //タグ設定
'_apop_tax_order', //カスタムタクソノミー設定 '_apop_tax_order', //カスタムタクソノミー設定
); );
foreach ( $settings as $setting ) { foreach ( $settings as $setting ) {
$opt = $_POST[ $setting ]; $opt = $_POST[ $setting ];
update_option( $setting, $opt ); update_option( $setting, $opt );
} }
require_once self::TEMPLATE_DIR . 'success.php'; require_once self::TEMPLATE_DIR . 'success.php';
} }
@@ -60,6 +70,20 @@ if ( ! class_exists( 'APOP' ) ) {
public function show_option_page() { public function show_option_page() {
//検索の投稿表示順設定
if ( isset( $_POST['_apop_post_search'] ) ) {
check_admin_referer( 'sh_options' );
$this->update_search_sort( 'search' );
require_once self::TEMPLATE_DIR . 'success.php';
}
if ( isset( $_POST['_apop_search_order'] ) ) {
check_admin_referer( 'sh_options' );
update_option( '_apop_search_order', $_POST['_apop_search_order'] );
require_once self::TEMPLATE_DIR . 'success.php';
}
//カテゴリーの投稿表示順設定 //カテゴリーの投稿表示順設定
if ( isset( $_POST['_apop_post_category'] ) ) { if ( isset( $_POST['_apop_post_category'] ) ) {
check_admin_referer( 'sh_options' ); check_admin_referer( 'sh_options' );
@@ -96,6 +120,13 @@ if ( ! class_exists( 'APOP' ) ) {
} }
} }
private function update_search_sort() {
$posts_sort = $_POST['_apop_post_search']['post_sort'];
foreach ( $posts_sort as $post_id => $sort ) {
update_post_meta( $post_id, '_apop_post_search', $sort );
}
}
} }
} }
+42 -1
View File
@@ -1,7 +1,31 @@
<?php <?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
} // Exit if accessed directly
if ( ! class_exists( 'APOP_UI' ) ) {
class APOP_UI { class APOP_UI {
public static function get_all_search_posts( $type, $exclude_posts = [] ) {
$args = [
'post_status' => array( 'publish', 'draft' ),
'posts_per_page' => - 1,
];
if ( $type == 'registered' ) {
$args['orderby'] = 'meta_value_num';
$args['order'] = 'ASC';
$args['meta_key'] = '_apop_post_search';
} else {
$exclude = implode( ',', $exclude_posts );
if ( ! empty( $exclude ) ) {
$args['exclude'] = $exclude;
}
}
return get_posts( $args );
}
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' ) );
@@ -164,5 +188,22 @@ value="' . $sort_num_no_order . '">
return get_posts( $args ); return get_posts( $args );
} }
public static function create_cat_per_page( $opt_per_page, $type ): array {
$cat_per_page = $opt_per_page[ $type ] ?? 'default';
$set = '';
$cat_per_page_num = '';
if ( isset( $opt_per_page[ $type ] ) ) {
if ( $opt_per_page[ $type ] != 'default' && $opt_per_page[ $type ] != '-1' ) {
$set = ' checked="checked"';
$cat_per_page_num = $cat_per_page;
}
}
return array(
'_per_page' => $cat_per_page,
'_set' => $set,
'_per_page_num' => $cat_per_page_num,
);
}
}
} }
+37 -1
View File
@@ -51,18 +51,50 @@ h3 {
.list-orders-inner { .list-orders-inner {
width: calc((100% - 10px) / 5); width: calc((100% - 10px) / 5);
margin-right: .5em; margin: 1.5em 1em 0 0;
padding: .5em; padding: .5em;
border: 1px solid #999; border: 1px solid #999;
position: relative; position: relative;
} }
.list-orders-inner.search_inner {
width: calc((100% - 10px) / 2);
}
.apop_search_order_target {
display: flex;
}
.apop_search_order_target li:first-child{
margin-right: 2em;
}
p.submit.post-order .button-primary { p.submit.post-order .button-primary {
position: absolute; position: absolute;
bottom: 1em; bottom: 1em;
right: 1.5em; right: 1.5em;
} }
dl.post_per_page_list {
width: 100%;
}
dl.post_per_page_list dt {
float: left;
}
dl.post_per_page_list dd {
margin-left: 200px;
}
dl.post_per_page_list dd:first-of-type {
margin-bottom: 2em;
}
.form-table tr {
border-bottom: 1px solid #ccc;
}
.post-order-list { .post-order-list {
margin: .5em 1em 1em 0; margin: .5em 1em 1em 0;
} }
@@ -91,3 +123,7 @@ p.submit.post-order .button-primary {
.sort-num-label::after { .sort-num-label::after {
content: ""; content: "";
} }
.per_page_cat_input {
}
+86 -9
View File
@@ -1,5 +1,37 @@
jQuery(function ($) { jQuery(function ($) {
change_tab_menu();
order_list();
change_search_per_page();
change_search_order_options();
change_cat_per_page();
change_tag_per_page();
change_tax_per_page();
function change_tab_menu() {
let submit_type = $('.post-order-box-outer').data('submit_type');
let order_nav_list = $('.post-order-nav li');
let order_box = $('.post-order-box');
order_nav_list.removeClass('en');
order_box.hide();
$('.post-order-nav li:eq(' + submit_type + ')').addClass('en');
$('.post-order-box:eq(' + submit_type + ')').show()
order_nav_list.click(function () {
if (!$(this).hasClass('en')) {
let target_index = $(this).index();
order_nav_list.removeClass('en');
$(this).addClass('en');
order_box.hide();
$('.post-order-box:eq(' + target_index + ')').show();
}
});
}
function order_list() {
let cat_ul_count = $('.post-order-list').length; //カテゴリ総数 let cat_ul_count = $('.post-order-list').length; //カテゴリ総数
for (let i = 0; i < cat_ul_count; i++) { for (let i = 0; i < cat_ul_count; i++) {
let my_list = $('.post-order-list:eq(' + i + ')'); let my_list = $('.post-order-list:eq(' + i + ')');
@@ -17,16 +49,61 @@ 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();
} }
});
function change_search_per_page() {
let per_page_cat = $('.per_page_search');
let per_page_input = $('.per_page_search_input');
change_per_page(per_page_cat, per_page_input);
}
function change_cat_per_page() {
let per_page_cat = $('.per_page_cat');
let per_page_input = $('.per_page_cat_input');
change_per_page(per_page_cat, per_page_input);
}
function change_tag_per_page() {
let per_page_cat = $('.per_page_tag');
let per_page_input = $('.per_page_tag_input');
change_per_page(per_page_cat, per_page_input);
}
function change_tax_per_page() {
let per_page_cat = $('.per_page_tax');
let per_page_input = $('.per_page_tax_input');
change_per_page(per_page_cat, per_page_input);
}
function change_per_page(per_page_cat, per_page_input) {
if (per_page_input.val() == '') {
per_page_input.prop('disabled', true);
} else {
per_page_input.prop('disabled', false);
}
per_page_cat.click(function () {
if ($(this).val() == 'set') {
per_page_input.prop('disabled', false);
} else {
per_page_input.val('');
per_page_input.prop('disabled', true);
}
})
}
function change_search_order_options() {
let s_radio = $('.apop_search_order');
let s_box = $('.search_sort_box');
let default_index = $('.apop_search_order_target').data('search_order_target') - 1;
s_box.hide();
$('.search_sort_box:eq(' + default_index + ')').show();
s_radio.click(function () {
let target_index = $(this).val() - 1;
s_box.hide();
$('.search_sort_box:eq(' + target_index + ')').show();
})
}
}); });
+140 -6
View File
@@ -1,18 +1,150 @@
<?php <?php
$search_lists = APOP_UI::get_all_search_posts( 'registered' );
$category_lists = array( 'category' => APOP_UI::get_cat_tag_list( 'cat', 'category' ) ); $category_lists = array( 'category' => APOP_UI::get_cat_tag_list( 'cat', 'category' ) );
$tag_lists = array( 'tag_id' => APOP_UI::get_cat_tag_list( 'tag', 'post_tag' ) ); $tag_lists = array( 'tag_id' => APOP_UI::get_cat_tag_list( 'tag', 'post_tag' ) );
$taxonomy_lists = array( 'taxonomy' => APOP_UI::get_cat_tag_list( 'tax', 'taxonomy' ) ); $taxonomy_lists = array( 'taxonomy' => APOP_UI::get_cat_tag_list( 'tax', 'taxonomy' ) );
$submit_type = $_POST['submit_type'] ?? '0';
$apop_search_order = get_option( '_apop_search_order' ) ?? '1';
?> ?>
<h2>並べ替え</h2>
<nav class="post-order-nav"> <nav class="post-order-nav">
<ul> <ul>
<li class="en">カテゴリー</li> <li class="en">検索</li>
<li>カテゴリー</li>
<li>タグ</li> <li>タグ</li>
<li>カスタム分類</li> <li>カスタム分類</li>
</ul> </ul>
</nav> </nav>
<div class="post-order-box-outer"> <div class="post-order-box-outer" data-submit_type="<?php echo $submit_type; ?>">
<div class="post-order-box">
<div class="list-orders-outer">
<?php $exclude_posts = []; ?>
<div class="list-orders-inner search_inner">
<form action="" method="post">
<?php wp_nonce_field( 'sh_options' ); ?>
<ul class="apop_search_order_target" data-search_order_target="<?php echo $apop_search_order; ?>">
<li><label>
<input class="apop_search_order" type="radio" name="_apop_search_order"
value="1"<?php echo checked( $apop_search_order, 1 ); ?>>標準</label></li>
<li><label>
<input class="apop_search_order" type="radio" name="_apop_search_order"
value="2"<?php echo checked( $apop_search_order, 2 ); ?>>カスタム</label></li>
</ul>
<hr>
<div class="search_sort_box">
<ul class="post-order-list search_normal_sort">
<li class="product-list">
<label>
<input type="hidden" name="_apop_search_order_param[id][use]" value="0">
<input type="checkbox" name="_apop_search_order_param[id][use]" value="1">
<b>ID</b>
</label>
&nbsp;&nbsp;
<label><input class="apop_search_order_param" type="radio"
name="_apop_search_order_param[id][sort]"
value="1">昇順</label>
&nbsp;&nbsp;
<label><input class="apop_search_order_param" type="radio"
name="_apop_search_order_param[id][sort]"
value="2">降順
</label>
<input type="hidden" class="list_order"
name="_apop_search_order_param[post_sort][id]"
value="">
</li>
<li class="product-list">
<label>
<input type="hidden" name="_apop_search_order_param[title][use]" value="0">
<input type="checkbox" name="_apop_search_order_param[title][use]" value="1">
<b>タイトル</b>
</label>
&nbsp;&nbsp;
<label><input class="apop_search_order_param" type="radio"
name="_apop_search_order_param[title][sort]"
value="1">昇順</label>
&nbsp;&nbsp;
<label><input class="apop_search_order_param" type="radio"
name="_apop_search_order_param[title][sort]"
value="2">降順
</label>
<input type="hidden" class="list_order"
name="_apop_search_order_param[post_sort][title]"
value="">
</li>
<li class="product-list">
<label>
<input type="hidden" name="_apop_search_order_param[register][use]" value="0">
<input type="checkbox" name="_apop_search_order_param[register][use]" value="1">
<b>登録日</b>
</label>
&nbsp;&nbsp;
<label><input class="apop_search_order_param" type="radio"
name="_apop_search_order_param[register][sort]"
value="1">昇順</label>
&nbsp;&nbsp;
<label><input class="apop_search_order_param" type="radio"
name="_apop_search_order_param[register][sort]"
value="2">降順</label>
<input type="hidden" class="list_order"
name="_apop_search_order_param[post_sort][register]"
value="">
</li>
<li class="product-list">
<label>
<input type="hidden" name="_apop_search_order_param[modified][use]" value="0">
<input type="checkbox" name="_apop_search_order_param[modified][use]" value="1">
<b>更新日</b>
</label>
&nbsp;&nbsp;
<label><input class="apop_search_order_param" type="radio"
name="_apop_search_order_param[modified][sort]"
value="1">昇順</label>
&nbsp;&nbsp;
<label><input class="apop_search_order_param" type="radio"
name="_apop_search_order_param[modified][sort]"
value="2">降順</label>
<input type="hidden" class="list_order"
name="_apop_search_order_param[post_sort][modified]"
value="">
</li>
</ul>
</div>
<div class="search_sort_box">
<ul class="post-order-list">
<?php foreach ( $search_lists as $i => $search_list ): $search_order = $i + 1; ?>
<?php $exclude_posts[] = $search_list->ID; ?>
<li class="product-list">
<span class="sort-num-label"><?php echo $search_order; ?></span>
<?php echo $search_list->post_title; ?>
<input type="hidden" class="list_order"
name="_apop_post_search[post_sort][<?php echo $search_list->ID; ?>]"
value="<?php echo $search_order; ?>">
</li>
<?php endforeach; ?>
<?php $search_none_lists = APOP_UI::get_all_search_posts( 'none', $exclude_posts ); ?>
<?php $search_order = $search_order ?? 0; ?>
<?php foreach ( $search_none_lists as $i => $search_list ): $search_order = $search_order + $i + 1; ?>
<li class="product-list no_order">
<span class="sort-num-label"><?php echo $search_order; ?></span>
<?php echo $search_list->post_title; ?>
<input type="hidden" class="list_order"
name="_apop_post_search[post_sort][<?php echo $search_list->ID; ?>]"
value="<?php echo $search_order; ?>">
</li>
<?php endforeach; ?>
</ul>
</div>
<input type="hidden" name="submit_type" value="0">
<p class="submit post-order"><input type="submit" name="Submit" class="button-primary"
value="変更を保存"/></p>
</form>
</div>
</div>
</div>
<div class="post-order-box"> <div class="post-order-box">
<h2>カテゴリー</h2>
<?php foreach ( $category_lists as $tax_key => $tax_list ) : ?> <?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 ): $exclude_posts = []; ?> <?php if ( count( $tax_list ) > 0 ): $exclude_posts = []; ?>
@@ -28,6 +160,7 @@ $taxonomy_lists = array( 'taxonomy' => APOP_UI::get_cat_tag_list( 'tax', 'taxono
echo APOP_UI::create_product_none_order_list( $tax_data, $tax_key, $exclude_posts, $sort_num ); echo APOP_UI::create_product_none_order_list( $tax_data, $tax_key, $exclude_posts, $sort_num );
?> ?>
</ul> </ul>
<input type="hidden" name="submit_type" value="1">
<p class="submit post-order"><input type="submit" name="Submit" class="button-primary" <p class="submit post-order"><input type="submit" name="Submit" class="button-primary"
value="変更を保存"/></p> value="変更を保存"/></p>
</form> </form>
@@ -38,7 +171,6 @@ $taxonomy_lists = array( 'taxonomy' => APOP_UI::get_cat_tag_list( 'tax', 'taxono
<?php endforeach; ?> <?php endforeach; ?>
</div> </div>
<div class="post-order-box"> <div class="post-order-box">
<h2>タグ</h2>
<?php foreach ( $tag_lists as $tax_key => $tax_list ) : ?> <?php foreach ( $tag_lists as $tax_key => $tax_list ) : ?>
<div class="list-orders-outer"> <div class="list-orders-outer">
<?php if ( count( $tax_list ) > 0 ): $exclude_posts = []; ?> <?php if ( count( $tax_list ) > 0 ): $exclude_posts = []; ?>
@@ -54,6 +186,7 @@ $taxonomy_lists = array( 'taxonomy' => APOP_UI::get_cat_tag_list( 'tax', 'taxono
echo APOP_UI::create_product_none_order_list( $tax_data, $tax_key, $exclude_posts, $sort_num ); echo APOP_UI::create_product_none_order_list( $tax_data, $tax_key, $exclude_posts, $sort_num );
?> ?>
</ul> </ul>
<input type="hidden" name="submit_type" value="2">
<p class="submit post-order"><input type="submit" name="Submit" class="button-primary" <p class="submit post-order"><input type="submit" name="Submit" class="button-primary"
value="変更を保存"/></p> value="変更を保存"/></p>
</form> </form>
@@ -64,7 +197,6 @@ $taxonomy_lists = array( 'taxonomy' => APOP_UI::get_cat_tag_list( 'tax', 'taxono
<?php endforeach; ?> <?php endforeach; ?>
</div> </div>
<div class="post-order-box"> <div class="post-order-box">
<h2>カスタム分類</h2>
<?php foreach ( $taxonomy_lists as $tax_key => $taxonomy_list ): ?> <?php foreach ( $taxonomy_lists as $tax_key => $taxonomy_list ): ?>
<div class="list-orders-outer"> <div class="list-orders-outer">
<?php foreach ( $taxonomy_list as $tax_list ) : ?> <?php foreach ( $taxonomy_list as $tax_list ) : ?>
@@ -81,7 +213,9 @@ $taxonomy_lists = array( 'taxonomy' => APOP_UI::get_cat_tag_list( 'tax', 'taxono
echo APOP_UI::create_product_none_order_list( $tax_data, $tax_key, $exclude_posts, $sort_num ); echo APOP_UI::create_product_none_order_list( $tax_data, $tax_key, $exclude_posts, $sort_num );
?> ?>
</ul> </ul>
<p class="submit post-order"><input type="submit" name="Submit" class="button-primary" <input type="hidden" name="submit_type" value="3">
<p class="submit post-order"><input type="submit" name="Submit"
class="button-primary"
value="変更を保存"/></p> value="変更を保存"/></p>
</form> </form>
</div> </div>
+112 -1
View File
@@ -9,15 +9,70 @@ $all_tax = APOP_UI::get_all_taxonomies( 'taxonomy' );
<?php <?php
wp_nonce_field( 'sh_options' ); wp_nonce_field( 'sh_options' );
$opt_cat = get_option( '_apop_cat_order' ); $opt_cat = get_option( '_apop_cat_order' );
$opt_per_page = get_option( '_apop_per_page' );
$opt_tag = get_option( '_apop_tag_order' ); $opt_tag = get_option( '_apop_tag_order' );
$opt_tax = get_option( '_apop_tax_order' ); $opt_tax = get_option( '_apop_tax_order' );
?> ?>
<h2>対象タクソノミー選択</h2> <h2>設定</h2>
<table class="form-table"> <table class="form-table">
<tr>
<th scope="row">検索</th>
<td>
<dl class="post_per_page_list">
<dt>1ページ表示件数</dt>
<dd><?php $per_page_data = APOP_UI::create_cat_per_page( $opt_per_page, 'search' ); ?>
<ul>
<li>
<label>
<input class="per_page_search" type="radio" name="_apop_per_page[search]"
value="default"<?php echo checked( $per_page_data['_per_page'], 'default' ); ?>>表示設定に従う</label>
</li>
<li><label>
<input class="per_page_search" type="radio" name="_apop_per_page[search]"
value="-1"<?php echo checked( $per_page_data['_per_page'], '-1' ); ?>>全件</label>
</li>
<li>
<label><input class="per_page_search" type="radio"
name="_apop_per_page[search]"
value="set"<?php echo $per_page_data['_set']; ?>>表示数設定
<input class="per_page_search_input" type="text"
name="_apop_per_page[search]"
value="<?php echo $per_page_data['_per_page_num']; ?>" required>
</label></li>
</ul>
</dd>
</dl>
</td>
</tr>
<?php if ( count( $all_categories ) > 0 ): ?> <?php if ( count( $all_categories ) > 0 ): ?>
<tr> <tr>
<th scope="row">カテゴリー</th> <th scope="row">カテゴリー</th>
<td> <td>
<dl class="post_per_page_list">
<dt>1ページ表示件数</dt>
<dd><?php $per_page_data = APOP_UI::create_cat_per_page( $opt_per_page, 'category' ); ?>
<ul>
<li>
<label>
<input class="per_page_cat" type="radio" name="_apop_per_page[category]"
value="default"<?php echo checked( $per_page_data['_per_page'], 'default' ); ?>>表示設定に従う</label>
</li>
<li><label>
<input class="per_page_cat" type="radio" name="_apop_per_page[category]"
value="-1"<?php echo checked( $per_page_data['_per_page'], '-1' ); ?>>全件</label>
</li>
<li>
<label><input class="per_page_cat" type="radio"
name="_apop_per_page[category]"
value="set"<?php echo $per_page_data['_set']; ?>>表示数設定
<input class="per_page_cat_input" type="text"
name="_apop_per_page[category]"
value="<?php echo $per_page_data['_per_page_num']; ?>" required>
</label></li>
</ul>
</dd>
<dt>対象カテゴリー</dt>
<dd>
<ul class="order_setting_list"> <ul class="order_setting_list">
<?php foreach ( $all_categories as $category ): ?> <?php foreach ( $all_categories as $category ): ?>
<?php $check_slug = $opt_cat['target_cat'][ $category->term_id ] ?? ''; ?> <?php $check_slug = $opt_cat['target_cat'][ $category->term_id ] ?? ''; ?>
@@ -37,6 +92,8 @@ $all_tax = APOP_UI::get_all_taxonomies( 'taxonomy' );
</li> </li>
<?php endforeach; ?> <?php endforeach; ?>
</ul> </ul>
</dd>
</dl>
</td> </td>
</tr> </tr>
<?php endif; ?> <?php endif; ?>
@@ -44,6 +101,30 @@ $all_tax = APOP_UI::get_all_taxonomies( 'taxonomy' );
<tr> <tr>
<th scope="row">タグ</th> <th scope="row">タグ</th>
<td> <td>
<dl class="post_per_page_list">
<dt>1ページ表示件数</dt>
<dd><?php $per_page_data = APOP_UI::create_cat_per_page( $opt_per_page, 'tag' ); ?>
<ul>
<li>
<label>
<input class="per_page_tag" type="radio" name="_apop_per_page[tag]"
value="default"<?php echo checked( $per_page_data['_per_page'], 'default' ); ?>>表示設定に従う</label>
</li>
<li><label>
<input class="per_page_tag" type="radio" name="_apop_per_page[tag]"
value="-1"<?php echo checked( $per_page_data['_per_page'], '-1' ); ?>>全件</label>
</li>
<li>
<label><input class="per_page_tag" type="radio" name="_apop_per_page[tag]"
value="set"<?php echo $per_page_data['_set']; ?>>表示数設定
<input class="per_page_tag_input" type="text"
name="_apop_per_page[tag]"
value="<?php echo $per_page_data['_per_page_num']; ?>" required>
</label></li>
</ul>
</dd>
<dt>対象タグ</dt>
<dd>
<ul class="order_setting_list"> <ul class="order_setting_list">
<?php foreach ( $all_tags as $tag ): ?> <?php foreach ( $all_tags as $tag ): ?>
<?php $check_slug = $opt_tag['target_cat'][ $tag->term_id ] ?? ''; ?> <?php $check_slug = $opt_tag['target_cat'][ $tag->term_id ] ?? ''; ?>
@@ -63,6 +144,9 @@ $all_tax = APOP_UI::get_all_taxonomies( 'taxonomy' );
</li> </li>
<?php endforeach; ?> <?php endforeach; ?>
</ul> </ul>
</dd>
</dl>
</td> </td>
</tr> </tr>
<?php endif; ?> <?php endif; ?>
@@ -70,6 +154,31 @@ $all_tax = APOP_UI::get_all_taxonomies( 'taxonomy' );
<tr> <tr>
<th scope="row">カスタム分類</th> <th scope="row">カスタム分類</th>
<td> <td>
<dl class="post_per_page_list">
<dt>1ページ表示件数</dt>
<dd>
<?php $per_page_data = APOP_UI::create_cat_per_page( $opt_per_page, 'tax' ); ?>
<ul>
<li>
<label>
<input class="per_page_tax" type="radio" name="_apop_per_page[tax]"
value="default"<?php echo checked( $per_page_data['_per_page'], 'default' ); ?>>表示設定に従う</label>
</li>
<li><label>
<input class="per_page_tax" type="radio" name="_apop_per_page[tax]"
value="-1"<?php echo checked( $per_page_data['_per_page'], '-1' ); ?>>全件</label>
</li>
<li>
<label><input class="per_page_tax" type="radio" name="_apop_per_page[tax]"
value="set"<?php echo $per_page_data['_set']; ?>>表示数設定
<input class="per_page_tax_input" type="text"
name="_apop_per_page[tax]"
value="<?php echo $per_page_data['_per_page_num']; ?>" required>
</label></li>
</ul>
</dd>
<dt>対象タクソノミー</dt>
<dd>
<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 ) ?>
<?php foreach ( $all_taxs as $tax ): ?> <?php foreach ( $all_taxs as $tax ): ?>
@@ -91,6 +200,8 @@ $all_tax = APOP_UI::get_all_taxonomies( 'taxonomy' );
<?php endforeach; ?> <?php endforeach; ?>
<?php endforeach; ?> <?php endforeach; ?>
</ul> </ul>
</dd>
</dl>
</td> </td>
</tr> </tr>
<?php endif; ?> <?php endif; ?>
+106
View File
@@ -0,0 +1,106 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
} // Exit if accessed directly
if ( ! trait_exists( 'APOP_ORDER_SETTING' ) ) {
trait APOP_ORDER_SETTING {
public function set_query() {
add_action( 'pre_get_posts', array( $this, 'apop_posts_per_page' ), 1 );
}
public function apop_posts_per_page( $query ) {
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
if ( is_home() ) {
return;
}
if ( is_search() ) {
$this->set_per_page( $query, 'search' );
$this->set_search_orderby( $query );
}
if ( is_category() ) {
$cat = get_category_by_slug( $query->query_vars['category_name'] );
$this->set_per_page( $query, 'category' );
$this->set_orderby( $query, $cat->term_id, 'category', 'cat' );
}
if ( is_tag() ) {
$tag = get_term_by( 'slug', $query->query_vars['tag'], 'post_tag' );
$this->set_per_page( $query, 'tag' );
$this->set_orderby( $query, $tag->term_id, 'post_tag', 'tag' );
}
if ( is_tax() ) {
$this->set_per_page( $query, 'tax' );
$this->set_orderby( $query, get_queried_object_id(), 'tax', 'tax' );
}
}
private function set_per_page( $query, $target ) {
$per_page_option = get_option( '_apop_per_page' );
if ( ! isset( $per_page_option[ $target ] ) ) {
return;
}
if ( $per_page_option[ $target ] == 'default' ) {
return;
}
$query->set( 'posts_per_page', $per_page_option[ $target ] );
}
private function set_search_orderby( $query ) {
$apop_search_order = get_option( '_apop_search_order' ) ?? 1;
if ( $apop_search_order == 2 ) {
$query->set( 'meta_key', '_apop_post_search' );
$query->set( 'orderby', array( 'meta_value_num' => 'ASC' ) );
}
}
private function set_orderby( $query, $id, $target, $order_key ) {
$order_settings = get_option( '_apop_' . $order_key . '_order' );
if ( isset( $order_settings['target_cat'][ $id ] ) && $order_settings['target_cat'][ $id ] == 1 ) {
$sort_meta_key = '_apop_post_' . $target . '_' . $id;
$query->set( 'meta_key', $sort_meta_key );
$query->set( 'orderby', array( 'meta_value_num' => 'ASC' ) );
}
}
public static function get_orderby( $id, $target, $order_key ): ?array {
$order_settings = get_option( '_apop_' . $order_key . '_order' );
if ( isset( $order_settings['target_cat'][ $id ] ) && $order_settings['target_cat'][ $id ] == 1 ) {
$sort_meta_key = '_apop_post_' . $target . '_' . $id;
return array(
'meta_key' => $sort_meta_key,
'orderby' => array( 'meta_value_num' => 'ASC' )
);
}
return null;
}
public static function get_per_page( $target ) {
$per_page_option = get_option( '_apop_per_page' );
if ( ! isset( $per_page_option[ $target ] ) ) {
return;
}
if ( $per_page_option[ $target ] == 'default' ) {
return;
}
return array( 'posts_per_page' => $per_page_option[ $target ] );
}
}
}