Merge branch 'master' of https://github.com/koselig/library
Diff
src/Models/Comment.php | 2 +-
src/Models/Post.php | 30 ++++++++--------
src/Proxy/WordpressCache.php | 62 +++++++++++++++++++++------------
src/Support/RecursiveCommentIterator.php | 27 ++++++++++----
src/Support/RecursiveMenuIterator.php | 37 +++++++++++++++-----
src/Support/Wordpress.php | 1 +-
src/helpers.php | 2 +-
7 files changed, 112 insertions(+), 49 deletions(-)
@@ -119,6 +119,7 @@ class Comment extends Model
* Get an avatar for the comment.
*
* @param array $args args to pass to {@link get_avatar_url}
*
* @return false|string
*/
public function avatar($args = [])
@@ -130,6 +131,7 @@ class Comment extends Model
* Get the {@link WP_Comment} instance for this Comment.
*
* @deprecated Use the methods already provided by this model.
*
* @return WP_Comment
*/
public function toWordpressComment()
@@ -31,20 +31,6 @@ class Post extends Model
protected $prefix = DB_PREFIX;
/**
* The "booting" method of the model.
*
* @return void
*/
protected static function boot()
{
parent::boot();
static::addGlobalScope('published', function (Builder $builder) {
$builder->where('post_status', 'publish');
});
}
/**
* Length of time to cache this model for.
*
* @var int
@@ -340,6 +326,7 @@ class Post extends Model
* Get an iterator for all the comments in this post.
*
* @param int $flags flags to pass to the {@link RecursiveIteratorIterator}
*
* @return RecursiveIteratorIterator
*/
public function getCommentIterator($flags = RecursiveIteratorIterator::SELF_FIRST)
@@ -351,10 +338,25 @@ class Post extends Model
* Get the {@link WP_Post} instance for this Post.
*
* @deprecated Use the methods already provided by this model.
*
* @return WP_Post
*/
public function toWordpressPost()
{
return new WP_Post((object) $this->toArray());
}
/**
* The "booting" method of the model.
*
* @return void
*/
protected static function boot()
{
parent::boot();
static::addGlobalScope('published', function (Builder $builder) {
$builder->where('post_status', 'publish');
});
}
}
@@ -12,6 +12,11 @@ use Illuminate\Cache\TaggableStore;
class WordpressCache
{
/**
* Prefix used for anything cached using the Wordpress API so it doesn't conflict
* with our normal caching.
*/
const PREFIX = 'wp';
/**
* The amount of times the cache data was already stored in the cache.
*
* @var int
@@ -19,7 +24,7 @@ class WordpressCache
protected $cacheHits = 0;
/**
* Track how may requests were not cached
* Track how may requests were not cached.
*
* @var int
*/
@@ -54,12 +59,6 @@ class WordpressCache
protected $nonPersistentStore = [];
/**
* Prefix used for anything cached using the Wordpress API so it doesn't conflict
* with our normal caching.
*/
const PREFIX = 'wp';
/**
* WordpressCache constructor.
*/
public function __construct()
@@ -87,6 +86,7 @@ class WordpressCache
* @param mixed $value The value to store.
* @param string $group The group value appended to the $key.
* @param int $expiration The expiration time, defaults to 0.
*
* @return bool Returns TRUE on success or FALSE on failure.
*/
public function add($key, $value, $group = 'default', $expiration = 0)
@@ -95,7 +95,7 @@ class WordpressCache
return false;
}
if (in_array($group, $this->nonPersistentGroups)) {
if (in_array($group, $this->nonPersistentGroups, true)) {
if (!isset($this->nonPersistentStore[$group])) {
$this->nonPersistentStore[$group] = [];
} elseif (isset($this->nonPersistentStore[$group][$key])) {
@@ -103,6 +103,7 @@ class WordpressCache
}
$this->nonPersistentStore[$group][$key] = $value;
return true;
}
@@ -112,6 +113,7 @@ class WordpressCache
if ($expiration === 0) {
static::getCache($group)->forever($this->buildKey($group, $key), $value);
return true;
}
@@ -128,16 +130,18 @@ class WordpressCache
* @param mixed $value The value to store.
* @param string $group The group value appended to the $key.
* @param int $expiration The expiration time, defaults to 0.
*
* @return bool Returns TRUE on success or FALSE on failure.
*/
public function replace($key, $value, $group = 'default', $expiration = 0)
{
if (in_array($group, $this->nonPersistentGroups)) {
if (in_array($group, $this->nonPersistentGroups, true)) {
if (!isset($this->nonPersistentStore[$group][$key])) {
return false;
}
$this->nonPersistentStore[$group][$key] = $value;
return true;
}
@@ -147,10 +151,12 @@ class WordpressCache
if ($expiration === 0) {
static::getCache($group)->forever($this->buildKey($group, $key), $value);
return true;
}
static::getCache($group)->put($this->buildKey($group, $key), $value, $expiration / 60);
return true;
}
@@ -159,11 +165,12 @@ class WordpressCache
*
* @param string $key The key under which to store the value.
* @param string $group The group value appended to the $key.
*
* @return bool Returns TRUE on success or FALSE on failure.
*/
public function delete($key, $group = 'default')
{
if (in_array($group, $this->nonPersistentGroups)) {
if (in_array($group, $this->nonPersistentGroups, true)) {
if (isset($this->nonPersistentStore[$group][$key])) {
unset($this->nonPersistentStore[$group][$key]);
}
@@ -201,20 +208,23 @@ class WordpressCache
*
* @param string $key The key under which to store the value.
* @param string $group The group value appended to the $key.
* @param boolean $force Optional. Ignored
* @param bool $force Optional. Ignored
* @param bool &$found Optional. Whether the key was found in the cache. Disambiguates a return of
* false, a storable value. Passed by reference. Default null.
*
* @return bool|mixed Cached object value.
*/
public function get($key, $group = 'default', $force = false, &$found = null)
{
if (in_array($group, $this->nonPersistentGroups)) {
if (in_array($group, $this->nonPersistentGroups, true)) {
if (!isset($this->nonPersistentStore[$group][$key])) {
$found = false;
return false;
}
$found = true;
return $this->nonPersistentStore[$group][$key];
}
@@ -226,6 +236,7 @@ class WordpressCache
} else {
$this->cacheMisses++;
$found = false;
return false;
}
@@ -233,16 +244,17 @@ class WordpressCache
}
/**
* Increment a counter by the amount specified
* Increment a counter by the amount specified.
*
* @param string $key
* @param int $offset
* @param string $group
*
* @return int|bool False on failure, the item's new value on success.
*/
public function incr($key, $offset = 1, $group = 'default')
{
if (in_array($group, $this->nonPersistentGroups)) {
if (in_array($group, $this->nonPersistentGroups, true)) {
if (!isset($this->nonPersistentStore[$group])) {
$this->nonPersistentStore[$group] = [];
}
@@ -252,6 +264,7 @@ class WordpressCache
}
$this->nonPersistentStore[$group][$key]++;
return true;
}
@@ -261,16 +274,17 @@ class WordpressCache
}
/**
* Decrease a counter by the amount specified
* Decrease a counter by the amount specified.
*
* @param string $key
* @param int $offset
* @param string $group
*
* @return int|bool False on failure, the item's new value on success.
*/
public function decr($key, $offset = 1, $group = 'default')
{
if (in_array($group, $this->nonPersistentGroups)) {
if (in_array($group, $this->nonPersistentGroups, true)) {
if (!isset($this->nonPersistentStore[$group])) {
$this->nonPersistentStore[$group] = [];
}
@@ -280,6 +294,7 @@ class WordpressCache
}
$this->nonPersistentStore[$group][$key]--;
return true;
}
@@ -297,21 +312,24 @@ class WordpressCache
* @param mixed $value The value to store.
* @param string $group The group value appended to the $key.
* @param int $expiration The expiration time, defaults to 0.
*
* @return bool Returns TRUE on success or FALSE on failure.
*/
public function set($key, $value, $group = 'default', $expiration = 0)
{
if (in_array($group, $this->nonPersistentGroups)) {
if (in_array($group, $this->nonPersistentGroups, true)) {
if (!isset($this->nonPersistentStore[$group])) {
$this->nonPersistentStore[$group] = [];
}
$this->nonPersistentStore[$group][$key] = $value;
return true;
}
if ($expiration === 0) {
static::getCache($group)->forever($this->buildKey($group, $key), $value);
return true;
}
@@ -331,7 +349,7 @@ class WordpressCache
}
/**
* Sets the list of non-persistent cache groups
* Sets the list of non-persistent cache groups.
*
* @param array $groups List of groups that should not be persisted.
*/
@@ -344,13 +362,14 @@ class WordpressCache
* Get the cache driver we should be saving to and add a tag if the driver supports it.
*
* @param string $group
*
* @return Cache|\Illuminate\Cache\TaggedCache
*/
protected static function getCache($group = 'default')
{
return Cache::getStore() instanceof TaggableStore ? Cache::tags([
sprintf('%s:%s:%s', static::PREFIX . config('cache.prefix'), $group ?: 'default'),
sprintf('%s:%s', static::PREFIX, config('cache.prefix'))
sprintf('%s:%s', static::PREFIX, config('cache.prefix')),
]) : cache();
}
@@ -359,15 +378,16 @@ class WordpressCache
*
* @param string $group The group value appended to the $key.
* @param string $key The key under which to store the value.
*
* @return string
*/
protected function buildKey($group = 'default', $key)
protected function buildKey($group, $key)
{
if (empty($group)) {
$group = 'default';
}
$prefix = (is_multisite() && !in_array($group, $this->globalGroups)) ? ($this->blogPrefix . ':') : '';
$prefix = (is_multisite() && !in_array($group, $this->globalGroups, true)) ? ($this->blogPrefix . ':') : '';
return sprintf('%s:%s%s:%s', static::PREFIX, $prefix, $group, $key);
}
@@ -26,10 +26,12 @@ class RecursiveCommentIterator implements RecursiveIterator
}
/**
* Return the current element
* Return the current element.
*
* @link http://php.net/manual/en/iterator.current.php
*
* @return Comment Can return any type.
*
* @since 5.0.0
*/
public function current()
@@ -38,10 +40,12 @@ class RecursiveCommentIterator implements RecursiveIterator
}
/**
* Move forward to next element
* Move forward to next element.
*
* @link http://php.net/manual/en/iterator.next.php
*
* @return void Any returned value is ignored.
*
* @since 5.0.0
*/
public function next()
@@ -50,10 +54,12 @@ class RecursiveCommentIterator implements RecursiveIterator
}
/**
* Return the key of the current element
* Return the key of the current element.
*
* @link http://php.net/manual/en/iterator.key.php
*
* @return mixed scalar on success, or null on failure.
*
* @since 5.0.0
*/
public function key()
@@ -62,11 +68,13 @@ class RecursiveCommentIterator implements RecursiveIterator
}
/**
* Checks if current position is valid
* Checks if current position is valid.
*
* @link http://php.net/manual/en/iterator.valid.php
* @return boolean The return value will be casted to boolean and then evaluated.
*
* @return bool The return value will be casted to boolean and then evaluated.
* Returns true on success or false on failure.
*
* @since 5.0.0
*/
public function valid()
@@ -75,9 +83,12 @@ class RecursiveCommentIterator implements RecursiveIterator
}
/**
* Rewind the Iterator to the first element
* Rewind the Iterator to the first element.
*
* @link http://php.net/manual/en/iterator.rewind.php
*
* @return void Any returned value is ignored.
*
* @since 5.0.0
*/
public function rewind()
@@ -89,7 +100,9 @@ class RecursiveCommentIterator implements RecursiveIterator
* Returns if an iterator can be created for the current entry.
*
* @link http://php.net/manual/en/recursiveiterator.haschildren.php
*
* @return bool true if the current entry can be iterated over, otherwise returns false.
*
* @since 5.1.0
*/
public function hasChildren()
@@ -101,7 +114,9 @@ class RecursiveCommentIterator implements RecursiveIterator
* Returns an iterator for the current entry.
*
* @link http://php.net/manual/en/recursiveiterator.getchildren.php
*
* @return RecursiveIterator An iterator for the current entry.
*
* @since 5.1.0
*/
public function getChildren()
@@ -10,8 +10,8 @@ use RecursiveIterator;
*/
class RecursiveMenuIterator implements RecursiveIterator
{
private $current = 0;
public $items;
private $current = 0;
/**
* Create a new RecursiveMenuIterator instance.
@@ -33,7 +33,7 @@ class RecursiveMenuIterator implements RecursiveIterator
// only have nodes without a parent at the top level of the tree
$this->items = collect($itemsArray)->filter(function ($item) {
return $item->menu_item_parent == 0;
return $item->menu_item_parent === 0;
})->reverse()->values();
} else {
$this->items = $menu;
@@ -41,9 +41,12 @@ class RecursiveMenuIterator implements RecursiveIterator
}
/**
* Return the current element
* Return the current element.
*
* @link http://php.net/manual/en/iterator.current.php
*
* @return \WP_Post Can return any type.
*
* @since 5.0.0
*/
public function current()
@@ -52,9 +55,12 @@ class RecursiveMenuIterator implements RecursiveIterator
}
/**
* Move forward to next element
* Move forward to next element.
*
* @link http://php.net/manual/en/iterator.next.php
*
* @return void Any returned value is ignored.
*
* @since 5.0.0
*/
public function next()
@@ -63,9 +69,12 @@ class RecursiveMenuIterator implements RecursiveIterator
}
/**
* Return the key of the current element
* Return the key of the current element.
*
* @link http://php.net/manual/en/iterator.key.php
*
* @return mixed scalar on success, or null on failure.
*
* @since 5.0.0
*/
public function key()
@@ -74,10 +83,13 @@ class RecursiveMenuIterator implements RecursiveIterator
}
/**
* Checks if current position is valid
* Checks if current position is valid.
*
* @link http://php.net/manual/en/iterator.valid.php
* @return boolean The return value will be casted to boolean and then evaluated.
*
* @return bool The return value will be casted to boolean and then evaluated.
* Returns true on success or false on failure.
*
* @since 5.0.0
*/
public function valid()
@@ -86,9 +98,12 @@ class RecursiveMenuIterator implements RecursiveIterator
}
/**
* Rewind the Iterator to the first element
* Rewind the Iterator to the first element.
*
* @link http://php.net/manual/en/iterator.rewind.php
*
* @return void Any returned value is ignored.
*
* @since 5.0.0
*/
public function rewind()
@@ -98,8 +113,11 @@ class RecursiveMenuIterator implements RecursiveIterator
/**
* Returns if an iterator can be created for the current entry.
*
* @link http://php.net/manual/en/recursiveiterator.haschildren.php
*
* @return bool true if the current entry can be iterated over, otherwise returns false.
*
* @since 5.1.0
*/
public function hasChildren()
@@ -109,8 +127,11 @@ class RecursiveMenuIterator implements RecursiveIterator
/**
* Returns an iterator for the current entry.
*
* @link http://php.net/manual/en/recursiveiterator.getchildren.php
*
* @return RecursiveIterator An iterator for the current entry.
*
* @since 5.1.0
*/
public function getChildren()
@@ -105,6 +105,7 @@ class Wordpress
*
* @param string $name name of the option to get
* @param mixed $default value to return if we don't have a value for the option.
*
* @return mixed
*/
public static function option($name, $default = false)
@@ -70,12 +70,14 @@ if (!function_exists('menu')) {
* @param string $name name of the menu to get
* @param int $depth how far to recurse down the nodes
* @param int $mode flags to pass to the {@link RecursiveIteratorIterator}
*
* @return RecursiveIteratorIterator
*/
function menu($name, $depth = -1, $mode = RecursiveIteratorIterator::SELF_FIRST)
{
$iterator = new RecursiveIteratorIterator(new RecursiveMenuIterator($name), $mode);
$iterator->setMaxDepth($depth);
return $iterator;
}
}