Skip to content

Commit

Permalink
Adding post type classes for built-in types;
Browse files Browse the repository at this point in the history
Removing template hooks;
Bugfixing
  • Loading branch information
rasmuswinter committed Apr 24, 2017
1 parent c5e139d commit 3472441
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 75 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.1",
"version": "2.0.2",
"autoload": {
"psr-4": {
"Outlandish\\Wordpress\\Oowp\\": "src/"
Expand Down
74 changes: 3 additions & 71 deletions oowp.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,74 +66,6 @@ function unregister_taxonomy($taxonomy, $object_type = ''){
}
}


/**
* @return string The full path of the wrapped template
*/
function oowp_layout_template_file() {
return OOWP_Layout::$innerTemplate;
}

/**
* @return string The name of the wrapped template
*/
function oowp_layout_template_name() {
return OOWP_Layout::$templateName;
}

/**
* This wraps all requested templates in a layout.
*
* Create layout.php in your root theme directory to use the same layout on all pages.
* Create layout-{template}.php for specific versions
*
* Example layout: include header, sidebar and footer on all pages, and wrap standard template in a section and a div
*
* <?php get_header( oowp_layout_template_name() ); ?>
*
* <section id="primary">
* <div id="content" role="main">
* <?php include oowp_layout_template_file(); ?>
* </div><!-- #content -->
* </section><!-- #primary -->
*
* <?php get_sidebar( oowp_layout_template_name() ); ?>
* <?php get_footer( oowp_layout_template_name() ); ?>
*
* See http://scribu.net/wordpress/theme-wrappers.html
*/

add_filter( 'template_include', array( 'OOWP_Layout', 'wrap' ), 99 );
class OOWP_Layout {

/**
* Stores the full path to the main template file
*/
static $innerTemplate = null;

/**
* Stores the base name of the template file; e.g. 'page' for 'page.php' etc.
*/
static $templateName = null;

static function wrap( $template ) {
self::$innerTemplate = $template;

self::$templateName = substr( basename( self::$innerTemplate ), 0, -4 );

$templates = array( 'layout.php' );

if ( 'index' == self::$templateName ) {
self::$templateName = null;
} else {
// prepend the more specific wrapper filename
array_unshift( $templates, sprintf( 'layout-%s.php', self::$templateName ) );
}

// revert to the template passed in if no layout template is found
return locate_template( $templates ) ?: $template;
}
}


add_shortcode(ListPostsShortcode::NAME, array(ListPostsShortcode::class, 'apply'));
add_shortcode(ListPostsShortcode::NAME, function($params, $content) {
ListPostsShortcode::apply($params, $content);
});
4 changes: 1 addition & 3 deletions src/OowpQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,8 @@ public function sortByIds($ids) {
public function &get_posts() {
parent::get_posts();

$manager = PostTypeManager::get();
foreach ($this->posts as $i => $post) {
$classname = $manager->getClassName($post->post_type);
$this->posts[$i] = new $classname($post);
$this->posts[$i] = WordpressPost::createWordpressPost($post);
}

if (count($this->posts)) {
Expand Down
6 changes: 6 additions & 0 deletions src/PostTypeManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Outlandish\Wordpress\Oowp;

use Outlandish\Wordpress\Oowp\PostTypes\OowpPage;
use Outlandish\Wordpress\Oowp\PostTypes\OowpPost;
use Outlandish\Wordpress\Oowp\PostTypes\WordpressPost;
use Outlandish\Wordpress\Oowp\Util\AdminUtils;

Expand Down Expand Up @@ -81,6 +83,10 @@ public function registerPostTypes($classNames)

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

$classNames[] = OowpPage::class;
$classNames[] = OowpPost::class;
$classNames = array_unique($classNames);

// register all classes
foreach ($classNames as $className) {
$this->registerPostType($className);
Expand Down
12 changes: 12 additions & 0 deletions src/PostTypes/OowpPage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Outlandish\Wordpress\Oowp\PostTypes;

class OowpPage extends WordpressPost
{
static function postType()
{
return 'page';
}

}
12 changes: 12 additions & 0 deletions src/PostTypes/OowpPost.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Outlandish\Wordpress\Oowp\PostTypes;

class OowpPost extends WordpressPost
{
static function postType()
{
return 'post';
}

}
4 changes: 4 additions & 0 deletions src/PostTypes/WordpressPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,7 @@ public function children($queryArgs = array())
$queryArgs = wp_parse_args($queryArgs, $defaults);
$children = static::fetchAll($queryArgs);
$children->posts = array_merge($children->posts, $posts);
$children->post_count = count($children->posts);
return $children;
}

Expand Down Expand Up @@ -864,6 +865,9 @@ public static function createWordpressPost($data = null) {
$postData = self::getPostObject($data);
if ($postData) {
$className = PostTypeManager::get()->getClassName($postData->post_type);
if (!$className) {
$className = MiscPost::class;
}
if ($postData instanceof $className) {
return $postData;
} else {
Expand Down
9 changes: 9 additions & 0 deletions src/Views/PostView.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,13 @@ abstract class PostView extends OowpView
/** @var WordpressPost */
public $post;

public function __construct()
{
parent::__construct();

global $post;
$this->post = WordpressPost::createWordpressPost($post);
}


}

0 comments on commit 3472441

Please sign in to comment.