WP PLUGIN 修正(カスタムフィールド追加)

・ソートのカスタムフィールドを4つに変更
・配列の初期化書式を書き換え
・ソートのカスタムフィールドを4つに変更
・ソートのJSでカスタムフィールドをdisabledにしないよう変更
This commit is contained in:
2021-05-15 16:43:56 +09:00
parent 3e38f5f26f
commit 48148820cc
4 changed files with 53 additions and 82 deletions
-1
View File
@@ -23,7 +23,6 @@ License: GPLv2
*/ */
/** /**
* todo:カスタムフィールド追加処理
* 指定したキーが無ければ、投稿の管理画面表示時にメタボックスを追加する。 * 指定したキーが無ければ、投稿の管理画面表示時にメタボックスを追加する。
* メタキーは下記のオプションテーブルから取得する * メタキーは下記のオプションテーブルから取得する
* _apop_normal_order_param * _apop_normal_order_param
+28 -17
View File
@@ -21,7 +21,7 @@ if ( ! class_exists( 'APOP_UI' ) ) {
$meta_key = '_apop_post_' . $key; $meta_key = '_apop_post_' . $key;
$args = self::create_search_normal_args( $meta_key ); $args = self::create_search_normal_args( $meta_key );
$posts_data = get_posts( $args ); $posts_data = get_posts( $args );
$list = []; $list = array();
foreach ( $posts_data as $i => $post_data ) { foreach ( $posts_data as $i => $post_data ) {
$order = $i + 1; $order = $i + 1;
$no_order = self::is_sort_post_registered( $post_data->ID, $meta_key ) ? '' : ' no_order'; $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' ) ); return get_terms( array( 'taxonomy' => $key, 'get' => 'all' ) );
} }
$all_custom_tax = get_taxonomies( array( 'public' => true, '_builtin' => false ) ); $all_custom_tax = get_taxonomies( array( 'public' => true, '_builtin' => false ) );
$custom_tax_list = []; $custom_tax_list = array();
foreach ( $all_custom_tax as $custom_tax ) { foreach ( $all_custom_tax as $custom_tax ) {
$custom_tax_list = array_merge( $custom_tax_list, get_terms( array( $custom_tax_list = array_merge( $custom_tax_list, get_terms( array(
'taxonomy' => $custom_tax, 'taxonomy' => $custom_tax,
@@ -93,7 +93,7 @@ if ( ! class_exists( 'APOP_UI' ) ) {
} }
private static function create_custom_tax_term( $opt ): array { private static function create_custom_tax_term( $opt ): array {
$tax_data = []; $tax_data = array();
foreach ( $opt['target_cat'] as $tax_id => $status ) { foreach ( $opt['target_cat'] as $tax_id => $status ) {
//フラグが立っているカスタムタクソノミーは情報を取得する //フラグが立っているカスタムタクソノミーは情報を取得する
if ( $status ) { if ( $status ) {
@@ -137,7 +137,7 @@ if ( ! class_exists( 'APOP_UI' ) ) {
public static function create_order_list( $tax_data, $tax_key ): string { 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 ); $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 ) { foreach ( $return_data['data'] as $key => $target_post ) {
$sort_num = $key + 1; $sort_num = $key + 1;
$no_order = self::is_sort_post_registered( $target_post->ID, $return_data['meta_key'] ) ? '' : ' no_order'; $no_order = self::is_sort_post_registered( $target_post->ID, $return_data['meta_key'] ) ? '' : ' no_order';
@@ -287,7 +287,10 @@ value="' . $sort_num . '">
'title' => 'タイトル', 'title' => 'タイトル',
'ID' => 'ID', 'ID' => 'ID',
'modified' => '更新日', 'modified' => '更新日',
'custom_field' => 'カスタムフィールド', 'custom_field' => 'カスタムフィールド1',
'custom_field_2' => 'カスタムフィールド2',
'custom_field_3' => 'カスタムフィールド3',
'custom_field_4' => 'カスタムフィールド4',
); );
$list = array(); $list = array();
foreach ( $target_keys as $target_key ) { foreach ( $target_keys as $target_key ) {
@@ -297,9 +300,9 @@ value="' . $sort_num . '">
$alert = $cnv_order_params['alert']; $alert = $cnv_order_params['alert'];
$no_order_class = $cnv_order_params['no_order_class']; $no_order_class = $cnv_order_params['no_order_class'];
$field_metakey_input = ''; $field_metakey_input = '';
if ( $target_key == 'custom_field' ) { if ( strpos( $target_key, 'custom_field' ) !== false ) {
$meta_key = $cnv_order_params['custom_field']['meta_key']; $meta_key = $cnv_order_params[ $target_key ]['meta_key'];
$value_type = $cnv_order_params['custom_field']['value_type']; $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 ); $field_metakey_input = self::create_custom_field_sort_type( $name_key, $target_key, $meta_key, $value_type );
$target_key_check_class = 'custom_field_check'; $target_key_check_class = 'custom_field_check';
} else { } else {
@@ -358,8 +361,8 @@ value="meta_value_num"' . self::set_search_normal_checked( $value_type, 'meta_va
'alert' => '<p>並べ替えを登録するには「変更を保存」をクリックしてください。</p>', 'alert' => '<p>並べ替えを登録するには「変更を保存」をクリックしてください。</p>',
'no_order_class' => ' no_order', 'no_order_class' => ' no_order',
); );
if ( $target_key == 'custom_field' ) { if ( strpos( $target_key, 'custom_field' ) !== false ) {
$param['custom_field'] = array( $param[ $target_key ] = array(
'meta_key' => '', 'meta_key' => '',
'value_type' => 'meta_value', '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'] ? '<p>並べ替えを登録するには「変更を保存」をクリックしてください。</p>' : '', 'alert' => ! $order_param[ $target_key ]['use'] ? '<p>並べ替えを登録するには「変更を保存」をクリックしてください。</p>' : '',
'no_order_class' => ! $order_param[ $target_key ]['use'] ? ' no_order' : '', 'no_order_class' => ! $order_param[ $target_key ]['use'] ? ' no_order' : '',
); );
if ( $target_key == 'custom_field' ) { if ( strpos( $target_key, 'custom_field' ) !== false ) {
$param['custom_field'] = array( $param[ $target_key ] = array(
'meta_key' => $order_param[ $target_key ]['field']['meta_key'] ?? '', 'meta_key' => $order_param[ $target_key ]['field']['meta_key'] ?? '',
'value_type' => $order_param[ $target_key ]['field']['value_type'] ?? 'meta_value', '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 ) { 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', 'date',
'title', 'title',
'ID', 'ID',
'modified', '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 { private static function set_search_normal_checked( $param, $default ): string {
-23
View File
@@ -16,7 +16,6 @@ jQuery(function ($) {
change_search_per_page(); change_search_per_page();
change_tax_per_page(); change_tax_per_page();
change_custom_field_sort();
change_normal_field_sort(); change_normal_field_sort();
function change_tab_menu() { 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() { function change_normal_field_sort() {
let s_box = $('.sort_box'); let s_box = $('.sort_box');
let targets = '.custom_field_check, .sort_date_check, .sort_title_check, .sort_ID_check, .sort_modified_check'; let targets = '.custom_field_check, .sort_date_check, .sort_title_check, .sort_ID_check, .sort_modified_check';
+20 -36
View File
@@ -88,39 +88,21 @@ if ( ! trait_exists( 'APOP_ORDER_SETTING' ) ) {
$query->set( 'posts_per_page', $per_page_option_data ); $query->set( 'posts_per_page', $per_page_option_data );
} }
private function set_custom_filed_orderby( $query, $apop_order_param ) { private static function set_custom_filed_sort( &$orderby, $order_col, $orders, $sort_param ): array {
$sort_param = array( 1 => 'ASC', 2 => 'DESC' ); if ( strpos( $order_col, 'custom_field' ) !== false ) {
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' ) {
$meta_key = $orders['field']['meta_key']; $meta_key = $orders['field']['meta_key'];
$orderby_key = $orders['field']['value_type'];
$orderby_sort = $orders['sort']; $orderby_sort = $orders['sort'];
$orderby[ $orderby_key ] = $sort_param[ $orderby_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 ];
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' ); $this->set_search_normal_orderby( $query, 'normal' );
} else { } else {
$query->set( 'meta_query', self::get_all_post_args( $sort_meta_key ) ); $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 { } else {
$meta_orderby = self::set_tax_custom_field_orderby( $id ); $meta_orderby = self::set_tax_custom_field_orderby( $id );
@@ -177,16 +159,18 @@ if ( ! trait_exists( 'APOP_ORDER_SETTING' ) ) {
return $target; 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( return array(
'relation' => 'OR', 'relation' => 'OR',
array( array(
'key' => $sort_meta_key, 'key' => $sort_meta_key,
'compare' => 'EXISTS', 'compare' => 'EXISTS',
'type' => $type,
), ),
array( array(
'key' => $sort_meta_key, 'key' => $sort_meta_key,
'compare' => 'NOT EXISTS', 'compare' => 'NOT EXISTS',
'type' => $type,
), ),
); );
} }
@@ -196,13 +180,13 @@ if ( ! trait_exists( 'APOP_ORDER_SETTING' ) ) {
return array(); return array();
} }
$sort_param = array( 1 => 'ASC', 2 => 'DESC' ); $sort_param = array( 1 => 'ASC', 2 => 'DESC' );
$meta_query = null; $meta_query = array( 'relation' => 'AND' );
$orderby = null; $orderby = null;
foreach ( $apop_order_param as $order_col => $orders ) { foreach ( $apop_order_param as $order_col => $orders ) {
if ( $orders['use'] == 1 ) { if ( $orders['use'] == 1 ) {
if ( $order_col == 'custom_field' ) { if ( strpos( $order_col, 'custom_field' ) !== false ) {
$sort_meta_key = self::set_custom_filed_sort( $orderby, $order_col, $orders, $sort_param ); 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 ); $meta_query[] = self::get_all_post_args( $sort_meta_key, $type );
} else { } else {
$orderby[ $order_col ] = $sort_param[ $orders['sort'] ]; $orderby[ $order_col ] = $sort_param[ $orders['sort'] ];
} }