diff --git a/class/class.apop.ui.php b/class/class.apop.ui.php index d826950..2d0f6fd 100644 --- a/class/class.apop.ui.php +++ b/class/class.apop.ui.php @@ -21,7 +21,7 @@ if ( ! class_exists( 'APOP_UI' ) ) { $meta_key = '_apop_post_' . $key; $args = self::create_search_normal_args( $meta_key ); $posts_data = get_posts( $args ); - $list = []; + $list = array(); foreach ( $posts_data as $i => $post_data ) { $order = $i + 1; $no_order = self::is_sort_post_registered( $post_data->ID, $meta_key ) ? '' : ' no_order'; @@ -67,7 +67,7 @@ if ( ! class_exists( 'APOP_UI' ) ) { return get_terms( array( 'taxonomy' => $key, 'get' => 'all' ) ); } $all_custom_tax = get_taxonomies( array( 'public' => true, '_builtin' => false ) ); - $custom_tax_list = []; + $custom_tax_list = array(); foreach ( $all_custom_tax as $custom_tax ) { $custom_tax_list = array_merge( $custom_tax_list, get_terms( array( 'taxonomy' => $custom_tax, @@ -93,7 +93,7 @@ if ( ! class_exists( 'APOP_UI' ) ) { } private static function create_custom_tax_term( $opt ): array { - $tax_data = []; + $tax_data = array(); foreach ( $opt['target_cat'] as $tax_id => $status ) { //フラグが立っているカスタムタクソノミーは情報を取得する if ( $status ) { @@ -137,7 +137,7 @@ if ( ! class_exists( 'APOP_UI' ) ) { public static function create_order_list( $tax_data, $tax_key ): string { $return_data = self::get_sort_post_list( $tax_data->term_id, $tax_key, $tax_data->taxonomy ); - $list = []; + $list = array(); foreach ( $return_data['data'] as $key => $target_post ) { $sort_num = $key + 1; $no_order = self::is_sort_post_registered( $target_post->ID, $return_data['meta_key'] ) ? '' : ' no_order'; @@ -283,11 +283,14 @@ value="' . $sort_num . '"> } $target_keys = self::set_search_normal_target_keys( $order_param ); $target_values = array( - 'date' => '登録日', - 'title' => 'タイトル', - 'ID' => 'ID', - 'modified' => '更新日', - 'custom_field' => 'カスタムフィールド', + 'date' => '登録日', + 'title' => 'タイトル', + 'ID' => 'ID', + 'modified' => '更新日', + 'custom_field' => 'カスタムフィールド1', + 'custom_field_2' => 'カスタムフィールド2', + 'custom_field_3' => 'カスタムフィールド3', + 'custom_field_4' => 'カスタムフィールド4', ); $list = array(); foreach ( $target_keys as $target_key ) { @@ -297,9 +300,9 @@ value="' . $sort_num . '"> $alert = $cnv_order_params['alert']; $no_order_class = $cnv_order_params['no_order_class']; $field_metakey_input = ''; - if ( $target_key == 'custom_field' ) { - $meta_key = $cnv_order_params['custom_field']['meta_key']; - $value_type = $cnv_order_params['custom_field']['value_type']; + if ( strpos( $target_key, 'custom_field' ) !== false ) { + $meta_key = $cnv_order_params[ $target_key ]['meta_key']; + $value_type = $cnv_order_params[ $target_key ]['value_type']; $field_metakey_input = self::create_custom_field_sort_type( $name_key, $target_key, $meta_key, $value_type ); $target_key_check_class = 'custom_field_check'; } else { @@ -358,8 +361,8 @@ value="meta_value_num"' . self::set_search_normal_checked( $value_type, 'meta_va 'alert' => '
並べ替えを登録するには「変更を保存」をクリックしてください。
', 'no_order_class' => ' no_order', ); - if ( $target_key == 'custom_field' ) { - $param['custom_field'] = array( + if ( strpos( $target_key, 'custom_field' ) !== false ) { + $param[ $target_key ] = array( 'meta_key' => '', 'value_type' => 'meta_value', ); @@ -372,8 +375,8 @@ value="meta_value_num"' . self::set_search_normal_checked( $value_type, 'meta_va 'alert' => ! $order_param[ $target_key ]['use'] ? '並べ替えを登録するには「変更を保存」をクリックしてください。
' : '', 'no_order_class' => ! $order_param[ $target_key ]['use'] ? ' no_order' : '', ); - if ( $target_key == 'custom_field' ) { - $param['custom_field'] = array( + if ( strpos( $target_key, 'custom_field' ) !== false ) { + $param[ $target_key ] = array( 'meta_key' => $order_param[ $target_key ]['field']['meta_key'] ?? '', 'value_type' => $order_param[ $target_key ]['field']['value_type'] ?? 'meta_value', ); @@ -399,17 +402,25 @@ value="meta_value_num"' . self::set_search_normal_checked( $value_type, 'meta_va } private static function set_search_normal_target_keys( $post_apop_search_order_param ) { - if ( $post_apop_search_order_param ) { - return array_keys( $post_apop_search_order_param ); - } - return array( + $set_keys = array( 'date', 'title', 'ID', 'modified', - 'custom_field' + 'custom_field', + 'custom_field_2', + 'custom_field_3', + 'custom_field_4', ); + + if ( $post_apop_search_order_param ) { + $register_keys = array_keys( $post_apop_search_order_param ); + + return array_unique( array_merge( $register_keys, $set_keys ) ); + } + + return $set_keys; } private static function set_search_normal_checked( $param, $default ): string { diff --git a/js/apop-style.js b/js/apop-style.js index 4b9fec7..ab354fb 100644 --- a/js/apop-style.js +++ b/js/apop-style.js @@ -16,7 +16,6 @@ jQuery(function ($) { change_search_per_page(); change_tax_per_page(); - change_custom_field_sort(); change_normal_field_sort(); function change_tab_menu() { @@ -143,28 +142,6 @@ jQuery(function ($) { }); } - function change_custom_field_sort() { - let s_box = $('.sort_box'); - let custom_field_check = $('.custom_field_check'); - - s_box.find(custom_field_check).each(function () { - if ($(this).prop('checked') == false) { - $(this).parents('.product-list-sort-type').find('.custom_field_key').prop('disabled', true); - $(this).parents('.product-list-sort-type').find('.custom_field_meta_value').prop('disabled', true); - } - }); - - custom_field_check.on('click', function () { - if ($(this).prop('checked') == true) { - $(this).parents('.product-list-sort-type').find('.custom_field_key').prop('disabled', false); - $(this).parents('.product-list-sort-type').find('.custom_field_meta_value').prop('disabled', false); - } else { - $(this).parents('.product-list-sort-type').find('.custom_field_key').val('').prop('disabled', true); - $(this).parents('.product-list-sort-type').find('.custom_field_meta_value').prop('disabled', true); - } - }) - } - function change_normal_field_sort() { let s_box = $('.sort_box'); let targets = '.custom_field_check, .sort_date_check, .sort_title_check, .sort_ID_check, .sort_modified_check'; diff --git a/util/apop-order-setting.php b/util/apop-order-setting.php index f34d694..bee7095 100644 --- a/util/apop-order-setting.php +++ b/util/apop-order-setting.php @@ -88,39 +88,21 @@ if ( ! trait_exists( 'APOP_ORDER_SETTING' ) ) { $query->set( 'posts_per_page', $per_page_option_data ); } - 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'] ]; - } + private static function set_custom_filed_sort( &$orderby, $order_col, $orders, $sort_param ): array { + if ( strpos( $order_col, 'custom_field' ) !== false ) { + $meta_key = $orders['field']['meta_key']; + $orderby_sort = $orders['sort']; + $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'; } - } + $orderby[ $meta_key ] = $sort_param[ $orderby_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' ) { - $meta_key = $orders['field']['meta_key']; - $orderby_key = $orders['field']['value_type']; - $orderby_sort = $orders['sort']; - $orderby[ $orderby_key ] = $sort_param[ $orderby_sort ]; - - return $meta_key; + return array( $meta_key, $type ); } } @@ -132,7 +114,7 @@ if ( ! trait_exists( 'APOP_ORDER_SETTING' ) ) { $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' ) ); + $query->set( 'orderby', array( 'meta_value' => 'ASC', ) ); } } else { $meta_orderby = self::set_tax_custom_field_orderby( $id ); @@ -177,16 +159,18 @@ if ( ! trait_exists( 'APOP_ORDER_SETTING' ) ) { return $target; } - private static function get_all_post_args( $sort_meta_key ): array { + 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, ), ); } @@ -196,13 +180,13 @@ if ( ! trait_exists( 'APOP_ORDER_SETTING' ) ) { return array(); } $sort_param = array( 1 => 'ASC', 2 => 'DESC' ); - $meta_query = null; + $meta_query = array( 'relation' => 'AND' ); $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 ); + if ( strpos( $order_col, 'custom_field' ) !== false ) { + list( $sort_meta_key, $type ) = self::set_custom_filed_sort( $orderby, $order_col, $orders, $sort_param ); + $meta_query[] = self::get_all_post_args( $sort_meta_key, $type ); } else { $orderby[ $order_col ] = $sort_param[ $orders['sort'] ]; }