Skip to content

Commit

Permalink
Adding new fetchIds function and corresponding handling in get_posts;
Browse files Browse the repository at this point in the history
Caching postTypeParent result to save repeated lookups
  • Loading branch information
rasmuswinter committed Apr 29, 2022
1 parent 745b73c commit 2db7d91
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 18 deletions.
2 changes: 1 addition & 1 deletion oowp.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Plugin Name: Object-oriented WordPress (OOWP)
Plugin URI: https://github.com/outlandishideas/oowp
Description: OOWP is a tool for WordPress theme developers that makes templating in WordPress more sensible. It replaces [The Loop](https://codex.wordpress.org/The_Loop) and contextless functions such as the_title() with object-oriented methods such as $event->title(), $event->parent() and $event->getConnected('people').
Version: 0.9
Version: 3.0.3
*/

use Outlandish\Wordpress\Oowp\Shortcodes\ListPostsShortcode;
Expand Down
23 changes: 15 additions & 8 deletions src/OowpQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,18 @@ class OowpQuery extends \WP_Query implements \IteratorAggregate, \ArrayAccess, \
*/
function __construct($query = '')
{
global $wp_post_types;

$defaults = array(
$defaults = [
'posts_per_page' => -1,
'post_status' => 'publish'
);
];
$query = wp_parse_args($query, $defaults);

// If there is no post type, or the post type is singular and isn't valid, replace it with any *except*
// 'attachment' which can cause crashes on ?preview=true if a file title matches a render-able post's.
if (!isset($query['post_type']) || (!is_array($query['post_type']) && !array_key_exists($query['post_type'],
$wp_post_types))) {
$query['post_type'] = array_values(array_diff(get_post_types(), array('attachment')));
$validPostTypes = get_post_types();
$requestedPostType = $query['post_type'] ?? 'any';
if (is_scalar($requestedPostType) && !array_key_exists($requestedPostType, $validPostTypes)) {
$query['post_type'] = array_values(array_diff($validPostTypes, ['attachment']));
}

parent::__construct($query);
Expand Down Expand Up @@ -115,12 +114,20 @@ public function sortByIds($ids)

/**
* Convert WP_Post objects to WordpressPost
* @return WordpressPost[]
* If $query['fields'] is 'ids', then this will just return the post IDs
* If $query['fields'] is 'id->parent', then this will return an array of objects that represents the parent-child relationships
* See https://developer.wordpress.org/reference/classes/wp_query/#return-fields-parameter
* @return WordpressPost[]|int[]|\stdClass[]
*/
public function &get_posts()
{
parent::get_posts();

$fields = $this->query['fields'] ?? 'all';
if ($fields && $fields !== 'all') {
return $this->posts;
}

foreach ($this->posts as $i => $post) {
$this->posts[$i] = WordpressPost::createWordpressPost($post);
}
Expand Down
49 changes: 40 additions & 9 deletions src/PostTypes/WordpressPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
*/
abstract class WordpressPost
{

protected $_cache = array();
protected static $_staticCache = array();

Expand Down Expand Up @@ -499,16 +498,12 @@ public function parent()
{
$parentId = $this->post_parent;
if (!$parentId) {
$parentId = static::postTypeParentId();
}
$parentSlug = static::postTypeParentSlug();
if (empty($parentId) && empty($parentSlug)) {
return null;
return static::postTypeParent();
}

$parent = $this->getCacheValue();
if (!$parent) {
$parent = $parentId ? WordpressPost::fetchById($parentId) : WordpressPost::fetchBySlug($parentSlug);
$parent = WordpressPost::fetchById($parentId);
$this->setCacheValue($parent);
}
return $parent;
Expand Down Expand Up @@ -536,6 +531,28 @@ public static function postTypeParentSlug()
return '';
}

/**
* Gets the parent for posts of this type, based on either the ID or slug, whichever is defined for the post type
* @return WordpressPost|null
*/
public static function postTypeParent()
{
$key = 'post_type_parent__' . static::postType();
if (!array_key_exists($key, WordpressPost::$_staticCache)) {
$parentId = static::postTypeParentId();
$parentSlug = static::postTypeParentSlug();
if ($parentId) {
$parent = WordpressPost::fetchById($parentId);
} elseif ($parentSlug) {
$parent = WordpressPost::fetchBySlug($parentSlug);
} else {
$parent = null;
}
WordpressPost::$_staticCache[$key] = $parent;
}
return WordpressPost::$_staticCache[$key];
}

/**
* Traverses up the getParent() hierarchy until finding one with no parent, which is returned
*/
Expand Down Expand Up @@ -1022,8 +1039,8 @@ static function getQueriedObject()
*/
static function registerConnection($targetPostType, $parameters = array(), $connectionName = null)
{
return PostTypeManager::get()->registerConnection(self::postType(), $targetPostType, $parameters,
$connectionName);
return PostTypeManager::get()
->registerConnection(self::postType(), $targetPostType, $parameters, $connectionName);
}

/**
Expand Down Expand Up @@ -1100,6 +1117,20 @@ public static function fetchAll($queryArgs = array())
return $query;
}

/**
* @static
* Does the same querying as in fetchAll, but only returns the IDs of the matches posts
*
* @param array $queryArgs Accepts a wp_query $queryArgs array which overwrites the defaults
* @return int[]
*/
public static function fetchIds($queryArgs = [])
{
$queryArgs['fields'] = 'ids';
$query = static::fetchAll($queryArgs);
return $query->posts;
}

/**
* @deprecated
*/
Expand Down

0 comments on commit 2db7d91

Please sign in to comment.