Array.php
9 years ago
Date.php
9 years ago
File.php
9 years ago
Html.php
9 years ago
Icon.php
9 years ago
Image.php
9 years ago
Network.php
9 years ago
Post.php
9 years ago
String.php
9 years ago
Taxonomy.php
9 years ago
User.php
9 years ago
Html.php
365 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 ) { |
| 102 | if ( $label && $tooltip ) { |
| 103 | $label = '<span ' . $this->get_tooltip_attr( $tooltip ) . '>' . $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|false HTML |
| 137 | */ |
| 138 | public function toggle_box_ajax( $id, $label, $column_name ) { |
| 139 | if ( ! $label ) { |
| 140 | return false; |
| 141 | } |
| 142 | |
| 143 | return ac_helper()->html->link( '#', $label . '<div class="spinner"></div>', array( |
| 144 | 'class' => 'ac-toggle-box-link', |
| 145 | 'data-column' => $column_name, |
| 146 | 'data-item-id' => $id, |
| 147 | 'data-ajax-populate' => 1, |
| 148 | ) ); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * @param string $string |
| 153 | * @param int $max_chars |
| 154 | * |
| 155 | * @return string |
| 156 | */ |
| 157 | public function codearea( $string, $max_chars = 1000 ) { |
| 158 | if ( ! $string ) { |
| 159 | return false; |
| 160 | } |
| 161 | |
| 162 | return '<textarea style="color: #808080; width: 100%; min-height: 60px;" readonly>' . substr( $string, 0, $max_chars ) . '</textarea>'; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * @param array $attributes |
| 167 | * |
| 168 | * @return string |
| 169 | */ |
| 170 | private function get_attributes( $attributes ) { |
| 171 | $_attributes = array(); |
| 172 | |
| 173 | foreach ( $attributes as $attribute => $value ) { |
| 174 | if ( in_array( $attribute, array( 'title', 'id', 'class', 'style', 'target' ) ) || 'data-' === substr( $attribute, 0, 5 ) ) { |
| 175 | $_attributes[] = $this->get_attribute_as_string( $attribute, $value ); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | return ' ' . implode( ' ', $_attributes ); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * @param string $string |
| 184 | * |
| 185 | * @return bool |
| 186 | */ |
| 187 | private function contains_html( $string ) { |
| 188 | return $string && is_string( $string ) ? $string !== strip_tags( $string ) : false; |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Display indicator icon in the column settings header |
| 193 | * |
| 194 | * @param string $name |
| 195 | */ |
| 196 | public function indicator( $class, $id, $title = false ) { ?> |
| 197 | <span class="indicator-<?php echo esc_attr( $class ); ?>" data-indicator-id="<?php echo esc_attr( $id ); ?>" title="<?php echo esc_attr( $title ); ?>"></span> |
| 198 | <?php |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Adds a divider to the implode |
| 203 | * |
| 204 | * @param $array |
| 205 | * |
| 206 | * @return string |
| 207 | */ |
| 208 | public function implode( $array, $divider = true ) { |
| 209 | if ( ! is_array( $array ) ) { |
| 210 | return $array; |
| 211 | } |
| 212 | |
| 213 | // Remove empty values |
| 214 | $array = $this->remove_empty( $array ); |
| 215 | |
| 216 | if ( true === $divider ) { |
| 217 | $divider = $this->divider(); |
| 218 | } |
| 219 | |
| 220 | return implode( $divider, $array ); |
| 221 | } |
| 222 | |
| 223 | public function remove_empty( $array ) { |
| 224 | return array_filter( $array, array( ac_helper()->string, 'is_not_empty' ) ); |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Remove attribute from an html tag |
| 229 | * |
| 230 | * @param string $html HTML tag |
| 231 | * @param string|array $attribute Attribute: style, class, alt, data etc. |
| 232 | * |
| 233 | * @return mixed |
| 234 | */ |
| 235 | public function strip_attributes( $html, $attributes ) { |
| 236 | if ( $this->contains_html( $html ) ) { |
| 237 | foreach ( (array) $attributes as $attribute ) { |
| 238 | $html = preg_replace( '/(<[^>]+) ' . $attribute . '=".*?"/i', '$1', $html ); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | return $html; |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Small HTML block with grey background and rounded corners |
| 247 | * |
| 248 | * @param string|array $items |
| 249 | * |
| 250 | * @return string |
| 251 | */ |
| 252 | public function small_block( $items ) { |
| 253 | $blocks = array(); |
| 254 | |
| 255 | foreach ( (array) $items as $item ) { |
| 256 | if ( $item && is_string( $item ) ) { |
| 257 | $blocks[] = '<span class="ac-small-block">' . $item . '</span>'; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | return implode( $blocks ); |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * @param array $args |
| 266 | * |
| 267 | * @return string |
| 268 | */ |
| 269 | public function progress_bar( $args = array() ) { |
| 270 | $defaults = array( |
| 271 | 'current' => 0, |
| 272 | 'total' => 100, // -1 is infinitive |
| 273 | 'label_left' => '', |
| 274 | 'label_right' => '', |
| 275 | 'label_main' => '', |
| 276 | ); |
| 277 | |
| 278 | $args = wp_parse_args( $args, $defaults ); |
| 279 | |
| 280 | if ( -1 === $args['total'] ) { |
| 281 | $args['current'] = 0; |
| 282 | $args['total'] = 100; |
| 283 | $args['label_right'] = '∞'; |
| 284 | } |
| 285 | |
| 286 | $args['current'] = absint( $args['current'] ); |
| 287 | $args['total'] = absint( $args['total'] ); |
| 288 | |
| 289 | if ( $args['total'] < 0 ) { |
| 290 | return false; |
| 291 | } |
| 292 | |
| 293 | $percentage = 0; |
| 294 | |
| 295 | if ( $args['total'] > 0 ) { |
| 296 | $percentage = round( ( $args['current'] / $args['total'] ) * 100 ); |
| 297 | } |
| 298 | |
| 299 | // Allowed size is zero, but current has a value |
| 300 | if ( 0 === $args['total'] && $args['current'] > 0 ) { |
| 301 | $percentage = 101; |
| 302 | } |
| 303 | |
| 304 | $class = ''; |
| 305 | if ( $percentage > 100 ) { |
| 306 | $percentage = 100; |
| 307 | $class = ' full'; |
| 308 | } |
| 309 | |
| 310 | ob_start(); |
| 311 | ?> |
| 312 | <div class="ac-progress-bar<?php echo esc_attr( $class ); ?>"> |
| 313 | <?php if ( $args['label_main'] ) : ?> |
| 314 | <span class="ac-label-main"><?php echo esc_html( $args['label_main'] ); ?></span> |
| 315 | <?php endif; ?> |
| 316 | <div class="ac-bar-container"> |
| 317 | <span class="ac-label-left"><?php echo esc_html( $args['label_left'] ); ?></span> |
| 318 | <span class="ac-label-right"><?php echo esc_html( $args['label_right'] ); ?></span> |
| 319 | <?php if ( $percentage ) : ?> |
| 320 | <div class="ac-bar" style="width:<?php echo esc_attr( $percentage ); ?>%"></div> |
| 321 | <?php endif; ?> |
| 322 | </div> |
| 323 | </div> |
| 324 | <?php |
| 325 | |
| 326 | return ob_get_clean(); |
| 327 | } |
| 328 | |
| 329 | public function more( $array, $number = 10, $glue = ', ' ) { |
| 330 | $first_set = array_slice( $array, 0, $number ); |
| 331 | $last_set = array_slice( $array, $number ); |
| 332 | |
| 333 | ob_start(); |
| 334 | |
| 335 | if ( $first_set ) { |
| 336 | |
| 337 | echo implode( $glue, $first_set ); |
| 338 | |
| 339 | if ( $last_set ) { ?> |
| 340 | <span class="ac-more-link-show">( <a><?php printf( __( 'Show %s more', 'codepress-admin-columns' ), count( $last_set ) ); ?></a> )</span> |
| 341 | <span class="ac-show-more-block"> |
| 342 | <?php echo $glue . implode( $glue, $first_set ); ?> |
| 343 | <br/> |
| 344 | <span class="ac-more-link-hide">( <a><?php _e( 'Hide', 'codepress-admin-columns' ); ?></a> )</span> |
| 345 | </span> |
| 346 | <?php |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | return ob_get_clean(); |
| 351 | } |
| 352 | |
| 353 | /** |
| 354 | * Return round HTML span |
| 355 | * |
| 356 | * @param $string |
| 357 | * |
| 358 | * @return string |
| 359 | */ |
| 360 | public function rounded( $string ) { |
| 361 | return '<span class="ac-rounded">' . $string . '</span>'; |
| 362 | } |
| 363 | |
| 364 | } |
| 365 |