Skip to content

Instantly share code, notes, and snippets.

@pimpmywp
Created December 29, 2012 08:21
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save pimpmywp/4405428 to your computer and use it in GitHub Desktop.
Save pimpmywp/4405428 to your computer and use it in GitHub Desktop.
[WordPress] 記事の公開開始日時・公開終了日時をカスタムフィールドで個別/同時に指定する

#使い方

  1. class-pm-schedule-post.php のコードをテーマの functions.php またはプラグインファイルに貼付けるか、ファイル自体をインクルードします。

  2. how2use.php のコードをテーマの functions.php またはプラグインファイルに貼付けます。
    ※PM_Schedule_Post() の引数をお好みで指定します。
    'pubstart' .... 公開開始日時用カスタムフィールドの名前(キー)英数字推奨
    'pubend' ..... 公開終了日時用カスタムフィールドの名前(キー) 英数字推奨 '公開終了' ... 公開終了ステータスの表示用ラベル

  3. 記事のカスタムフィールドで公開開始日時、公開終了日時を両方、またはどちらか一方を指定します。
    日時は PHP の関数 strtotime() で認識できる書式で指定します。
    WordPress 標準のカスタムフィールドでも、meta box 内に出力した input フィールドでもどちらでも動作します。

<?php
if ( ! class_exists( 'PM_Schedule_Post' ) ) {
class PM_Schedule_Post {
protected $from = '';
protected $to = '';
protected $label = '';
public function __construct( $from, $to, $label = 'expired' ) {
$this->from = $from;
$this->to = $to;
$this->label = $label;
add_action( 'init' , array( $this, 'register' ) );
add_filter( 'wp_insert_post_data' , array( $this, 'set_future' ), 10, 2 );
add_action( 'transition_post_status', array( $this, 'schedule_expire' ), 10, 3 );
add_action( 'expire_post' , array( $this, 'expire_post' ) );
}
public function register() {
register_post_status( 'expired', array(
'label' => $this->label,
'protected' => true,
'label_count' => _n_noop(
$this->label . ' <span class="count">(%s)</span>',
$this->label . ' <span class="count">(%s)</span>'
),
) );
}
protected function get_date( $date, $opt = '' ) {
if( $opt )
$date = strtotime( $opt, strtotime( $date ) );
else
$date = strtotime( $date );
return date( 'Y-m-d H:i:s', $date );
}
protected function get_meta( $key ) {
$val = '';
if ( isset( $_POST[$key] ) ) {
$val = $key = $_POST[$key];
} elseif( isset( $_POST['meta'] ) ) {
foreach ( (array) $_POST['meta'] as $meta ) {
if ( $key == $meta['key'] ) {
$val = $meta['value'];
break;
}
}
}
return $val;
}
public function set_future( $data, $postarr ) {
$from = $this->get_meta( $this->from );
$to = $this->get_meta( $this->to );
if ( $from && $to && $from > $to )
return;
$now = date( 'Y-m-d H:i:59' );
if ( ! empty( $from ) ) {
$start = $this->get_date ( $from );
$start_gmt = get_gmt_from_date( $start );
if ( mysql2date( 'U', $start_gmt, false ) > mysql2date( 'U', $now, false ) ) {
$data['post_date' ] = $start;
$data['post_date_gmt'] = $start_gmt;
$data['post_status' ] = 'future';
}
}
if ( 'future' != $data['post_status'] && ! empty( $to ) ) {
$end = $this->get_date ( $to );
$end_gmt = get_gmt_from_date( $end );
if ( mysql2date( 'U', $end_gmt, false ) > mysql2date( 'U', $now, false ) ) {
$data['post_modified' ] = current_time( 'mysql' );
$data['post_modified_gmt'] = $now;
$data['post_status' ] = 'publish';
}
}
return $data;
}
public function schedule_expire( $new_status, $old_status, $post ) {
$to = $this->get_meta( $this->to );
if ( empty( $to ) )
$to = $post->{$this->to};
if ( 'publish' == $new_status && ! empty( $to ) ) {
wp_clear_scheduled_hook( 'expire_post', array( $post->ID ) );
wp_schedule_single_event(
strtotime( get_gmt_from_date( $this->get_date( $to ) ) . ' GMT' ),
'expire_post',
array( $post->ID )
);
}
}
public function expire_post( $post_id ) {
global $wpdb;
$post = get_post($post_id);
if ( empty($post) )
return;
if ( 'publish' != $post->post_status )
return;
$wpdb->update( $wpdb->posts, array( 'post_status' => 'expired' ), array( 'ID' => $post_id ) );
}
}
}
<?php
// PM_Schedule_Post クラスと同じファイルで add_action() するなら require_once() は不要
require_once( 'class-pm-schedule-post.php' );
add_action( 'after_setup_theme', create_function( '', 'new PM_Schedule_Post( "pubstart", "pubend", "公開終了" );' ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment