Beispiel #1
0
 /**
  * Get all posts of the given type.
  *
  * @param  string                         $type      Post type
  * @param  array                          $queryVars Extra query vars. See the params passed to WP_Query
  * @return \Tev\Post\Model\AbstractPost[]            Post objects
  */
 public function getAllByPostType($type, array $queryVars = array())
 {
     $q = new WP_Query(array_merge(array('post_type' => $type, 'nopaging' => true), $queryVars));
     $posts = array();
     foreach ($q->get_posts() as $p) {
         $posts[] = $this->postFactory->create($p);
     }
     return $posts;
 }
Beispiel #2
0
 /**
  * {@inheritDoc}
  */
 public function bootstrap(Application $app)
 {
     // Bind a post factory instance
     $app->bind('post_factory', function ($app) {
         $f = new Factory($app->fetch('author_factory'), $app->fetch('taxonomy_factory'), $app->fetch('field_factory'));
         // Register defaut post types
         return $f->register('post', 'Tev\\Post\\Model\\Post')->register('page', 'Tev\\Post\\Model\\Page')->register('attachment', 'Tev\\Post\\Model\\Attachment');
     });
     // Bind a post repository instance
     $app->bind('post_repo', function ($app) {
         return new PostRepository($app->fetch('post_factory'));
     });
 }
Beispiel #3
0
 /**
  * Get a single post object or array of post objects.
  *
  * If no posts are configured, returned will result will be an empty array
  * if this is a mutli-select, or null if not.
  *
  * @return \Tev\Post\Model\Post|\Tev\Post\Model\Post[]|null
  */
 public function getValue()
 {
     $val = $this->base['value'];
     if (is_array($val)) {
         $posts = array();
         foreach ($val as $p) {
             $posts[] = $this->postFactory->create($this->getPostObject($p));
         }
         return $posts;
     } elseif (strlen($val)) {
         return $this->postFactory->create($this->getPostObject($val));
     } else {
         if (isset($this->base['multiple']) && $this->base['multiple']) {
             return array();
         } else {
             return null;
         }
     }
 }
Beispiel #4
0
 /**
  * Get the parent post, if set.
  *
  * @return \Tev\Post\Model\AbstractPost|null
  */
 public function getParent()
 {
     if ($this->parent === null) {
         if (($parentId = $this->getParentPostId()) !== null) {
             $this->parent = $this->postFactory->create(get_post($parentId));
         } else {
             $this->parent = false;
         }
     }
     return $this->parent ?: null;
 }
Beispiel #5
0
 /**
  * Generate an array of breadcrumbs, based on the current page.
  *
  * Each item in the array is a hash containing 'title' and 'url' (which
  * might be null).
  *
  * You can use this to generate nice breadcrumb lists in your page.
  *
  * @param  \Closure|\Closure[] Optional. Set of user-defined callbacks that can
  *                             return breadcrumbs. If any of the supplied callbacks
  *                             return data, then their data will be used rather than
  *                             the defaults supplied by this method. Note that
  *                             the initial 'Home' crumb is always included
  * @return array
  */
 public function breadcrumbs($callbacks = null)
 {
     $breadcrumbs = array();
     // Load blog page - it's used later, if configured
     $blogPage = null;
     if ($bp = get_page(get_option('page_for_posts'))) {
         $blogPage = $this->postFactory->create($bp);
     }
     // Home page is always present in breadcrumb trail
     $breadcrumbs[] = array('title' => 'Home', 'url' => !is_front_page() ? get_option('home') : null);
     // Check if we've got any custom user defined crumbs
     if ($callbacks !== null) {
         if (!is_array($callbacks)) {
             $callbacks = array($callbacks);
         }
         foreach ($callbacks as $cb) {
             if ($cb instanceof Closure) {
                 if ($res = $cb()) {
                     $breadcrumbs[] = $res;
                 }
             }
         }
     }
     // If we haven't got any user crumbs, proceed with default logic
     if (count($breadcrumbs) === 1) {
         // Blog home page (when different to front page)
         if ($blogPage && is_home() && !is_front_page()) {
             $breadcrumbs[] = array('title' => $blogPage->getTitle(), 'url' => null);
         }
         // Custom post type archive page, no filtering
         if (is_archive()) {
             // Custom post type archive
             if (is_post_type_archive()) {
                 if (!$this->isFilteredArchive()) {
                     $breadcrumbs[] = array('title' => post_type_archive_title('', false), 'url' => null);
                 } else {
                     $breadcrumbs[] = array('title' => post_type_archive_title('', false), 'url' => get_post_type_archive_link(get_post_type()));
                     $breadcrumbs[] = array('title' => $this->archiveTitle(), 'url' => null);
                 }
             } else {
                 if ($blogPage && !$this->isBlogPageHome()) {
                     $breadcrumbs[] = array('title' => $blogPage->getTitle(), 'url' => $blogPage->getUrl());
                 }
                 if ($this->isFilteredArchive()) {
                     $breadcrumbs[] = array('title' => $this->archiveTitle(), 'url' => null);
                 }
             }
         }
         // Search page
         if (is_search()) {
             $breadcrumbs[] = array('title' => 'Search', 'url' => null);
         }
         // 404 page
         if (is_404()) {
             $breadcrumbs[] = array('title' => '404 Not Found', 'url' => null);
         }
         // Single post
         if (is_single()) {
             if (is_singular('post')) {
                 if ($blogPage && !$this->isBlogPageHome()) {
                     $breadcrumbs[] = array('title' => $blogPage->getTitle(), 'url' => $blogPage->getUrl());
                 }
             } else {
                 $postType = get_post_type();
                 $breadcrumbs[] = array('title' => get_post_type_object($postType)->labels->name, 'url' => get_post_type_archive_link($postType));
             }
             $breadcrumbs[] = array('title' => get_the_title(), 'url' => null);
         }
         // Single Page
         if (is_page()) {
             global $post;
             $page = $post;
             $pageTree = array(array('title' => $page->post_title, 'url' => null));
             while ($parentId = $page->post_parent) {
                 $page = get_post($parentId);
                 array_unshift($pageTree, array('title' => $page->post_title, 'url' => get_permalink($parentId)));
             }
             foreach ($pageTree as $t) {
                 $breadcrumbs[] = $t;
             }
         }
     }
     return $breadcrumbs;
 }