diff --git a/includes/Schedule.php b/includes/Schedule.php index f667ead..bdb4e5a 100644 --- a/includes/Schedule.php +++ b/includes/Schedule.php @@ -14,8 +14,8 @@ */ class Schedule { - const ATTACH = 'attach'; - const DETACH = 'detach'; + public const ATTACH = 'attach'; + public const DETACH = 'detach'; /** * Schedule tyoe. diff --git a/includes/Term_Manager.php b/includes/Term_Manager.php index 1f3bf1d..f716cb0 100644 --- a/includes/Term_Manager.php +++ b/includes/Term_Manager.php @@ -14,6 +14,8 @@ */ class Term_Manager { + public const SCHEDULED_HOOK_NAME = 'schedule_terms_update_post_term_relations'; + /** * Post meta key for save term info. * @@ -49,7 +51,7 @@ public function __construct( string $post_meta_key, string $term_meta_key, int $ $this->time = $time ?? time(); add_action( 'wp_after_insert_post', array( $this, 'update_post_term_relations' ), 100, 1 ); 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( self::SCHEDULED_HOOK_NAME, array( $this, 'update_post_term_relations' ), 10, 4 ); } /** @@ -63,6 +65,7 @@ private function is_active_term( ?WP_Term $term ): bool { if ( ! $term ) { return false; } + return ! ! get_term_meta( $term->term_id, $this->term_meta_key, true ); } @@ -139,8 +142,8 @@ public function update_schedule( int $post_id ) { if ( ! $schedule->is_expired( $this->time ) ) { $time = $schedule->get_timestamp(); $params = array( $post_id, array( $schedule->get_type() ), $schedule->get_taxonomy(), $schedule->get_term() ); - wp_clear_scheduled_hook( 'schedule_terms_update_post_term_relations', $params ); - wp_schedule_single_event( $time, 'schedule_terms_update_post_term_relations', $params ); + wp_clear_scheduled_hook( self::SCHEDULED_HOOK_NAME, $params ); + wp_schedule_single_event( $time, self::SCHEDULED_HOOK_NAME, $params ); } } } diff --git a/tests/Term_Manager_Test.php b/tests/Term_Manager_Test.php index 3492ee9..4804efa 100644 --- a/tests/Term_Manager_Test.php +++ b/tests/Term_Manager_Test.php @@ -8,7 +8,6 @@ namespace HAMWORKS\WP\Schedule_Terms\Tests; use HAMWORKS\WP\Schedule_Terms\Term_Manager; -use PHPUnit\Framework\TestCase; use WP_UnitTestCase; /** @@ -20,7 +19,7 @@ class Term_Manager_Test extends WP_UnitTestCase { * Test for Term_Manager::update_post_term_relations(). */ public function test_update_post_term_relations() { - $term_manager = new Term_Manager( 'schedule_terms', 'schedule_terms_active' ); + $term_manager = new Term_Manager( 'schedule_terms', 'schedule_terms_active', strtotime( '2022-05-29T08:05:41+00:00' ) ); $category_id = $this->factory()->category->create( array( 'name' => 'category-1' ) ); update_term_meta( $category_id, 'schedule_terms_active', true ); @@ -33,22 +32,125 @@ public function test_update_post_term_relations() { 'type' => 'attach', 'taxonomy' => 'category', 'term' => 'category-1', - 'datetime' => '2012-07-05T22:09:28+09:00', + 'datetime' => '2020-07-05T22:09:28+09:00', ), false ); + add_post_meta( + $post_id, + 'schedule_terms', + array( + 'type' => 'detach', + 'taxonomy' => 'category', + 'term' => 'category-1', + 'datetime' => '2022-07-05T22:09:28+09:00', + ), + false + ); $term_manager->update_post_term_relations( $post_id ); - $this->assertTrue( has_category( 'category-1', $post_id ) ); + + $reflection = new \ReflectionClass( $term_manager ); + $time = $reflection->getProperty( 'time' ); + $time->setAccessible( true ); + $time->setValue( $term_manager, strtotime( '2023-05-29T08:05:41+00:00' ) ); + $term_manager->update_post_term_relations( $post_id ); + $this->assertFalse( has_category( 'category-1', $post_id ) ); } /** * Test for Term_Manager::update_schedule(). */ public function test_update_schedule() { - $this->assertEquals( 1, 1 ); + $term_manager = new Term_Manager( 'schedule_terms', 'schedule_terms_active', strtotime( '2022-05-29T08:05:41+00:00' ) ); + + foreach ( range( 1, 2 ) as $index ) { + $category_id = $this->factory()->category->create( array( 'name' => 'category-' . $index ) ); + update_term_meta( $category_id, 'schedule_terms_active', true ); + } + + $post_id = $this->factory()->post->create(); + add_post_meta( + $post_id, + 'schedule_terms', + array( + 'type' => 'attach', + 'taxonomy' => 'category', + 'term' => 'category-1', + 'datetime' => '2022-05-29T08:05:40+00:00', + ), + false + ); + add_post_meta( + $post_id, + 'schedule_terms', + array( + 'type' => 'detach', + 'taxonomy' => 'category', + 'term' => 'category-1', + 'datetime' => '2022-05-29T08:05:42+00:00', + ), + false + ); + add_post_meta( + $post_id, + 'schedule_terms', + array( + 'type' => 'attach', + 'taxonomy' => 'category', + 'term' => 'category-2', + 'datetime' => '2022-05-29T08:05:42+00:00', + ), + false + ); + add_post_meta( + $post_id, + 'schedule_terms', + array( + 'type' => 'detach', + 'taxonomy' => 'category', + 'term' => 'category-2', + 'datetime' => '2022-05-29T08:05:43+00:00', + ), + false + ); + $term_manager->update_schedule( $post_id ); + + $events = array_filter( + self::get_events(), + function ( $event ) { + return Term_Manager::SCHEDULED_HOOK_NAME === $event->hook; + } + ); + + $this->assertEquals( 3, count( $events ) ); + } + + /** + * Get events. + * + * @return array + */ + public static function get_events(): array { + $crons = _get_cron_array(); + $events = array(); + foreach ( $crons as $time => $hooks ) { + foreach ( $hooks as $hook => $hook_events ) { + foreach ( $hook_events as $sig => $data ) { + $events[] = (object) array( + 'hook' => $hook, + 'time' => $time, + 'sig' => $sig, + 'args' => $data['args'], + 'schedule' => $data['schedule'], + ); + } + } + } + + return $events; } }