|
<?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 ) ); |
|
} |
|
} |
|
} |