| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* E2pdf Convert Helper |
| 5 |
* |
| 6 |
* @copyright Copyright 2017 https://e2pdf.com |
| 7 |
* @license GPL v2 |
| 8 |
* @version 1 |
| 9 |
* @link https://e2pdf.com |
| 10 |
* @since 1.01.02 |
| 11 |
*/ |
| 12 |
if (!defined('ABSPATH')) { |
| 13 |
die('Access denied.'); |
| 14 |
} |
| 15 |
|
| 16 |
class Helper_E2pdf_Convert { |
| 17 |
|
| 18 |
public function to_hex_color($hex = '') { |
| 19 |
$color = array( |
| 20 |
0x00, 0x00, 0x00 |
| 21 |
); |
| 22 |
$hex = ltrim($hex, '#'); |
| 23 |
if (strlen($hex) === 3) { |
| 24 |
$color = array( |
| 25 |
hexdec(substr($hex, 0, 1)), |
| 26 |
hexdec(substr($hex, 1, 1)), |
| 27 |
hexdec(substr($hex, 2, 1)) |
| 28 |
); |
| 29 |
} elseif (strlen($hex) === 6) { |
| 30 |
$color = array( |
| 31 |
hexdec(substr($hex, 0, 2)), |
| 32 |
hexdec(substr($hex, 2, 2)), |
| 33 |
hexdec(substr($hex, 4, 2)) |
| 34 |
); |
| 35 |
} |
| 36 |
return $color; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Convert from bytes to Human View |
| 41 |
* |
| 42 |
* @param int $size - Size in Bytes |
| 43 |
* |
| 44 |
* @return string - Converted size |
| 45 |
*/ |
| 46 |
public function to_bytes($size) { |
| 47 |
$unit = array('B', 'KB', 'MB', 'GB', 'TB', 'PB'); |
| 48 |
return @round($size / pow(1024, ($i = floor(log($size, 1024)))), 2) . '' . $unit[$i]; |
| 49 |
} |
| 50 |
|
| 51 |
/* |
| 52 |
stritr - case insensitive version of strtr |
| 53 |
Author: Alexander Peev |
| 54 |
Posted in PHP.NET |
| 55 |
*/ |
| 56 |
|
| 57 |
public function stritr($haystack, $needle) { |
| 58 |
if (is_array($needle)) { |
| 59 |
$pos1 = 0; |
| 60 |
$result = $haystack; |
| 61 |
while (count($needle) > 0) { |
| 62 |
$positions = array(); |
| 63 |
foreach ($needle as $from => $to) { |
| 64 |
if (( $pos2 = stripos($result, $from, $pos1) ) === FALSE) { |
| 65 |
unset($needle[$from]); |
| 66 |
} else { |
| 67 |
$positions[$from] = $pos2; |
| 68 |
} |
| 69 |
} |
| 70 |
if (count($needle) <= 0) { |
| 71 |
break; |
| 72 |
} |
| 73 |
|
| 74 |
$winner = min($positions); |
| 75 |
$key = array_search($winner, $positions); |
| 76 |
$result = ( substr($result, 0, $winner) . $needle[$key] . substr($result, ( $winner + strlen($key))) ); |
| 77 |
$pos1 = ( $winner + strlen($needle[$key]) ); |
| 78 |
} |
| 79 |
return $result; |
| 80 |
} else { |
| 81 |
return $haystack; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
public function to_shortcode_array($value, $fields = array()) { |
| 86 |
$list = explode(',', $value); |
| 87 |
$data = array(); |
| 88 |
foreach ($list as $item) { |
| 89 |
$data[] = $this->parse_fields($item); |
| 90 |
} |
| 91 |
return $data; |
| 92 |
} |
| 93 |
|
| 94 |
private function parse_fields($value) { |
| 95 |
$data = array(); |
| 96 |
$fields = explode("|", $value); |
| 97 |
foreach ($fields as $field) { |
| 98 |
$field_data = explode(":", $field); |
| 99 |
$field_key = $field_data['0']; |
| 100 |
unset($field_data['0']); |
| 101 |
$field_value = implode(":", $field_data); |
| 102 |
$data = array_merge_recursive($data, $this->parse_field($field_key, $field_value)); |
| 103 |
} |
| 104 |
return $data; |
| 105 |
} |
| 106 |
|
| 107 |
private function parse_field($field_key, $field_value) { |
| 108 |
$keys = array_reverse(explode('_', $field_key)); |
| 109 |
foreach ($keys as $key) { |
| 110 |
$field_value = [$key => $field_value]; |
| 111 |
} |
| 112 |
return $field_value; |
| 113 |
} |
| 114 |
|
| 115 |
} |
| 116 |
|