f07c318f93
・通常、検索、タクソノミーにソート用カスタムフィールドの設定入力欄を追加 ・通常と検索にカスタムフィールド検索処理を実装 ・タクソノミーに通常+カスタムフィールドのメニューを追加 ・タクソノミーに通常+カスタムフィールドとドラッグソートの切り替え用ラジオボタンを追加
203 lines
5.7 KiB
PHP
203 lines
5.7 KiB
PHP
<?php
|
|
/*
|
|
* Todo: タクソノミーの通常+カスタムフィールドソート処理の追加
|
|
*/
|
|
|
|
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() ) {
|
|
$this->set_search_normal_orderby( $query, 'normal' );
|
|
}
|
|
|
|
if ( is_search() ) {
|
|
$this->set_search_normal_orderby( $query, 'search' );
|
|
}
|
|
|
|
if ( is_category() ) {
|
|
$cat = get_category_by_slug( $query->query_vars['category_name'] );
|
|
if ( isset( $cat->term_id ) ) {
|
|
$this->set_orderby( $query, $cat->term_id, 'category', 'cat' );
|
|
}
|
|
}
|
|
|
|
if ( is_tag() ) {
|
|
$tag = get_term_by( 'slug', $query->query_vars['tag'], 'post_tag' );
|
|
if ( isset( $tag->term_id ) ) {
|
|
$this->set_orderby( $query, $tag->term_id, 'post_tag', 'tag' );
|
|
}
|
|
}
|
|
|
|
if ( is_tax() ) {
|
|
if ( get_queried_object_id() ) {
|
|
$this->set_orderby( $query, get_queried_object_id(), 'tax', 'tax' );
|
|
}
|
|
}
|
|
}
|
|
|
|
private function set_per_page( $query, $target, $id = null ) {
|
|
$per_page_option = get_option( '_apop_per_page' );
|
|
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' ) {
|
|
$target = 'search';
|
|
}
|
|
|
|
$query->set( 'posts_per_page', $per_page_option_data );
|
|
}
|
|
|
|
private function set_search_normal_orderby( $query, $type ) {
|
|
$apop_order = get_option( '_apop_' . $type . '_order' ) ?? 1;
|
|
if ( $apop_order == 2 ) {
|
|
$query->set( 'orderby', array( 'meta_value_num' => 'ASC' ) );
|
|
$query->set( 'meta_query', self::get_all_post_args( '_apop_post_' . $type ) );
|
|
} else {
|
|
$apop_order_param = get_option( '_apop_' . $type . '_order_param' );
|
|
$sort_param = array( 1 => 'ASC', 2 => 'DESC' );
|
|
if ( ! $apop_order_param ) {
|
|
return;
|
|
}
|
|
foreach ( $apop_order_param as $order_col => $orders ) {
|
|
if ( $orders['use'] == 1 ) {
|
|
if ( $order_col == 'custom_field' ) {
|
|
$this->set_custom_filed_sort( $orderby, $query, $order_col, $orders, $sort_param );
|
|
} else {
|
|
$orderby[ $order_col ] = $sort_param[ $orders['sort'] ];
|
|
}
|
|
}
|
|
}
|
|
if ( isset( $orderby ) ) {
|
|
$query->set(
|
|
'orderby', $orderby
|
|
);
|
|
}
|
|
}
|
|
$this->set_per_page( $query, 'search' );
|
|
}
|
|
|
|
private function set_custom_filed_sort( &$orderby, $query, $order_col, $orders, $sort_param ) {
|
|
if ( $order_col == 'custom_field' ) {
|
|
$meta_key = $orders['field']['meta_key'];
|
|
$orderby_key = $orders['field']['value_type'];
|
|
$orderby_sort = $orders['sort'];
|
|
$orderby[ $orderby_key ] = $sort_param[ $orderby_sort ];
|
|
$query->set( 'meta_query', self::get_all_post_args( $meta_key ) );
|
|
}
|
|
}
|
|
|
|
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_query', self::get_all_post_args( $sort_meta_key ) );
|
|
$query->set( 'orderby', array( 'meta_value_num' => 'ASC' ) );
|
|
$this->set_per_page( $query, self::get_per_page_tag( $target ), $id );
|
|
} else {
|
|
self::set_search_normal_orderby( $query, 'normal' );
|
|
}
|
|
}
|
|
|
|
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 ): array {
|
|
return array(
|
|
'relation' => 'OR',
|
|
array(
|
|
'key' => $sort_meta_key,
|
|
'compare' => 'EXISTS',
|
|
),
|
|
array(
|
|
'key' => $sort_meta_key,
|
|
'compare' => 'NOT EXISTS',
|
|
),
|
|
);
|
|
}
|
|
|
|
public static function get_normal_orderby() {
|
|
$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' );
|
|
$sort_param = array( 1 => 'ASC', 2 => 'DESC' );
|
|
foreach ( $apop_order_param as $order_col => $orders ) {
|
|
if ( $orders['use'] == 1 ) {
|
|
$orderby[ $order_col ] = $sort_param[ $orders['sort'] ];
|
|
}
|
|
}
|
|
if ( isset( $orderby ) ) {
|
|
return array(
|
|
'orderby' => $orderby,
|
|
);
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public static function get_tax_orderby( $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;
|
|
|
|
return array(
|
|
'meta_key' => $sort_meta_key,
|
|
'orderby' => array( 'meta_value_num' => 'ASC' )
|
|
);
|
|
} else {
|
|
return self::get_normal_orderby();
|
|
}
|
|
|
|
}
|
|
|
|
public static function get_per_page( $target ) {
|
|
$per_page_option = get_option( '_apop_per_page' );
|
|
if ( ! isset( $per_page_option ) ) {
|
|
return;
|
|
}
|
|
if ( $per_page_option == 'default' ) {
|
|
return;
|
|
}
|
|
|
|
return array( 'posts_per_page' => $per_page_option );
|
|
}
|
|
|
|
}
|
|
}
|