ARCHIVE POST ORDER PLUS:カスタム投稿アーカイブの追加

カスタム投稿アーカイブ対応
全体設定で「全件」指定時に「全体設定に従う」の設定が反映されないバグの修正
This commit is contained in:
2021-07-19 19:47:26 +09:00
parent b8dcf1c44c
commit 4bc96228bd
16 changed files with 848 additions and 338 deletions
+144
View File
@@ -0,0 +1,144 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
} // Exit if accessed directly
if ( ! trait_exists( 'CUSTOMPOSTS' ) ) {
/**
* カスタム投稿アーカイブのソート
* Trait CUSTOMPOSTS
*/
trait CUSTOMPOSTS {
public static function get_enable_custompost_archive(): array {
$opt = get_option( '_apop_custompost_archive_order' );
if ( ! isset( $opt['target_post'] ) ) {
return array();
}
$custom_post_list = array();
foreach ( $opt['target_post'] as $target => $check ) {
if ( $check == '1' ) {
$custom_post_list[] = [
'label' => get_post_type_object( $target )->label,
'slug' => $target,
];
}
}
return $custom_post_list;
}
public static function create_custompost_archive_order_list( $slug ): string {
$args = array(
'posts_per_page' => - 1,
'post_type' => array( $slug ),
'orderby' => 'meta_value_num',
'order' => 'ASC',
);
self::create_sort_post_list_meta_query( $args, '_apop_customposts_' . $slug );
$return_data = get_posts( $args );
$list = array();
foreach ( $return_data as $key => $target_post ) {
$sort_num = $key + 1;
$no_order = self::is_sort_custompost_archive_registered( $target_post->ID, $slug ) ? '' : ' no-order';
$list[] = '
<li class="product-list' . esc_attr( $no_order ) . '">' . '<span class="sort-num-label">' . esc_attr( $sort_num ) . '</span>' . get_the_title( $target_post->ID ) . '
<input type="hidden" class="list-order" name="_apop_customposts[' . esc_attr( $slug ) . '][' . esc_attr( $target_post->ID ) . ']" value="' . esc_attr( $sort_num ) . '">
</li>';
}
return implode( PHP_EOL, $list );
}
private static function is_sort_custompost_archive_registered( $id, $slug ): bool {
$post_meta = get_post_meta( $id, '_apop_customposts_' . $slug, true );
if ( ! empty( $post_meta ) ) {
return true;
}
return false;
}
public static function create_custompost_archive_normal_list( $slug ) {
$name_keys = array(
'name_key' => 'apop_custompost_archive_order_param[' . $slug . ']',
'get_option_key' => '_apop_custompost_archive_order_param',
);
$name_key = $name_keys['name_key'];
$get_option_key = $name_keys['get_option_key'];
$order_param_base = get_option( $get_option_key );
$order_param = '';
if ( isset( $order_param_base[ $slug ] ) ) {
$order_param = $order_param_base[ $slug ];
}
self::create_normal_sort_list( $name_key, $order_param );
}
private function set_custompost_orderby( $query, $post_type ) {
$sort_type = get_option( '_apop_custompost_archive_sort_type' )[ $post_type ];
if ( $sort_type == 1 ) {
$sort_meta_key = self::set_custompost_sort_meta_key( $post_type );
if ( is_null( $sort_meta_key ) ) {
$this->set_search_normal_orderby( $query, 'custompost_archive' );
} else {
$query->set( 'meta_query', self::get_all_post_args( $sort_meta_key ) );
$query->set( 'orderby', array( 'meta_value' => 'ASC', ) );
}
} else {
$meta_orderby = self::set_custompost_custom_field_orderby( $post_type );
if ( isset( $meta_orderby['meta_query'] ) ) {
$query->set( 'meta_query', $meta_orderby['meta_query'] );
}
if ( isset( $meta_orderby['orderby'] ) ) {
$query->set( 'orderby', $meta_orderby['orderby'] );
}
}
$per_page_option_data = self::set_custompost_per_page( $post_type );
$query->set( 'posts_per_page', $per_page_option_data );
}
private static function set_custompost_sort_meta_key( $post_type ) {
$order_settings = get_option( '_apop_custompost_archive_order' );
if ( isset( $order_settings['target_post'][ $post_type ] ) && $order_settings['target_post'][ $post_type ] == 1 ) {
return '_apop_customposts_' . $post_type;
}
return null;
}
private static function set_custompost_custom_field_orderby( $post_type ): array {
$order_param = get_option( '_apop_custompost_archive_order_param' );
if ( isset( $order_param[ $post_type ] ) ) {
return self::set_custom_field_orderby( $order_param[ $post_type ] );
}
}
private static function set_custompost_per_page( $post_type ) {
$per_page_option = get_option( '_apop_per_page' );
if ( ! $per_page_option ) {
return;
}
$per_page_option_data = $per_page_option['custompost_archive'][ $post_type ];
if ( ! isset( $per_page_option_data ) ) {
return;
}
if ( $per_page_option_data == 'default' ) {
return;
}
if ( $per_page_option_data == 'all' ) {
if ( $per_page_option['search'] == 'default' ) {
return;
}
$per_page_option_data = $per_page_option['search'];
}
return $per_page_option_data;
}
}
}
+89 -203
View File
@@ -12,6 +12,11 @@ if ( ! trait_exists( 'APOP_ORDER_SETTING' ) ) {
add_action( 'pre_get_posts', array( $this, 'apop_posts_per_page' ), 1 );
}
/**
* ソートのクエリを生成
*
* @param $query
*/
public function apop_posts_per_page( $query ) {
if ( is_admin() || ! $query->is_main_query() ) {
@@ -26,6 +31,13 @@ if ( ! trait_exists( 'APOP_ORDER_SETTING' ) ) {
$this->set_search_normal_orderby( $query, 'search' );
}
if ( is_post_type_archive() ) {
$post_type = get_query_var( 'post_type' );
if ( $post_type ) {
$this->set_custompost_orderby( $query, $post_type );
}
}
if ( is_category() ) {
$cat = get_category_by_slug( $query->query_vars['category_name'] );
if ( isset( $cat->term_id ) ) {
@@ -47,33 +59,9 @@ if ( ! trait_exists( 'APOP_ORDER_SETTING' ) ) {
}
}
private static function set_per_page( $target, $id = null ) {
$per_page_option = get_option( '_apop_per_page' );
if(!$per_page_option){
return;
}
if ( is_null( $id ) ) {
$per_page_option_data = $per_page_option[ $target ];
} else {
$per_page_target = $target == 'tax' ? 'taxonomy' : $target;
$per_page_option_data = $per_page_option[ $per_page_target ][ $id ];
}
if ( ! isset( $per_page_option_data ) ) {
return;
}
if ( $per_page_option_data == 'default' ) {
return;
}
if ( $per_page_option_data == 'all' ) {
$per_page_option_data = $per_page_option['search'];;
}
return $per_page_option_data;
}
//-----------------------------------------------------------
// 最近の投稿と検索のソートのクエリ生成
//-----------------------------------------------------------
private function set_search_normal_orderby( $query, $type ) {
$apop_order = get_option( '_apop_' . $type . '_order' ) ?? 1;
if ( $apop_order == 2 ) {
@@ -93,98 +81,9 @@ if ( ! trait_exists( 'APOP_ORDER_SETTING' ) ) {
$query->set( 'posts_per_page', $per_page_option_data );
}
private static function set_custom_filed_sort( &$orderby, $order_col, $orders, $sort_param ): array {
if ( strpos( $order_col, 'custom_field' ) !== false ) {
if($orders['field']['custom_field_type'] == 2){
$meta_key = APOP_CUSTOM_FIELD_PREFIX . $orders['field']['meta_key'];
}else{
$meta_key = $orders['field']['meta_key'];
}
$type = 'CHAR';
if ( $orders['field']['value_type'] == 'meta_value_num' ) {
$type = 'NUMERIC';
} elseif ( $orders['field']['value_type'] == 'meta_value' ) {
$type = 'CHAR';
} elseif ( $orders['field']['value_type'] == 'meta_date' ) {
$type = 'DATE';
}
if ( isset( $orders['sort'] ) ) {
$orderby[ $meta_key ] = $sort_param[ $orders['sort'] ];
}
return array( $meta_key, $type );
}
}
private function set_orderby( $query, $id, $target, $order_key ) {
$sort_type = self::get_tax_sort_type( $id );
if ( $sort_type == 1 ) {
$sort_meta_key = self::set_tax_sort_meta_key( $id, $target, $order_key );
if ( is_null( $sort_meta_key ) ) {
$this->set_search_normal_orderby( $query, 'normal' );
} else {
$query->set( 'meta_query', self::get_all_post_args( $sort_meta_key ) );
$query->set( 'orderby', array( 'meta_value' => 'ASC', ) );
}
} else {
$meta_orderby = self::set_tax_custom_field_orderby( $id );
if ( isset( $meta_orderby['meta_query'] ) ) {
$query->set( 'meta_query', $meta_orderby['meta_query'] );
}
if ( isset( $meta_orderby['orderby'] ) ) {
$query->set( 'orderby', $meta_orderby['orderby'] );
}
}
$per_page_option_data = self::set_per_page( self::get_per_page_tag( $target ), $id );
$query->set( 'posts_per_page', $per_page_option_data );
}
private static function get_tax_sort_type( $id ) {
$sort_type = get_option( '_apop_tax_sort_type' );
return $sort_type[ $id ] ?? 1;
}
private static function set_tax_sort_meta_key( $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 ) {
return '_apop_post_' . $target . '_' . $id;
}
return null;
}
private static function set_tax_custom_field_orderby( $id ): array {
$tax_order_param = get_option( '_apop_tax_order_param' );
if ( isset( $tax_order_param[ $id ] ) ) {
return self::set_custom_field_orderby( $tax_order_param[ $id ] );
}
}
private static function get_per_page_tag( $target ) {
if ( $target == 'post_tag' ) {
return 'tag';
}
return $target;
}
private static function get_all_post_args( $sort_meta_key, $type = 'numeric' ): array {
return array(
'relation' => 'OR',
array(
'key' => $sort_meta_key,
'compare' => 'EXISTS',
'type' => $type,
),
array(
'key' => $sort_meta_key,
'compare' => 'NOT EXISTS',
'type' => $type,
),
);
}
//-----------------------------------------------------------
//標準ソート+カスタムフィールドソートのカスタムフィールドデータ取得
//-----------------------------------------------------------
private static function set_custom_field_orderby( $apop_order_param ): array {
if ( ! $apop_order_param ) {
return array();
@@ -211,94 +110,81 @@ if ( ! trait_exists( 'APOP_ORDER_SETTING' ) ) {
);
}
/**
*
* 1ページ表示件数を取得する
* APOP::per_page(ID, [TARGET]);
* [TARGET]
* cat
* tag
* tax
*
* @param string $target
* @param null $id
*
* @return array
*/
public static function per_page( $id = null, string $target = 'search' ) {
if ( $target == 'cat' ) {
$target = 'category';
}
return array( 'posts_per_page' => self::set_per_page( $target, $id ) );
}
/**
* 最新の投稿ページのソートを取得する
* APOP::orderby_normal()
* @return array|null
*/
public static function orderby_normal() {
$apop_order = get_option( '_apop_normal_order' ) ?? 1;
if ( $apop_order == 2 ) {
return array(
'meta_query' => self::get_all_post_args( '_apop_normal_order' ),
'orderby' => array( 'meta_value_num' => 'ASC' ),
);
} else {
$apop_order_param = get_option( '_apop_normal_order_param' );
$meta_orderby = self::set_custom_field_orderby( $apop_order_param );
if ( is_null( $meta_orderby['meta_query'] ) ) {
unset( $meta_orderby['meta_query'] );
}
if ( is_null( $meta_orderby['orderby'] ) ) {
unset( $meta_orderby['orderby'] );
}
return $meta_orderby;
}
return;
}
/**
* カテゴリー、タグ、カスタム分類のソートを取得する
* APOP::orderby_tax([ID, TARGET], );
* [TARGET]
* cat
* tag
* tax
*
* @param $id
* @param $target
*
* @return array
*/
public static function orderby_tax( $id, $target ) {
$sort_type = self::get_tax_sort_type( $id );
if ( $sort_type == 1 ) {
$order_key = $target;
if ( $target == 'tag' ) {
$target == 'post_tag';
} elseif ( $target = 'cat' ) {
$target == 'category';
}
$sort_meta_key = self::set_tax_sort_meta_key( $id, $target, $order_key );
if ( is_null( $sort_meta_key ) ) {
return self::orderby_normal();
private static function set_custom_filed_sort( &$orderby, $order_col, $orders, $sort_param ): array {
if ( strpos( $order_col, 'custom_field' ) !== false ) {
if ( $orders['field']['custom_field_type'] == 2 ) {
$meta_key = APOP_CUSTOM_FIELD_PREFIX . $orders['field']['meta_key'];
} else {
return array(
'meta_query' => self::get_all_post_args( $sort_meta_key ),
'orderby' => array( 'meta_value_num' => 'ASC' ),
);
$meta_key = $orders['field']['meta_key'];
}
} else {
return self::set_tax_custom_field_orderby( $id );
$type = 'CHAR';
if ( $orders['field']['value_type'] == 'meta_value_num' ) {
$type = 'NUMERIC';
} elseif ( $orders['field']['value_type'] == 'meta_value' ) {
$type = 'CHAR';
} elseif ( $orders['field']['value_type'] == 'meta_date' ) {
$type = 'DATE';
}
if ( isset( $orders['sort'] ) ) {
$orderby[ $meta_key ] = $sort_param[ $orders['sort'] ];
}
return array( $meta_key, $type );
}
}
//-----------------------------------------------------------
// ソートキーが無くともデータを抽出するようEXISTSとNOT EXISTSをリレーションする
//-----------------------------------------------------------
private static function get_all_post_args( $sort_meta_key, $type = 'numeric' ): array {
return array(
'relation' => 'OR',
array(
'key' => $sort_meta_key,
'compare' => 'EXISTS',
'type' => $type,
),
array(
'key' => $sort_meta_key,
'compare' => 'NOT EXISTS',
'type' => $type,
),
);
}
//-----------------------------------------------------------
// 1ページ表示件数の取得
//-----------------------------------------------------------
private static function set_per_page( $target, $id = null ) {
$per_page_option = get_option( '_apop_per_page' );
if ( ! $per_page_option ) {
return;
}
return;
if ( is_null( $id ) ) {
$per_page_option_data = $per_page_option[ $target ];
} else {
$per_page_target = $target == 'tax' ? 'taxonomy' : $target;
$per_page_option_data = $per_page_option[ $per_page_target ][ $id ];
}
if ( ! isset( $per_page_option_data ) ) {
return;
}
if ( $per_page_option_data == 'default' ) {
return;
}
if ( $per_page_option_data == 'all' ) {
if ( $per_page_option['search'] == 'default' ) {
return;
}
$per_page_option_data = $per_page_option['search'];
}
return $per_page_option_data;
}
}
+102
View File
@@ -0,0 +1,102 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
} // Exit if accessed directly
if ( ! trait_exists( 'APOP_OUTPUT' ) ) {
/**
* TODO: 出力用のコード
* Trait APOP_OUTPUT
*/
trait APOP_OUTPUT {
/**
*
* 1ページ表示件数を取得する
* APOP::per_page(ID, [TARGET]);
* [TARGET]
* cat
* tag
* tax
*
* @param string $target
* @param null $id
*
* @return array
*/
public static function per_page( $id = null, string $target = 'search' ) {
if ( $target == 'cat' ) {
$target = 'category';
}
return array( 'posts_per_page' => self::set_per_page( $target, $id ) );
}
/**
* 最新の投稿ページのソートを取得する
* APOP::orderby_normal()
* @return array|null
*/
public static function orderby_normal() {
$apop_order = get_option( '_apop_normal_order' ) ?? 1;
if ( $apop_order == 2 ) {
return array(
'meta_query' => self::get_all_post_args( '_apop_normal_order' ),
'orderby' => array( 'meta_value_num' => 'ASC' ),
);
} else {
$apop_order_param = get_option( '_apop_normal_order_param' );
$meta_orderby = self::set_custom_field_orderby( $apop_order_param );
if ( is_null( $meta_orderby['meta_query'] ) ) {
unset( $meta_orderby['meta_query'] );
}
if ( is_null( $meta_orderby['orderby'] ) ) {
unset( $meta_orderby['orderby'] );
}
return $meta_orderby;
}
return;
}
/**
* カテゴリー、タグ、カスタム分類のソートを取得する
* APOP::orderby_tax([ID, TARGET], );
* [TARGET]
* cat
* tag
* tax
*
* @param $id
* @param $target
*
* @return array
*/
public static function orderby_tax( $id, $target ) {
$sort_type = self::get_tax_sort_type( $id );
if ( $sort_type == 1 ) {
$order_key = $target;
if ( $target == 'tag' ) {
$target == 'post_tag';
} elseif ( $target = 'cat' ) {
$target == 'category';
}
$sort_meta_key = self::set_tax_sort_meta_key( $id, $target, $order_key );
if ( is_null( $sort_meta_key ) ) {
return self::orderby_normal();
} else {
return array(
'meta_query' => self::get_all_post_args( $sort_meta_key ),
'orderby' => array( 'meta_value_num' => 'ASC' ),
);
}
} else {
return self::set_tax_custom_field_orderby( $id );
}
return;
}
}
}
+65
View File
@@ -0,0 +1,65 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
} // Exit if accessed directly
if ( ! trait_exists( 'TAX' ) ) {
/**
* タクソノミーのソート
* Trait TAX
*/
trait TAX {
private function set_orderby( $query, $id, $target, $order_key ) {
$sort_type = self::get_tax_sort_type( $id );
if ( $sort_type == 1 ) {
$sort_meta_key = self::set_tax_sort_meta_key( $id, $target, $order_key );
if ( is_null( $sort_meta_key ) ) {
$this->set_search_normal_orderby( $query, 'normal' );
} else {
$query->set( 'meta_query', self::get_all_post_args( $sort_meta_key ) );
$query->set( 'orderby', array( 'meta_value' => 'ASC', ) );
}
} else {
$meta_orderby = self::set_tax_custom_field_orderby( $id );
if ( isset( $meta_orderby['meta_query'] ) ) {
$query->set( 'meta_query', $meta_orderby['meta_query'] );
}
if ( isset( $meta_orderby['orderby'] ) ) {
$query->set( 'orderby', $meta_orderby['orderby'] );
}
}
$per_page_option_data = self::set_per_page( self::get_per_page_tag( $target ), $id );
$query->set( 'posts_per_page', $per_page_option_data );
}
private static function get_tax_sort_type( $id ) {
$sort_type = get_option( '_apop_tax_sort_type' );
return $sort_type[ $id ] ?? 1;
}
private static function set_tax_sort_meta_key( $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 ) {
return '_apop_post_' . $target . '_' . $id;
}
return null;
}
private static function set_tax_custom_field_orderby( $id ): array {
$tax_order_param = get_option( '_apop_tax_order_param' );
if ( isset( $tax_order_param[ $id ] ) ) {
return self::set_custom_field_orderby( $tax_order_param[ $id ] );
}
}
private static function get_per_page_tag( $target ) {
if ( $target == 'post_tag' ) {
return 'tag';
}
return $target;
}
}
}