WPDK  1.5.0
WordPress Development Kit
 All Data Structures Files Functions Variables Pages
wpdk-ui-modal-dialog.php
Go to the documentation of this file.
1 <?php
2 
55 
63  public $__version = '1.0.0';
64 
72  public $title = '';
73 
81  public $content = '';
82 
90  public $width = '';
91 
99  public $height = '';
100 
109  public $close_button = false;
110 
118  public $dismissButton = true;
119 
127  public $buttons = array();
128 
136  public $keyboard = 'true';
137 
146  public $backdrop = 'true';
147 
155  public $static = false;
156 
165  public $dismiss_button_glyph = '×';
166 
178  public function __construct( $id, $title, $content = '' )
179  {
180 
181  $this->id = sanitize_title( $id );
182  $this->title = $title;
183  $this->content = $content;
184  }
185 
193  private function aria_title()
194  {
195  return sprintf( '%s-title', $this->id );
196  }
197 
205  private function dismissButton()
206  {
207  $result = '';
208  if ( $this->dismissButton ) {
209  $result = '<button type="button" class="close" data-dismiss="modal" aria-hidden="true">' . $this->dismiss_button_glyph . '</button>';
210  }
211  return $result;
212  }
213 
222  private function width()
223  {
224  $result = '';
225  if ( !empty( $this->width ) ) {
226  $result = sprintf( 'width:%spx', rtrim( $this->width, 'px' ) );
227  }
228  return $result;
229  }
230 
239  private function height()
240  {
241  $result = '';
242  if ( !empty( $this->height ) ) {
243  $result = sprintf( 'style="height:%spx"', rtrim( $this->height, 'px' ) );
244  }
245  return $result;
246  }
247 
255  private function _buttons()
256  {
257  $result = '';
258  $buttons = $this->buttons();
259 
260  if ( !empty( $buttons ) ) {
261  $stack = '';
262  foreach ( $buttons as $key => $value ) {
263  $class = isset( $value['class'] ) ? $value['class'] : '';
264  $label = isset( $value['label'] ) ? $value['label'] : '';
265  $title = isset( $value['title'] ) ? 'title="' . $value['title'] . '"' : '';
266  if ( !empty( $title ) ) {
267  $class .= ' wpdk-has-tooltip';
268  }
269  $data_dismiss = ( isset( $value['dismiss'] ) && true == $value['dismiss'] ) ? 'data-dismiss="modal"' : '';
270  $stack .= sprintf( '<button type="button" %s id="%s" class="button %s" %s aria-hidden="true">%s</button>', $title, $key, $class, $data_dismiss, $label );
271  }
272  }
273 
274  if ( !empty( $stack ) ) {
275  $result = sprintf( '<div class="modal-footer">%s</div>', $stack );
276  }
277 
278  return $result;
279  }
280 
288  public function buttons()
289  {
290  // You can override this method
291  return $this->buttons;
292  }
293 
301  public function content()
302  {
303  // You can override this method
304  return $this->content;
305  }
306 
314  public function open()
315  {
316  $this->display();
317  $this->show();
318  }
319 
327  public function html()
328  {
329 
330  // Get default data as properties
331  $this->data['keyboard'] = $this->keyboard;
332  $this->data['backdrop'] = $this->backdrop;
333 
335 
336  <div style="<?php echo $this->static ? 'position:relative;top:auto;left:auto;right:auto;margin:0 auto 20px;z-index:1;max-width:100%' : 'display:none;' ?>"
337  class="wpdk-modal <?php echo $this->static ? '' : 'fade' ?>"
338  <?php echo self::dataInline( $this->data ) ?>
339  id="<?php echo $this->id ?>"
340  tabindex="-1"
341  role="dialog"
342  aria-labelledby="<?php echo $this->aria_title() ?>"
343  aria-hidden="true">
344  <div style="<?php echo $this->width() ?>" class="modal-dialog">
345  <div class="modal-content">
346  <div class="modal-header">
347  <?php echo $this->dismissButton() ?>
348  <h4 class="modal-title" id="<?php echo $this->aria_title() ?>"><?php echo $this->title ?></h4>
349  </div>
350  <div class="modal-body" <?php echo $this->height() ?>>
351  <?php echo $this->content() ?>
352  </div>
353  <?php echo $this->_buttons() ?>
354  </div>
355  </div>
356  </div>
357 
358  <?php
359 
360  return WPDKHTML::endCompress();
361  }
362 
368  public function show()
369  {
371  <script type="text/javascript">
372  jQuery( function ( $ )
373  {
374  "use strict";
375  $( '#<?php echo $this->id ?>' ).wpdkModal( 'show' );
376  <?php do_action( 'wpdk_tbs_dialog_javascript_show' ) ?>
377  <?php do_action( 'wpdk_tbs_dialog_javascript_show-' . $this->id ) ?>
378  } );
379  </script>
380  <?php echo WPDKHTML::endJavascriptCompress();
381  }
382 
388  public function toggle()
389  {
391  <script type="text/javascript">
392  jQuery( function ( $ )
393  {
394  $( '#<?php echo $this->id ?>' ).wpdkModal( 'toggle' );
395  } );
396  </script>
397  <?php echo WPDKHTML::endJavascriptCompress();
398  }
399 
405  public function hide()
406  {
408  <script type="text/javascript">
409  jQuery( function ( $ )
410  {
411  $( '#<?php echo $this->id ?>' ).wpdkModal( 'hide' );
412  } );
413  </script>
414  <?php echo WPDKHTML::endJavascriptCompress();
415  }
416 
427  public function buttonOpenModal( $label, $class = '' )
428  {
429  $id = sprintf( '#%s', $this->id );
430  $result = sprintf( '<button class="button %s" type="button" data-toggle="modal" data-target="%s">%s</button>', $class, $id, $label );
431  return $result;
432  }
433 
444  public function addButton( $id, $label, $dismiss = true, $class = '' )
445  {
446  $this->buttons[$id] = array(
447  'label' => $label,
448  'class' => $class,
449  'dismiss' => $dismiss
450  );
451  }
452 
453  // -------------------------------------------------------------------------------------------------------------------
454  // Deprecated
455  // -------------------------------------------------------------------------------------------------------------------
456 
460  public function modal( $echo = true )
461  {
462  if ( $echo ) {
463  $this->display();
464  }
465  else {
466  return $this->html();
467  }
468  }
469 
473  public function add_buttons( $id, $label, $dismiss = true, $class = '' )
474  {
475  _deprecated_function( __METHOD__, '1.0.0.b4', 'addButton()' );
476  $this->addButton( $id, $label, $dismiss, $class );
477  }
478 
479 
483  public function add_data( $key, $value )
484  {
485  _deprecated_function( __METHOD__, '1.0.0.b4', 'addData()' );
486  //$this->data[] = array( $key => $value );
487  $this->addData( $key, $value );
488  }
489 
493  public function button_open_modal( $label, $class = '' )
494  {
495  _deprecated_function( __METHOD__, '1.0.0.b4', 'buttonOpenModal()' );
496  return $this->buttonOpenModal( $label, $class );
497  }
498 }