Skip to content

Commit

Permalink
Merge pull request #208 from hametuha/bugfix/issue-205
Browse files Browse the repository at this point in the history
#206 古い投稿を期限切れにする機能を追加、編集者が選べる作品集の増加
  • Loading branch information
fumikito committed Apr 2, 2024
2 parents b1de10a + e31bdbc commit 74231e1
Show file tree
Hide file tree
Showing 6 changed files with 257 additions and 10 deletions.
9 changes: 9 additions & 0 deletions assets/sass/admin/_series.scss
Expand Up @@ -45,3 +45,12 @@
}
}

.table-view-list.posts {
table-layout: auto;

th#categories {
max-width: 5em;
width: auto;
}

}
16 changes: 12 additions & 4 deletions src/Hametuha/Admin/Fields/MetaboxFieldSeries.php
Expand Up @@ -78,7 +78,6 @@ protected function user_editable( $data ) {
$post = get_post( $data );
if ( ! $post ) {
return true;

}
return current_user_can( 'edit_post', $post->ID );
}
Expand Down Expand Up @@ -112,16 +111,25 @@ protected function get_options() {
];
$args = [
'post_type' => 'series',
'posts_per_page' => 100,
'posts_per_page' => 200,
'orderby' => [ 'date' => 'DESC' ],
'post_status' => 'any',
'meta_query' => [
[
'key' => '_stale',
'compare' => 'NOT EXISTS',
],
],
];
if ( ! current_user_can( 'edit_others_posts' ) ) {
// 自分が所有していない投稿が割り当てられている場合
if ( current_user_can( 'edit_others_posts' ) ) {
$args['posts_per_page'] = 1000;
} else {
if ( $this->fixed_parent ) {
// 自分が所有していない投稿が割り当てられている場合は他の選択肢をなくす
$args['p'] = $this->fixed_parent;
$options = [];
} else {
// 自分の所有している投稿のみを表示
$args['author'] = get_current_user_id();
}
}
Expand Down
26 changes: 26 additions & 0 deletions src/Hametuha/Commands/Post.php
Expand Up @@ -3,6 +3,7 @@
namespace Hametuha\Commands;


use Hametuha\Hooks\StaleStatus;
use Hametuha\Model\Jobs;
use Hametuha\Sharee\Master\Address;
use WPametu\Utility\Command;
Expand Down Expand Up @@ -393,4 +394,29 @@ public function share_pic( $args ) {
self::e( $e->getCode() . ': ' . $e->getMessage() );
}
}

/**
* List command to stale.
*
* @synopsis <days> [--dry-run]
* @param array $args Option.
* @return void
*/
public function stales( $args, $assoc ) {
$dry_run = $assoc['dry-run'] ?? false;
list( $days ) = $args;
$result = StaleStatus::get_instance()->bulk_stale( $days, $dry_run );
if ( is_array( $result ) ) {
// Display table.
$table = new Table();
$table->setHeaders( [ 'ID', 'Type', 'Title', 'Author', 'Date' ] );
foreach ( $result as $post ) {
$table->addRow( [ $post->ID, $post->post_type, $post->post_title, get_the_author_meta( 'display_name', $post->post_author ), $post->post_date ] );
}
$table->display();
} else {
// Display count.
\WP_CLI::success( sprintf( __( '%d件の投稿が期限切れになりました。', 'hametuha' ), $result ) );
}
}
}
7 changes: 7 additions & 0 deletions src/Hametuha/Hooks/Editor.php
Expand Up @@ -138,6 +138,13 @@ public function post_state( $post_states, $post ) {
break;
}
break;
case 'post':
if ( 0 < $post->post_parent && get_post( $post->post_parent ) ) {
$link = get_edit_post_link( $post->post_parent );
$title = get_the_title( $post->post_parent );
$post_states['post-parent'] = $link ? sprintf( '<a href="%s">%s</a>', $link, esc_html( $title ) ) : esc_html( $title );
}
break;
}
return $post_states;
}
Expand Down
194 changes: 194 additions & 0 deletions src/Hametuha/Hooks/StaleStatus.php
@@ -0,0 +1,194 @@
<?php

namespace Hametuha\Hooks;


use Hametuha\Model\Series;
use WPametu\Pattern\Singleton;

