PluginProbe
E2Pdf – Export Pdf Tool for WordPress / 1.32.53
E2Pdf – Export Pdf Tool for WordPress v1.32.53
1.32.53 1.32.51 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 All 74 releases
← All changes | classes/helper/e2pdf-helper.php +370 -116 1.01.01 → 1.32.53 View file →
@@ -1,14 +1,12 @@
1 1 <?php
2 2
3 3 /**
4 - * E2pdf Helper
5 - *
6 - * @copyright Copyright 2017 https://e2pdf.com
7 - * @license GPL v2
8 - * @version 1
9 - * @link https://e2pdf.com
10 - * @since 0.00.01
4 + * File: /helper/e2pdf-helper.php
5 + *
6 + * @package E2Pdf
7 + * @license GPLv3
8 + * @link https://e2pdf.com
11 9 */
12 10 if (!defined('ABSPATH')) {
13 11 die('Access denied.');
14 12 }
@@ -14,9 +12,9 @@
14 12 }
15 13
16 14 class Helper_E2pdf_Helper {
17 15
18 - protected static $instance = NULL;
16 + protected static $instance;
19 17 private $helper;
20 18
21 19 const CHMOD_DIR = 0755;
22 20 const CHMOD_FILE = 0644;
@@ -27,14 +25,36 @@
27 25 }
28 26 return self::$instance;
29 27 }
30 28
31 - /**
32 - * Set option by Key
33 - *
34 - * @param string $key - Key of option
35 - * @param mixed $value - Value of option
36 - */
29 + public function __construct() {
30 + $this->set('upload_dir', $this->get_wp_upload_dir('basedir') . '/e2pdf/');
31 + $this->set('tmp_dir', $this->get('upload_dir') . 'tmp/');
32 + $this->set('cache_dir', $this->get('upload_dir') . 'tmp/cache/');
33 + $this->set('pdf_dir', $this->get('upload_dir') . 'pdf/');
34 + $this->set('fonts_dir', $this->get('upload_dir') . 'fonts/');
35 + $this->set('tpl_dir', $this->get('upload_dir') . 'tpl/');
36 + $this->set('viewer_dir', $this->get('upload_dir') . 'viewer/');
37 + $this->set('bulk_dir', $this->get('upload_dir') . 'bulks/');
38 + $this->set('wpcf7_dir', $this->get('upload_dir') . 'wpcf7/');
39 + if (defined('E2PDF_ROOT_FILE')) {
40 + $info = get_file_data(E2PDF_ROOT_FILE, ['version' => 'Version'], false);
41 + $this->set('version', $info['version']);
42 + $this->set('plugin_dir', plugin_dir_path(E2PDF_ROOT_FILE));
43 + $this->set('plugin_file_path', E2PDF_ROOT_FILE);
44 + $this->set('plugin', plugin_basename(E2PDF_ROOT_FILE));
45 + $this->set('slug', dirname(plugin_basename(E2PDF_ROOT_FILE)));
46 + }
47 + $this->set('cache', get_option('e2pdf_cache', '1'));
48 + $parse_args = wp_parse_args(home_url(add_query_arg([], false)));
49 + $this->set('page', reset($parse_args));
50 + if (get_option('e2pdf_memory_time', '0')) {
51 + $this->set('memory_debug', memory_get_usage());
52 + $this->set('time_debug', microtime(true));
53 + }
54 + }
55 +
56 + // set
37 57 public function set($key, $value) {
38 58 if (!$this->helper) {
39 59 $this->helper = new stdClass();
40 60 }
@@ -40,14 +60,9 @@
40 60 }
41 61 $this->helper->$key = $value;
42 62 }
43 63
44 - /**
45 - * Add value to option by Key
46 - *
47 - * @param string $key - Key of option
48 - * @param mixed $value - Value of option
49 - */
64 + // add
50 65 public function add($key, $value) {
51 66 if (!$this->helper) {
52 67 $this->helper = new stdClass();
53 68 }
@@ -56,18 +71,14 @@
56 71 if (is_array($this->helper->$key)) {
57 72 array_push($this->helper->$key, $value);
58 73 }
59 74 } else {
60 - $this->helper->$key = array();
75 + $this->helper->$key = [];
61 76 array_push($this->helper->$key, $value);
62 77 }
63 78 }
64 79
65 - /**
66 - * Unset option
67 - *
68 - * @param string $key - Key of option
69 - */
80 + // deset
70 81 public function deset($key) {
71 82 if (isset($this->helper->$key)) {
72 83 unset($this->helper->$key);
73 84 }
@@ -72,15 +83,9 @@
72 83 unset($this->helper->$key);
73 84 }
74 85 }
75 86
76 - /**
77 - * Set option
78 - *
79 - * @param string $key - Key of option
80 - *
81 - * @return mixed - Get value of option by Key
82 - */
87 + // get
83 88 public function get($key) {
84 89 if (isset($this->helper->$key)) {
85 90 return $this->helper->$key;
86 91 } else {
@@ -87,93 +92,25 @@
87 92 return '';
88 93 }
89 94 }
90 95
91 - /**
92 - * Get url path
93 - *
94 - * @param string $url - Url path
95 - *
96 - * @return string - Url path
97 - */
96 + // get url path
98 97 public function get_url_path($url) {
99 98 return plugins_url($url, $this->get('plugin_file_path'));
100 99 }
101 100
102 - /**
103 - * Get url
104 - *
105 - * @param array $data - Array list of url params
106 - * @param string $prefix - Prefix of url
107 - *
108 - * @return string Url
109 - */
110 - public function get_url($data = array(), $prefix = 'admin.php?') {
101 + // get url
102 + public function get_url($data = [], $prefix = 'admin.php?') {
111 103 $url = $prefix . http_build_query($data);
112 104 return admin_url($url);
113 105 }
114 106
115 - /**
116 - * Get Ip address
117 - *
118 - * @return mixed - IP address or FALSE
119 - */
120 - public function get_ip() {
121 -
122 - if (isset($_SERVER['REMOTE_ADDR'])) {
123 - $ip = $_SERVER['REMOTE_ADDR'];
124 - } else {
125 - $ip = false;
126 - }
127 -
128 - return $ip;
129 - }
130 -
131 - /**
132 - * Convert from bytes to Human View
133 - *
134 - * @param int $size - Size in Bytes
135 - *
136 - * @return string - Converted size
137 - */
138 - public function convert($size) {
139 - $unit = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
140 - return @round($size / pow(1024, ($i = floor(log($size, 1024)))), 2) . '' . $unit[$i];
141 - }
142 -
143 - public function hexColorAllocate($hex = '') {
144 - $color = array(
145 - 0x00, 0x00, 0x00
146 - );
147 - $hex = ltrim($hex, '#');
148 - if (strlen($hex) === 3) {
149 - $color = array(
150 - hexdec(substr($hex, 0, 1)),
151 - hexdec(substr($hex, 1, 1)),
152 - hexdec(substr($hex, 2, 1))
153 - );
154 - } elseif (strlen($hex) === 6) {
155 - $color = array(
156 - hexdec(substr($hex, 0, 2)),
157 - hexdec(substr($hex, 2, 2)),
158 - hexdec(substr($hex, 4, 2))
159 - );
160 - }
161 - return $color;
162 - }
163 -
164 - /**
165 - * Remove dir and its content
166 - *
167 - * @param string $dir - Path of directory to remove
168 - */
107 + // delete dir
169 108 public function delete_dir($dir) {
170 109 if (!is_dir($dir)) {
171 110 return;
172 111 }
173 - if (substr($dir, strlen($dir) - 1, 1) != '/') {
174 - $dir .= '/';
175 - }
112 + $dir = trailingslashit($dir);
176 113 $files = glob($dir . '*', GLOB_MARK);
177 114 foreach ($files as $file) {
178 115 if (is_dir($file)) {
179 116 $this->delete_dir($file);
@@ -180,38 +117,355 @@
180 117 } else {
181 118 unlink($file);
182 119 }
183 120 }
121 + if (file_exists($dir . '.htaccess')) {
122 + unlink($dir . '.htaccess');
123 + }
184 124 rmdir($dir);
185 125 }
186 126
187 - public function create_dir($dir = false) {
127 + // create dir
128 + public function create_dir($dir = false, $recursive = false, $create_index = true, $create_htaccess = false) {
188 129 if ($dir && !file_exists($dir)) {
189 - if (mkdir($dir, self::CHMOD_DIR)) {
190 - $index = $dir . 'index.php';
191 - $this->create_file($index, "<?php\n// Silence is golden.\n?>");
130 + if (mkdir($dir, self::CHMOD_DIR, $recursive)) {
131 + if ($create_index) {
132 + $index = $dir . 'index.php';
133 + if (!file_exists($index)) {
134 + $this->create_file($index, "<?php\n// Silence is golden.\n?>");
135 + }
136 + }
137 + if ($create_htaccess) {
138 + $htaccess = $dir . '.htaccess';
139 + if (!file_exists($htaccess)) {
140 + $this->create_file($htaccess, 'DENY FROM ALL');
141 + }
142 + }
192 143 }
193 144 }
194 145 return is_dir($dir);
195 146 }
196 147
148 + // create file
197 149 public function create_file($file = false, $content = '') {
198 - if ($file && !file_exists($file)) {
199 - if (file_put_contents($file, $content)) {
200 - chmod($file, self::CHMOD_FILE);
150 + $dir = dirname($file);
151 + if (is_dir($dir) && is_writable($dir)) {
152 + if ($file && !file_exists($file)) {
153 + // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_file_put_contents
154 + if (file_put_contents($file, $content, LOCK_EX)) {
155 + chmod($file, self::CHMOD_FILE);
156 + }
201 157 }
202 158 }
203 159 return is_file($file);
204 160 }
205 161
206 - public function get_upload_url($path = false) {
162 + // get wp upload dir
163 + public function get_wp_upload_dir($key = 'basedir') {
207 164
208 165 $wp_upload_dir = wp_upload_dir();
166 + if (defined('E2PDF_UPLOADS')) {
167 + $siteurl = get_option('siteurl');
168 + $upload_path = trim(get_option('upload_path'));
209 169
170 + if (empty($upload_path) || 'wp-content/uploads' === $upload_path) {
171 + $dir = WP_CONTENT_DIR . '/uploads';
172 + } elseif (0 !== strpos($upload_path, ABSPATH)) {
173 + // $dir is absolute, $upload_path is (maybe) relative to ABSPATH.
174 + $dir = path_join(ABSPATH, $upload_path);
175 + } else {
176 + $dir = $upload_path;
177 + }
178 +
179 + $url = get_option('upload_url_path');
180 + if (!$url) {
181 + if (empty($upload_path) || ('wp-content/uploads' === $upload_path) || ($upload_path == $dir)) {
182 + $url = WP_CONTENT_URL . '/uploads';
183 + } else {
184 + $url = trailingslashit($siteurl) . $upload_path;
185 + }
186 + }
187 +
188 + if (!(is_multisite() && get_site_option('ms_files_rewriting'))) {
189 + $dir = ABSPATH . E2PDF_UPLOADS;
190 + $url = trailingslashit($siteurl) . E2PDF_UPLOADS;
191 + }
192 +
193 + if (is_multisite() && !(is_main_network() && is_main_site() && defined('MULTISITE'))) {
194 + if (!get_site_option('ms_files_rewriting')) {
195 + if (defined('MULTISITE')) {
196 + $ms_dir = '/sites/' . get_current_blog_id();
197 + } else {
198 + $ms_dir = '/' . get_current_blog_id();
199 + }
200 + $dir .= $ms_dir;
201 + $url .= $ms_dir;
202 + } elseif (!ms_is_switched()) {
203 + $dir = ABSPATH . E2PDF_UPLOADS;
204 + $url = trailingslashit($siteurl) . 'files';
205 + }
206 + }
207 +
208 + $basedir = $dir;
209 + $baseurl = $url;
210 + $subdir = '';
211 +
212 + if (get_option('uploads_use_yearmonth_folders')) {
213 + $time = current_time('mysql');
214 + $y = substr($time, 0, 4);
215 + $m = substr($time, 5, 2);
216 + $subdir = "/$y/$m";
217 + }
218 +
219 + $dir .= $subdir;
220 + $url .= $subdir;
221 +
222 + if (!file_exists($basedir)) {
223 + $this->create_dir($basedir, true, false, false);
224 + }
225 +
226 + $wp_upload_dir = [
227 + 'path' => $dir,
228 + 'url' => $url,
229 + 'subdir' => $subdir,
230 + 'basedir' => $basedir,
231 + 'baseurl' => $baseurl,
232 + 'error' => false,
233 + ];
234 + }
235 +
236 + if ($key && isset($wp_upload_dir[$key])) {
237 + return $wp_upload_dir[$key];
238 + } else {
239 + return '';
240 + }
241 + }
242 +
243 + // get upload url
244 + public function get_upload_url($path = false) {
210 245 if ($path) {
211 - return $wp_upload_dir['baseurl'] . "/" . basename(untrailingslashit($this->get('upload_dir'))) . "/" . $path;
246 + return $this->get_wp_upload_dir('baseurl') . '/' . basename(untrailingslashit($this->get('upload_dir'))) . '/' . $path;
212 247 } else {
213 - return $wp_upload_dir['baseurl'] . "/" . basename(untrailingslashit($this->get('upload_dir')));
248 + return $this->get_wp_upload_dir('baseurl') . '/' . basename(untrailingslashit($this->get('upload_dir')));
214 249 }
215 250 }
216 251
252 + // is multidimensional array
253 + public function is_multidimensional($a) {
254 + if (is_array($a)) {
255 + foreach ($a as $v) {
256 + if (is_array($v) || is_object($v)) {
257 + return true;
258 + }
259 + }
260 + }
261 + return false;
262 + }
263 +
264 + // get caps
265 + public function get_caps() {
266 + $caps = [
267 + 'e2pdf' => [
268 + 'name' => __('Create PDF', 'e2pdf'),
269 + 'cap' => 'e2pdf',
270 + ],
271 + 'e2pdf_templates' => [
272 + 'name' => __('Templates', 'e2pdf'),
273 + 'cap' => 'e2pdf_templates',
274 + ],
275 + 'e2pdf_settings' => [
276 + 'name' => __('Settings', 'e2pdf'),
277 + 'cap' => 'e2pdf_settings',
278 + ],
279 + 'e2pdf_license' => [
280 + 'name' => __('License', 'e2pdf'),
281 + 'cap' => 'e2pdf_license',
282 + ],
283 + 'e2pdf_debug' => [
284 + 'name' => __('Debug', 'e2pdf'),
285 + 'cap' => 'e2pdf_debug',
286 + ],
287 + ];
288 + return $caps;
289 + }
290 +
291 + // load
292 + public function load($helper) {
293 + $model = null;
294 + $class = 'Helper_E2pdf_' . ucfirst($helper);
295 + if (class_exists($class)) {
296 + if (!$this->get($class)) {
297 + $this->set($class, new $class());
298 + }
299 + $model = $this->get($class);
300 + }
301 + return $model;
302 + }
303 +
304 + // get frontend site url
305 + public function get_frontend_site_url() {
306 + $url_format = get_option('e2pdf_url_format', 'auto');
307 + if ($url_format === 'auto') {
308 + if (function_exists('pll_home_url')) {
309 + return pll_home_url();
310 + }
311 + if (function_exists('icl_t') && !defined('POLYLANG_VERSION')) {
312 + return home_url('/');
313 + }
314 + }
315 + return $url_format === 'home' ? home_url('/') : site_url('/');
316 + }
317 +
318 + // get frontend pdf url
319 + public function get_frontend_pdf_url($url_data = [], $site_url = false, $filters = []) {
320 +
321 + if ($site_url === false) {
322 + $site_url = $this->get_frontend_site_url();
323 + }
324 +
325 + $site_url = apply_filters('e2pdf_helper_get_frontend_pdf_url_pre_site_url', $site_url);
326 +
327 + if (!empty($filters)) {
328 + foreach ($filters as $filter) {
329 + $site_url = apply_filters($filter, $site_url);
330 + }
331 + }
332 +
333 + $url_query = wp_parse_url($site_url, PHP_URL_QUERY);
334 + if ($url_query) {
335 + $site_url = str_replace('?' . $url_query, '', $site_url);
336 + $queries = explode('&', $url_query);
337 + foreach ($queries as $query) {
338 + $q = explode('=', $query);
339 + if (isset($q[0]) && isset($q[1])) {
340 + $url_data[$q[0]] = $q[1];
341 + } elseif (isset($q[0])) {
342 + $url_data[$q[0]] = '';
343 + }
344 + }
345 + }
346 +
347 + if (get_option('e2pdf_mod_rewrite', '0')) {
348 + $site_url = rtrim($site_url, '/') . '/' . get_option('e2pdf_mod_rewrite_url', 'e2pdf/%uid%/');
349 + if (isset($url_data['uid'])) {
350 + $site_url = str_replace('%uid%', $url_data['uid'], $site_url);
351 + unset($url_data['uid']);
352 + } else {
353 + $site_url = str_replace('%uid%', '', $site_url);
354 + }
355 + if (isset($url_data['page'])) {
356 + unset($url_data['page']);
357 + }
358 + }
359 +
360 + if (isset($url_data['download_name'])) {
361 + $site_url = trailingslashit($site_url) . $url_data['download_name'];
362 + unset($url_data['download_name']);
363 + }
364 +
365 + $site_url = apply_filters('e2pdf_helper_get_frontend_pdf_url_site_url', $site_url);
366 + $url_data = apply_filters('e2pdf_helper_get_frontend_pdf_url_url_data', $url_data);
367 +
368 + $url = add_query_arg($url_data, $site_url);
369 +
370 + return $this->load('translator')->translate_url($url);
371 + }
372 +
373 + // get frontend local pdf url
374 + public function get_frontend_local_pdf_url($pdf, $site_url = false, $filters = []) {
375 + if ($site_url === false) {
376 + $site_url = $this->get_frontend_site_url();
377 + }
378 +
379 + $site_url = apply_filters('e2pdf_helper_get_frontend_pdf_url_pre_site_url', $site_url);
380 +
381 + if (!empty($filters)) {
382 + foreach ($filters as $filter) {
383 + $site_url = apply_filters($filter, $site_url);
384 + }
385 + }
386 + return $site_url . str_replace(ABSPATH, '', $pdf);
387 + }
388 +
389 + // get site url
390 + public function get_site_url() {
391 + $site_url = false;
392 + if (class_exists('SitePress')) {
393 + $settings = get_option('icl_sitepress_settings');
394 + if (isset($settings['language_negotiation_type']) && $settings['language_negotiation_type'] == '2') {
395 + global $wpdb;
396 + $site_url = $wpdb->get_var($wpdb->prepare('SELECT option_value FROM `' . $wpdb->options . '` WHERE option_name = %s LIMIT 1', 'siteurl'));
397 + }
398 + }
399 + if (function_exists('pll_home_url')) {
400 + $settings = get_option('polylang');
401 + if (isset($settings['force_lang']) && ($settings['force_lang'] == '2' || $settings['force_lang'] == '3')) {
402 + global $wpdb;
403 + $site_url = $wpdb->get_var($wpdb->prepare('SELECT option_value FROM `' . $wpdb->options . '` WHERE option_name = %s LIMIT 1', 'siteurl'));
404 + }
405 + }
406 + if (!$site_url) {
407 + $site_url = site_url();
408 + }
409 + return $site_url;
410 + }
411 +
412 + // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodDoubleUnderscore
413 + public function __return_true() {
414 + return true;
415 + }
416 +
417 + // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodDoubleUnderscore
418 + public function __return_false() {
419 + return false;
420 + }
421 +
422 + // set time limit
423 + public function set_time_limit($timeout = 0) {
424 + $timeout = (int) $timeout;
425 + if (function_exists('set_time_limit') && is_callable('set_time_limit')) {
426 + // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
427 + @set_time_limit($timeout);
428 + } elseif (function_exists('ini_set') && is_callable('ini_set')) {
429 + // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged, WordPress.PHP.IniSet.max_execution_time_Blacklisted
430 + @ini_set('max_execution_time', $timeout);
431 + }
432 + }
433 +
434 + // shortcodes
435 + public function shortcodes() {
436 + return [
437 + 'e2pdf-download',
438 + 'e2pdf-view',
439 + 'e2pdf-save',
440 + 'e2pdf-zapier',
441 + 'e2pdf-adobesign',
442 + 'e2pdf-attachment',
443 + 'e2pdf-vc-download',
444 + 'e2pdf-vc-download-item',
445 + 'e2pdf-vc-view',
446 + 'e2pdf-vc-view-item',
447 + 'e2pdf-format-number',
448 + 'e2pdf-format-date',
449 + 'e2pdf-format-output',
450 + 'e2pdf-frm-field-value',
451 + 'e2pdf-translate',
452 + 'e2pdf-math',
453 + 'e2pdf-content',
454 + 'e2pdf-exclude',
455 + 'e2pdf-filter',
456 + 'e2pdf-page-number',
457 + 'e2pdf-page-total',
458 + 'e2pdf-user',
459 + 'e2pdf-wp',
460 + 'e2pdf-wp-term',
461 + 'e2pdf-wp-posts',
462 + 'e2pdf-wp-users',
463 + 'e2pdf-wc-product',
464 + 'e2pdf-wc-order',
465 + 'e2pdf-wc-cart',
466 + 'e2pdf-wc-customer',
467 + 'e2pdf-foreach',
468 + 'e2pdf-acf-repeater',
469 + ];
470 + }
217 471 }