Skip to content
This repository has been archived by the owner on Aug 16, 2024. It is now read-only.

Show post type name in purge initiated notices #104

Merged
merged 1 commit into from
Jul 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ Full change log available at [GitHub](https://github.com/typisttech/sunny/blob/m
= 2.4.0 =

* Extract targets service provider
* Show post type name in purge initiated notices

= 2.3.0 =

Expand Down
15 changes: 9 additions & 6 deletions src/Posts/PostListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,11 @@ public static function getHooks(): array
*/
public function handlePostEdited(int $_id, WP_Post $post)
{
// Translators: %1$s is the post id; %2$s is the old status; %3$s is the new status.
$reasonFormat = __('Post (ID: %1$s) is being edited', 'sunny');
$reason = sprintf($reasonFormat, $post->ID);
$postType = PostType::getSingularNameFor($post);

// Translators: %1$s is the post type; %2$s is the post id.
$reasonFormat = __('%1$s (ID: %2$s) is being edited', 'sunny');
$reason = sprintf($reasonFormat, $postType, $post->ID);

$command = $this->purgeCommandFactory->buildForPost($post, $reason);
$this->purger->execute($command);
Expand Down Expand Up @@ -112,9 +114,10 @@ public function handlePostStatusTransited(string $newStatus, string $oldStatus,
return;
}

// Translators: %1$s is the post id; %2$s is the old status; %3$s is the new status.
$reasonFormat = __('Post (ID: %1$s) changed from %2$s to %3$s', 'sunny');
$reason = sprintf($reasonFormat, $post->ID, $oldStatus, $newStatus);
$postType = PostType::getSingularNameFor($post);
// Translators: %1$s is the post type; %2$s is the post id; %3$s is the old status; %4$s is the new status.
$reasonFormat = __('%1$s (ID: %2$s) changed from %3$s to %4$s', 'sunny');
$reason = sprintf($reasonFormat, $postType, $post->ID, $oldStatus, $newStatus);

$command = $this->purgeCommandFactory->buildForPost($post, $reason);
$this->purger->execute($command);
Expand Down
45 changes: 45 additions & 0 deletions src/Posts/PostType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* Sunny
*
* Automatically purge CloudFlare cache, including cache everything rules.
*
* @package Sunny
*
* @author Typist Tech <[email protected]>
* @copyright 2017 Typist Tech
* @license GPL-2.0+
*
* @see https://www.typist.tech/projects/sunny
* @see https://wordpress.org/plugins/sunny/
*/

declare(strict_types=1);

namespace TypistTech\Sunny\Posts;

use WP_Post;

/**
* Final class PostType
*/
final class PostType
{
/**
* Get singular post type name for a given post.
*
* @param WP_Post $post The post object to determine singular post type name.
*
* @return string
*/
public static function getSingularNameFor(WP_Post $post): string
{
$postTypeObject = get_post_type_object($post->post_type);

if (null === $postTypeObject) {
return __('Unknown Post Type', 'sunny');
}

return $postTypeObject->labels->singular_name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
$I->amGoingTo('publish a draft post');
$I->click('#publish');

$I->wantToTest('purge initiated notice shows');
$I->wantToTest('purge initiated notice shows with post type of `Post`');
$I->see('Sunny: Purge initiated.');
$I->see('Reason: Post');
19 changes: 19 additions & 0 deletions tests/acceptance/Posts/Listener/PurgeWhenNewPagePublishedCept.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

use TypistTech\Sunny\AcceptanceTester;

$I = new AcceptanceTester($scenario);
$I->wantToTest('purge initiated when new post published');

$I->loginAsAdmin();
$I->amOnAdminPage('post-new.php?post_type=page');

$I->amGoingTo('publish a new page');
$I->fillField('post_title', 'Foo');
$I->click('#publish');

$I->wantToTest('purge initiated notice shows with post type of `Page`');
$I->see('Sunny: Purge initiated.');
$I->see('Reason: Page');
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
$I->fillField('post_title', 'Foo');
$I->click('#publish');

$I->wantToTest('purge initiated notice shows');
$I->wantToTest('purge initiated notice shows with post type of `Post`');
$I->see('Sunny: Purge initiated.');
$I->see('Reason: Post');
19 changes: 19 additions & 0 deletions tests/acceptance/Posts/Listener/PurgeWhenPageUpdatedCept.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

use TypistTech\Sunny\AcceptanceTester;

$I = new AcceptanceTester($scenario);
$I->wantToTest('purge initiated when post updated');

$I->loginAsAdmin();
$I->amOnAdminPage('post.php?post=2&action=edit');

$I->amGoingTo('update a published post');
$I->fillField('post_title', 'Foo');
$I->click('#publish');

$I->wantToTest('purge initiated notice shows with post type of `Page`');
$I->see('Sunny: Purge initiated.');
$I->see('Reason: Page (ID: 2) is being edited');
3 changes: 2 additions & 1 deletion tests/acceptance/Posts/Listener/PurgeWhenPostUpdatedCept.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
$I->fillField('post_title', 'Foo');
$I->click('#publish');

$I->wantToTest('purge initiated notice shows');
$I->wantToTest('purge initiated notice shows with post type of `Post`');
$I->see('Sunny: Purge initiated.');
$I->see('Reason: Post (ID: 1) is being edited');