PluginProbe
E2Pdf – Export Pdf Tool for WordPress / 1.07.11
E2Pdf – Export Pdf Tool for WordPress v1.07.11
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.07.11, at classes/helper/e2pdf-convert.php

201 lines 5.6 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 to_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_file_name($file_name = '') {
58 if ($file_name) {
59 $search = array(
60 '/',
61 '\\',
62 '"',
63 '&#91;',
64 '&#93;',
65 '&#39;',
66 '&#34;',
67 '&#91;',
68 '&#93;',
69 '&amp;',
70 ';',
71 );
72 $replace = array(
73 '',
74 '_',
75 '',
76 '[',
77 ']',
78 '\'',
79 '',
80 '[',
81 ']',
82 '&',
83 ''
84 );
85
86 $file_name = str_replace($search, $replace, $file_name);
87 }
88
89 return $file_name;
90 }
91
92 /*
93 stritr - case insensitive version of strtr
94 Author: Alexander Peev
95 Posted in PHP.NET
96 */
97
98 public function stritr($haystack, $needle) {
99 if (is_array($needle)) {
100 $pos1 = 0;
101 $result = $haystack;
102 while (count($needle) > 0) {
103 $positions = array();
104 foreach ($needle as $from => $to) {
105 if (( $pos2 = stripos($result, $from, $pos1) ) === FALSE) {
106 unset($needle[$from]);
107 } else {
108 $positions[$from] = $pos2;
109 }
110 }
111 if (count($needle) <= 0) {
112 break;
113 }
114
115 $winner = min($positions);
116 $key = array_search($winner, $positions);
117 $result = ( substr($result, 0, $winner) . $needle[$key] . substr($result, ( $winner + strlen($key))) );
118 $pos1 = ( $winner + strlen($needle[$key]) );
119 }
120 return $result;
121 } else {
122 return $haystack;
123 }
124 }
125
126 public function to_shortcode_array($value, $fields = array()) {
127 $list = explode(',', $value);
128 $data = array();
129 foreach ($list as $item) {
130 $data[] = $this->parse_fields($item);
131 }
132 return $data;
133 }
134
135 public function path_to_url($path = '') {
136 $url = str_replace(
137 wp_normalize_path(untrailingslashit(ABSPATH)), site_url(), wp_normalize_path($path)
138 );
139 return esc_url_raw($url);
140 }
141
142 public function to_content_key($content_key = false, $value = '') {
143 $response = '';
144
145 if ($content_key) {
146 $shortcode_tags = array(
147 'e2pdf-content'
148 );
149 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $value, $matches);
150 $tagnames = array_intersect($shortcode_tags, $matches[1]);
151
152 if (!empty($tagnames)) {
153
154 $pattern = $this->helper->load('shortcode')->get_shortcode_regex($tagnames);
155
156 preg_match_all("/$pattern/", $value, $shortcodes);
157
158 foreach ($shortcodes[0] as $key => $shortcode_value) {
159 $shortcode = array();
160 $shortcode[1] = $shortcodes[1][$key];
161 $shortcode[2] = $shortcodes[2][$key];
162 $shortcode[3] = $shortcodes[3][$key];
163 $shortcode[4] = $shortcodes[4][$key];
164 $shortcode[5] = $shortcodes[5][$key];
165 $shortcode[6] = $shortcodes[6][$key];
166
167 $atts = shortcode_parse_atts($shortcode[3]);
168
169 if (isset($atts['key']) && $atts['key'] == $content_key) {
170 $response = $shortcode[5];
171 }
172 }
173 }
174 }
175
176 return $response;
177 }
178
179 private function parse_fields($value) {
180 $data = array();
181 $fields = explode("|", $value);
182 foreach ($fields as $field) {
183 $field_data = explode(":", $field);
184 $field_key = $field_data['0'];
185 unset($field_data['0']);
186 $field_value = implode(":", $field_data);
187 $data = array_merge_recursive($data, $this->parse_field($field_key, $field_value));
188 }
189 return $data;
190 }
191
192 private function parse_field($field_key, $field_value) {
193 $keys = array_reverse(explode('_', $field_key));
194 foreach ($keys as $key) {
195 $field_value = [$key => $field_value];
196 }
197 return $field_value;
198 }
199
200 }
201