Skip to content

Commit

Permalink
Merge pull request #53 from Upstatement/52-constructor-exception
Browse files Browse the repository at this point in the history
Set constructor to use conditional instead of throwing an exception
  • Loading branch information
lggorman authored Mar 6, 2017
2 parents 2a23f7a + 1ba2080 commit 225a7c7
Showing 1 changed file with 27 additions and 17 deletions.
44 changes: 27 additions & 17 deletions includes/timber-stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,32 +76,42 @@ class TimberStream extends TimberPost {
public $options = null;

/**
* Init Stream object
* Construct Timber\Post, kick off stream init
*
* @param integer|boolean|string $pid Post ID or slug
*
* @todo allow creating a TimberStream w/out database
*/
public function __construct($pid = null) {
parent::__construct($pid);
if ($this->post_type !== 'sm_stream') {
throw new Exception("TimberStream of $pid is not of sm_stream post type");
}
if ( !$this->post_content ) $this->post_content = serialize(array());
$this->options = array_merge( $this->default_options, unserialize($this->post_content) );
$this->options['query'] = apply_filters('stream-manager/query', $this->options['query']);
$this->options = apply_filters( 'stream-manager/options/id=' . $this->ID, $this->options, $this );
$this->options = apply_filters( 'stream-manager/options/'.$this->slug, $this->options, $this );

$taxes = apply_filters( 'stream-manager/taxonomy/'.$this->slug, array(), $this );
if (is_array($taxes) && !empty($taxes)) {
$taxes = StreamManagerUtilities::build_tax_query($taxes);
if (isset($this->options['query']['tax_query'])) {
$this->options['query']['tax_query'] = array_merge($this->options['query']['tax_query'], $taxes);
} else {
$this->options['query']['tax_query'] = $taxes;
$this->init_stream($pid);
}

/**
* Init Stream object
*
* @param integer|boolean|string $pid Post ID or slug
*
*/
public function init_stream($pid) {
if ($this->post_type === 'sm_stream') {
if ( !$this->post_content ) $this->post_content = serialize(array());
$this->options = array_merge( $this->default_options, unserialize($this->post_content) );
$this->options['query'] = apply_filters('stream-manager/query', $this->options['query']);
$this->options = apply_filters( 'stream-manager/options/id=' . $this->ID, $this->options, $this );
$this->options = apply_filters( 'stream-manager/options/'.$this->slug, $this->options, $this );

$taxes = apply_filters( 'stream-manager/taxonomy/'.$this->slug, array(), $this );
if (is_array($taxes) && !empty($taxes)) {
$taxes = StreamManagerUtilities::build_tax_query($taxes);
if (isset($this->options['query']['tax_query'])) {
$this->options['query']['tax_query'] = array_merge($this->options['query']['tax_query'], $taxes);
} else {
$this->options['query']['tax_query'] = $taxes;
}
}
}

}

/**
Expand Down

0 comments on commit 225a7c7

Please sign in to comment.