commit 2ed2b251945af1e5a562b3b917c4285be9fb68b9 Author: nobu Date: Mon May 3 18:51:02 2021 +0900 WP PLUGIN アーカイブ毎に投稿表示順を設定する ・ベース部分(カテゴリー、投稿)の表示順用カスタムフィールド設定登録の作成 diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..af2f81c --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# デフォルトの無視ファイル +/shelf/ +/workspace.xml +# データソースのローカルストレージがファイルを無視しました +/dataSources/ +/dataSources.local.xml +# エディターベースの HTTP クライアントリクエスト +/httpRequests/ diff --git a/.idea/cake_config_setting_v0_6.xml b/.idea/cake_config_setting_v0_6.xml new file mode 100644 index 0000000..0c23d4a --- /dev/null +++ b/.idea/cake_config_setting_v0_6.xml @@ -0,0 +1,31 @@ + + + + + + \ No newline at end of file diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 0000000..4c9e052 --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,56 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..79ee123 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..baef841 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/nb-post-sort-cat-order.iml b/.idea/nb-post-sort-cat-order.iml new file mode 100644 index 0000000..c956989 --- /dev/null +++ b/.idea/nb-post-sort-cat-order.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/php.xml b/.idea/php.xml new file mode 100644 index 0000000..3571588 --- /dev/null +++ b/.idea/php.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/css/base.css b/css/base.css new file mode 100644 index 0000000..21fe4c9 --- /dev/null +++ b/css/base.css @@ -0,0 +1,77 @@ +@charset "UTF-8"; + +.post-order-nav ul { + display: flex; + margin-bottom: 0; +} + +.post-order-box { + width: 95%; + margin: 2em 0; + +} + +.post-order-nav li { + width: 20em; + padding: .5em; + margin-bottom: -1px; + font-size: 18px; + text-align: center; + background: #2c2c2e; + color: #fff; + cursor: pointer; + border-top-right-radius: 6px; + border-top-left-radius: 6px; + opacity: .5; +} + +.post-order-nav li.en { + opacity: 1; +} + +.post-order-nav li:first-child { + margin-right: .3em; +} + +h3 { + font-weight: normal; + font-size: 16px; +} + +.list-orders-outer { + width: 100%; + display: flex; + justify-content: flex-start; + flex-wrap: wrap; +} + +.list-orders-inner { + width: calc((100% - 10px) / 3); + border: 1px solid #999; + background: #fff; + padding: .5em; +} + +.post-order-list { + margin: .5em 1em 1em 0; +} + +.product-list { + padding: .5em; + border-radius: 4px; + background: #2271B1; + cursor: move; + color: #fff; +} + +.list_order { + width: 2em; +} + +.sort-num-label::before { + content: "["; +} + +.sort-num-label::after { + content: "]"; +} \ No newline at end of file diff --git a/js/post-sort-cat-order.js b/js/post-sort-cat-order.js new file mode 100644 index 0000000..56ec788 --- /dev/null +++ b/js/post-sort-cat-order.js @@ -0,0 +1,22 @@ +jQuery(function ($) { + + let cat_ul_count = $('.post-order-list').length; //カテゴリ総数 + for (let i = 0; i < cat_ul_count; i++) { + let my_list = $('.post-order-list:eq(' + i + ')'); + my_list.sortable( + { + update: function () { + let list_count = $('.product-list', my_list).length; + for (let j = 0; j < list_count; j++) { + let list_order = j + 1; + $('.list_order:eq(' + j + ')', my_list).val(list_order); + $('.list_order:eq(' + j + ')', my_list).siblings('.sort-num-label').text(list_order); + } + } + } + ); + my_list.disableSelection(); + } + + +}); \ No newline at end of file diff --git a/lib/activate.php b/lib/activate.php new file mode 100644 index 0000000..67cb2d7 --- /dev/null +++ b/lib/activate.php @@ -0,0 +1,43 @@ +term_id ); + foreach ( $posts as $post ) { + self::create_sort_field( $post->ID, $category->term_id ); + } + } + } + + //カテゴリーの投稿を取得する + public static function get_category_posts( $cat_id ) { + $args = array( + 'post_type' => 'post', + 'post_status' => array( 'publish', 'draft' ), + 'posts_per_page' => - 1, + 'category' => $cat_id, + ); + + return get_posts( $args ); + } + + /** + * ソート用のカスタムフィールド作成 + * + * @param $post_id + * @param $cat_id + */ + public static function create_sort_field( $post_id, $cat_id ) { + $sort_key = 'nb_post_sort_' . $cat_id; + //カスタムフィールドが存在しなければ追加する + $sort_filed = get_post_meta( $post_id, $sort_key, true ); + if ( empty( $sort_filed ) ) { + update_post_meta( $post_id, $sort_key, 0 ); + } + } + +} \ No newline at end of file diff --git a/lib/post-sort-cat-order.php b/lib/post-sort-cat-order.php new file mode 100644 index 0000000..5a35825 --- /dev/null +++ b/lib/post-sort-cat-order.php @@ -0,0 +1,125 @@ +update_post_sort(); + require_once self::TEMPLATE_DIR . 'success.php'; + } + + require_once self::TEMPLATE_DIR . 'sort.php'; + } + + //投稿表示順の設定 + private function update_post_sort() { + $posts_sort = $_POST['nb_post_cat']['post_sort']; + foreach ( $posts_sort as $cat_id => $posts ) { + $sort_key = 'nb_post_sort_' . $cat_id; + foreach ( $posts as $post_id => $sort ) { + update_post_meta( $post_id, $sort_key, $sort ); + } + } + } + + + /** + * 対象カテゴリー + * + * @param $opt + * + * @return array + */ + public static function get_category_list( $opt ): array { + if ( isset( $opt['target_cat'] ) ) { + $include_cat = implode( ',', $opt['target_cat'] ); + if ( ! empty( $include_cat ) ) { + $category_args = array( + 'hide_empty' => 0, + 'include' => $include_cat, + ); + + return get_categories( $category_args ); + } + } + + return array(); + } + + +// public function add_cat_post_order( $new_status, $old_status, $post ) { +// //リビジョンはパスする +// if ( wp_is_post_revision( $post->ID ) ) { +// return; +// } +// +// } + + public static function get_sort_post_list( $cat_id ) { + $product_args = [ + 'post_type' => 'post', + 'post_status' => array( 'publish', 'draft' ), + 'posts_per_page' => - 1, + 'category' => $cat_id, + 'orderby' => 'meta_value_num', + 'order' => 'ASC', + 'meta_key' => 'nb_post_sort_' . $cat_id, + ]; + + return get_posts( $product_args ); + } + + } +} + diff --git a/nb-post-sort-cat-order.php b/nb-post-sort-cat-order.php new file mode 100644 index 0000000..b0dbb95 --- /dev/null +++ b/nb-post-sort-cat-order.php @@ -0,0 +1,27 @@ + + +
+
+ +

対象カテゴリー選択

+
    + +
  • +
    + +
    +
  • + +
+

+

+
\ No newline at end of file diff --git a/template/sort.php b/template/sort.php new file mode 100644 index 0000000..a0ff51a --- /dev/null +++ b/template/sort.php @@ -0,0 +1,34 @@ + +
+
+ +

投稿表示順設定

+
+ +

対象カテゴリーを選択してください。[ カテゴリー選択

+ + +
+

name; ?>

+ term_id ); ?> +
    + $target_post ): $sort_num = $key + 1; ?> +
  • + + ID ); ?> + +
  • + +
+
+ + +
+

+
+
\ No newline at end of file diff --git a/template/success.php b/template/success.php new file mode 100644 index 0000000..cb679fc --- /dev/null +++ b/template/success.php @@ -0,0 +1,3 @@ +
+

設定を保存しました

+