WPDK  1.5.0
WordPress Development Kit
 All Data Structures Files Functions Variables Pages
wpdk-view.php
Go to the documentation of this file.
1 <?php
63 class WPDKView extends WPDKObject {
64 
72  public $__version = '1.1.2';
73 
81  public $class;
82 
90  public $style;
91 
99  public $content;
100 
106  public $data;
107 
115  public $id;
116 
124  public $subviews;
125 
133  public $superview;
134 
143  protected $views;
144 
155  public function __construct( $id, $class = '' )
156  {
157  $this->id = sanitize_title( $id );
158  $this->class = WPDKHtmlTag::sanitizeClasses( $class );
159  $this->content = '';
160  $this->superview = null;
161  $this->subviews = array();
162  }
163 
175  public static function initWithContent( $id, $class = '', $content = '' )
176  {
177  if ( !empty( $content ) ) {
178  $instance = new WPDKView( $id, $class );
179  $instance->content = $content;
180  return $instance;
181  }
182  return false;
183  }
184 
185  /* @todo Experimental */
186  public static function initWithFrame( $id, $rect )
187  {
188  }
189 
197  public function html()
198  {
199  ob_start();
200  $this->display();
201  $content = ob_get_contents();
202  ob_end_clean();
203  return $content;
204  }
205 
215  public function display()
216  {
217  $classes = WPDKHTMLTag::classInline( $this->class );
218  $style = WPDKHTMLTag::styleInline( $this->style );
219  $data = WPDKHTMLTag::dataInline( $this->data );
220  ?>
221  <div data-type="wpdk-view"
222  style="<?php echo $style ?>"
223  id="<?php echo $this->id ?>"
224  class="<?php echo $classes ?>" <?php echo $data ?> >
225 
226  <?php do_action( 'wpdk_view_' . $this->id . '_before_draw', $this ) ?>
227 
228  <?php do_action( 'wpdk_view_will_draw_content', $this ) ?>
229  <?php $this->draw() ?>
230  <?php do_action( 'wpdk_view_did_draw_content', $this ) ?>
231 
232  <?php do_action( 'wpdk_view_' . $this->id . '_after_draw', $this ) ?>
233 
234  <?php if ( is_array( $this->subviews ) ) : ?>
235  <?php
239  foreach ( $this->subviews as $view ) : ?>
240  <?php $view->display() ?>
241  <?php endforeach ?>
242  <?php endif ?>
243 
244  <?php do_action( 'wpdk_view_' . $this->id . '_before_close', $this ) ?>
245 
246  </div>
247 
248  <?php
249  }
250 
257  public function draw()
258  {
259  /* This method can be over-ridden in a sub-class. */
260  echo $this->content;
261  }
262 
263 
273  public function addSubview( $view )
274  {
275  $continue = apply_filters( 'wpdk_view_should_add_subview', true, $view );
276  if ( $continue ) {
277  $view->superview = $this;
278  $this->subviews[$view->id] = $view;
279  do_action( 'wpdk_view_did_add_subview', $view );
280  }
281  return $view;
282  }
283 
289  public function removeFromSuperview()
290  {
291  if ( !empty( $this->superview ) ) {
292  unset( $this->superview->subviews[$this->id] );
293  }
294  }
295 }