Skip to content

Instantly share code, notes, and snippets.

@torounit
Created November 30, 2015 09:31
Show Gist options
  • Save torounit/e2dc97b20f3d3f88f0f2 to your computer and use it in GitHub Desktop.
Save torounit/e2dc97b20f3d3f88f0f2 to your computer and use it in GitHub Desktop.
タクソノミーをカスタムフィールドの日付を参照して自動設定する。イベント系の奴に使える。
<?php
/**
* Created by PhpStorm.
* User: torounit
* Date: 15/03/06
* Time: 11:03
*
* Taxonomy Decorator.
*/
namespace Torounit\WP\Auto_Terms_By_Date;
use Torounit\WP\Taxonomy;
/**
* Class State
*/
class Auto_Terms_By_Date {
/** @var Taxonomy $taxonomy */
private $taxonomy;
private $end_key;
private $open = 'open';
private $terms_cache = [];
private $closed = 'closed';
/**
* @param Taxonomy $taxonomy
* @param String $key
*/
public function __construct( Taxonomy $taxonomy, $start_key, $end_key = null ) {
$this->taxonomy = $taxonomy;
$this->start_key = $start_key;
$this->end_key = $end_key;
if ( ! $end_key ) {
$this->end_key = $start_key;
}
$this->set_terms();
$this->init();
}
/**
* add closed term.
*/
public function set_terms() {
$this->taxonomy->add_default_term( 'アーカイブ' , $this->closed );
$this->taxonomy->add_default_term( '公開中' , $this->open );
}
/**
* execution.
*/
public function init() {
add_action( 'update_option_'.Config::UPDATED_OPTION, [$this, 'set_status_to_posts'] );
add_action( 'save_post', [$this,'on_save_post'], 999, 2);
}
/**
* @return array
*/
public function get_posts() {
$tax = get_taxonomy($this->taxonomy->get_taxonomy());
$args = [
'post_type' => $tax->object_type,
'nopaging' => true,
];
return get_posts( $args );
}
/**
* @param \WP_Post $post
* @return bool
*/
public function is_closed( \WP_Post $post ) {
$post_dates = get_post_meta( $post->ID, $this->end_key, false );
if( empty( $post_dates ) ) {
return false;
}
$post_date_numbers = $this->remove_hyphen_from_dates( $post_dates );
$last_date = max( $post_date_numbers );
$date = intval( date( 'Ymd', current_time('timestamp') ) );
return ($last_date < $date);
}
/**
* @param \WP_Post $post
* @return bool
*/
public function is_open( \WP_Post $post ) {
$post_dates = get_post_meta( $post->ID, $this->start_key, false );
if( empty( $post_dates ) ) {
return false;
}
$post_date_numbers = $this->remove_hyphen_from_dates( $post_dates );
$
$first_date = min( $post_date_numbers );
$date = intval( date( 'Ymd', current_time('timestamp') ) );
return ( $first_date <= $date);
}
/**
* @param Array $post_dates
* @return Array
*/
public function remove_hyphen_from_dates( $post_dates ) {
return array_map( function( $post_date ) {
return intval( str_replace('-','', $post_date) );
}, $post_dates );
}
/**
* @param $id
* @param \WP_Post $post
*/
public function on_save_post( $id, \WP_Post $post ) {
$this->set_term( $post );
}
/**
* @param string $term_slug
* @return mixed
*/
public function get_term_by_slug( $term_slug ) {
if( !empty( $this->terms_cache[ $term_slug ] ) ) {
return $this->terms_cache[ $term_slug ];
}
else {
$term = get_term_by('slug', $term_slug, $this->taxonomy->get_taxonomy() );
$this->terms_cache[ $term_slug ] = $term;
return $term;
}
}
/**
* @param \WP_Post $post
*/
public function set_term( \WP_Post $post ) {
if( $this->is_closed( $post ) ) {
$term = $this->get_term_by_slug( $this->closed );
wp_set_object_terms( $post->ID, $term->slug, $this->taxonomy->get_taxonomy() );
}
elseif( $this->is_open( $post ) ) {
$term = $this->get_term_by_slug( $this->open );
wp_set_object_terms( $post->ID, $term->slug, $this->taxonomy->get_taxonomy() );
}
else {
wp_delete_object_term_relationships( $post->ID, $this->taxonomy->get_taxonomy());
}
}
/**
* 抽出した投稿に
* タクソノミーを設定
*/
public function set_status_to_posts() {
array_walk( $this->get_posts(), function( $post ){
$this->set_term( $post );
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment