WPDK  1.5.0
WordPress Development Kit
 All Data Structures Files Functions Variables Pages
wpdk-ui-alert.php
Go to the documentation of this file.
1 <?php
2 
16  const SUCCESS = 'alert-success';
17  const INFORMATION = 'alert-info';
18  const WARNING = 'alert-warning';
19  const DANGER = 'alert-danger';
20  const WHITE = 'alert-white';
21 }
22 
58 class WPDKUIAlert extends WPDKHTMLTag {
59 
60  // Used to store for each user the dismiss alert
61  const USER_META_KEY_PERMANENT_DISMISS = '_wpdk_alert_dismiss';
62 
71  public $dismissable = true;
72 
81  public $dismissToolTip = '';
82 
90  public $title = '';
91 
99  public $dismissButton = true;
100 
108  public $dismiss_button_glyph = '×';
109 
117  public $content = '';
118 
126  public $type;
127 
136  public $permanent_dismiss = false;
137 
146  public $block;
147 
155  protected $dismissed = array();
156 
170  {
171  $this->id = sanitize_title( $id );
172  $this->content = $content;
173  $this->type = $type;
174  $this->title = $title;
175 
176  // @since 1.4.21 permanent dismissed
177  if ( is_user_logged_in() ) {
178  $user_id = get_current_user_id();
179  $this->dismissed = get_user_meta( $user_id, self::USER_META_KEY_PERMANENT_DISMISS, true );
180  }
181  }
182 
190  private function dismissButton()
191  {
192  $result = '';
193 
194  // Title
195  $title = '';
196 
197  // Classes
198  $classes = array( 'close' );
199 
200  // Custom title/tooltip in button close
201  if ( !empty( $this->dismissToolTip ) ) {
202  $classes[] = 'wpdk-has-tooltip';
203  $title = sprintf( 'title="%s"', $this->dismissToolTip );
204  }
205 
206  // Permanent dismiss by user logged in
207  if( true === $this->permanent_dismiss ) {
208  $classes[] = 'wpdk-alert-permanent-dismiss';
209  }
210 
211  if ( $this->dismissButton ) {
213  <button
214  type="button"
215  class="<?php echo WPDKHTMLTag::classInline( $classes ) ?>"
216  <?php echo $title ?>
217  data-dismiss="alert"><?php echo $this->dismiss_button_glyph ?></button>
218  <?php
219  $result = WPDKHTML::endHTMLCompress();
220  }
221 
222  return $result;
223  }
224 
232  private function _content()
233  {
234  return empty( $this->content ) ? wpautop( $this->content() ) : wpautop( $this->content );
235  }
236 
244  public function content()
245  {
246  // You can override this method
247  return $this->content;
248  }
249 
256  private function _title()
257  {
258  if ( empty( $this->title ) ) {
259  $this->title = $this->title();
260  }
261 
262  return empty( $this->title ) ? '' : sprintf( '<h4>%s</h4>', $this->title );
263  }
264 
271  public function title()
272  {
273  // You can override this method
274  return $this->title;
275  }
276 
285  public function html()
286  {
287  // Permanent dismiss
288  if ( !empty( $this->dismissed ) && in_array( $this->id, $this->dismissed ) ) {
289  return;
290  }
291 
293  <div
294  <?php echo empty( $this->id ) ? '' : 'id="' . $this->id . '"' ?>
295  <?php echo empty( $this->data ) ? '' : self::dataInline( $this->data ) ?>
296  class="<?php echo self::classInline( $this->class, array(
297  $this->type,
298  'wpdk-alert',
299  $this->dismissable ? 'alert-dismissable' : '',
300  'fade',
301  'in',
302  'clearfix'
303  ) ) ?>">
304  <?php echo $this->dismissButton() ?>
305  <?php echo $this->_title() ?>
306  <?php echo $this->_content() ?>
307  </div>
308 
309  <?php
310  return WPDKHTML::endHTMLCompress();
311  }
312 
321  public function toControl()
322  {
323  $item = array(
324  'type' => WPDKUIControlType::ALERT,
325  'id' => $this->id,
326  'alert_type' => $this->type,
327  'dismiss_button' => $this->dismissButton,
328  'value' => $this->content,
329  'title' => $this->title,
330  );
331  return array( $item );
332  }
333 
334  // -------------------------------------------------------------------------------------------------------------------
335  // Deprecated
336  // -------------------------------------------------------------------------------------------------------------------
337 
345  private function alert_block()
346  {
347  _deprecated_function( __CLASS__ . '::' . __FUNCTION__, '1.3.1 and Bootstrap 3.0.0', '' );
348  return $this->block ? 'alert-block' : '';
349  }
350 
354  function alert( $echo = true )
355  {
356  if ( $echo ) {
357  $this->display();
358  }
359  else {
360  return $this->html();
361  }
362  }
363 
364 }