WP PLUGIN アーカイブ毎に投稿表示順を設定する

・ファイル名、クラス名の変更
・タクソノミーの有効化で、カテゴリー、タグ、カスタム分類のメタキーを分割
・表示用クラスの追加
・readmeの追加等
This commit is contained in:
2021-05-04 15:57:30 +09:00
parent 2ed2b25194
commit 7722f53f10
12 changed files with 409 additions and 211 deletions
+42
View File
@@ -0,0 +1,42 @@
<?php
class apop_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_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 = '_apop_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 );
}
}
}
+94
View File
@@ -0,0 +1,94 @@
<?php
/**
* TODO:設定対象にカスタムタクソノミーの投稿リストを作成する
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
} // Exit if accessed directly
if ( ! class_exists( 'APOP' ) ) {
class APOP {
const TEMPLATE_DIR = __DIR__ . '/../template/';
public function __construct() {
add_action( 'admin_menu', array( $this, 'add_pages' ) );
}
public function add_pages() {
add_menu_page(
'APO +',
'APO +',
'level_8',
'apop_post_sort',
array( $this, 'show_option_page' ),
'',
50
);
add_submenu_page(
'apop_post_sort', // parent_slug
'Select taxonomy', // page_title
'タクソノミー選択', // menu_title
'administrator', // capability
'apop_post_sort_setting', // menu_slug
array( $this, 'display_setting_page' ) // function
);
}
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_tag_order', //タグ設定
'_apop_tax_order', //カスタムタクソノミー設定
);
foreach ( $settings as $setting ) {
$opt = $_POST[ $setting ];
update_option( $setting, $opt );
}
require_once self::TEMPLATE_DIR . 'success.php';
}
require_once self::TEMPLATE_DIR . 'setting.php';
}
public function show_option_page() {
if ( isset( $_POST['_apop_post_category'] ) || isset( $_POST['_apop_post_post_tag'] ) ) {
check_admin_referer( 'sh_options' );
//投稿表示順の設定
$this->update_post_sort( 'category' );
$this->update_post_sort( 'post_tag' );
require_once self::TEMPLATE_DIR . 'success.php';
}
require_once self::TEMPLATE_DIR . 'order.php';
}
//投稿表示順の設定
private function update_post_sort( $target ) {
$target = '_apop_post_' . $target;
$posts_sort = $_POST[ $target ]['post_sort'];
foreach ( $posts_sort as $cat_id => $posts ) {
$sort_key = $target . '_' . $cat_id;
foreach ( $posts as $post_id => $sort ) {
update_post_meta( $post_id, $sort_key, $sort );
}
}
}
}
}
+73
View File
@@ -0,0 +1,73 @@
<?php
class APOP_UI {
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_tax_name( $tax_key ): string {
$tax_names = array(
'category' => 'カテゴリー',
'tag_id' => 'タグ',
'custom_tax' => 'カスタム分類',
);
return $tax_names[ $tax_key ];
}
public static function get_cat_tag_list( $target, $key ): array {
$opt = get_option( '_apop_' . $target . '_order' );
$include = array();
if ( isset( $opt['target_cat'] ) ) {
foreach ( $opt['target_cat'] as $tax_id => $check ) {
if ( $check ) {
$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 get_sort_post_list( $tax_id, $search_param, $tax_key ) {
$args = [
'post_status' => array( 'publish', 'draft' ),
'posts_per_page' => - 1,
$search_param => $tax_id,
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_key' => '_apop_post_' . $tax_key . '_' . $tax_id,
];
return get_posts( $args );
}
public static function get_none_sort_post_list( $tax_id, $key, $exclude_posts ) {
$exclude = implode( ',', $exclude_posts );
$args = [
'post_status' => array( 'publish', 'draft' ),
'posts_per_page' => - 1,
$key => $tax_id,
];
if ( ! empty( $exclude ) ) {
$args['exclude'] = $exclude;
}
return get_posts( $args );
}
}