Skip to content
This repository was archived by the owner on Aug 16, 2024. It is now read-only.
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 .styleci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ disabled:
- no_spaces_inside_offset
- phpdoc_no_package
- phpdoc_summary
- simplified_null_return
- trim_array_spaces

enabled:
Expand Down
36 changes: 26 additions & 10 deletions src/Purge/Command.php → src/Caches/PurgeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@

declare(strict_types=1);

namespace TypistTech\Sunny\Purge;
namespace TypistTech\Sunny\Caches;

/**
* Final class Command
*
* Immutable data transfer object that holds necessary information about this action.
* Final class PurgeCommand
*/
final class Command
final class PurgeCommand
{
/**
* Reason to trigger a purge
Expand All @@ -37,18 +35,36 @@ final class Command
*
* @var string[]
*/
private $urls;
private $urls = [];

/**
* Command constructor.
*
* @param string $reason Reason to trigger a purge.
* @param string|string[] ...$urls Urls to be purged.
* @param string $reason Reason to trigger a purge.
* @param string[] $urls Urls to be purged.
*/
public function __construct(string $reason, string ...$urls)
public function __construct(string $reason, array $urls)
{
$this->reason = $reason;
$this->urls = $urls;
$this->setUrls($urls);
}

/**
* Urls setter.
*
* @param string[] $urls Urls to be purged. Maybe multidimensional.
*
* @return void
*/
private function setUrls(array $urls)
{
array_map(function ($item) {
if (is_array($item)) {
$this->setUrls($item);
} elseif (is_string($item)) {
$this->urls[] = $item;
}
}, $urls);
}

/**
Expand Down
61 changes: 61 additions & 0 deletions src/Caches/PurgeCommandFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/**
* Sunny
*
* Automatically purge CloudFlare cache, including cache everything rules.
*
* @package Sunny
*
* @author Typist Tech <sunny@typist.tech>
* @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\Caches;

use TypistTech\Sunny\RelatedUrls\RelatedUrls;
use WP_Post;

/**
* Final class PurgeCommandFactory
*/
final class PurgeCommandFactory
{
/**
* Related urls finder
*
* @var RelatedUrls
*/
private $relatedUrls;

/**
* PurgeCommandFactory constructor.
*
* @param RelatedUrls $relatedUrls Related urls finder.
*/
public function __construct(RelatedUrls $relatedUrls)
{
$this->relatedUrls = $relatedUrls;
}

/**
* Build a purge command for a post.
*
* @param WP_Post $post Post to be purged.
* @param string $reason Reason to perform a purge.
*
* @return PurgeCommand
*/
public function buildForPost(WP_Post $post, string $reason): PurgeCommand
{
return new PurgeCommand(
$reason,
$this->relatedUrls->allByPost($post)
);
}
}
31 changes: 12 additions & 19 deletions src/Purge/Handler.php → src/Caches/Purger.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,14 @@

declare(strict_types=1);

namespace TypistTech\Sunny\Purge;
namespace TypistTech\Sunny\Caches;

use TypistTech\Sunny\Cloudflare\Cache;
use TypistTech\Sunny\LoadableInterface;
use TypistTech\Sunny\Vendor\TypistTech\WPContainedHook\Action;

/**
* Final class Handler
* Final class Purger
*/
final class Handler implements LoadableInterface
final class Purger
{
/**
* Api adopter
Expand All @@ -45,24 +43,19 @@ public function __construct(Cache $cache)
}

/**
* {@inheritdoc}
*/
public static function getHooks(): array
{
return [ new Action('sunny_do_purge', __CLASS__, 'handle') ];
}

/**
* Handle purge command
* Purge urls from Cloudflare cache.
*
* @todo Process in 30 urls at a time.
* @todo Check has at least one url.
*
* @param Command $event Immutable data transfer object that holds necessary information about this action.
* @param PurgeCommand $command Purge command.
*
* @return void
* @return array|\WP_Error
*/
public function handle(Command $event)
public function execute(PurgeCommand $command)
{
$this->cache->purge(
...$event->getUrls()
return $this->cache->purgeFiles(
...$command->getUrls()
);
}
}
4 changes: 1 addition & 3 deletions src/Cloudflare/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,11 @@ public function __construct(OptionStore $optionStore, CloudflareCache $client =
* Purge individual files (permission needed: #zone:edit).
* Remove one or more files from CloudFlare's cache.
*
* @todo Warn if more than 30 urls given.
*
* @param string|string[] ...$urls URLs that should be removed from cache. Maximum: 30 urls.
*
* @return array|\WP_Error
*/
public function purge(string ...$urls)
public function purgeFiles(string ...$urls)
{
$this->setUpClient();

Expand Down
44 changes: 0 additions & 44 deletions src/Container.php

This file was deleted.

20 changes: 8 additions & 12 deletions src/Post/Finder.php → src/Posts/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@

declare(strict_types=1);

namespace TypistTech\Sunny\Post;
namespace TypistTech\Sunny\Posts;

/**
* Final class Finder
* Final class Post
*/
final class Finder
final class Post
{
/**
* Find WP_Post by its Url.
Expand All @@ -30,20 +30,16 @@ final class Finder
*
* @return \WP_Post|null
*/
public static function findWpPostByUrl(string $url)
public static function findByUrl(string $url)
{
$cleanUrl = esc_url_raw($url);

if (empty($cleanUrl)) {
return;
}

// @codingStandardsIgnoreStart
$postId = url_to_postid($cleanUrl);

// @codingStandardsIgnoreEnd

return self::findWpPostById($postId);
return self::findById($postId);
}

/**
Expand All @@ -53,12 +49,12 @@ public static function findWpPostByUrl(string $url)
*
* @return \WP_Post|null
*/
public static function findWpPostById(int $postId)
public static function findById(int $postId)
{
if ($postId < 1) {
return;
return null;
}

return get_post(absint($postId));
return get_post($postId);
}
}
Loading