PluginProbe ʕ •ᴥ•ʔ
Admin Columns / 3.1.1
Admin Columns v3.1.1
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
486 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 $string
182 * @param array $internal_domains Domains which determine internal links. Default is home_url().
183 *
184 * @return false|array [ internal | external ]
185 */
186 public function get_internal_external_links( $string, $internal_domains = array() ) {
187 if ( ! class_exists( 'DOMDocument' ) ) {
188 return false;
189 }
190
191 // Just do a very simple check to check for possible links
192 if ( false === strpos( $string, '<a' ) ) {
193 return false;
194 }
195
196 if ( ! $internal_domains ) {
197 $internal_domains = array( home_url() );
198 }
199
200 $internal_links = array();
201 $external_links = array();
202
203 $dom = new DOMDocument();
204 $dom->loadHTML( $string );
205
206 $links = $dom->getElementsByTagName( 'a' );
207
208 foreach ( $links as $link ) {
209 /** @var DOMElement $link */
210 $href = $link->getAttribute( 'href' );
211
212 if ( 0 === strpos( $href, '#' ) ) {
213 continue;
214 }
215
216 $internal = false;
217
218 foreach ( (array) $internal_domains as $domain ) {
219 if ( false !== strpos( $href, $domain ) ) {
220 $internal = true;
221 }
222 }
223
224 if ( $internal ) {
225 $internal_links[] = $href;
226 } else {
227 $external_links[] = $href;
228 }
229 }
230
231 if ( empty( $internal_links ) && empty( $external_links ) ) {
232 return false;
233 }
234
235 return array(
236 $internal_links, $external_links,
237 );
238 }
239
240 /**
241 * @param string $string
242 *
243 * @return bool
244 */
245 private function contains_html( $string ) {
246 return $string && is_string( $string ) ? $string !== strip_tags( $string ) : false;
247 }
248
249 /**
250 * Display indicator icon in the column settings header
251 *
252 * @param string $name
253 */
254 public function indicator( $class, $id, $title = false ) { ?>
255 <span class="indicator-<?php echo esc_attr( $class ); ?>" data-indicator-id="<?php echo esc_attr( $id ); ?>" title="<?php echo esc_attr( $title ); ?>"></span>
256 <?php
257 }
258
259 /**
260 * Adds a divider to the implode
261 *
262 * @param $array
263 *
264 * @return string
265 */
266 public function implode( $array, $divider = true ) {
267 if ( ! is_array( $array ) ) {
268 return $array;
269 }
270
271 // Remove empty values
272 $array = $this->remove_empty( $array );
273
274 if ( true === $divider ) {
275 $divider = $this->divider();
276 }
277
278 return implode( $divider, $array );
279 }
280
281 public function remove_empty( $array ) {
282 return array_filter( $array, array( ac_helper()->string, 'is_not_empty' ) );
283 }
284
285 /**
286 * Remove attribute from an html tag
287 *
288 * @param string $html HTML tag
289 * @param string|array $attribute Attribute: style, class, alt, data etc.
290 *
291 * @return mixed
292 */
293 public function strip_attributes( $html, $attributes ) {
294 if ( $this->contains_html( $html ) ) {
295 foreach ( (array) $attributes as $attribute ) {
296 $html = preg_replace( '/(<[^>]+) ' . $attribute . '=".*?"/i', '$1', $html );
297 }
298 }
299
300 return $html;
301 }
302
303 /**
304 * Small HTML block with grey background and rounded corners
305 *
306 * @param string|array $items
307 *
308 * @return string
309 */
310 public function small_block( $items ) {
311 $blocks = array();
312
313 foreach ( (array) $items as $item ) {
314 if ( $item && is_string( $item ) ) {
315 $blocks[] = '<span class="ac-small-block">' . $item . '</span>';
316 }
317 }
318
319 return implode( $blocks );
320 }
321
322 /**
323 * @param array $args
324 *
325 * @return string
326 */
327 public function progress_bar( $args = array() ) {
328 $defaults = array(
329 'current' => 0,
330 'total' => 100, // -1 is infinitive
331 'label_left' => '',
332 'label_right' => '',
333 'label_main' => '',
334 );
335
336 $args = wp_parse_args( $args, $defaults );
337
338 if ( -1 === $args['total'] ) {
339 $args['current'] = 0;
340 $args['total'] = 100;
341 $args['label_right'] = '&infin;';
342 }
343
344 $args['current'] = absint( $args['current'] );
345 $args['total'] = absint( $args['total'] );
346
347 if ( $args['total'] < 0 ) {
348 return false;
349 }
350
351 $percentage = 0;
352
353 if ( $args['total'] > 0 ) {
354 $percentage = round( ( $args['current'] / $args['total'] ) * 100 );
355 }
356
357 // Allowed size is zero, but current has a value
358 if ( 0 === $args['total'] && $args['current'] > 0 ) {
359 $percentage = 101;
360 }
361
362 $class = '';
363 if ( $percentage > 100 ) {
364 $percentage = 100;
365 $class = ' full';
366 }
367
368 ob_start();
369 ?>
370 <div class="ac-progress-bar<?php echo esc_attr( $class ); ?>">
371 <?php if ( $args['label_main'] ) : ?>
372 <span class="ac-label-main"><?php echo esc_html( $args['label_main'] ); ?></span>
373 <?php endif; ?>
374 <div class="ac-bar-container">
375 <span class="ac-label-left"><?php echo esc_html( $args['label_left'] ); ?></span>
376 <span class="ac-label-right"><?php echo esc_html( $args['label_right'] ); ?></span>
377 <?php if ( $percentage ) : ?>
378 <div class="ac-bar" style="width:<?php echo esc_attr( $percentage ); ?>%"></div>
379 <?php endif; ?>
380 </div>
381 </div>
382 <?php
383
384 return ob_get_clean();
385 }
386
387 public function more( $array, $number = 10, $glue = ', ' ) {
388 $first_set = array_slice( $array, 0, $number );
389 $last_set = array_slice( $array, $number );
390
391 ob_start();
392
393 if ( $first_set ) {
394
395 echo implode( $glue, $first_set );
396
397 if ( $last_set ) { ?>
398 <span class="ac-more-link-show">( <a><?php printf( __( 'Show %s more', 'codepress-admin-columns' ), count( $last_set ) ); ?></a> )</span>
399 <span class="ac-show-more-block">
400 <?php echo $glue . implode( $glue, $first_set ); ?>
401 <br/>
402 <span class="ac-more-link-hide">( <a><?php _e( 'Hide', 'codepress-admin-columns' ); ?></a> )</span>
403 </span>
404 <?php
405 }
406 }
407
408 return ob_get_clean();
409 }
410
411 /**
412 * Return round HTML span
413 *
414 * @param $string
415 *
416 * @return string
417 */
418 public function rounded( $string ) {
419 return '<span class="ac-rounded">' . $string . '</span>';
420 }
421
422 /**
423 * Returns star rating based on X start from $max count. Does support decimals.
424 *
425 * @param int $count
426 * @param int $max
427 *
428 * @return string
429 */
430 public function stars( $count, $max = 0 ) {
431 $stars = array(
432 'filled' => floor( $count ),
433 'half' => floor( round( ( $count * 2 ) ) - ( floor( $count ) * 2 ) ) ? 1 : 0,
434 'empty' => 0,
435 );
436
437 $max = absint( $max );
438
439 if ( $max > 0 ) {
440 $star_count = $stars['filled'] + $stars['half'];
441
442 $stars['empty'] = $max - $star_count;
443
444 if ( $star_count > $max ) {
445 $stars['filled'] = $max;
446 $stars['half'] = 0;
447 }
448 }
449
450 $icons = array();
451
452 foreach ( $stars as $type => $count ) {
453 for ( $i = 1; $i <= $count; $i++ ) {
454 $icons[] = ac_helper()->icon->dashicon( array( 'icon' => 'star-' . $type, 'class' => 'ac-value-star' ) );
455 }
456 }
457
458 ob_start();
459 ?>
460 <span class="ac-value-stars">
461 <?php echo implode( ' ', $icons ); ?>
462 </span>
463 <?php
464 return ob_get_clean();
465 }
466
467 /**
468 * @param string $value HTML
469 * @param int $removed
470 *
471 * @return string
472 */
473 public function images( $value, $removed = false ) {
474 if ( ! $value ) {
475 return false;
476 }
477
478 if ( $removed ) {
479 $value .= ac_helper()->html->rounded( '+' . $removed );
480 }
481
482 return '<div class="ac-image-container">' . $value . '</div>';
483 }
484
485 }
486