WP PLUGIN アーカイブページの投稿表示順設定
・検索と通常表示のオプションを追加 ・項目の表示順を修正 ・選択値によって表示/非表示の切り替えやフォームをDisabledにする等修正 ・アンインストーラーを追加
This commit is contained in:
+77
-33
@@ -29,7 +29,7 @@ if ( ! class_exists( 'APOP' ) ) {
|
||||
add_submenu_page(
|
||||
'apop_post_sort', // parent_slug
|
||||
'Sort taxonomy', // page_title
|
||||
'設定', // menu_title
|
||||
'表示件数・対象設定', // menu_title
|
||||
'administrator', // capability
|
||||
'apop_post_sort', // menu_slug
|
||||
array( $this, 'display_setting_page' ) // function
|
||||
@@ -45,63 +45,108 @@ if ( ! class_exists( 'APOP' ) ) {
|
||||
}
|
||||
|
||||
public function display_setting_page() {
|
||||
if ( isset( $_POST['_apop_cat_order'] )
|
||||
|| isset( $_POST['_apop_tag_order'] )
|
||||
|| isset( $_POST['_apop_tax_order'] ) ) {
|
||||
|
||||
check_admin_referer( 'sh_options' );
|
||||
$settings = array(
|
||||
'_apop_cat_order', //カテゴリー設定
|
||||
'_apop_per_page', //1ページ表示件数
|
||||
'_apop_tag_order', //タグ設定
|
||||
'_apop_tax_order', //カスタムタクソノミー設定
|
||||
);
|
||||
|
||||
$settings = array(
|
||||
'_apop_cat_order', //カテゴリー設定
|
||||
'_apop_per_page', //1ページ表示件数
|
||||
'_apop_tag_order', //タグ設定
|
||||
'_apop_tax_order', //カスタムタクソノミー設定
|
||||
);
|
||||
foreach ( $settings as $setting ) {
|
||||
$opt = $_POST[ $setting ];
|
||||
foreach ( $settings as $setting ) {
|
||||
$opt = APOP_UI::input_post_filter( $setting, 'array' );
|
||||
if ( $opt ) {
|
||||
check_admin_referer( 'sh_options' );
|
||||
update_option( $setting, $opt );
|
||||
require_once self::TEMPLATE_DIR . 'success.php';
|
||||
}
|
||||
require_once self::TEMPLATE_DIR . 'success.php';
|
||||
|
||||
}
|
||||
|
||||
require_once self::TEMPLATE_DIR . 'setting.php';
|
||||
|
||||
}
|
||||
|
||||
|
||||
public function show_option_page() {
|
||||
//-------------------------------------
|
||||
// 通常表示
|
||||
// ------------------------------------
|
||||
$apop_normal_order = APOP_UI::input_post_filter( '_apop_normal_order', 'str' );
|
||||
$apop_normal_order_param = APOP_UI::input_post_filter( '_apop_normal_order_param', 'array' );
|
||||
$apop_post_normal = APOP_UI::input_post_filter( '_apop_post_normal', 'array' );
|
||||
|
||||
//検索の投稿表示順設定
|
||||
if ( isset( $_POST['_apop_post_search'] ) ) {
|
||||
if ( $apop_normal_order ) {
|
||||
check_admin_referer( 'sh_options' );
|
||||
$this->update_search_sort( 'search' );
|
||||
update_option( '_apop_normal_order', $apop_normal_order );
|
||||
require_once self::TEMPLATE_DIR . 'success.php';
|
||||
}
|
||||
|
||||
if ( isset( $_POST['_apop_search_order'] ) ) {
|
||||
//表示順設定
|
||||
if ( $apop_normal_order_param ) {
|
||||
check_admin_referer( 'sh_options' );
|
||||
update_option( '_apop_search_order', $_POST['_apop_search_order'] );
|
||||
update_option( '_apop_normal_order_param', $apop_normal_order_param );
|
||||
require_once self::TEMPLATE_DIR . 'success.php';
|
||||
}
|
||||
|
||||
//カスタム表示順設定
|
||||
if ( $apop_post_normal ) {
|
||||
check_admin_referer( 'sh_options' );
|
||||
$this->update_search_normal_sort( $apop_post_normal, 'normal' );
|
||||
require_once self::TEMPLATE_DIR . 'success.php';
|
||||
}
|
||||
|
||||
//-------------------------------------
|
||||
// 検索表示
|
||||
// ------------------------------------
|
||||
$apop_search_order = APOP_UI::input_post_filter( '_apop_search_order', 'str' );
|
||||
$apop_search_order_param = APOP_UI::input_post_filter( '_apop_search_order_param', 'array' );
|
||||
$apop_post_search = APOP_UI::input_post_filter( '_apop_post_search', 'array' );
|
||||
|
||||
if ( $apop_search_order ) {
|
||||
check_admin_referer( 'sh_options' );
|
||||
update_option( '_apop_search_order', $apop_search_order );
|
||||
require_once self::TEMPLATE_DIR . 'success.php';
|
||||
}
|
||||
|
||||
//表示順設定
|
||||
if ( $apop_search_order_param ) {
|
||||
check_admin_referer( 'sh_options' );
|
||||
update_option( '_apop_search_order_param', $apop_search_order_param );
|
||||
require_once self::TEMPLATE_DIR . 'success.php';
|
||||
}
|
||||
|
||||
//カスタム表示順設定
|
||||
if ( $apop_post_search ) {
|
||||
check_admin_referer( 'sh_options' );
|
||||
$this->update_search_normal_sort( $apop_post_search, 'search' );
|
||||
require_once self::TEMPLATE_DIR . 'success.php';
|
||||
}
|
||||
|
||||
//-------------------------------------
|
||||
// タクソノミー
|
||||
// ------------------------------------
|
||||
$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 ( isset( $_POST['_apop_post_category'] ) ) {
|
||||
if ( $apop_post_category ) {
|
||||
check_admin_referer( 'sh_options' );
|
||||
$this->update_post_sort( 'category' );
|
||||
$this->update_post_sort( 'category', $apop_post_category );
|
||||
require_once self::TEMPLATE_DIR . 'success.php';
|
||||
}
|
||||
|
||||
//タグの投稿表示順設定
|
||||
if ( isset( $_POST['_apop_post_post_tag'] ) ) {
|
||||
if ( $apop_post_post_tag ) {
|
||||
check_admin_referer( 'sh_options' );
|
||||
$this->update_post_sort( 'post_tag' );
|
||||
$this->update_post_sort( 'post_tag', $apop_post_post_tag );
|
||||
require_once self::TEMPLATE_DIR . 'success.php';
|
||||
}
|
||||
|
||||
//カスタム分類の投稿表示順設定
|
||||
if ( isset( $_POST['_apop_post_tax'] ) ) {
|
||||
if ( $apop_post_tax ) {
|
||||
check_admin_referer( 'sh_options' );
|
||||
$this->update_post_sort( 'tax' );
|
||||
$this->update_post_sort( 'tax', $apop_post_tax );
|
||||
require_once self::TEMPLATE_DIR . 'success.php';
|
||||
}
|
||||
|
||||
@@ -109,21 +154,20 @@ if ( ! class_exists( 'APOP' ) ) {
|
||||
}
|
||||
|
||||
//投稿表示順の設定
|
||||
private function update_post_sort( $target ) {
|
||||
$target = '_apop_post_' . $target;
|
||||
$posts_sort = $_POST[ $target ]['post_sort'];
|
||||
private function update_post_sort( $target, $post_value ) {
|
||||
$posts_sort = $post_value['post_sort'];
|
||||
foreach ( $posts_sort as $cat_id => $posts ) {
|
||||
$sort_key = $target . '_' . $cat_id;
|
||||
$sort_key = '_apop_post_' . $target . '_' . $cat_id;
|
||||
foreach ( $posts as $post_id => $sort ) {
|
||||
update_post_meta( $post_id, $sort_key, $sort );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function update_search_sort() {
|
||||
$posts_sort = $_POST['_apop_post_search']['post_sort'];
|
||||
private function update_search_normal_sort( $apop_post_search_normal, $key ) {
|
||||
$posts_sort = $apop_post_search_normal['post_sort'];
|
||||
foreach ( $posts_sort as $post_id => $sort ) {
|
||||
update_post_meta( $post_id, '_apop_post_search', $sort );
|
||||
update_post_meta( $post_id, '_apop_post_' . $key, $sort );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+109
-7
@@ -6,7 +6,16 @@ if ( ! defined( 'ABSPATH' ) ) {
|
||||
if ( ! class_exists( 'APOP_UI' ) ) {
|
||||
class APOP_UI {
|
||||
|
||||
public static function get_all_search_posts( $type, $exclude_posts = [] ) {
|
||||
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,
|
||||
@@ -15,13 +24,14 @@ if ( ! class_exists( 'APOP_UI' ) ) {
|
||||
if ( $type == 'registered' ) {
|
||||
$args['orderby'] = 'meta_value_num';
|
||||
$args['order'] = 'ASC';
|
||||
$args['meta_key'] = '_apop_post_search';
|
||||
$args['meta_key'] = '_apop_post_' . $key;
|
||||
} else {
|
||||
$exclude = implode( ',', $exclude_posts );
|
||||
if ( ! empty( $exclude ) ) {
|
||||
$args['exclude'] = $exclude;
|
||||
}
|
||||
}
|
||||
|
||||
return get_posts( $args );
|
||||
}
|
||||
|
||||
@@ -156,7 +166,7 @@ value="' . $sort_num . '">
|
||||
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">
|
||||
<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 . ']"
|
||||
@@ -190,20 +200,112 @@ value="' . $sort_num_no_order . '">
|
||||
|
||||
public static function create_cat_per_page( $opt_per_page, $type ): array {
|
||||
$cat_per_page = $opt_per_page[ $type ] ?? 'default';
|
||||
$set = '';
|
||||
$checked = '';
|
||||
$cat_per_page_num = '';
|
||||
if ( isset( $opt_per_page[ $type ] ) ) {
|
||||
if ( $opt_per_page[ $type ] != 'default' && $opt_per_page[ $type ] != '-1' ) {
|
||||
$set = ' checked="checked"';
|
||||
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,
|
||||
'_set' => $set,
|
||||
'_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>';
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user