-
Notifications
You must be signed in to change notification settings - Fork 8
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
namespace GovUKBlogs\Theme; | ||
|
||
class FixNonExistentAuthors implements \Dxw\Iguana\Registerable | ||
{ | ||
public function register() | ||
{ | ||
add_action('gds_byline', [$this, 'replaceAbsentAuthor']); | ||
} | ||
|
||
public function replaceAbsentAuthor() | ||
{ | ||
global $post; | ||
$post_author_id = get_post_field('post_author', $post->ID); | ||
if ($post_author_id > 1) { | ||
$post_author = get_user_by('id', $post_author_id); | ||
if (!($post_author)) { | ||
error_log("author of post {$post->ID} is deleted user $post_author_id", 0); | ||
add_filter('wp_insert_post_data', [$this, 'setArchiveAuthor'], 99, 2); | ||
wp_update_post($post); | ||
} | ||
} | ||
} | ||
|
||
public function setArchiveAuthor($postData, $postArray) | ||
{ | ||
$fix_types = ["page", "post", "attachment"]; | ||
|
||
if (!in_array($postData['post_type'], $fix_types)) { | ||
return $postData; | ||
} | ||
|
||
$archive_user_option = get_network_option(null, 'archive_author'); | ||
|
||
if (!empty($archive_user_option)) { | ||
if (is_int($archive_user_option)) { | ||
$archive_author_id = $archive_user_option; | ||
} elseif (is_string($archive_user_option) && ctype_digit($archive_user_option)) { | ||
$archive_author_id = intval($archive_user_option); | ||
} | ||
if (!empty($archive_author_id)) { | ||
$postData['post_author'] = $archive_author_id; | ||
if (taxonomy_exists('author')) { | ||
wp_delete_object_term_relationships($postArray['ID'], 'author'); | ||
} | ||
} | ||
} | ||
return $postData; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
<?php | ||
|
||
describe(\GovUKBlogs\Theme\FixNonExistentAuthors::class, function () { | ||
beforeEach(function () { | ||
$this->fixNonExistentAuthors = new \GovUKBlogs\Theme\FixNonExistentAuthors(); | ||
}); | ||
|
||
it('implements the Registerable interface', function () { | ||
expect($this->fixNonExistentAuthors)->toBeAnInstanceOf(\Dxw\Iguana\Registerable::class); | ||
}); | ||
|
||
describe('->register()', function () { | ||
it('adds the action', function () { | ||
allow('add_action')->toBeCalled(); | ||
expect('add_action')->toBeCalled()->once()->with('gds_byline', [$this->fixNonExistentAuthors, 'replaceAbsentAuthor']); | ||
$this->fixNonExistentAuthors->register(); | ||
}); | ||
}); | ||
|
||
describe('->replaceAbsentAuthor()', function () { | ||
context('the post author ID is 1', function () { | ||
it('does nothing', function () { | ||
global $post; | ||
$post = (object) [ | ||
"ID" => 123 | ||
]; | ||
allow('get_post_field')->toBeCalled()->andReturn(1); | ||
expect('wp_update_post')->not->toBeCalled(); | ||
|
||
$this->fixNonExistentAuthors->replaceAbsentAuthor(); | ||
}); | ||
}); | ||
context('the post author ID is greater than 1', function () { | ||
context('but the user exists', function () { | ||
it('does nothing', function () { | ||
global $post; | ||
$post = (object) [ | ||
"ID" => 123 | ||
]; | ||
allow('get_post_field')->toBeCalled()->andReturn(2); | ||
allow('get_user_by')->toBeCalled()->andReturn((object) [ | ||
"ID" => 2, | ||
"user_login" => "valid_user" | ||
]); | ||
expect('wp_update_post')->not->toBeCalled(); | ||
|
||
$this->fixNonExistentAuthors->replaceAbsentAuthor(); | ||
}); | ||
}); | ||
context('and the user does not exist', function () { | ||
it('adds the filter and calls update_post', function () { | ||
global $post; | ||
$post = (object) [ | ||
"ID" => 123 | ||
]; | ||
allow('get_post_field')->toBeCalled()->andReturn(2); | ||
allow('get_user_by')->toBeCalled()->andReturn(false); | ||
allow('add_filter')->toBeCalled(); | ||
allow('error_log')->toBeCalled(); | ||
expect('error_log')->toBeCalled()->once()->with('author of post 123 is deleted user 2', 0); | ||
expect('add_filter')->toBeCalled()->once()->with('wp_insert_post_data', [$this->fixNonExistentAuthors, 'setArchiveAuthor'], 99, 2); | ||
allow('wp_update_post')->toBeCalled(); | ||
expect('wp_update_post')->toBeCalled()->once(); | ||
|
||
$this->fixNonExistentAuthors->replaceAbsentAuthor(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('->setArchiveAuthor()', function () { | ||
context('the post is not one of the types we want to fix', function () { | ||
it('returns the post data unamended', function () { | ||
$postData = [ | ||
'post_type' => 'custom_post_type', | ||
'post_author' => 123 | ||
]; | ||
expect('get_network_option')->not->toBeCalled(); | ||
|
||
$result = $this->fixNonExistentAuthors->setArchiveAuthor($postData, []); | ||
|
||
expect($result)->toEqual($postData); | ||
}); | ||
}); | ||
context('the post is one of the type we want to fix', function () { | ||
beforeEach(function () { | ||
$this->postData = [ | ||
'post_type' => 'post', | ||
'post_author' => 123 | ||
]; | ||
}); | ||
context('but the archive_author option is not set', function () { | ||
it('returns the post data unamended', function () { | ||
allow('get_network_option')->toBeCalled()->andReturn(false); | ||
|
||
$result = $this->fixNonExistentAuthors->setArchiveAuthor($this->postData, []); | ||
|
||
expect($result)->toEqual($this->postData); | ||
}); | ||
}); | ||
context('and the archive_author option is an integer', function () { | ||
it('amends the post data to set the post_author to the archive_author value', function () { | ||
allow('get_network_option')->toBeCalled()->andReturn(456); | ||
allow('taxonomy_exists')->toBeCalled()->andReturn(false); | ||
|
||
$result = $this->fixNonExistentAuthors->setArchiveAuthor($this->postData, []); | ||
|
||
expect($result)->toEqual([ | ||
'post_type' => 'post', | ||
'post_author' => 456 | ||
]); | ||
}); | ||
}); | ||
context('and the archive_author option is a string containing only an integer', function () { | ||
it('amends the post data to set the post_author to the archive_author value', function () { | ||
allow('get_network_option')->toBeCalled()->andReturn('456'); | ||
allow('taxonomy_exists')->toBeCalled()->andReturn(false); | ||
|
||
$result = $this->fixNonExistentAuthors->setArchiveAuthor($this->postData, []); | ||
|
||
expect($result)->toEqual([ | ||
'post_type' => 'post', | ||
'post_author' => 456 | ||
]); | ||
}); | ||
}); | ||
context('and the archive_author option is a string containing non-numeric characters', function () { | ||
it('returns the post data unamended', function () { | ||
allow('get_network_option')->toBeCalled()->andReturn('456foo'); | ||
|
||
$result = $this->fixNonExistentAuthors->setArchiveAuthor($this->postData, []); | ||
|
||
expect($result)->toEqual($this->postData); | ||
}); | ||
}); | ||
context('and the "author" taxonomy exists, indicating that co-authors is in use', function () { | ||
it('removes any existing co-author relationships with this post, as well as amending the author ID', function () { | ||
allow('get_network_option')->toBeCalled()->andReturn(456); | ||
allow('taxonomy_exists')->toBeCalled()->andReturn(true); | ||
allow('wp_delete_object_term_relationships')->toBeCalled(); | ||
expect('wp_delete_object_term_relationships')->toBeCalled()->once()->with(123, 'author'); | ||
|
||
$result = $this->fixNonExistentAuthors->setArchiveAuthor($this->postData, ['ID' => 123]); | ||
|
||
expect($result)->toEqual([ | ||
'post_type' => 'post', | ||
'post_author' => 456 | ||
]); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.