PluginProbe
E2Pdf – Export Pdf Tool for WordPress / 1.08.06
E2Pdf – Export Pdf Tool for WordPress v1.08.06
1.32.49 1.32.48 1.32.43 1.32.40 1.32.34 1.32.32 1.32.31 1.32.26 1.32.22 1.32.23 1.32.18 1.32.17 1.32.15 trunk 1.00.00 1.00.13 1.01.01 1.02.02 1.03.07 1.04.07 1.05.03 1.06.02 1.07.11 1.08.00 1.08.06 All 72 releases
e2pdf / classes / helper / e2pdf-convert.php

e2pdf-convert.php in E2Pdf – Export Pdf Tool for WordPress 1.08.06, at classes/helper/e2pdf-convert.php

217 lines 6.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 private $helper;
19
20 public function __construct() {
21 $this->helper = Helper_E2pdf_Helper::instance();
22 }
23
24 public function to_hex_color($hex = '') {
25 $color = array(
26 0x00, 0x00, 0x00
27 );
28 $hex = ltrim($hex, '#');
29 if (strlen($hex) === 3) {
30 $color = array(
31 hexdec(substr($hex, 0, 1)),
32 hexdec(substr($hex, 1, 1)),
33 hexdec(substr($hex, 2, 1))
34 );
35 } elseif (strlen($hex) === 6) {
36 $color = array(
37 hexdec(substr($hex, 0, 2)),
38 hexdec(substr($hex, 2, 2)),
39 hexdec(substr($hex, 4, 2))
40 );
41 }
42 return $color;
43 }
44
45 /**
46 * Convert from bytes to Human View
47 *
48 * @param int $size - Size in Bytes
49 *
50 * @return string - Converted size
51 */
52 public function from_bytes($size) {
53 $unit = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
54 return @round($size / pow(1024, ($i = floor(log($size, 1024)))), 2) . '' . $unit[$i];
55 }
56
57 public function to_bytes($size) {
58 $unit = preg_replace('/[^bkmgtpezy]/i', '', $size);
59 $size = preg_replace('/[^0-9\.]/', '', $size);
60 if ($unit) {
61 return round($size * pow(1024, stripos('bkmgtpezy', $unit[0])));
62 } else {
63 return round($size);
64 }
65 }
66
67 public function to_file_name($file_name = '') {
68 if ($file_name) {
69 $search = array(
70 '/',
71 '\\',
72 '"',
73 '&#91;',
74 '&#93;',
75 '&#39;',
76 '&#34;',
77 '&#91;',
78 '&#93;',
79 '&amp;',
80 ';',
81 );
82 $replace = array(
83 '',
84 '_',
85 '',
86 '[',
87 ']',
88 '\'',
89 '',
90 '[',
91 ']',
92 '&',
93 ''
94 );
95
96 $file_name = str_replace($search, $replace, $file_name);
97 }
98
99 return $file_name;
100 }
101
102 /*
103 stritr - case insensitive version of strtr
104 Author: Alexander Peev
105 Posted in PHP.NET
106 */
107
108 public function stritr($haystack, $needle) {
109 if (is_array($needle)) {
110 $pos1 = 0;
111 $result = $haystack;
112 while (count($needle) > 0) {
113 $positions = array();
114 foreach ($needle as $from => $to) {
115 if (( $pos2 = stripos($result, $from, $pos1) ) === FALSE) {
116 unset($needle[$from]);
117 } else {
118 $positions[$from] = $pos2;
119 }
120 }
121 if (count($needle) <= 0) {
122 break;
123 }
124
125 $winner = min($positions);
126 $key = array_search($winner, $positions);
127 $result = ( substr($result, 0, $winner) . $needle[$key] . substr($result, ( $winner + strlen($key))) );
128 $pos1 = ( $winner + strlen($needle[$key]) );
129 }
130 return $result;
131 } else {
132 return $haystack;
133 }
134 }
135
136 public function to_shortcode_array($value, $fields = array()) {
137 $list = explode(',', $value);
138 $data = array();
139 foreach ($list as $item) {
140 $data[] = $this->parse_fields($item);
141 }
142 return $data;
143 }
144
145 public function path_to_url($path = '') {
146 $url = str_replace(
147 wp_normalize_path(untrailingslashit(ABSPATH)), site_url(), wp_normalize_path($path)
148 );
149 return esc_url_raw($url);
150 }
151
152 public function preg_replace($pattern = '', $replacement = '', $value = '') {
153 if ($pattern && $value) {
154 return preg_replace($pattern, $replacement, $value);
155 }
156 return $value;
157 }
158
159 public function to_content_key($content_key = false, $value = '') {
160 $response = '';
161
162 if ($content_key) {
163 $shortcode_tags = array(
164 'e2pdf-content'
165 );
166 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $value, $matches);
167 $tagnames = array_intersect($shortcode_tags, $matches[1]);
168
169 if (!empty($tagnames)) {
170
171 $pattern = $this->helper->load('shortcode')->get_shortcode_regex($tagnames);
172
173 preg_match_all("/$pattern/", $value, $shortcodes);
174
175 foreach ($shortcodes[0] as $key => $shortcode_value) {
176 $shortcode = array();
177 $shortcode[1] = $shortcodes[1][$key];
178 $shortcode[2] = $shortcodes[2][$key];
179 $shortcode[3] = $shortcodes[3][$key];
180 $shortcode[4] = $shortcodes[4][$key];
181 $shortcode[5] = $shortcodes[5][$key];
182 $shortcode[6] = $shortcodes[6][$key];
183
184 $atts = shortcode_parse_atts($shortcode[3]);
185 if (isset($atts['key']) && $atts['key'] == $content_key) {
186 $response = $shortcode[5];
187 }
188 }
189 }
190 }
191
192 return $response;
193 }
194
195 private function parse_fields($value) {
196 $data = array();
197 $fields = explode("|", $value);
198 foreach ($fields as $field) {
199 $field_data = explode(":", $field);
200 $field_key = $field_data['0'];
201 unset($field_data['0']);
202 $field_value = implode(":", $field_data);
203 $data = array_merge_recursive($data, $this->parse_field($field_key, $field_value));
204 }
205 return $data;
206 }
207
208 private function parse_field($field_key, $field_value) {
209 $keys = array_reverse(explode('_', $field_key));
210 foreach ($keys as $key) {
211 $field_value = [$key => $field_value];
212 }
213 return $field_value;
214 }
215
216 }
217