Skip to content

Commit

Permalink
Reset the post_author when user has been deleted
Browse files Browse the repository at this point in the history
Sets the post_author to 2 if the actual post_author is not found
  • Loading branch information
leedxw committed Dec 13, 2024
1 parent 44d04ed commit d5733b4
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 13 deletions.
1 change: 1 addition & 0 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@
require(__DIR__.'/lib/tiny_mce.php');
require(__DIR__.'/lib/image-default-link-type.php');
require(__DIR__.'/lib/feeds.php');
require(__DIR__.'/lib/set-archive-author.php');
16 changes: 5 additions & 11 deletions lib/byline.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,11 @@
function gds_byline()
{
?>
<span class="govuk-visually-hidden">Posted by: </span><?php
$tmp_author_id = get_post_field('post_author', $post->ID);
$tmp_user = get_user_by('id', $author_id);
if (empty($tmp_user->user_login)) {
echo "Deleted user $tmp_author_id";
error_log("{$post->ID} has deleted user $tmp_author_id", 0);
<span class="govuk-visually-hidden">Posted by: </span>
<?php
if (function_exists('coauthors_posts_links')) {
coauthors_posts_links(', ');
} else {
if (function_exists('coauthors_posts_links')) {
coauthors_posts_links(', ');
} else {
?> <a href="<?php echo get_author_posts_url(get_the_author_meta('ID')) ?>" rel="author" class="govuk-link"><?php echo get_the_author() ?></a> <?php
}
?> <a href="<?php echo get_author_posts_url(get_the_author_meta('ID')) ?>" rel="author" class="govuk-link"><?php echo get_the_author() ?></a> <?php
}
}
16 changes: 16 additions & 0 deletions lib/set-archive-author.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

// Set the post_author to be "2"

function set_archive_author($data, $postarr)
{
$fix_types = ["page", "post", "attachment"];

if (!in_array($data['post_type'], $fix_types)) {
return $data;
}

$data['post_author'] = 2;

return $data;
}
17 changes: 15 additions & 2 deletions templates/entry-meta.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
<div class="govuk-body-s">
<?php gds_byline(); ?>, <span class="govuk-visually-hidden">Posted on: </span><time class="updated" datetime="<?php echo get_the_time('c') ?>"><?php echo get_the_date('j F Y') ?></time>
<div class="govuk-body-s"><?php

// If the post is owned by a deleted user, set it to the archive user
$tmp_author_id = get_post_field('post_author', $post->ID);
if ($tmp_author_id > 2) {
$tmp_user = get_user_by('id', $tmp_author_id);
if (empty($tmp_user->user_login)) {
error_log("post {$post->ID} has deleted user $tmp_author_id", 0);
add_filter('wp_insert_post_data', 'set_archive_author', '99', 2);
wp_update_post($post);
}
}

gds_byline();
?>, <span class="govuk-visually-hidden">Posted on: </span><time class="updated" datetime="<?php echo get_the_time('c') ?>"><?php echo get_the_date('j F Y') ?></time>
-
<span class="govuk-visually-hidden">Categories: </span>
<?php echo get_the_category_list(', ') ?>
Expand Down

0 comments on commit d5733b4

Please sign in to comment.