WP PLUGIN ソート対象拡張

・通常、検索、タクソノミーにソート用カスタムフィールドの設定入力欄を追加
・通常と検索にカスタムフィールド検索処理を実装
・タクソノミーに通常+カスタムフィールドのメニューを追加
・タクソノミーに通常+カスタムフィールドとドラッグソートの切り替え用ラジオボタンを追加
This commit is contained in:
2021-05-09 18:38:56 +09:00
parent 3dc1fc30fa
commit f07c318f93
9 changed files with 317 additions and 178 deletions
+16
View File
@@ -125,10 +125,26 @@ if ( ! class_exists( 'APOP' ) ) {
//-------------------------------------
// タクソノミー
// ------------------------------------
$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_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_tax = APOP_UI::input_post_filter( '_apop_post_tax', 'array' );
//カテゴリーのソートタイプ設定
if ( $tax_sort_type ) {
check_admin_referer( 'sh_options' );
update_option( '_apop_tax_sort_type', $tax_sort_type );
require_once self::TEMPLATE_DIR . 'success.php';
}
//カテゴリーの標準ソートパラメータ
if ( $apop_tax_order_param ) {
check_admin_referer( 'sh_options' );
update_option( '_apop_tax_order_param', $apop_tax_order_param );
require_once self::TEMPLATE_DIR . 'success.php';
}
//カテゴリーの投稿表示順設定
if ( $apop_post_category ) {
check_admin_referer( 'sh_options' );
+105 -28
View File
@@ -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>';
}