WP PLUGIN ソート対象拡張
・通常、検索、タクソノミーにソート用カスタムフィールドの設定入力欄を追加 ・通常と検索にカスタムフィールド検索処理を実装 ・タクソノミーに通常+カスタムフィールドのメニューを追加 ・タクソノミーに通常+カスタムフィールドとドラッグソートの切り替え用ラジオボタンを追加
This commit is contained in:
+105
-28
@@ -15,7 +15,9 @@ if ( ! class_exists( 'APOP_UI' ) ) {
|
||||
return $type_data;
|
||||
}
|
||||
|
||||
public static function get_all_search_normal_posts( $key ): string {
|
||||
public static function get_all_search_normal_posts(
|
||||
$key
|
||||
): string {
|
||||
$meta_key = '_apop_post_' . $key;
|
||||
$args = self::create_search_normal_args( $meta_key );
|
||||
$posts_data = get_posts( $args );
|
||||
@@ -95,17 +97,20 @@ if ( ! class_exists( 'APOP_UI' ) ) {
|
||||
foreach ( $opt['target_cat'] as $tax_id => $status ) {
|
||||
//フラグが立っているカスタムタクソノミーは情報を取得する
|
||||
if ( $status ) {
|
||||
$args = array(
|
||||
$args = array(
|
||||
'taxonomy' => get_term( $tax_id )->taxonomy,
|
||||
'hide_empty' => 0,
|
||||
'include' => $tax_id,
|
||||
);
|
||||
$tax_data[] = get_terms( $args );
|
||||
$tax_data = array_merge( $tax_data, get_terms( $args ) );
|
||||
|
||||
}
|
||||
}
|
||||
if ( count( $tax_data ) > 0 ) {
|
||||
return $tax_data;
|
||||
}
|
||||
|
||||
return $tax_data;
|
||||
return array();
|
||||
}
|
||||
|
||||
private static function create_tax_term( $opt, $key ) {
|
||||
@@ -261,46 +266,125 @@ value="' . $sort_num . '">
|
||||
);
|
||||
}
|
||||
|
||||
public static function create_search_normal_list( $type ): array {
|
||||
$name_key = 'apop_' . $type . '_order_param';
|
||||
$order_param = get_option( '_' . $name_key );
|
||||
public static function create_search_normal_list( $type, $id = null ): array {
|
||||
$name_keys = self::create_name_keys( $id, $type );
|
||||
$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 ( is_null( $id ) ) {
|
||||
$order_param = $order_param_base;
|
||||
}
|
||||
if ( isset( $order_param_base[ $id ] ) ) {
|
||||
$order_param = $order_param_base[ $id ];
|
||||
}
|
||||
$target_keys = self::set_search_normal_target_keys( $order_param );
|
||||
$target_values = array(
|
||||
'date' => '登録日',
|
||||
'title' => 'タイトル',
|
||||
'ID' => 'ID',
|
||||
'modified' => '更新日',
|
||||
'date' => '登録日',
|
||||
'title' => 'タイトル',
|
||||
'ID' => 'ID',
|
||||
'modified' => '更新日',
|
||||
'custom_field' => 'カスタムフィールド',
|
||||
);
|
||||
$list = array();
|
||||
foreach ( $target_keys as $target_key ) {
|
||||
$use = $order_param[ $target_key ]['use'] ?? 0;
|
||||
$sort = $order_param[ $target_key ]['sort'] ?? 2;
|
||||
$alert = ! $order_param[ $target_key ]['use'] ? '<p>並べ替えを登録するには「変更を保存」をクリックしてください。</p>' : '';
|
||||
$no_order_class = ! $order_param[ $target_key ]['use'] ? ' no_order' : '';
|
||||
$list[] = '<li class="product-list' . $no_order_class . '">
|
||||
$cnv_order_params = self::set_order_list_param( $order_param, $target_key );
|
||||
$use = $cnv_order_params['use'];
|
||||
$sort = $cnv_order_params['sort'];
|
||||
$alert = $cnv_order_params['alert'];
|
||||
$no_order_class = $cnv_order_params['no_order_class'];
|
||||
if ( $target_key == 'custom_field' ) {
|
||||
$meta_key = $cnv_order_params['custom_field']['meta_key'];
|
||||
$value_type = $cnv_order_params['custom_field']['value_type'];
|
||||
$field_metakey_input = self::create_custom_field_sort_type( $name_key, $target_key, $meta_key, $value_type );
|
||||
}
|
||||
$list[] = '<li class="product-list' . $no_order_class . '">
|
||||
<div class="product-list-type-label"><b>' . $target_values[ $target_key ] . '</b></div>
|
||||
<div class="product-list-sort-type">
|
||||
<label>
|
||||
<input type="hidden" name="_' . $name_key . '[' . $target_key . '][use]" value="0"' . self::set_search_normal_checked( $use, 0 ) . '>
|
||||
有効:<input type="checkbox" name="_' . $name_key . '[' . $target_key . '][use]" value="1"' . self::set_search_normal_checked( $use, 1 ) . '>
|
||||
</label>
|
||||
<label><input class="' . $name_key . '" type="radio"
|
||||
<label>
|
||||
<input class="' . $name_key . '" type="radio"
|
||||
name="_' . $name_key . '[' . $target_key . '][sort]"
|
||||
value="1"' . self::set_search_normal_checked( $sort, 1 ) . '>昇順</label>
|
||||
|
||||
<label><input class="' . $name_key . '" type="radio"
|
||||
<label>
|
||||
<input class="' . $name_key . '" type="radio"
|
||||
name="_' . $name_key . '[' . $target_key . '][sort]"
|
||||
value="2"' . self::set_search_normal_checked( $sort, 2 ) . '>降順</label>
|
||||
</div>
|
||||
</div>
|
||||
</li>';
|
||||
}
|
||||
|
||||
return array(
|
||||
implode( PHP_EOL, $list ),
|
||||
$alert,
|
||||
$field_metakey_input,
|
||||
);
|
||||
}
|
||||
|
||||
private static function create_custom_field_sort_type( $name_key, $target_key, $meta_key, $value_type ): string {
|
||||
return '<div class="sort_custom_field">カスタムフィールドキー:
|
||||
<input type="text" name="_' . $name_key . '[' . $target_key . '][field][meta_key]" value="' . $meta_key . '">
|
||||
値タイプ:
|
||||
<label><input type="radio"
|
||||
name="_' . $name_key . '[' . $target_key . '][field][value_type]"
|
||||
value="meta_value"' . self::set_search_normal_checked( $value_type, 'meta_value' ) . '>テキスト</label>
|
||||
<label><input type="radio"
|
||||
name="_' . $name_key . '[' . $target_key . '][field][value_type]"
|
||||
value="meta_value_num"' . self::set_search_normal_checked( $value_type, 'meta_value_num' ) . '>数値</label>
|
||||
</div>';
|
||||
}
|
||||
|
||||
private static function set_order_list_param( $order_param, $target_key ): array {
|
||||
|
||||
$param = array(
|
||||
'use' => 0,
|
||||
'sort' => 2,
|
||||
'alert' => '<p>並べ替えを登録するには「変更を保存」をクリックしてください。</p>',
|
||||
'no_order_class' => ' no_order',
|
||||
);
|
||||
if ( $target_key == 'custom_field' ) {
|
||||
$param['custom_field'] = array(
|
||||
'meta_key' => '',
|
||||
'value_type' => 'meta_value',
|
||||
);
|
||||
}
|
||||
|
||||
if ( isset( $order_param[ $target_key ] ) ) {
|
||||
$param = array(
|
||||
'use' => $order_param[ $target_key ]['use'] ?? 0,
|
||||
'sort' => $order_param[ $target_key ]['sort'] ?? 2,
|
||||
'alert' => ! $order_param[ $target_key ]['use'] ? '<p>並べ替えを登録するには「変更を保存」をクリックしてください。</p>' : '',
|
||||
'no_order_class' => ! $order_param[ $target_key ]['use'] ? ' no_order' : '',
|
||||
);
|
||||
if ( $target_key == 'custom_field' ) {
|
||||
$param['custom_field'] = array(
|
||||
'meta_key' => $order_param[ $target_key ]['field']['meta_key'] ?? '',
|
||||
'value_type' => $order_param[ $target_key ]['field']['value_type'] ?? 'meta_value',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return $param;
|
||||
|
||||
}
|
||||
|
||||
private static function create_name_keys( $id, $type ): array {
|
||||
if ( is_null( $id ) ) {
|
||||
return array(
|
||||
'name_key' => 'apop_' . $type . '_order_param',
|
||||
'get_option_key' => '_' . 'apop_' . $type . '_order_param',
|
||||
);
|
||||
} else {
|
||||
return array(
|
||||
'name_key' => 'apop_tax_order_param[' . $id . ']',
|
||||
'get_option_key' => '_apop_tax_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 );
|
||||
@@ -311,6 +395,7 @@ value="' . $sort_num . '">
|
||||
'title',
|
||||
'ID',
|
||||
'modified',
|
||||
'custom_field'
|
||||
);
|
||||
}
|
||||
|
||||
@@ -337,14 +422,6 @@ value="' . $sort_num . '">
|
||||
}
|
||||
}
|
||||
|
||||
public static function create_disp_class( $disp ): string {
|
||||
if ( ! $disp ) {
|
||||
return ' class="hide_list"';
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
public static function create_none_select_msg( $str ): string {
|
||||
return '<p>並べ替えをカスタマイズする' . $str . 'は選択されていません。</p>';
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user