/**
* 作品のステータスを管理する
*/
class StaleStatus extends Singleton {

/**
* @var string[] Supported post types.
*/
protected $supported_post_types = [ 'post', 'series' ];

/**
* Is post type supported?
*
* @param string $post_type Post type.
* @return bool
*/
protected function is_supported( $post_type ) {
return in_array( $post_type, $this->supported_post_types, true );
}

/**
* {@inheritDoc}
*/
protected function __construct( array $setting = array() ) {
add_action( 'add_meta_boxes', [ $this, 'register_meta_box' ] );
add_action( 'save_post', [ $this, 'save_post' ], 10, 2 );
add_filter( 'display_post_states', [ $this, 'display_status' ], 10, 2 );
}

/**
* Register meta box.
*
* @param string $post_type Post type name.
* @return void
*/
public function register_meta_box( $post_type ) {
if ( $this->is_supported( $post_type ) ){
add_meta_box( 'stale_status', __( '作品のステータス', 'hametuha' ), [ $this, 'render_meta_box' ], $post_type, 'side', 'low' );
}
}

/**
* Save stale date.
*
* @param int $post_id Post ID.
* @param \WP_Post $post Post object.
* @return void
*/
public function save_post( $post_id, $post ) {
if ( ! $this->is_supported( $post->post_type ) ) {
return;
}
if ( ! wp_verify_nonce( filter_input( INPUT_POST, '_stale_status_nonce' ), 'stale_status' ) ) {
return;
}
$staled = filter_input( INPUT_POST, 'post-is-stale' );
if ( '1' === $staled ) {
if ( ! get_post_meta( $post_id, '_stale', true ) ) {
// No current value, save newly.
update_post_meta( $post_id, '_stale', current_time( 'mysql' ) );
}
} else {
// Delete staled flag.
delete_post_meta( $post_id, '_stale' );
}
}

/**
* Render meta box for post.
*
* @param \WP_Post $post Post object.
* @return void
*/
public function render_meta_box( $post ) {
wp_nonce_field( 'stale_status', '_stale_status_nonce', false );
$staled_at = get_post_meta( $post->ID, '_stale', true );
?>
<p>
<label>
<input type="checkbox" value="1" name="post-is-stale" <?php checked( ! empty( $staled_at ) ) ?> />
<?php esc_html_e( '投稿を期限切れにする', 'hametuha' ); ?>
<?php if ( $staled_at ) : ?>
<small><?php printf( esc_html__( '%sに期限切れと判例', 'hametuha' ), mysql2date( get_option( 'date_format' ), $staled_at ) ) ?></small>
<?php endif; ?>
</label>
</p>
<p class="description">
<?php esc_html_e( '一定期間の間、更新されなかった投稿は自動的に「期限切れ」になります。非公開や削除などを検討してください。', 'hametuha' ); ?>
</p>
<?php
}

/**
* Is post stabled?
*
* @param int|null|\WP_Post $post Post object.
* @return bool
*/
public static function is_stabled( $post = null ) {
$post = get_post( $post );
if ( ! $post ) {
return false;
}
$staled_at = get_post_meta( $post->ID, '_stale', true );
return ! empty( $staled_at );
}

/**
* Display status at list table.
*
* @param string[] $statuses Statuses.
* @param \WP_Post $post Post object.
* @return string[]
*/
public function display_status( $statuses, $post ) {
if ( ! $this->is_supported( $post->post_type ) ) {
return $statuses;
}
if ( self::is_stabled( $post ) ) {
$statuses[ 'post-is-staled' ] = __( '期限切れ', 'hametuha' );
}
return $statuses;
}

/**
* 指定した期間以上過ぎている投稿を一括で期限切れにする
*
* @param int $period デフォルトは365日
* @parma bool $dry_run trueにすると投稿のリストを返す
* @return int|\WP_Post[]
*/
public function bulk_stale( $period = 365, $dry_run = false ) {
$args = [
'post_type' => [ 'post', 'series' ],
'posts_per_page' => 100,
'paged' => 1,
'post_status' => 'draft',
'meta_query' => [
'key' => '_stable',
'compare' => 'NOT EXISTS',
],
];
$args['date_query'] = [
[
'before' => sprintf( '-%d days', $period ),
],
];
$list = [];
$updated = 0;
$has_next = true;
while ( $has_next ) {
$query = new \WP_Query( $args );
if ( ! $query->have_posts() ) {
$has_next = false;
break;
}
// 件数を精査する
foreach ( $query->posts as $post ) {
$is_stale = false;
switch ( $post->post_type ) {
case 'post':
// タイトルか本文が空
$is_stale = ! $post->post_title || ! $post->post_content;
break;
case 'series':
// 紐づけられた投稿が一件もない
$is_stale = Series::get_instance()->get_total( $post ) < 1;
break;
}
if ( ! $is_stale ) {
// This is not stale.
continue 1;
}
if ( $dry_run ) {
$list[] = $post;
continue 1;
}
update_post_meta( $post->ID, '_stale', current_time( 'mysql' ) );
$updated++;
}
// ページを増やして次のループへ
$args['paged']++;
}
return $dry_run ? $list : $updated;
}
}
15 changes: 9 additions & 6 deletions src/Hametuha/Model/Series.php
Expand Up @@ -295,12 +295,15 @@ public function get_external( $series_id ) {
*/
public function get_total( $post = null ) {
$post = get_post( $post );

return (int) $this->select( 'COUNT(ID)' )->from( $this->db->posts )
->where( 'post_type = %s', 'post' )
->where( 'post_status = %s', 'publish' )
->where( 'post_parent = %d', $post->ID )
->get_var();
$query = new \WP_Query( [
'post_type' => 'post',
'post_status' => [ 'publish', 'private' ],
'post_parent' => $post->ID,
'no_found_rows' => true,
'posts_per_page' => -1,
'fields' => 'ids',
] );
return count( $query->posts );
}

/**
Expand Down

0 comments on commit 74231e1

Please sign in to comment.