WPDK  1.5.0
WordPress Development Kit
 All Data Structures Files Functions Variables Pages
wpdk-ui.php
Go to the documentation of this file.
1 <?php
2 
16 final class WPDKUI {
17 
18  // -------------------------------------------------------------------------------------------------------------------
19  // Buttons
20  // -------------------------------------------------------------------------------------------------------------------
21 
27  public static function buttonsUpdateReset()
28  {
29  $button_update = self::button();
30  $button_reset = self::button( __( 'Reset to default', WPDK_TEXTDOMAIN ), array(
31  'name' => 'resetToDefault',
32  'classes' => 'button-secondary'
33  ) );
34  return sprintf( '<p>%s%s</p>', $button_reset, $button_update );
35  }
36 
46  public static function submit( $args = array() )
47  {
48  $default_args = array(
49  'name' => 'button-submit',
50  'class' => 'button button-primary alignright',
51  'value' => __( 'Submit', WPDK_TEXTDOMAIN )
52  );
53 
54  $item = wp_parse_args( $args, $default_args );
55 
56  $item['type'] = WPDKUIControlType::SUBMIT;
57 
58  $submit = new WPDKUIControlSubmit( $item );
59 
60  return $submit->html();
61  }
62 
63 
80  public static function button( $label = '', $args = array() )
81  {
82 
83  $default_args = array(
84  'type' => 'submit',
85  'name' => 'button-update',
86  'classes' => ' button button-primary alignright',
87  'additional_classes' => '',
88  'data' => array(),
89  );
90 
91  $args = wp_parse_args( $args, $default_args );
92 
93  /* Label */
94  if ( empty( $label ) ) {
95  $label = __( 'Update', WPDK_TEXTDOMAIN );
96  }
97 
98  /* Name attribute */
99  if ( empty( $args['name'] ) ) {
100  $name = '';
101  }
102  else {
103  $name = sprintf( 'name="%s"', $args['name'] );
104  }
105 
106  /* Create data attributes. */
107  $data = '';
108  if ( !empty( $args['data'] ) ) {
109  $stack = array();
110  foreach ( $args['data'] as $key => $value ) {
111  $stack[] = sprintf( 'data-%s="%s"', $key, htmlspecialchars( stripslashes( $value ) ) );
112  }
113  $data = implode( ',', $stack );
114  }
115 
116  /* Additional classes */
117  $classes = '';
118  if ( !empty( $args['additional_classes'] ) ) {
119  if ( !is_array( $args['additional_classes'] ) ) {
120  $new = explode( ' ', $args['additional_classes'] );
121  if ( empty( $new ) ) {
122  $new = array( $args['additional_classes'] );
123  }
124  }
125 
126  /* Split default class to remove duplicate. */
127  $default = explode( ' ', $args['classes'] );
128  $classes = implode( ' ', array_merge( $default, $new ) );
129  }
130 
131  $classes .= $args['classes'];
132 
133  ob_start(); ?>
134 
135  <input type="<?php echo $args['type'] ?>" <?php echo $name ?>
136  <?php echo $data ?>
137  class="<?php echo $classes ?>"
138  value="<?php echo $label ?>" />
139 
140  <?php
141  $content = ob_get_contents();
142  ob_end_clean();
143  return $content;
144  }
145 
146  // -------------------------------------------------------------------------------------------------------------------
147  // Badges
148  // -------------------------------------------------------------------------------------------------------------------
149 
153  public static function badged( $count = 0, $classes = '', $tooltip = '', $placement = '' )
154  {
155  _deprecated_function( __METHOD__, '1.0.0.b3', 'badge()' );
156  self::badge( $count, $classes, $tooltip, $placement );
157  }
158 
171  public static function badge( $count = 0, $classes = '', $tooltip = '', $placement = '' )
172  {
173  $classes = !empty( $classes ) ? ' ' . $classes : '';
174 
175  if ( !empty( $tooltip ) ) {
176  $classes .= ' wpdk-has-tooltip';
177  $placement = sprintf( 'data-placement="%s"', empty( $placement ) ? 'bottom' : $placement );
178  }
179 
180  if ( !empty( $count ) ) {
181  $result = sprintf( '<span title="%s" %s class="wpdk-badge update-plugins count-%s%s"><span class="plugin-count">%s</span></span>', $tooltip, $placement, $count, $classes, number_format_i18n( $count ) );
182  }
183  else {
184  /* Restituisco comunque un placeholder comodo per poter inserire onfly via javascript un badge. */
185  $result = sprintf( '<span class="%s"></span>', $classes );
186  }
187  return $result;
188  }
189 
190 
191  // -------------------------------------------------------------------------------------------------------------------
192  // View
193  // -------------------------------------------------------------------------------------------------------------------
194 
209  public static function view( $id, $title, $icon_class, $content )
210  {
211  $html = <<< HTML
212 <div class="wrap">
213  <div class="{$icon_class}"></div>
214  <h2>{$title}</h2>
215  <div class="wpdk-border-container {$id}">
216  {$content}
217  </div>
218 </div>
219 HTML;
220  return $html;
221  }
222 
223  // -------------------------------------------------------------------------------------------------------------------
224  // Form
225  // -------------------------------------------------------------------------------------------------------------------
226 
237  public static function inputNonce( $id )
238  {
239  $nonce = md5( $id );
240  $input_hidden_nonce = new WPDKHTMLTagInput( '', $nonce, $nonce );
241  $input_hidden_nonce->type = WPDKHTMLTagInputType::HIDDEN;
242  $input_hidden_nonce->value = wp_create_nonce( $id );
243  return $input_hidden_nonce->html();
244  }
245 
258  public static function labelTruncate( $value, $size = 'small' )
259  {
260  $html = <<< HTML
261  <div class="wpdk-ui-truncate wpdk-ui-truncate-size_{$size}" title="{$value}">
262  <span>{$value}</span>
263  </div>
264 HTML;
265  return $html;
266  }
267 }