WP PLUGIN アーカイブページの投稿表示順設定
・不要な初期化を削除 ・カスタムタクソノミー対応 ・設定画面のタブ化 ・設定画面各タクソノミーの投稿リスト表示をメソッド化 ・name属性の閉じ忘れ修正 ・不要なreturn削除 ・APOP_UI::create_product_none_order_list() returnに変数が未設定の場合の条件を追加 ・readmeに若干加筆
This commit is contained in:
@@ -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 );
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,8 +1,4 @@
|
||||
<?php
|
||||
/**
|
||||
* TODO:設定対象にカスタムタクソノミーの投稿リストを作成する
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
} // Exit if accessed directly
|
||||
@@ -64,15 +60,27 @@ if ( ! class_exists( 'APOP' ) ) {
|
||||
|
||||
|
||||
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' );
|
||||
//投稿表示順の設定
|
||||
$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' );
|
||||
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';
|
||||
}
|
||||
|
||||
@@ -88,7 +96,7 @@ if ( ! class_exists( 'APOP' ) ) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
+126
-31
@@ -3,71 +3,166 @@
|
||||
class APOP_UI {
|
||||
|
||||
public static function get_all_taxonomies( $key ) {
|
||||
if($key !== 'taxonomy'){
|
||||
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' => 'カスタム分類',
|
||||
);
|
||||
public static function get_cat_tag_list( $target, $key ): array {
|
||||
$opt = get_option( '_apop_' . $target . '_order' );
|
||||
|
||||
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 {
|
||||
$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,
|
||||
private static function create_custom_tax_term( $opt ): array {
|
||||
$tax_data = [];
|
||||
foreach ( $opt['target_cat'] as $tax_id => $status ) {
|
||||
//フラグが立っているカスタムタクソノミーは情報を取得する
|
||||
if ( $status ) {
|
||||
$args = array(
|
||||
'taxonomy' => get_term( $tax_id )->taxonomy,
|
||||
'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();
|
||||
}
|
||||
|
||||
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 = [
|
||||
'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,
|
||||
'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 );
|
||||
}
|
||||
|
||||
public static function get_none_sort_post_list( $tax_id, $key, $exclude_posts ) {
|
||||
$exclude = implode( ',', $exclude_posts );
|
||||
$args = [
|
||||
private static function create_post_sort_key( $tax_name, $tax_key ) {
|
||||
if ( $tax_key == 'taxonomy' ) {
|
||||
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' ),
|
||||
'posts_per_page' => - 1,
|
||||
$key => $tax_id,
|
||||
];
|
||||
);
|
||||
|
||||
$exclude = implode( ',', $exclude_posts );
|
||||
if ( ! empty( $exclude ) ) {
|
||||
$args['exclude'] = $exclude;
|
||||
}
|
||||
|
||||
self::create_post_tax_query( $args, $search_param, $tax_name, $tax_id );
|
||||
|
||||
return get_posts( $args );
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user