Skip to content

Commit

Permalink
Merge pull request #14 from outlandishideas/13-consistent-post-type-c…
Browse files Browse the repository at this point in the history
…onstraints

Align `fetchOne()` post type constraints with similar methods
  • Loading branch information
rasmuswinter authored Oct 28, 2020
2 parents 17aa8f8 + f78b66e commit be23981
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/.idea
composer.lock
15 changes: 15 additions & 0 deletions src/PostTypeManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,21 @@ public function getClassName($postType)
return array_key_exists($postType, $classNames) ? $classNames[$postType] : null;
}

/**
* Returns true if the given fully-qualified class name represents a post type
* registered via registerPostTypes().
* @param string $className Fully-qualified name of a class which might be a post type.
* @return bool
*/
public function postClassIsRegistered($className)
{
if (!class_exists($className)) {
return false;
}

return array_key_exists($className, $this->postTypes);
}

/**
* Returns true if the post type was registered via registerPostTypes()
* @param string $postType
Expand Down
28 changes: 21 additions & 7 deletions src/PostTypes/WordpressPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -983,11 +983,7 @@ public static function fetchById($ids) {
}

public static function fetchBySlug($slug){
return static::fetchOne(array(
'name' => $slug,
'post_type' => static::postType(),
'numberposts' => 1
));
return static::fetchOne(array('name' => $slug));
}

/**
Expand All @@ -998,7 +994,7 @@ public static function fetchBySlug($slug){
public static function fetchAll($queryArgs = array())
{
$defaults = array(
'post_type' => static::postType()
'post_type' => static::getSelfPostTypeConstraint()
);
if (static::isHierarchical()) {
$defaults['orderby'] = 'menu_order';
Expand Down Expand Up @@ -1047,7 +1043,12 @@ public function isHomepage() {
*/
static function fetchOne($queryArgs)
{
$queryArgs['posts_per_page'] = 1;
$queryArgs['posts_per_page'] = 1; // Force-override this rather than only setting a default.
$defaults = array(
'post_type' => static::getSelfPostTypeConstraint()
);
$queryArgs = wp_parse_args($queryArgs, $defaults);

$query = new OowpQuery($queryArgs);
return $query->posts ? $query->post : null;
}
Expand Down Expand Up @@ -1078,6 +1079,19 @@ static function isHierarchical($postType = null) {
return is_post_type_hierarchical($postType);
}

private static function getSelfPostTypeConstraint()
{
// If `get*()` methods are called on abstract post classes directly (not a registered post subclass), do not
// constrain the type of posts returned unless specified.
if (!PostTypeManager::get()->postClassIsRegistered(static::class)) {
return 'any';
}

// Otherwise, default to constraining to the type associated with the class on which the
// method was invoked.
return static::postType();
}

#endregion


Expand Down

0 comments on commit be23981

Please sign in to comment.