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..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,7 +14,8 @@
/**
* Actions and filters.
*/
-add_action( 'transition_post_status', __NAMESPACE__ . '\flag_status_change', 10, 3 );
+// 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 );
/**
* Check if logging is enabled for patterns.
@@ -30,15 +31,17 @@ function logging_enabled() {
}
/**
- * Add a log entry to a pattern when a flag's status changes.
+ * Add a log entry to a pattern when a flag's status changes, once the flag is fully saved.
*
- * @param string $new_status New post status.
- * @param string $old_status Previous post status.
- * @param \WP_Post $post Post being processed.
+ * @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,17 +60,24 @@ function flag_status_change( $new_status, $old_status, $post ) {
return;
}
- $new = get_post_status_object( $new_status );
- $user = get_user_by( 'id', $post->post_author );
- $user_handle = sprintf(
- '@%s',
- $user->user_login
- );
+ // 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 = $update ? $post_before->post_status : 'new';
- $msg = '';
if ( $new_status === $old_status ) {
return;
- } elseif ( 'new' === $old_status && PENDING_STATUS === $new_status ) {
+ }
+
+ $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 );
+
+ $msg = '';
+ 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
new file mode 100644
index 00000000..094cb2b8
--- /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 the stand-in recorded on the pattern.
+ *
+ * @return string[]
+ */
+ protected function get_log_messages(): array {
+ $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' ) );
+ }
+
+ /**
+ * 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 $author_id = 0 ): int {
+ return wp_insert_post(
+ array(
+ 'post_type' => FLAG_POST_TYPE,
+ 'post_parent' => self::$pattern_id,
+ 'post_author' => $author_id ? $author_id : 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()
+ );
+ }
+
+ /**
+ * 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
new file mode 100644
index 00000000..a4e6ab58
--- /dev/null
+++ b/public_html/wp-content/plugins/pattern-directory/tests/phpunit/stub-internal-notes.php
@@ -0,0 +1,32 @@
+ $post_id ) );
+ }
+}
diff --git a/public_html/wp-content/tests/phpunit/bootstrap.php b/public_html/wp-content/tests/phpunit/bootstrap.php
index ac3ce97a..521bd8ab 100644
--- a/public_html/wp-content/tests/phpunit/bootstrap.php
+++ b/public_html/wp-content/tests/phpunit/bootstrap.php
@@ -59,6 +59,9 @@ function _manually_load_plugins() {
require_once $_locales;
}
+ // Loaded for the whole run, as the real plugin is in production, so no test depends on file load order.
+ require_once dirname( dirname( __DIR__ ) ) . '/plugins/pattern-directory/tests/phpunit/stub-internal-notes.php';
+
require dirname( dirname( __DIR__ ) ) . '/plugins/pattern-directory/bootstrap.php';
require dirname( dirname( __DIR__ ) ) . '/plugins/pattern-creator/pattern-creator.php';
require dirname( dirname( __DIR__ ) ) . '/plugins/pattern-translations/pattern-translations.php';