From 075233d374d7e12f091f51b5e27397a97d8cf93d Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 04:35:46 +0000 Subject: [PATCH 1/3] Pattern Directory: Log flag status changes only once the flag is fully saved Flag status notes were written on `transition_post_status`, which fires in the middle of `wp_insert_post()`, before the flag's terms and meta are saved, and before a REST request has finished. Move the logging to `wp_after_insert_post`, as the notifications already do, reading the old status from the post before the update. It runs at priority 5 so a new flag is still logged ahead of any status change `check_flag_threshold()` makes on the pattern. A flag without a reporter account is skipped instead of failing on the missing user. Fixes #480. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01YBAW4tyUi7UPtZygTvpuaQ --- .../pattern-directory/includes/logging.php | 25 ++- .../tests/phpunit/class-flag-logging-test.php | 167 ++++++++++++++++++ .../tests/phpunit/stub-internal-notes.php | 30 ++++ 3 files changed, 216 insertions(+), 6 deletions(-) create mode 100644 public_html/wp-content/plugins/pattern-directory/tests/phpunit/class-flag-logging-test.php create mode 100644 public_html/wp-content/plugins/pattern-directory/tests/phpunit/stub-internal-notes.php diff --git a/public_html/wp-content/plugins/pattern-directory/includes/logging.php b/public_html/wp-content/plugins/pattern-directory/includes/logging.php index e81a3817..b316796c 100644 --- a/public_html/wp-content/plugins/pattern-directory/includes/logging.php +++ b/public_html/wp-content/plugins/pattern-directory/includes/logging.php @@ -14,7 +14,7 @@ /** * Actions and filters. */ -add_action( 'transition_post_status', __NAMESPACE__ . '\flag_status_change', 10, 3 ); +add_action( 'wp_after_insert_post', __NAMESPACE__ . '\flag_status_change', 5, 4 ); /** * Check if logging is enabled for patterns. @@ -32,13 +32,19 @@ function logging_enabled() { /** * Add a log entry to a pattern when a flag's status changes. * - * @param string $new_status New post status. - * @param string $old_status Previous post status. - * @param \WP_Post $post Post being processed. + * Runs once the flag is fully saved (terms and meta included), and before + * `check_flag_threshold()` so the new flag is logged ahead of any status + * change it causes on the pattern. + * + * @param int $post_id Post ID. + * @param \WP_Post $post Post object. + * @param bool $update Whether this is an existing post being updated. + * @param null|\WP_Post $post_before Null for new posts, the WP_Post object prior + * to the update for updated posts. * * @return void */ -function flag_status_change( $new_status, $old_status, $post ) { +function flag_status_change( $post_id, $post, $update, $post_before ) { if ( ! logging_enabled() ) { return; } @@ -57,8 +63,15 @@ function flag_status_change( $new_status, $old_status, $post ) { return; } + $new_status = $post->post_status; + $old_status = $post_before ? $post_before->post_status : 'new'; + + $user = get_user_by( 'id', $post->post_author ); + if ( ! $user ) { + return; + } + $new = get_post_status_object( $new_status ); - $user = get_user_by( 'id', $post->post_author ); $user_handle = sprintf( '@%s', $user->user_login diff --git a/public_html/wp-content/plugins/pattern-directory/tests/phpunit/class-flag-logging-test.php b/public_html/wp-content/plugins/pattern-directory/tests/phpunit/class-flag-logging-test.php new file mode 100644 index 00000000..4c2eff1a --- /dev/null +++ b/public_html/wp-content/plugins/pattern-directory/tests/phpunit/class-flag-logging-test.php @@ -0,0 +1,167 @@ +user->create_and_get( array( 'user_login' => 'flag-reporter' ) ); + self::$pattern_id = $factory->post->create( + array( + 'post_type' => POST_TYPE, + 'post_status' => 'publish', + ) + ); + } + + /** + * Start each test without recorded notes. + */ + public function set_up(): void { + parent::set_up(); + + $GLOBALS['pattern_directory_test_notes'] = array(); + } + + /** + * The log messages written on the pattern. + * + * Reads the stand-in's record, or the saved notes when the real plugin is loaded. + * + * @return string[] + */ + protected function get_log_messages(): array { + if ( isset( $GLOBALS['pattern_directory_test_notes'] ) && ! post_type_exists( InternalNotes\LOG_POST_TYPE ) ) { + $notes = array_filter( + $GLOBALS['pattern_directory_test_notes'], + function ( array $note ): bool { + return self::$pattern_id === $note['post_parent'] && InternalNotes\LOG_POST_TYPE === ( $note['post_type'] ?? '' ); + } + ); + + return array_values( array_column( $notes, 'post_excerpt' ) ); + } + + $notes = get_posts( + array( + 'post_type' => InternalNotes\LOG_POST_TYPE, + 'post_parent' => self::$pattern_id, + 'post_status' => 'any', + 'orderby' => 'ID', + 'order' => 'ASC', + ) + ); + + return wp_list_pluck( $notes, 'post_excerpt' ); + } + + /** + * Insert a flag the way the REST API does: after-insert hooks run once the request has saved everything. + * + * @param bool $fire_after_hooks Whether to run the after-insert hooks right away. + * @return int The flag ID. + */ + protected function insert_flag( bool $fire_after_hooks = true ): int { + return wp_insert_post( + array( + 'post_type' => FLAG_POST_TYPE, + 'post_parent' => self::$pattern_id, + 'post_author' => self::$reporter->ID, + 'post_status' => PENDING_STATUS, + ), + false, + $fire_after_hooks + ); + } + + /** + * Catches a new flag going unlogged. + */ + public function test_new_flag_is_logged(): void { + $this->insert_flag(); + + $this->assertSame( array( 'New flag submitted by @flag-reporter' ), $this->get_log_messages() ); + } + + /** + * Catches a flag logged before its save finished, as with a REST request whose later steps fail. + */ + public function test_flag_is_logged_only_after_it_is_fully_saved(): void { + $flag_id = $this->insert_flag( false ); + + $this->assertSame( array(), $this->get_log_messages(), 'The flag was logged before its save finished.' ); + + wp_after_insert_post( $flag_id, false, null ); + + $this->assertSame( array( 'New flag submitted by @flag-reporter' ), $this->get_log_messages() ); + } + + /** + * Catches resolving or trashing a flag going unlogged, or an unchanged status being logged. + */ + public function test_status_changes_are_logged(): void { + $flag_id = $this->insert_flag(); + + wp_update_post( + array( + 'ID' => $flag_id, + 'post_title' => 'Same status', + ) + ); + wp_update_post( + array( + 'ID' => $flag_id, + 'post_status' => RESOLVED_STATUS, + ) + ); + wp_trash_post( $flag_id ); + + $resolved = get_post_status_object( RESOLVED_STATUS )->label; + $this->assertSame( + array( + 'New flag submitted by @flag-reporter', + 'Flag submitted by @flag-reporter marked as ' . $resolved, + 'Flag submitted by @flag-reporter moved to trash.', + ), + $this->get_log_messages() + ); + } +} diff --git a/public_html/wp-content/plugins/pattern-directory/tests/phpunit/stub-internal-notes.php b/public_html/wp-content/plugins/pattern-directory/tests/phpunit/stub-internal-notes.php new file mode 100644 index 00000000..6e3fcccc --- /dev/null +++ b/public_html/wp-content/plugins/pattern-directory/tests/phpunit/stub-internal-notes.php @@ -0,0 +1,30 @@ + $post_id ) ); + } +} From 2431102e73095d2f2dea3b384f1cf17918b8d4c8 Mon Sep 17 00:00:00 2001 From: Konstantin Obenland Date: Sat, 14 Nov 2026 23:33:23 -0600 Subject: [PATCH 2/3] Tests: Declare strict types and parameter types in the internal notes stub Co-Authored-By: Claude Opus 5.5 --- .../pattern-directory/tests/phpunit/stub-internal-notes.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/public_html/wp-content/plugins/pattern-directory/tests/phpunit/stub-internal-notes.php b/public_html/wp-content/plugins/pattern-directory/tests/phpunit/stub-internal-notes.php index 6e3fcccc..4fc16a51 100644 --- a/public_html/wp-content/plugins/pattern-directory/tests/phpunit/stub-internal-notes.php +++ b/public_html/wp-content/plugins/pattern-directory/tests/phpunit/stub-internal-notes.php @@ -7,6 +7,8 @@ * @package WordPress\Pattern_Directory */ +declare( strict_types = 1 ); + namespace WordPressdotorg\InternalNotes; if ( ! function_exists( __NAMESPACE__ . '\create_note' ) ) { @@ -24,7 +26,7 @@ * * @return void */ - function create_note( $post_id, $data ) { + function create_note( int $post_id, array $data ): void { $GLOBALS['pattern_directory_test_notes'][] = array_merge( $data, array( 'post_parent' => $post_id ) ); } } From bc7d8971967debdbed836f480af3ddcef9d01740 Mon Sep 17 00:00:00 2001 From: Konstantin Obenland Date: Mon, 16 Nov 2026 13:29:59 -0600 Subject: [PATCH 3/3] Logging: Keep flags from deleted users in the log, and load the notes stub for every test - A flag whose reporter's account no longer exists was left out of the pattern's log entirely. Name the reporter "a deleted user" instead. - An update fired without the post as it was would be logged as a new flag. Skip it, since the old status is unknown. - Return on an unchanged status before looking up the user, as most saves now reach the hook without a status change. - Move the reason for the hook priority next to the add_action call. - Load the internal notes stub from the PHPUnit bootstrap. Defining it from one test file turned logging on for whichever tests ran after it, depending on load order. The test now only reads the stub's record, since the real plugin is never loaded. Co-Authored-By: Claude Opus 5.5 --- .../pattern-directory/includes/logging.php | 27 +++++----- .../tests/phpunit/class-flag-logging-test.php | 54 +++++++++---------- .../tests/phpunit/stub-internal-notes.php | 4 +- .../wp-content/tests/phpunit/bootstrap.php | 3 ++ 4 files changed, 44 insertions(+), 44 deletions(-) diff --git a/public_html/wp-content/plugins/pattern-directory/includes/logging.php b/public_html/wp-content/plugins/pattern-directory/includes/logging.php index b316796c..ba2b69f7 100644 --- a/public_html/wp-content/plugins/pattern-directory/includes/logging.php +++ b/public_html/wp-content/plugins/pattern-directory/includes/logging.php @@ -14,6 +14,7 @@ /** * Actions and filters. */ +// Before `check_flag_threshold()`, so a new flag is logged ahead of the pattern status change it causes. add_action( 'wp_after_insert_post', __NAMESPACE__ . '\flag_status_change', 5, 4 ); /** @@ -30,11 +31,7 @@ function logging_enabled() { } /** - * Add a log entry to a pattern when a flag's status changes. - * - * Runs once the flag is fully saved (terms and meta included), and before - * `check_flag_threshold()` so the new flag is logged ahead of any status - * change it causes on the pattern. + * Add a log entry to a pattern when a flag's status changes, once the flag is fully saved. * * @param int $post_id Post ID. * @param \WP_Post $post Post object. @@ -63,24 +60,24 @@ function flag_status_change( $post_id, $post, $update, $post_before ) { return; } + // Without the post as it was, an update's old status is unknown. + if ( $update && ! $post_before ) { + return; + } + $new_status = $post->post_status; - $old_status = $post_before ? $post_before->post_status : 'new'; + $old_status = $update ? $post_before->post_status : 'new'; - $user = get_user_by( 'id', $post->post_author ); - if ( ! $user ) { + if ( $new_status === $old_status ) { return; } + $user = get_user_by( 'id', $post->post_author ); + $user_handle = $user ? '@' . $user->user_login : __( 'a deleted user', 'wporg-patterns' ); $new = get_post_status_object( $new_status ); - $user_handle = sprintf( - '@%s', - $user->user_login - ); $msg = ''; - if ( $new_status === $old_status ) { - return; - } elseif ( 'new' === $old_status && PENDING_STATUS === $new_status ) { + if ( 'new' === $old_status && PENDING_STATUS === $new_status ) { $msg = sprintf( // translators: User name. __( 'New flag submitted by %s', 'wporg-patterns' ), diff --git a/public_html/wp-content/plugins/pattern-directory/tests/phpunit/class-flag-logging-test.php b/public_html/wp-content/plugins/pattern-directory/tests/phpunit/class-flag-logging-test.php index 4c2eff1a..094cb2b8 100644 --- a/public_html/wp-content/plugins/pattern-directory/tests/phpunit/class-flag-logging-test.php +++ b/public_html/wp-content/plugins/pattern-directory/tests/phpunit/class-flag-logging-test.php @@ -15,8 +15,6 @@ use const WordPressdotorg\Pattern_Directory\Pattern_Post_Type\POST_TYPE; use const WordPressdotorg\Pattern_Directory\Pattern_Flag_Post_Type\{ PENDING_STATUS, RESOLVED_STATUS, POST_TYPE as FLAG_POST_TYPE }; -require_once __DIR__ . '/stub-internal-notes.php'; - /** * Flag status changes are logged on the pattern once the flag is saved (#480). * @@ -62,49 +60,34 @@ public function set_up(): void { } /** - * The log messages written on the pattern. - * - * Reads the stand-in's record, or the saved notes when the real plugin is loaded. + * The log messages the stand-in recorded on the pattern. * * @return string[] */ protected function get_log_messages(): array { - if ( isset( $GLOBALS['pattern_directory_test_notes'] ) && ! post_type_exists( InternalNotes\LOG_POST_TYPE ) ) { - $notes = array_filter( - $GLOBALS['pattern_directory_test_notes'], - function ( array $note ): bool { - return self::$pattern_id === $note['post_parent'] && InternalNotes\LOG_POST_TYPE === ( $note['post_type'] ?? '' ); - } - ); - - return array_values( array_column( $notes, 'post_excerpt' ) ); - } - - $notes = get_posts( - array( - 'post_type' => InternalNotes\LOG_POST_TYPE, - 'post_parent' => self::$pattern_id, - 'post_status' => 'any', - 'orderby' => 'ID', - 'order' => 'ASC', - ) + $notes = array_filter( + $GLOBALS['pattern_directory_test_notes'], + function ( array $note ): bool { + return self::$pattern_id === $note['post_parent'] && InternalNotes\LOG_POST_TYPE === ( $note['post_type'] ?? '' ); + } ); - return wp_list_pluck( $notes, 'post_excerpt' ); + return array_values( array_column( $notes, 'post_excerpt' ) ); } /** * Insert a flag the way the REST API does: after-insert hooks run once the request has saved everything. * * @param bool $fire_after_hooks Whether to run the after-insert hooks right away. + * @param int $author_id The reporter, the shared one by default. * @return int The flag ID. */ - protected function insert_flag( bool $fire_after_hooks = true ): int { + protected function insert_flag( bool $fire_after_hooks = true, int $author_id = 0 ): int { return wp_insert_post( array( 'post_type' => FLAG_POST_TYPE, 'post_parent' => self::$pattern_id, - 'post_author' => self::$reporter->ID, + 'post_author' => $author_id ? $author_id : self::$reporter->ID, 'post_status' => PENDING_STATUS, ), false, @@ -164,4 +147,21 @@ public function test_status_changes_are_logged(): void { $this->get_log_messages() ); } + + /** + * Catches moderation of a flag whose reporter's account is gone dropping out of the pattern's log. + */ + public function test_flag_from_a_deleted_user_is_logged(): void { + $flag_id = $this->insert_flag( true, 999999 ); + + wp_trash_post( $flag_id ); + + $this->assertSame( + array( + 'New flag submitted by a deleted user', + 'Flag submitted by a deleted user moved to trash.', + ), + $this->get_log_messages() + ); + } } diff --git a/public_html/wp-content/plugins/pattern-directory/tests/phpunit/stub-internal-notes.php b/public_html/wp-content/plugins/pattern-directory/tests/phpunit/stub-internal-notes.php index 4fc16a51..a4e6ab58 100644 --- a/public_html/wp-content/plugins/pattern-directory/tests/phpunit/stub-internal-notes.php +++ b/public_html/wp-content/plugins/pattern-directory/tests/phpunit/stub-internal-notes.php @@ -1,8 +1,8 @@