Skip to content

Commit

Permalink
Making php5.4 safe;
Browse files Browse the repository at this point in the history
Not registering the same post type twice
  • Loading branch information
rasmuswinter committed Jul 13, 2017
1 parent 5c3d13c commit 66b7591
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "A plugin for WordPress that makes post types easier to manage",
"minimum-stability": "stable",
"license": "GPL-3.0",
"version": "2.0.4",
"version": "2.0.5",
"autoload": {
"psr-4": {
"Outlandish\\Wordpress\\Oowp\\": "src/"
Expand Down
15 changes: 11 additions & 4 deletions src/PostTypeManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,14 @@ protected function registerPostType($className)
{
/** @var WordpressPost|string $className */

if (!is_subclass_of($className, WordpressPost::class)) {
if (!is_subclass_of($className, 'Outlandish\Wordpress\Oowp\PostTypes\WordpressPost')) {
throw new \RuntimeException($className . ' is not a subclass of WordpressPost');
}
$postType = $className::postType();
if (in_array($postType, $this->postTypes)) {
// already registered
return;
}
if ($postType !== 'page' && $postType !== 'post' && $postType !== 'attachment' ) {
$defaults = array(
'labels' => AdminUtils::generateLabels($className::friendlyName(), $className::friendlyNamePlural()),
Expand Down Expand Up @@ -69,7 +73,9 @@ protected function registerPostType($className)
/**
* Registers the given classes as OOWP post types
*
* Usage:
* Usage (php < 5.5):
* $manager->registerPostTypes(['namespace\of\posttypes\MyPostType', 'namespace\of\posttypes\AnotherPostType'])
* Usage (php >= 5.5):
* $manager->registerPostTypes([MyPostType::class, AnotherPostType::class])
*
* @param string[] $classNames
Expand All @@ -83,8 +89,9 @@ public function registerPostTypes($classNames)

/** @var WordpressPost[]|string[] $classNames */

$classNames[] = OowpPage::class;
$classNames[] = OowpPost::class;
// add the built-in post/page classes after all of the others
$classNames[] = 'Outlandish\Wordpress\Oowp\PostTypes\OowpPage';
$classNames[] = 'Outlandish\Wordpress\Oowp\PostTypes\OowpPost';
$classNames = array_unique($classNames);

// register all classes
Expand Down
2 changes: 1 addition & 1 deletion src/PostTypes/WordpressPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -873,7 +873,7 @@ public static function createWordpressPost($data = null) {
if ($postData) {
$className = PostTypeManager::get()->getClassName($postData->post_type);
if (!$className) {
$className = MiscPost::class;
$className = 'Outlandish\Wordpress\Oowp\PostTypes\MiscPost';
}
if ($postData instanceof $className) {
return $postData;
Expand Down

0 comments on commit 66b7591

Please sign in to comment.