Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/handle non existent author #275

Merged
merged 4 commits into from
Jan 9, 2025
Merged

Conversation

RobjS
Copy link
Contributor

@RobjS RobjS commented Jan 9, 2025

This PR re-allocated authorship of a post if the original author ID corresponds to a user who no longer exists (e.g. if that user has been deleted without their posts being re-assigned).

The authorship of the post is re-assigned to the user whose ID matched the archive_author option, if that option exists. If that option doesn't exist (or is of a non-numeric value), nothing happens.

If the authorship is re-assigned, we also remove any of the co-author data associated with the post, as this can result in the post author still not being correctly displayed in the back-end.

How to test

  1. Run vendor/bin/php-cs-fixer fix --dry-run && vendor/bin/kahlan spec to run the automated tests
  2. Spin up an instance of gov.uk blogs, and checkout this branch of the govuk-blogs theme
  3. Create a temporary user, note their ID, and publish a post with authorship assigned to that user
  4. Delete that user from the database, e.g. DELETE FROM wp_users WHERE ID=[your-new-user]
  5. View the post in the front-end. The author info in the byline should be empty (i.e. just a comma is displayed). In the back-end, when editing the post, the "Author" info should never resolve, and a spinning circle is displayed instead
  6. Create a new user to act as the fallback archive author, and note their ID
  7. Set the archive_author option to that user's ID (e.g. wp db option set archive_author [the-ID])
  8. Refresh the post in the front-end twice. It should now display that it is authored by the selected archive author. That author should also be displayed in the back-end when editing the post, and in the post listing.

RobjS added 3 commits January 9, 2025 12:38
Before: if a user had authored a post, but then the user had
subsequently been deleted without their posts being re-assigned, this
resulted in a blank author being displayed in the front-end, and the
"Author" spinner in the block editor never resolving.

Now: we can set an "archive_author" option in `wp_options`. This should
be set to the ID of the author we want to default to in cases where the
original author has been removed without re-assignment. When that option
is set, the next time the post is viewed in the front-end, the author
will be re-written to be the archive author.
Before: when we re-allocated the post author if the existing post author
no longer existed, we just changed the author id attached to the post.
This fixed the author display in the front-end, but did not resolve the
ever-spinning circle in the back-end, as if co-authors is active,
co-author relationships (defined by taxonomy term relationships) was
still in place.

Now: if we have amended the post author ID, we also remove any co-author
term relationships with the post in question. This means the newly
allocated post author now displays in both the front and back-ends.

public function setArchiveAuthor($postData)
{
$fix_types = ["page", "post", "attachment"];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we care about revisions?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to worry about revisions, they're not currently causing any issues. This code only kicks in if the post is viewed in the front-end, which won't happen with revisions unless they're restored, at which point this would then fix the issue anyway.

return $postData;
}

$archive_user_option = get_option('archive_author');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: do we intend to set a default value for this? If not are we intending to go through the sites automatically or manually to set this value?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I'd been thinking that the wp_options table applies across the entire network, but of course it doesn't, it's per site. I'll modify this so it takes this value from a network option instead, so we'll have to manually set it, but just the once.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right, yes, that was Lee's assumption too!

Copy link
Contributor

@snim2 snim2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks fine but there are a couple of questions here

Therefore, it only needs to be set once for all sites, rather than once
per subsite. It is stored in the `wp_sitemeta` table.
@RobjS
Copy link
Contributor Author

RobjS commented Jan 9, 2025

I've pushed a change so the archive_author is stored at the network level, in the wp_sitemeta table. Testing steps are the same as before, accept the step for setting the archive author ID is wp network meta update 1 archive_author [archive-author-ID]. I've tested, and this works even on subsites where the archive author isn't granted any role on that subsite.

@RobjS RobjS merged commit 53a9baf into master Jan 9, 2025
3 checks passed
@RobjS RobjS deleted the fix/handle-non-existent-author branch January 9, 2025 17:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants