bacd6a1d72
・検索と通常表示のオプションを追加 ・項目の表示順を修正 ・選択値によって表示/非表示の切り替えやフォームをDisabledにする等修正 ・アンインストーラーを追加
311 lines
9.7 KiB
PHP
311 lines
9.7 KiB
PHP
<?php
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
exit;
|
|
} // Exit if accessed directly
|
|
|
|
if ( ! class_exists( 'APOP_UI' ) ) {
|
|
class APOP_UI {
|
|
|
|
public static function get_order_type( $type ) {
|
|
$type_data = get_option( $type );
|
|
if ( ! $type_data ) {
|
|
return 1;
|
|
}
|
|
|
|
return $type_data;
|
|
}
|
|
|
|
public static function get_all_search_normal_posts( $type, $key, $exclude_posts = [] ) {
|
|
$args = [
|
|
'post_status' => array( 'publish', 'draft' ),
|
|
'posts_per_page' => - 1,
|
|
];
|
|
|
|
if ( $type == 'registered' ) {
|
|
$args['orderby'] = 'meta_value_num';
|
|
$args['order'] = 'ASC';
|
|
$args['meta_key'] = '_apop_post_' . $key;
|
|
} else {
|
|
$exclude = implode( ',', $exclude_posts );
|
|
if ( ! empty( $exclude ) ) {
|
|
$args['exclude'] = $exclude;
|
|
}
|
|
}
|
|
|
|
return get_posts( $args );
|
|
}
|
|
|
|
|
|
public static function get_all_taxonomies( $key ) {
|
|
if ( $key !== 'taxonomy' ) {
|
|
return get_terms( array( 'taxonomy' => $key, 'get' => 'all' ) );
|
|
}
|
|
|
|
return get_taxonomies( array( 'public' => true, '_builtin' => false ) );
|
|
}
|
|
|
|
public static function get_cat_tag_list( $target, $key ): array {
|
|
$opt = get_option( '_apop_' . $target . '_order' );
|
|
|
|
if ( ! isset( $opt['target_cat'] ) ) {
|
|
return array();
|
|
}
|
|
|
|
if ( $key == 'taxonomy' ) {
|
|
return self::create_custom_tax_term( $opt );
|
|
} else {
|
|
return self::create_tax_term( $opt, $key );
|
|
}
|
|
}
|
|
|
|
private static function create_custom_tax_term( $opt ): array {
|
|
$tax_data = [];
|
|
foreach ( $opt['target_cat'] as $tax_id => $status ) {
|
|
//フラグが立っているカスタムタクソノミーは情報を取得する
|
|
if ( $status ) {
|
|
$args = array(
|
|
'taxonomy' => get_term( $tax_id )->taxonomy,
|
|
'hide_empty' => 0,
|
|
'include' => $tax_id,
|
|
);
|
|
$tax_data[] = get_terms( $args );
|
|
|
|
}
|
|
}
|
|
|
|
return $tax_data;
|
|
}
|
|
|
|
private static function create_tax_term( $opt, $key ) {
|
|
$include = array();
|
|
foreach ( $opt['target_cat'] as $tax_id => $status ) {
|
|
//フラグが立っているタクソノミーIDを取得
|
|
if ( $status ) {
|
|
$include[] = $tax_id;
|
|
}
|
|
}
|
|
$include_tax = implode( ',', $include );
|
|
if ( ! empty( $include_tax ) ) {
|
|
$args = array(
|
|
'taxonomy' => $key,
|
|
'hide_empty' => 0,
|
|
'include' => $include_tax,
|
|
);
|
|
|
|
return get_terms( $args );
|
|
}
|
|
|
|
return array();
|
|
}
|
|
|
|
public static function create_order_list( $tax_data, $tax_key, $exclude_posts ): array {
|
|
$target_posts = self::get_sort_post_list( $tax_data->term_id, $tax_key, $tax_data->taxonomy );
|
|
$list = [];
|
|
$sort_num = 0;
|
|
foreach ( $target_posts as $key => $target_post ) {
|
|
$sort_num = $key + 1;
|
|
$exclude_posts[] = $target_post->ID;
|
|
$list[] = '
|
|
<li class="product-list"><span class="sort-num-label">' . $sort_num . '</span>' . get_the_title( $target_post->ID ) . '
|
|
<input type="hidden" class="list_order"
|
|
name="_apop_post_' . self::create_post_sort_key( $tax_data->taxonomy, $tax_key ) . '[post_sort][' . $tax_data->term_id . '][' . $target_post->ID . ']"
|
|
value="' . $sort_num . '">
|
|
</li>';
|
|
}
|
|
|
|
return array(
|
|
implode( PHP_EOL, $list ),
|
|
$exclude_posts,
|
|
$sort_num,
|
|
);
|
|
|
|
}
|
|
|
|
private static function get_sort_post_list( $tax_id, $search_param, $tax_name ) {
|
|
$args = [
|
|
'post_status' => array( 'publish', 'draft' ),
|
|
'posts_per_page' => - 1,
|
|
'orderby' => 'meta_value_num',
|
|
'order' => 'ASC',
|
|
'meta_key' => '_apop_post_' . self::create_post_sort_key( $tax_name, $search_param ) . '_' . $tax_id,
|
|
];
|
|
|
|
|
|
self::create_post_tax_query( $args, $search_param, $tax_name, $tax_id );
|
|
|
|
return get_posts( $args );
|
|
}
|
|
|
|
private static function create_post_sort_key( $tax_name, $tax_key ) {
|
|
if ( $tax_key == 'taxonomy' ) {
|
|
return 'tax';
|
|
}
|
|
|
|
return $tax_name;
|
|
}
|
|
|
|
private static function create_post_tax_query( &$args, $key, $tax_name, $tax_id ) {
|
|
if ( $key == 'taxonomy' ) {
|
|
$args['tax_query'] = array(
|
|
array(
|
|
'taxonomy' => $tax_name,
|
|
'field' => 'term_id',
|
|
'terms' => $tax_id,
|
|
'include_children' => false
|
|
)
|
|
);
|
|
} else {
|
|
$args[ $key ] = $tax_id;
|
|
}
|
|
}
|
|
|
|
public static function create_product_none_order_list( $tax_data, $tax_key, $exclude_posts, $sort_num ): string {
|
|
$target_posts_no_order = self::get_none_sort_post_list( $tax_data->term_id, $tax_key, $exclude_posts, $tax_data->taxonomy );
|
|
if ( count( $target_posts_no_order ) > 0 ) {
|
|
$sort_num = $sort_num ?? 0;
|
|
foreach ( $target_posts_no_order as $key => $target_post_no_order ) {
|
|
$sort_num_no_order = $sort_num + $key + 1;
|
|
$list[] = '
|
|
<li class="product-list no_order">■
|
|
<span class="sort-num-label">' . $sort_num_no_order . '</span>' . get_the_title( $target_post_no_order->ID ) . '
|
|
<input type="hidden" class="list_order"
|
|
name="_apop_post_' . self::create_post_sort_key( $tax_data->taxonomy, $tax_key ) . '[post_sort][' . $tax_data->term_id . '][' . $target_post_no_order->ID . ']"
|
|
value="' . $sort_num_no_order . '">
|
|
</li>';
|
|
}
|
|
if ( isset( $list ) ) {
|
|
return implode( PHP_EOL, $list );
|
|
}
|
|
}
|
|
|
|
return '';
|
|
}
|
|
|
|
private static function get_none_sort_post_list( $tax_id, $search_param, $exclude_posts, $tax_name ) {
|
|
|
|
$args = array(
|
|
'post_status' => array( 'publish', 'draft' ),
|
|
'posts_per_page' => - 1,
|
|
);
|
|
|
|
$exclude = implode( ',', $exclude_posts );
|
|
if ( ! empty( $exclude ) ) {
|
|
$args['exclude'] = $exclude;
|
|
}
|
|
|
|
self::create_post_tax_query( $args, $search_param, $tax_name, $tax_id );
|
|
|
|
return get_posts( $args );
|
|
}
|
|
|
|
public static function create_cat_per_page( $opt_per_page, $type ): array {
|
|
$cat_per_page = $opt_per_page[ $type ] ?? 'default';
|
|
$checked = '';
|
|
$cat_per_page_num = '';
|
|
if ( isset( $opt_per_page[ $type ] ) ) {
|
|
if ( $opt_per_page[ $type ] != 'default'
|
|
&& $opt_per_page[ $type ] != '-1'
|
|
&& $opt_per_page[ $type ] != 'all' ) {
|
|
$checked = ' checked="checked"';
|
|
$cat_per_page_num = $cat_per_page;
|
|
}
|
|
}
|
|
|
|
return array(
|
|
'_per_page' => $cat_per_page,
|
|
'_checked' => $checked,
|
|
'_per_page_num' => $cat_per_page_num,
|
|
);
|
|
}
|
|
|
|
public static function create_search_normal_list( $type ): array {
|
|
$name_key = 'apop_' . $type . '_order_param';
|
|
$order_param = get_option( '_' . $name_key );
|
|
$target_keys = self::set_search_normal_target_keys( $order_param );
|
|
$target_values = array(
|
|
'date' => '登録日',
|
|
'title' => 'タイトル',
|
|
'ID' => 'ID',
|
|
'modified' => '更新日',
|
|
);
|
|
$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 . '">
|
|
<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 ) . '>
|
|
<b>' . $target_values[ $target_key ] . '</b>
|
|
</label>
|
|
|
|
<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"
|
|
name="_' . $name_key . '[' . $target_key . '][sort]"
|
|
value="2"' . self::set_search_normal_checked( $sort, 2 ) . '>降順</label>
|
|
</li>';
|
|
}
|
|
|
|
return array(
|
|
implode( PHP_EOL, $list ),
|
|
$alert,
|
|
);
|
|
}
|
|
|
|
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(
|
|
'date',
|
|
'title',
|
|
'ID',
|
|
'modified',
|
|
);
|
|
}
|
|
|
|
private static function set_search_normal_checked( $param, $default ): string {
|
|
if ( $param == $default ) {
|
|
return ' checked="checked"';
|
|
}
|
|
|
|
return '';
|
|
}
|
|
|
|
public static function input_post_filter( $var_name, $type ) {
|
|
if ( $type == 'array' ) {
|
|
return filter_input( INPUT_POST, $var_name, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY );
|
|
}
|
|
if ( $type == 'str' ) {
|
|
return filter_input( INPUT_POST, $var_name );
|
|
}
|
|
}
|
|
|
|
public static function is_disp_per_page( &$disp, $disp_per_page ) {
|
|
if ( $disp_per_page ) {
|
|
$disp = true;
|
|
}
|
|
}
|
|
|
|
public static function create_disp_class( $disp ) {
|
|
if ( ! $disp ) {
|
|
return ' class="hide_list"';
|
|
}
|
|
}
|
|
|
|
public static function create_none_select_msg( $str ) {
|
|
return '<p>並べ替えをカスタマイズする' . $str . 'は選択されていません。</p>';
|
|
}
|
|
|
|
public static function none_registerd_alert_msg() {
|
|
return '<div class="no_registered_exp">■の項目は未登録項目です。ドラッグして並び順を変更後に「変更を保存」をクリックしてください。</div>';
|
|
}
|
|
|
|
}
|
|
} |