diff --git a/oowp.php b/oowp.php index daa8ca0..6f55026 100644 --- a/oowp.php +++ b/oowp.php @@ -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; diff --git a/src/OowpQuery.php b/src/OowpQuery.php index a7d4ffd..ae554db 100644 --- a/src/OowpQuery.php +++ b/src/OowpQuery.php @@ -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); @@ -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); } diff --git a/src/PostTypes/WordpressPost.php b/src/PostTypes/WordpressPost.php index 305a34e..66d0309 100644 --- a/src/PostTypes/WordpressPost.php +++ b/src/PostTypes/WordpressPost.php @@ -43,7 +43,6 @@ */ abstract class WordpressPost { - protected $_cache = array(); protected static $_staticCache = array(); @@ -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; @@ -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 */ @@ -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); } /** @@ -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 */