WP PLUGIN アーカイブ毎に投稿表示順を設定する
・ベース部分(カテゴリー、投稿)の表示順用カスタムフィールド設定登録の作成
This commit is contained in:
Generated
+8
@@ -0,0 +1,8 @@
|
||||
# デフォルトの無視ファイル
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# データソースのローカルストレージがファイルを無視しました
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
||||
# エディターベースの HTTP クライアントリクエスト
|
||||
/httpRequests/
|
||||
Generated
+31
@@ -0,0 +1,31 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="CakeStormCakeSettings">
|
||||
<option name="cakeVersionAbsorption">
|
||||
<map>
|
||||
<entry key="Controller" value="/controllers/" />
|
||||
<entry key="View" value="/views/" />
|
||||
<entry key="Model" value="/models/" />
|
||||
<entry key="Helper" value="/views/helpers/" />
|
||||
<entry key="Component" value="/controllers/components/" />
|
||||
<entry key="Behavior" value="/models/behaviors/" />
|
||||
<entry key="Plugin" value="plugins/" />
|
||||
<entry key="Shell" value="/vendors/shell" />
|
||||
<entry key="Task" value="/vendors/Shell/Task" />
|
||||
<entry key="ControllerTest" value="/tests/cases/controllers/" />
|
||||
<entry key="ModelTest" value="/tests/cases/models/" />
|
||||
<entry key="BehaviorTest" value="/tests/cases/behaviors/" />
|
||||
<entry key="ComponentTest" value="/tests/cases/components/" />
|
||||
<entry key="HelperTest" value="/tests/cases/helpers/" />
|
||||
<entry key="TestFile" value="test" />
|
||||
<entry key="FileSeparator" value="." />
|
||||
<entry key="FileWordSeparator" value="_" />
|
||||
<entry key="Fixture" value="/tests/fixtures/" />
|
||||
<entry key="Element" value="elements/" />
|
||||
<entry key="FixtureFile" value="fixture" />
|
||||
<entry key="Layout" value="layouts/" />
|
||||
</map>
|
||||
</option>
|
||||
<option name="cakeVersion" value="1" />
|
||||
</component>
|
||||
</project>
|
||||
Generated
+56
@@ -0,0 +1,56 @@
|
||||
<component name="ProjectCodeStyleConfiguration">
|
||||
<code_scheme name="Project" version="173">
|
||||
<PHPCodeStyleSettings>
|
||||
<option name="ALIGN_KEY_VALUE_PAIRS" value="true" />
|
||||
<option name="ALIGN_ASSIGNMENTS" value="true" />
|
||||
<option name="PHPDOC_BLANK_LINES_AROUND_PARAMETERS" value="true" />
|
||||
<option name="LOWER_CASE_BOOLEAN_CONST" value="true" />
|
||||
<option name="LOWER_CASE_NULL_CONST" value="true" />
|
||||
<option name="GETTERS_SETTERS_NAMING_STYLE" value="SNAKE_CASE" />
|
||||
<option name="VARIABLE_NAMING_STYLE" value="SNAKE_CASE" />
|
||||
<option name="BLANK_LINES_BEFORE_RETURN_STATEMENT" value="1" />
|
||||
<option name="KEEP_RPAREN_AND_LBRACE_ON_ONE_LINE" value="true" />
|
||||
<option name="SPACES_AROUND_VAR_WITHIN_BRACKETS" value="true" />
|
||||
<option name="SPACE_BEFORE_UNARY_NOT" value="true" />
|
||||
<option name="SPACE_AFTER_UNARY_NOT" value="true" />
|
||||
</PHPCodeStyleSettings>
|
||||
<codeStyleSettings language="PHP">
|
||||
<option name="KEEP_CONTROL_STATEMENT_IN_ONE_LINE" value="false" />
|
||||
<option name="CLASS_BRACE_STYLE" value="1" />
|
||||
<option name="METHOD_BRACE_STYLE" value="1" />
|
||||
<option name="SPECIAL_ELSE_IF_TREATMENT" value="true" />
|
||||
<option name="ALIGN_MULTILINE_CHAINED_METHODS" value="true" />
|
||||
<option name="ALIGN_MULTILINE_PARAMETERS" value="false" />
|
||||
<option name="ALIGN_MULTILINE_FOR" value="false" />
|
||||
<option name="ALIGN_MULTILINE_BINARY_OPERATION" value="true" />
|
||||
<option name="SPACE_AROUND_UNARY_OPERATOR" value="true" />
|
||||
<option name="SPACE_WITHIN_PARENTHESES" value="true" />
|
||||
<option name="SPACE_WITHIN_METHOD_CALL_PARENTHESES" value="true" />
|
||||
<option name="SPACE_WITHIN_METHOD_PARENTHESES" value="true" />
|
||||
<option name="SPACE_WITHIN_IF_PARENTHESES" value="true" />
|
||||
<option name="SPACE_WITHIN_WHILE_PARENTHESES" value="true" />
|
||||
<option name="SPACE_WITHIN_FOR_PARENTHESES" value="true" />
|
||||
<option name="SPACE_WITHIN_CATCH_PARENTHESES" value="true" />
|
||||
<option name="SPACE_WITHIN_SWITCH_PARENTHESES" value="true" />
|
||||
<option name="SPACE_WITHIN_ARRAY_INITIALIZER_BRACES" value="true" />
|
||||
<option name="SPACE_AFTER_TYPE_CAST" value="true" />
|
||||
<option name="METHOD_PARAMETERS_LPAREN_ON_NEXT_LINE" value="true" />
|
||||
<option name="METHOD_PARAMETERS_RPAREN_ON_NEXT_LINE" value="true" />
|
||||
<option name="PARENTHESES_EXPRESSION_LPAREN_WRAP" value="true" />
|
||||
<option name="PARENTHESES_EXPRESSION_RPAREN_WRAP" value="true" />
|
||||
<option name="FOR_STATEMENT_LPAREN_ON_NEXT_LINE" value="true" />
|
||||
<option name="FOR_STATEMENT_RPAREN_ON_NEXT_LINE" value="true" />
|
||||
<option name="ARRAY_INITIALIZER_WRAP" value="5" />
|
||||
<option name="ARRAY_INITIALIZER_LBRACE_ON_NEXT_LINE" value="true" />
|
||||
<option name="ARRAY_INITIALIZER_RBRACE_ON_NEXT_LINE" value="true" />
|
||||
<option name="IF_BRACE_FORCE" value="3" />
|
||||
<option name="DOWHILE_BRACE_FORCE" value="3" />
|
||||
<option name="WHILE_BRACE_FORCE" value="3" />
|
||||
<option name="FOR_BRACE_FORCE" value="3" />
|
||||
<indentOptions>
|
||||
<option name="USE_TAB_CHARACTER" value="true" />
|
||||
<option name="SMART_TABS" value="true" />
|
||||
</indentOptions>
|
||||
</codeStyleSettings>
|
||||
</code_scheme>
|
||||
</component>
|
||||
Generated
+5
@@ -0,0 +1,5 @@
|
||||
<component name="ProjectCodeStyleConfiguration">
|
||||
<state>
|
||||
<option name="USE_PER_PROJECT_SETTINGS" value="true" />
|
||||
</state>
|
||||
</component>
|
||||
Generated
+8
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/nb-post-sort-cat-order.iml" filepath="$PROJECT_DIR$/.idea/nb-post-sort-cat-order.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
Generated
+8
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="WEB_MODULE" version="4">
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$" />
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
Generated
+6
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="PhpProjectSharedConfiguration" php_language_level="7">
|
||||
<option name="suggestChangeDefaultLanguageLevel" value="false" />
|
||||
</component>
|
||||
</project>
|
||||
@@ -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: "]";
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
});
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
class activate {
|
||||
|
||||
public static function add_sort_posts() {
|
||||
|
||||
$categories = get_categories();
|
||||
foreach ( $categories as $category ) {
|
||||
$posts = self::get_category_posts( $category->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 );
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
<?php
|
||||
/**
|
||||
* TODO:設定対象にカスタムタクソノミー、カスタム投稿を含める
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
} // Exit if accessed directly
|
||||
|
||||
if ( ! class_exists( 'post_sort_cat_order' ) ) {
|
||||
|
||||
class post_sort_cat_order {
|
||||
|
||||
const TEMPLATE_DIR = __DIR__ . '/../template/';
|
||||
|
||||
public function __construct() {
|
||||
add_action( 'admin_menu', array( $this, 'add_pages' ) );
|
||||
// add_action( 'transition_post_status', array( $this, 'add_cat_post_order' ), 10, 3 );
|
||||
}
|
||||
|
||||
public function add_pages() {
|
||||
add_menu_page(
|
||||
'PSBC',
|
||||
'PSBC',
|
||||
'level_8',
|
||||
'nb_post_sort',
|
||||
array( $this, 'show_option_page' ),
|
||||
'',
|
||||
50
|
||||
);
|
||||
add_submenu_page(
|
||||
'nb_post_sort', // parent_slug
|
||||
'Archive Setting', // page_title
|
||||
'カテゴリー選択', // menu_title
|
||||
'administrator', // capability
|
||||
'nb_post_sort_setting', // menu_slug
|
||||
array( $this, 'display_setting_page' ) // function
|
||||
);
|
||||
}
|
||||
|
||||
public function display_setting_page() {
|
||||
if ( isset( $_POST['post_cat_order'] ) ) {
|
||||
check_admin_referer( 'sh_options' );
|
||||
$opt = $_POST['post_cat_order'];
|
||||
//ソート対象カテゴリーの登録
|
||||
update_option( 'post_cat_order', $opt );
|
||||
require_once self::TEMPLATE_DIR . 'success.php';
|
||||
}
|
||||
require_once self::TEMPLATE_DIR . 'setting.php';
|
||||
}
|
||||
|
||||
|
||||
public function show_option_page() {
|
||||
|
||||
if ( isset( $_POST['nb_post_cat'] ) ) {
|
||||
check_admin_referer( 'sh_options' );
|
||||
//投稿表示順の設定
|
||||
$this->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 );
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
Plugin Name: Post Sort By Categories
|
||||
Plugin URI:
|
||||
Author: Nob Kim
|
||||
Description: カテゴリー毎に投稿の表示順を設定するプラグイン
|
||||
Version: 1.0.0
|
||||
License: GPLv2
|
||||
*/
|
||||
|
||||
//初期化
|
||||
require_once __DIR__ . '/lib/activate.php';
|
||||
register_activation_hook( __FILE__, array( 'activate', 'add_sort_posts' ) );
|
||||
|
||||
//メイン処理のクラスをインスタンス化
|
||||
require_once __DIR__ . '/lib/post-sort-cat-order.php';
|
||||
$post_sort_cat_order = new post_sort_cat_order;
|
||||
|
||||
//CSS, JSの読み込み
|
||||
add_action( 'admin_enqueue_scripts', 'register_my_styles' );
|
||||
function register_my_styles() {
|
||||
$plugin_url = plugin_dir_url( __FILE__ );
|
||||
wp_enqueue_style( 'hrc_post_style', $plugin_url . 'css/base.css' );
|
||||
wp_enqueue_script( 'jquery-ui-sortable' );
|
||||
wp_enqueue_script( 'post-sort-cat-order_js', $plugin_url . 'js/post-sort-cat-order.js' );
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
//各種パラメータ
|
||||
$all_categories = get_categories();
|
||||
?>
|
||||
|
||||
<div class="post-order-box">
|
||||
<form action="" method="post">
|
||||
<?php
|
||||
wp_nonce_field( 'sh_options' );
|
||||
$opt = get_option( 'post_cat_order' );
|
||||
?>
|
||||
<h2>対象カテゴリー選択</h2>
|
||||
<ul class="order_setting_list">
|
||||
<?php foreach ( $all_categories as $category ): ?>
|
||||
<li>
|
||||
<div class="select_cat">
|
||||
<label>
|
||||
<input type="hidden"
|
||||
name="post_cat_order[target_cat][<?php echo $category->slug; ?>]"
|
||||
value="0">
|
||||
<input type="checkbox"
|
||||
name="post_cat_order[target_cat][<?php echo $category->slug; ?>]"
|
||||
<?php echo checked( $opt['target_cat'][ $category->slug ], 1 ); ?>
|
||||
value="1">
|
||||
<?php echo $category->name; ?>
|
||||
</label>
|
||||
</div>
|
||||
</li>
|
||||
<?php endforeach; ?>
|
||||
</ul>
|
||||
<p class="submit"><input type="submit" name="Submit" class="button-primary" value="変更を保存"/>
|
||||
</form>
|
||||
</div>
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
$opt = get_option( 'post_cat_order' );
|
||||
$category_list = post_sort_cat_order::get_category_list( $opt );
|
||||
?>
|
||||
<div class="post-order-box">
|
||||
<form action="" method="post">
|
||||
<?php wp_nonce_field( 'sh_options' ); ?>
|
||||
<h2>投稿表示順設定</h2>
|
||||
<div class="list-orders-outer">
|
||||
<?php if ( count( $category_list ) == 0 ): ?>
|
||||
<p>対象カテゴリーを選択してください。[ <a href="./admin.php?page=nb_post_sort_setting">カテゴリー選択</a> ]</p>
|
||||
<?php else: ?>
|
||||
<?php foreach ( $category_list as $category ): ?>
|
||||
<div class="list-orders-inner">
|
||||
<h3><?php echo $category->name; ?></h3>
|
||||
<?php $target_posts = post_sort_cat_order::get_sort_post_list( $category->term_id ); ?>
|
||||
<ul class="post-order-list">
|
||||
<?php foreach ( $target_posts as $key => $target_post ): $sort_num = $key + 1; ?>
|
||||
<li class="product-list">
|
||||
<span class="sort-num-label"><?php echo $sort_num; ?></span>
|
||||
<?php echo get_the_title( $target_post->ID ); ?>
|
||||
<input type="hidden" class="list_order"
|
||||
name="nb_post_cat[post_sort][<?php echo $category->term_id; ?>][<?php echo $target_post->ID; ?>]"
|
||||
value="<?php echo $sort_num; ?>">
|
||||
</li>
|
||||
<?php endforeach; ?>
|
||||
</ul>
|
||||
</div>
|
||||
<?php endforeach; ?>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<p class="submit"><input type="submit" name="Submit" class="button-primary" value="変更を保存"/></p>
|
||||
</form>
|
||||
</div>
|
||||
@@ -0,0 +1,3 @@
|
||||
<div class="updated fade">
|
||||
<p><strong>設定を保存しました</strong></p>
|
||||
</div>
|
||||
Reference in New Issue
Block a user