Merge pull request 'WP PLUGIN ソート対象拡張' (#4) from タクソノミー修正_通常+カスタムフィールドソートの追加 into master
Reviewed-on: https://develop.n-k-y.net/repo/WP_PLUGIN/POST_ORDER_BY_ARCHIVE/pulls/4
This commit was merged in pull request #4.
This commit is contained in:
@@ -127,9 +127,9 @@ if ( ! class_exists( 'APOP' ) ) {
|
|||||||
// ------------------------------------
|
// ------------------------------------
|
||||||
$tax_sort_type = APOP_UI::input_post_filter( '_apop_tax_sort_type', 'array' );
|
$tax_sort_type = APOP_UI::input_post_filter( '_apop_tax_sort_type', 'array' );
|
||||||
$apop_tax_order_param = APOP_UI::input_post_filter( '_apop_tax_order_param', 'array' );
|
$apop_tax_order_param = APOP_UI::input_post_filter( '_apop_tax_order_param', 'array' );
|
||||||
$apop_post_category = APOP_UI::input_post_filter( '_apop_post_category', 'array' );
|
$apop_post_category = APOP_UI::input_post_filter( '_apop_post_category', 'array' );
|
||||||
$apop_post_post_tag = APOP_UI::input_post_filter( '_apop_post_post_tag', 'array' );
|
$apop_post_post_tag = APOP_UI::input_post_filter( '_apop_post_post_tag', 'array' );
|
||||||
$apop_post_tax = APOP_UI::input_post_filter( '_apop_post_tax', 'array' );
|
$apop_post_tax = APOP_UI::input_post_filter( '_apop_post_tax', 'array' );
|
||||||
|
|
||||||
//カテゴリーのソートタイプ設定
|
//カテゴリーのソートタイプ設定
|
||||||
if ( $tax_sort_type ) {
|
if ( $tax_sort_type ) {
|
||||||
|
|||||||
+165
-64
@@ -1,8 +1,4 @@
|
|||||||
<?php
|
<?php
|
||||||
/*
|
|
||||||
* Todo: タクソノミーの通常+カスタムフィールドソート処理の追加
|
|
||||||
*/
|
|
||||||
|
|
||||||
if ( ! defined( 'ABSPATH' ) ) {
|
if ( ! defined( 'ABSPATH' ) ) {
|
||||||
exit;
|
exit;
|
||||||
} // Exit if accessed directly
|
} // Exit if accessed directly
|
||||||
@@ -51,7 +47,7 @@ if ( ! trait_exists( 'APOP_ORDER_SETTING' ) ) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function set_per_page( $query, $target, $id = null ) {
|
private static function set_per_page( $target, $id = null ) {
|
||||||
$per_page_option = get_option( '_apop_per_page' );
|
$per_page_option = get_option( '_apop_per_page' );
|
||||||
if ( is_null( $id ) ) {
|
if ( is_null( $id ) ) {
|
||||||
$per_page_option_data = $per_page_option[ $target ];
|
$per_page_option_data = $per_page_option[ $target ];
|
||||||
@@ -67,10 +63,10 @@ if ( ! trait_exists( 'APOP_ORDER_SETTING' ) ) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if ( $per_page_option_data == 'all' ) {
|
if ( $per_page_option_data == 'all' ) {
|
||||||
$target = 'search';
|
$per_page_option_data = $per_page_option['search'];;
|
||||||
}
|
}
|
||||||
|
|
||||||
$query->set( 'posts_per_page', $per_page_option_data );
|
return $per_page_option_data;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function set_search_normal_orderby( $query, $type ) {
|
private function set_search_normal_orderby( $query, $type ) {
|
||||||
@@ -80,47 +76,96 @@ if ( ! trait_exists( 'APOP_ORDER_SETTING' ) ) {
|
|||||||
$query->set( 'meta_query', self::get_all_post_args( '_apop_post_' . $type ) );
|
$query->set( 'meta_query', self::get_all_post_args( '_apop_post_' . $type ) );
|
||||||
} else {
|
} else {
|
||||||
$apop_order_param = get_option( '_apop_' . $type . '_order_param' );
|
$apop_order_param = get_option( '_apop_' . $type . '_order_param' );
|
||||||
$sort_param = array( 1 => 'ASC', 2 => 'DESC' );
|
$meta_orderby = self::set_custom_field_orderby( $apop_order_param );
|
||||||
if ( ! $apop_order_param ) {
|
if ( isset( $meta_orderby['meta_query'] ) ) {
|
||||||
return;
|
$query->set( 'meta_query', $meta_orderby['meta_query'] );
|
||||||
}
|
}
|
||||||
foreach ( $apop_order_param as $order_col => $orders ) {
|
if ( isset( $meta_orderby['orderby'] ) ) {
|
||||||
if ( $orders['use'] == 1 ) {
|
$query->set( 'orderby', $meta_orderby['orderby'] );
|
||||||
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' );
|
$per_page_option_data = self::set_per_page( 'search' );
|
||||||
|
$query->set( 'posts_per_page', $per_page_option_data );
|
||||||
}
|
}
|
||||||
|
|
||||||
private function set_custom_filed_sort( &$orderby, $query, $order_col, $orders, $sort_param ) {
|
private function set_custom_filed_orderby( $query, $apop_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' ) {
|
||||||
|
$sort_meta_key = self::set_custom_filed_sort( $orderby, $order_col, $orders, $sort_param );
|
||||||
|
|
||||||
|
} else {
|
||||||
|
$orderby[ $order_col ] = $sort_param[ $orders['sort'] ];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( isset( $sort_meta_key ) ) {
|
||||||
|
$query->set( 'meta_query', self::get_all_post_args( $sort_meta_key ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( isset( $orderby ) ) {
|
||||||
|
$query->set( 'orderby', $orderby );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static function set_custom_filed_sort( &$orderby, $order_col, $orders, $sort_param ) {
|
||||||
if ( $order_col == 'custom_field' ) {
|
if ( $order_col == 'custom_field' ) {
|
||||||
$meta_key = $orders['field']['meta_key'];
|
$meta_key = $orders['field']['meta_key'];
|
||||||
$orderby_key = $orders['field']['value_type'];
|
$orderby_key = $orders['field']['value_type'];
|
||||||
$orderby_sort = $orders['sort'];
|
$orderby_sort = $orders['sort'];
|
||||||
$orderby[ $orderby_key ] = $sort_param[ $orderby_sort ];
|
$orderby[ $orderby_key ] = $sort_param[ $orderby_sort ];
|
||||||
$query->set( 'meta_query', self::get_all_post_args( $meta_key ) );
|
|
||||||
|
return $meta_key;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function set_orderby( $query, $id, $target, $order_key ) {
|
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_num' => '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' );
|
$order_settings = get_option( '_apop_' . $order_key . '_order' );
|
||||||
if ( isset( $order_settings['target_cat'][ $id ] ) && $order_settings['target_cat'][ $id ] == 1 ) {
|
if ( isset( $order_settings['target_cat'][ $id ] ) && $order_settings['target_cat'][ $id ] == 1 ) {
|
||||||
$sort_meta_key = '_apop_post_' . $target . '_' . $id;
|
return '_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 );
|
return null;
|
||||||
} else {
|
}
|
||||||
self::set_search_normal_orderby( $query, 'normal' );
|
|
||||||
|
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 ] );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -146,7 +191,33 @@ if ( ! trait_exists( 'APOP_ORDER_SETTING' ) ) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function get_normal_orderby() {
|
private static function set_custom_field_orderby( $apop_order_param ): array {
|
||||||
|
$sort_param = array( 1 => 'ASC', 2 => 'DESC' );
|
||||||
|
$meta_query = null;
|
||||||
|
$orderby = null;
|
||||||
|
foreach ( $apop_order_param as $order_col => $orders ) {
|
||||||
|
if ( $orders['use'] == 1 ) {
|
||||||
|
if ( $order_col == 'custom_field' ) {
|
||||||
|
$sort_meta_key = self::set_custom_filed_sort( $orderby, $order_col, $orders, $sort_param );
|
||||||
|
$meta_query = self::get_all_post_args( $sort_meta_key );
|
||||||
|
} else {
|
||||||
|
$orderby[ $order_col ] = $sort_param[ $orders['sort'] ];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return array(
|
||||||
|
'meta_query' => $meta_query,
|
||||||
|
'orderby' => $orderby
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通常ページの1ページ表示件数を取得する
|
||||||
|
* APOP::orderby_normal()
|
||||||
|
* @return array|null
|
||||||
|
*/
|
||||||
|
public static function orderby_normal() {
|
||||||
$apop_order = get_option( '_apop_normal_order' ) ?? 1;
|
$apop_order = get_option( '_apop_normal_order' ) ?? 1;
|
||||||
if ( $apop_order == 2 ) {
|
if ( $apop_order == 2 ) {
|
||||||
return array(
|
return array(
|
||||||
@@ -155,48 +226,78 @@ if ( ! trait_exists( 'APOP_ORDER_SETTING' ) ) {
|
|||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
$apop_order_param = get_option( '_apop_normal_order_param' );
|
$apop_order_param = get_option( '_apop_normal_order_param' );
|
||||||
$sort_param = array( 1 => 'ASC', 2 => 'DESC' );
|
$meta_orderby = self::set_custom_field_orderby( $apop_order_param );
|
||||||
foreach ( $apop_order_param as $order_col => $orders ) {
|
if ( is_null( $meta_orderby['meta_query'] ) ) {
|
||||||
if ( $orders['use'] == 1 ) {
|
unset( $meta_orderby['meta_query'] );
|
||||||
$orderby[ $order_col ] = $sort_param[ $orders['sort'] ];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if ( isset( $orderby ) ) {
|
if ( is_null( $meta_orderby['orderby'] ) ) {
|
||||||
|
unset( $meta_orderby['orderby'] );
|
||||||
|
}
|
||||||
|
|
||||||
|
return $meta_orderby;
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* カテゴリー、タグ、カスタム分類の1ページ表示件数を取得する
|
||||||
|
* 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(
|
return array(
|
||||||
'orderby' => $orderby,
|
'meta_query' => self::get_all_post_args( $sort_meta_key ),
|
||||||
|
'orderby' => array( 'meta_value_num' => 'ASC' ),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
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 {
|
} else {
|
||||||
return self::get_normal_orderby();
|
return self::set_tax_custom_field_orderby( $id );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function get_per_page( $target ) {
|
/**
|
||||||
$per_page_option = get_option( '_apop_per_page' );
|
*
|
||||||
if ( ! isset( $per_page_option ) ) {
|
* 1ページ表示件数を取得する
|
||||||
return;
|
* APOP::per_page(ID, [TARGET]);
|
||||||
}
|
* [TARGET]
|
||||||
if ( $per_page_option == 'default' ) {
|
* cat
|
||||||
return;
|
* 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' => $per_page_option );
|
return array( 'posts_per_page' => self::set_per_page( $target, $id ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user