Skip to content

Commit

Permalink
Add Schedule Class.
Browse files Browse the repository at this point in the history
Separate attach / detach action.
add more params for wp_schedule_single_event
  • Loading branch information
torounit committed May 23, 2022
1 parent 563711b commit 09f3f84
Show file tree
Hide file tree
Showing 2 changed files with 195 additions and 49 deletions.
133 changes: 133 additions & 0 deletions includes/Schedule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php
/**
* Schedule Class.
*
* @package Schedule_Terms
*/

namespace HAMWORKS\WP\Schedule_Terms;

use WP_Term;

/**
* Schedule Object
*/
class Schedule {


/**
* Schedule tyoe.
*
* @var 'attach'|'detach'
*/
private $type;

/**
* Taxonomy slug.
*
* @var string
*/
private $taxonomy;

/**
* Term slug.
*
* @var string
*/
private $term;

/**
* ISO 8601 formatted datetime.
*
* @var string
*/
private $datetime;
/**
* Meta value.
*
* @var array{ type: string, taxonomy: string, term: string, datetime: string }
*/
private $values;

/**
* Constructor.
*
* @param array{ type: string, taxonomy: string, term: string, datetime: string } $values values.
*/
public function __construct( array $values ) {
$this->values = $values;
$this->type = $values['type'];
$this->taxonomy = $values['taxonomy'];
$this->term = $values['term'];
$this->datetime = $values['datetime'];
}

/**
* Get unixtime.
*
* @return int
*/
public function get_timestamp(): int {
try {
$date_time = new \DateTime( $this->datetime );
} catch ( \Exception $e ) {
wp_die( esc_html( $e->getMessage() ) );
}

return $date_time->getTimestamp();
}

/**
* Check if schedule is expired.
*
* @param int $timestamp Unix timestamp.
*
* @return bool
*/
public function is_expired( int $timestamp ): bool {
$this->get_timestamp();

return $timestamp >= $this->get_timestamp();
}

/**
* Get action type.
*
* @return string
*/
public function get_type(): string {
return $this->type;
}

/**
* Get taxonomy slug.
*
* @return string
*/
public function get_taxonomy(): string {
return $this->taxonomy;
}

/**
* Get term slug.
*
* @return ?WP_Term
*/
public function get_term(): ?WP_Term {
$term = get_term_by( 'slug', $this->term, $this->taxonomy );
if ( is_wp_error( $term ) ) {
return null;
}

return $term;
}

/**
* Get meta value.
*
* @return array
*/
public function get_values(): array {
return $this->values;
}
}
111 changes: 62 additions & 49 deletions includes/Term_Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,63 +19,63 @@ class Term_Manager {
*/
private $post_meta_key;

/**
* Timestamp.
*
* @var int
*/
private $time;

/**
* Constructor.
*
* @param string $post_meta_key Post meta key.
* @param int|null $time timestamp.
*/
public function __construct( string $post_meta_key ) {
public function __construct( string $post_meta_key, int $time = null ) {
$this->post_meta_key = $post_meta_key;

$this->time = $time ?? time();
add_action( 'wp_after_insert_post', array( $this, 'update_schedule' ), 100, 1 );
add_action( 'schedule_terms_update_post_term_relations', array( $this, 'update_post_term_relations' ), 10, 4 );
add_action( 'schedule_terms_attach_post_term_relations', array( $this, 'attach_post_term_relations' ), 10, 4 );
add_action( 'schedule_terms_detach_post_term_relations', array( $this, 'detach_post_term_relations' ), 11, 4 );
}

/**
* Get term schedules.
*
* @param int $post_id Post id.
*
* @return array
* @return Schedule[]
*/
private function get_schedules( int $post_id ): array {
$meta_values = get_post_meta( $post_id, $this->post_meta_key, false );
if ( empty( $meta_values ) ) {
return array();
}

$attach_terms = array_filter(
$meta_values,
function ( $value ) {
return 'attach' === $value['type'];
}
);

$detach_terms = array_filter(
$meta_values,
return array_map(
function ( $value ) {
return 'detach' === $value['type'];
}
return new Schedule( $value );
},
$meta_values
);

return array_merge( $attach_terms, $detach_terms );
}

/**
* Get unixtime.
* Get filtered schedules.
*
* @param string $iso_datetime ISO 8601 formatted datetime.
* @param int $post_id Post id.
* @param string $type Schedule type.
*
* @return int
* @return Schedule[]
*/
private function get_timestamp( string $iso_datetime ): int {
try {
$date_time = new \DateTime( $iso_datetime );
} catch ( \Exception $e ) {
wp_die( esc_html( $e->getMessage() ) );
}

return $date_time->getTimestamp();
private function get_filtered_schedules( int $post_id, string $type ): array {
return array_filter(
$this->get_schedules( $post_id ),
function ( $schedule ) use ( $type ) {
return $type === $schedule->get_type();
}
);
}

/**
Expand All @@ -84,39 +84,52 @@ private function get_timestamp( string $iso_datetime ): int {
* @param int $post_id post ID.
*/
public function update_schedule( int $post_id ) {
wp_clear_scheduled_hook( 'schedule_terms_update_post_term_relations', array( $post_id ) );

$this->update_post_term_relations( $post_id );
$this->attach_post_term_relations( $post_id );
$this->detach_post_term_relations( $post_id );

foreach ( $this->get_schedules( $post_id ) as $meta_value ) {
if ( $meta_value ) {
$time = $this->get_timestamp( $meta_value['datetime'] );
if ( time() < $time ) {
wp_schedule_single_event( $time, 'schedule_terms_update_post_term_relations', array( $post_id ) );
foreach ( $this->get_schedules( $post_id ) as $schedule ) {
if ( ! $schedule->is_expired( $this->time ) ) {
$time = $schedule->get_timestamp();
$params = array( $post_id, $schedule->get_taxonomy(), $schedule->get_term() );
wp_clear_scheduled_hook( 'schedule_terms_attach_post_term_relations', $params );
wp_clear_scheduled_hook( 'schedule_terms_detach_post_term_relations', $params );
if ( $schedule->get_type() === 'attach' ) {
wp_schedule_single_event( $time, 'schedule_terms_attach_post_term_relations', $params );
} else {
wp_schedule_single_event( $time, 'schedule_terms_detach_post_term_relations', $params );
}
}
}
}

/**
* Update post terms relation.
* Attach post terms relation.
*
* @param int $post_id post ID.
*
* @return void
*/
public function update_post_term_relations( int $post_id ) {
foreach ( $this->get_schedules( $post_id ) as $meta_value ) {
if ( $meta_value ) {
$time = $this->get_timestamp( $meta_value['datetime'] );
$term = get_term_by( 'slug', $meta_value['term'], $meta_value['taxonomy'] );
if ( time() >= $time ) {
if ( 'attach' === $meta_value['type'] ) {
wp_set_post_terms( $post_id, array( $term->term_id ), $meta_value['taxonomy'], true );
} else {
wp_remove_object_terms( $post_id, $term->term_id, $meta_value['taxonomy'] );
}
}
public function attach_post_term_relations( int $post_id ) {
foreach ( $this->get_filtered_schedules( $post_id, 'attach' ) as $schedule ) {
if ( $schedule->is_expired( $this->time ) ) {
$term = $schedule->get_term();
wp_set_post_terms( $post_id, array( $term->term_id ), $schedule->get_taxonomy(), true );
}
}
}

/**
* Detach post terms relation.
*
* @param int $post_id post ID.
*
* @return void
*/
public function detach_post_term_relations( int $post_id ) {
foreach ( $this->get_filtered_schedules( $post_id, 'detach' ) as $schedule ) {
if ( $schedule->is_expired( $this->time ) ) {
$term = $schedule->get_term();
wp_remove_object_terms( $post_id, $term->term_id, $schedule->get_taxonomy() );
}
}
}
Expand Down

0 comments on commit 09f3f84

Please sign in to comment.