| 1 |
<?php |
| 2 |
|
| 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 |
| 11 |
*/ |
| 12 |
if (!defined('ABSPATH')) { |
| 13 |
die('Access denied.'); |
| 14 |
} |
| 15 |
|
| 16 |
class Helper_E2pdf_Helper { |
| 17 |
|
| 18 |
protected static $instance = NULL; |
| 19 |
private $helper; |
| 20 |
|
| 21 |
const CHMOD_DIR = 0755; |
| 22 |
const CHMOD_FILE = 0644; |
| 23 |
|
| 24 |
public static function instance() { |
| 25 |
if (!isset(self::$instance)) { |
| 26 |
self::$instance = new self(); |
| 27 |
} |
| 28 |
return self::$instance; |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Set option by Key |
| 33 |
* |
| 34 |
* @param string $key - Key of option |
| 35 |
* @param mixed $value - Value of option |
| 36 |
*/ |
| 37 |
public function set($key, $value) { |
| 38 |
if (!$this->helper) { |
| 39 |
$this->helper = new stdClass(); |
| 40 |
} |
| 41 |
$this->helper->$key = $value; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Add value to option by Key |
| 46 |
* |
| 47 |
* @param string $key - Key of option |
| 48 |
* @param mixed $value - Value of option |
| 49 |
*/ |
| 50 |
public function add($key, $value) { |
| 51 |
if (!$this->helper) { |
| 52 |
$this->helper = new stdClass(); |
| 53 |
} |
| 54 |
|
| 55 |
if (isset($this->helper->$key)) { |
| 56 |
if (is_array($this->helper->$key)) { |
| 57 |
array_push($this->helper->$key, $value); |
| 58 |
} |
| 59 |
} else { |
| 60 |
$this->helper->$key = array(); |
| 61 |
array_push($this->helper->$key, $value); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Unset option |
| 67 |
* |
| 68 |
* @param string $key - Key of option |
| 69 |
*/ |
| 70 |
public function deset($key) { |
| 71 |
if (isset($this->helper->$key)) { |
| 72 |
unset($this->helper->$key); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Set option |
| 78 |
* |
| 79 |
* @param string $key - Key of option |
| 80 |
* |
| 81 |
* @return mixed - Get value of option by Key |
| 82 |
*/ |
| 83 |
public function get($key) { |
| 84 |
if (isset($this->helper->$key)) { |
| 85 |
return $this->helper->$key; |
| 86 |
} else { |
| 87 |
return ''; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Get url path |
| 93 |
* |
| 94 |
* @param string $url - Url path |
| 95 |
* |
| 96 |
* @return string - Url path |
| 97 |
*/ |
| 98 |
public function get_url_path($url) { |
| 99 |
return plugins_url($url, $this->get('plugin_file_path')); |
| 100 |
} |
| 101 |
|
| 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?') { |
| 111 |
$url = $prefix . http_build_query($data); |
| 112 |
return admin_url($url); |
| 113 |
} |
| 114 |
|
| 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 |
*/ |
| 169 |
public function delete_dir($dir) { |
| 170 |
if (!is_dir($dir)) { |
| 171 |
return; |
| 172 |
} |
| 173 |
if (substr($dir, strlen($dir) - 1, 1) != '/') { |
| 174 |
$dir .= '/'; |
| 175 |
} |
| 176 |
$files = glob($dir . '*', GLOB_MARK); |
| 177 |
foreach ($files as $file) { |
| 178 |
if (is_dir($file)) { |
| 179 |
$this->delete_dir($file); |
| 180 |
} else { |
| 181 |
unlink($file); |
| 182 |
} |
| 183 |
} |
| 184 |
rmdir($dir); |
| 185 |
} |
| 186 |
|
| 187 |
public function create_dir($dir = false) { |
| 188 |
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?>"); |
| 192 |
} |
| 193 |
} |
| 194 |
return is_dir($dir); |
| 195 |
} |
| 196 |
|
| 197 |
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); |
| 201 |
} |
| 202 |
} |
| 203 |
return is_file($file); |
| 204 |
} |
| 205 |
|
| 206 |
public function get_upload_url($path = false) { |
| 207 |
|
| 208 |
$wp_upload_dir = wp_upload_dir(); |
| 209 |
|
| 210 |
if ($path) { |
| 211 |
return $wp_upload_dir['baseurl'] . "/" . basename(untrailingslashit($this->get('upload_dir'))) . "/" . $path; |
| 212 |
} else { |
| 213 |
return $wp_upload_dir['baseurl'] . "/" . basename(untrailingslashit($this->get('upload_dir'))); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
} |
| 218 |
|