PluginProbe ʕ •ᴥ•ʔ
Admin Columns / 3.0.5
Admin Columns v3.0.5
7.1.4 7.0.19 2.3.5 2.4 2.4.1 2.4.10 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.4.8 2.4.9 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.6.1 2.5.6.2 2.5.6.3 2.5.6.4 3.0 3.0.1 3.0.2 3.0.3 3.0.5 3.0.7 3.1 3.1.1 3.1.10 3.1.2 3.1.3 3.1.5 3.2.3 3.2.7 3.3.1 3.4.1 3.4.6 3.4.8 4.0.1 4.0.3 4.1.6 4.2.2 4.2.5 4.3 4.3.2 4.4.1 4.4.4 4.4.5 4.5.5 4.6.1 4.7.18 4.7.19 4.7.20 4.7.7 7.0.13 7.0.14 7.0.16 trunk 1.0 1.1 1.1.3 1.2 1.2.1 1.3 1.3.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.5.1 1.4.6 1.4.6.1 1.4.6.2 1.4.6.3 1.4.6.4 1.4.7 1.4.8 1.4.9 2.0.0 2.0.1 2.0.2 2.0.3 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.2 2.2.1 2.2.1.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.5.1 2.2.6 2.2.6.1 2.2.6.2 2.2.6.3 2.2.6.4 2.2.7 2.2.8 2.2.8.1 2.2.9 2.3.1 2.3.2 2.3.3
codepress-admin-columns / classes / Helper / Html.php
codepress-admin-columns / classes / Helper Last commit date
Array.php 8 years ago Date.php 8 years ago File.php 8 years ago Html.php 8 years ago Icon.php 8 years ago Image.php 8 years ago Media.php 8 years ago Network.php 8 years ago Post.php 8 years ago String.php 8 years ago Taxonomy.php 8 years ago User.php 8 years ago
Html.php
465 lines
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit;
5 }
6
7 class AC_Helper_Html {
8
9 /**
10 * @param string $key
11 * @param string $value
12 *
13 * @since 3.0
14 * @return string
15 */
16 public function get_attribute_as_string( $key, $value ) {
17 return sprintf( '%s="%s"', $key, esc_attr( trim( $value ) ) );
18 }
19
20 /**
21 * @param array $attributes
22 *
23 * @since 3.0
24 * @return string
25 */
26 public function get_attributes_as_string( array $attributes ) {
27 $output = array();
28
29 foreach ( $attributes as $key => $value ) {
30 $output[] = $this->get_attribute_as_string( $key, $value );
31 }
32
33 return implode( ' ', $output );
34 }
35
36 /**
37 * @param string $url
38 * @param string $label
39 *
40 * @return string|false HTML Anchor element
41 */
42 public function link( $url, $label = null, $attributes = array() ) {
43 if ( false === $label ) {
44 return $label;
45 }
46
47 if ( ! $url ) {
48 return $label;
49 }
50
51 if ( null === $label ) {
52 $label = $url;
53 }
54
55 if ( ! $label ) {
56 return false;
57 }
58
59 if ( ! $this->contains_html( $label ) ) {
60 $label = esc_html( $label );
61 }
62
63 if ( array_key_exists( 'tooltip', $attributes ) ) {
64 $attributes['data-ac-tip'] = $attributes['tooltip'];
65 unset( $attributes['tooltip'] );
66 }
67
68 $allowed = wp_allowed_protocols();
69 $allowed[] = 'skype';
70 $allowed[] = 'call';
71
72 return '<a href="' . esc_url( $url, $allowed ) . '" ' . $this->get_attributes( $attributes ) . '>' . $label . '</a>';
73 }
74
75 /**
76 * @since 2.5
77 */
78 public function divider() {
79 return '<span class="ac-divider"></span>';
80 }
81
82 /**
83 * @param string $content
84 *
85 * @return string
86 */
87 public function get_tooltip_attr( $content ) {
88 if ( ! $content ) {
89 return false;
90 }
91
92 return 'data-ac-tip="' . esc_attr( $content ) . '"';
93 }
94
95 /**
96 * @param $label
97 * @param $tooltip
98 *
99 * @return string
100 */
101 public function tooltip( $label, $tooltip, $attributes = array() ) {
102 if ( ac_helper()->string->is_not_empty( $label ) && $tooltip ) {
103 $label = '<span ' . $this->get_tooltip_attr( $tooltip ) . $this->get_attributes( $attributes ) . '>' . $label . '</span>';
104 }
105
106 return $label;
107 }
108
109 /**
110 * Displays a toggle Box.
111 *
112 * @param string $label
113 * @param string $contents
114 */
115 public function toggle_box( $label, $contents ) {
116 if ( ! $label ) {
117 return;
118 }
119
120 if ( $contents ) : ?>
121 <a class="ac-toggle-box-link" href="#"><?php echo $label; ?></a>
122 <div class="ac-toggle-box-contents"><?php echo $contents; ?></div>
123 <?php
124 else :
125 echo $label;
126 endif;
127 }
128
129 /**
130 * Display a toggle box which trigger an ajax event on click. The ajax callback calls AC_Column::get_ajax_value.
131 *
132 * @param int $id
133 * @param string $label
134 * @param string $column_name
135 *
136 * @return string
137 */
138 public function get_ajax_toggle_box_link( $id, $label, $column_name ) {
139 return ac_helper()->html->link( '#', $label . '<div class="spinner"></div>', array(
140 'class' => 'ac-toggle-box-link',
141 'data-column' => $column_name,
142 'data-item-id' => $id,
143 'data-ajax-populate' => 1,
144 ) );
145 }
146
147 /**
148 * @param string $string
149 * @param int $max_chars
150 *
151 * @return string
152 */
153 public function codearea( $string, $max_chars = 1000 ) {
154 if ( ! $string ) {
155 return false;
156 }
157
158 return '<textarea style="color: #808080; width: 100%; min-height: 60px;" readonly>' . substr( $string, 0, $max_chars ) . '</textarea>';
159 }
160
161 /**
162 * @param array $attributes
163 *
164 * @return string
165 */
166 private function get_attributes( $attributes ) {
167 $_attributes = array();
168
169 foreach ( $attributes as $attribute => $value ) {
170 if ( in_array( $attribute, array( 'title', 'id', 'class', 'style', 'target', 'rel', 'download' ) ) || 'data-' === substr( $attribute, 0, 5 ) ) {
171 $_attributes[] = $this->get_attribute_as_string( $attribute, $value );
172 }
173 }
174
175 return ' ' . implode( ' ', $_attributes );
176 }
177
178 /**
179 * Returns an array with internal / external links
180 *
181 * @param $string
182 *
183 * @return false|array [ internal | external ]
184 */
185 public function get_internal_external_links( $string ) {
186 if ( ! class_exists( 'DOMDocument' ) ) {
187 return false;
188 }
189
190 // Just do a very simple check to check for possible links
191 if ( false === strpos( $string, '<a' ) ) {
192 return false;
193 }
194
195 $internal_links = array();
196 $external_links = array();
197
198 $dom = new DOMDocument();
199 $dom->loadHTML( $string );
200
201 $links = $dom->getElementsByTagName( 'a' );
202
203 foreach ( $links as $link ) {
204 /** @var DOMElement $link */
205 $href = $link->getAttribute( 'href' );
206
207 if ( false !== strpos( $href, home_url() ) ) {
208 $internal_links[] = $href;
209 } else {
210 $external_links[] = $href;
211 }
212 }
213
214 if ( empty( $internal_links ) && empty( $external_links ) ) {
215 return false;
216 }
217
218 return array(
219 $internal_links, $external_links,
220 );
221 }
222
223 /**
224 * @param string $string
225 *
226 * @return bool
227 */
228 private function contains_html( $string ) {
229 return $string && is_string( $string ) ? $string !== strip_tags( $string ) : false;
230 }
231
232 /**
233 * Display indicator icon in the column settings header
234 *
235 * @param string $name
236 */
237 public function indicator( $class, $id, $title = false ) { ?>
238 <span class="indicator-<?php echo esc_attr( $class ); ?>" data-indicator-id="<?php echo esc_attr( $id ); ?>" title="<?php echo esc_attr( $title ); ?>"></span>
239 <?php
240 }
241
242 /**
243 * Adds a divider to the implode
244 *
245 * @param $array
246 *
247 * @return string
248 */
249 public function implode( $array, $divider = true ) {
250 if ( ! is_array( $array ) ) {
251 return $array;
252 }
253
254 // Remove empty values
255 $array = $this->remove_empty( $array );
256
257 if ( true === $divider ) {
258 $divider = $this->divider();
259 }
260
261 return implode( $divider, $array );
262 }
263
264 public function remove_empty( $array ) {
265 return array_filter( $array, array( ac_helper()->string, 'is_not_empty' ) );
266 }
267
268 /**
269 * Remove attribute from an html tag
270 *
271 * @param string $html HTML tag
272 * @param string|array $attribute Attribute: style, class, alt, data etc.
273 *
274 * @return mixed
275 */
276 public function strip_attributes( $html, $attributes ) {
277 if ( $this->contains_html( $html ) ) {
278 foreach ( (array) $attributes as $attribute ) {
279 $html = preg_replace( '/(<[^>]+) ' . $attribute . '=".*?"/i', '$1', $html );
280 }
281 }
282
283 return $html;
284 }
285
286 /**
287 * Small HTML block with grey background and rounded corners
288 *
289 * @param string|array $items
290 *
291 * @return string
292 */
293 public function small_block( $items ) {
294 $blocks = array();
295
296 foreach ( (array) $items as $item ) {
297 if ( $item && is_string( $item ) ) {
298 $blocks[] = '<span class="ac-small-block">' . $item . '</span>';
299 }
300 }
301
302 return implode( $blocks );
303 }
304
305 /**
306 * @param array $args
307 *
308 * @return string
309 */
310 public function progress_bar( $args = array() ) {
311 $defaults = array(
312 'current' => 0,
313 'total' => 100, // -1 is infinitive
314 'label_left' => '',
315 'label_right' => '',
316 'label_main' => '',
317 );
318
319 $args = wp_parse_args( $args, $defaults );
320
321 if ( -1 === $args['total'] ) {
322 $args['current'] = 0;
323 $args['total'] = 100;
324 $args['label_right'] = '&infin;';
325 }
326
327 $args['current'] = absint( $args['current'] );
328 $args['total'] = absint( $args['total'] );
329
330 if ( $args['total'] < 0 ) {
331 return false;
332 }
333
334 $percentage = 0;
335
336 if ( $args['total'] > 0 ) {
337 $percentage = round( ( $args['current'] / $args['total'] ) * 100 );
338 }
339
340 // Allowed size is zero, but current has a value
341 if ( 0 === $args['total'] && $args['current'] > 0 ) {
342 $percentage = 101;
343 }
344
345 $class = '';
346 if ( $percentage > 100 ) {
347 $percentage = 100;
348 $class = ' full';
349 }
350
351 ob_start();
352 ?>
353 <div class="ac-progress-bar<?php echo esc_attr( $class ); ?>">
354 <?php if ( $args['label_main'] ) : ?>
355 <span class="ac-label-main"><?php echo esc_html( $args['label_main'] ); ?></span>
356 <?php endif; ?>
357 <div class="ac-bar-container">
358 <span class="ac-label-left"><?php echo esc_html( $args['label_left'] ); ?></span>
359 <span class="ac-label-right"><?php echo esc_html( $args['label_right'] ); ?></span>
360 <?php if ( $percentage ) : ?>
361 <div class="ac-bar" style="width:<?php echo esc_attr( $percentage ); ?>%"></div>
362 <?php endif; ?>
363 </div>
364 </div>
365 <?php
366
367 return ob_get_clean();
368 }
369
370 public function more( $array, $number = 10, $glue = ', ' ) {
371 $first_set = array_slice( $array, 0, $number );
372 $last_set = array_slice( $array, $number );
373
374 ob_start();
375
376 if ( $first_set ) {
377
378 echo implode( $glue, $first_set );
379
380 if ( $last_set ) { ?>
381 <span class="ac-more-link-show">( <a><?php printf( __( 'Show %s more', 'codepress-admin-columns' ), count( $last_set ) ); ?></a> )</span>
382 <span class="ac-show-more-block">
383 <?php echo $glue . implode( $glue, $first_set ); ?>
384 <br/>
385 <span class="ac-more-link-hide">( <a><?php _e( 'Hide', 'codepress-admin-columns' ); ?></a> )</span>
386 </span>
387 <?php
388 }
389 }
390
391 return ob_get_clean();
392 }
393
394 /**
395 * Return round HTML span
396 *
397 * @param $string
398 *
399 * @return string
400 */
401 public function rounded( $string ) {
402 return '<span class="ac-rounded">' . $string . '</span>';
403 }
404
405 /**
406 * Returns star rating based on X start from $max count. Does support decimals.
407 *
408 * @param int $count
409 * @param int $max
410 *
411 * @return string
412 */
413 public function stars( $count, $max = 0 ) {
414 $stars = array(
415 'filled' => floor( $count ),
416 'half' => floor( round( ( $count * 2 ) ) - ( floor( $count ) * 2 ) ) ? 1 : 0,
417 'empty' => 0,
418 );
419
420 $max = absint( $max );
421
422 if ( $max > 0 ) {
423 $star_count = $stars['filled'] + $stars['half'];
424
425 $stars['empty'] = $max - $star_count;
426
427 if ( $star_count > $max ) {
428 $stars['filled'] = $max;
429 $stars['half'] = 0;
430 }
431 }
432
433 $icons = array();
434
435 foreach ( $stars as $type => $count ) {
436 for ( $i = 1; $i <= $count; $i++ ) {
437 $icons[] = ac_helper()->icon->dashicon( array( 'icon' => 'star-' . $type, 'class' => 'ac-value-star' ) );
438 }
439 }
440
441 ob_start();
442 ?>
443 <span class="ac-value-stars">
444 <?php echo implode( ' ', $icons ); ?>
445 </span>
446 <?php
447 return ob_get_clean();
448 }
449
450 /**
451 * @param string $value HTML
452 * @param int $removed
453 *
454 * @return string
455 */
456 public function images( $value, $removed = false ) {
457 if ( $removed ) {
458 $value .= ac_helper()->html->rounded( '+' . $removed );
459 }
460
461 return '<div class="ac-image-container">' . $value . '</div>';
462 }
463
464 }
465