| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* E2pdf View 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_View { |
| 17 |
|
| 18 |
protected static $instance = null; |
| 19 |
private $view_dir; |
| 20 |
public $uri; |
| 21 |
public $page; |
| 22 |
public $controller; |
| 23 |
public $helper; |
| 24 |
public $notification; |
| 25 |
public $tpl; |
| 26 |
public $tpl_page; |
| 27 |
public $view = null; |
| 28 |
public $tpl_args = null; |
| 29 |
public $get = null; |
| 30 |
public $post = null; |
| 31 |
public $files = null; |
| 32 |
|
| 33 |
public static function instance() { |
| 34 |
if (!isset(self::$instance)) { |
| 35 |
self::$instance = new self(); |
| 36 |
} |
| 37 |
return self::$instance; |
| 38 |
} |
| 39 |
|
| 40 |
public function __construct() { |
| 41 |
$this->view = new stdClass(); |
| 42 |
$this->uri = home_url(add_query_arg(null, null)); |
| 43 |
$this->get = new Helper_E2pdf_Get($this->uri); |
| 44 |
$this->post = new Helper_E2pdf_Post(); |
| 45 |
$this->files = new Helper_E2pdf_Files(); |
| 46 |
$this->page = $this->get->get_page(); |
| 47 |
$this->helper = Helper_E2pdf_Helper::instance(); |
| 48 |
$this->notification = new Model_E2pdf_Notification(); |
| 49 |
$this->view_dir = $this->helper->get('plugin_dir') . 'classes/view/'; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Get Tpl Value |
| 54 |
* |
| 55 |
* @param string $key - Tpl key |
| 56 |
* |
| 57 |
* @return mixed - Tpl value |
| 58 |
*/ |
| 59 |
public function render($type, $element, $args = array()) { |
| 60 |
$this->tpl_args = new Helper_E2pdf_Tplargs($args); |
| 61 |
$this->tpl = $this->view_dir . $type . '/' . $element . '.php'; |
| 62 |
if (file_exists($this->tpl)) { |
| 63 |
include($this->tpl); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Get Tpl Value |
| 69 |
* |
| 70 |
* @param string $key - Tpl key |
| 71 |
* |
| 72 |
* @return mixed - Tpl value |
| 73 |
*/ |
| 74 |
public function render_metabox($post, $metabox) { |
| 75 |
$this->render('metaboxes', $metabox['args']['tpl']); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Get Tpl Value |
| 80 |
* |
| 81 |
* @param string $key - Tpl key |
| 82 |
* |
| 83 |
* @return mixed - Tpl value |
| 84 |
*/ |
| 85 |
public function render_page() { |
| 86 |
|
| 87 |
if (get_option('e2pdf_developer')) { |
| 88 |
$model_e2pdf_options = new Model_E2pdf_Options(); |
| 89 |
if (in_array($this->helper->get_ip(), $model_e2pdf_options->get_option('e2pdf_developer_ips'))) { |
| 90 |
error_reporting(E_ALL); |
| 91 |
ini_set('display_startup_errors', 1); |
| 92 |
ini_set('display_errors', 1); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
$this->tpl_page = $this->view_dir . 'page-' . $this->page . '.php'; |
| 98 |
$controller = $this->page_to_controller($this->page); |
| 99 |
if (class_exists($controller)) { |
| 100 |
$this->controller = new $controller(); |
| 101 |
if ($this->get->get('action')) { |
| 102 |
$method = $this->get->get('action') . '_action'; |
| 103 |
} else { |
| 104 |
$method = 'index_action'; |
| 105 |
} |
| 106 |
if (method_exists($this->controller, $method)) { |
| 107 |
$this->controller->$method(); |
| 108 |
$this->view = $this->controller->view; |
| 109 |
} else { |
| 110 |
$this->error('404'); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
if (file_exists($this->tpl_page)) { |
| 115 |
include($this->tpl_page); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
public function render_frontend_page() { |
| 120 |
if ($this->page == 'e2pdf-download') { |
| 121 |
$this->tpl_page = $this->view_dir . '/frontend/page-' . $this->page . '.php'; |
| 122 |
$controller = $this->page_to_controller($this->page, true); |
| 123 |
if (class_exists($controller)) { |
| 124 |
$this->controller = new $controller(); |
| 125 |
if ($this->get->get('action')) { |
| 126 |
$method = $this->get->get('action') . '_action'; |
| 127 |
} else { |
| 128 |
$method = 'index_action'; |
| 129 |
} |
| 130 |
if (method_exists($this->controller, $method)) { |
| 131 |
$this->controller->$method(); |
| 132 |
} else { |
| 133 |
$this->error('404'); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
if (file_exists($this->tpl_page)) { |
| 138 |
include($this->tpl_page); |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Get Tpl Value |
| 145 |
* |
| 146 |
* @param string $key - Tpl key |
| 147 |
* |
| 148 |
* @return mixed - Tpl value |
| 149 |
*/ |
| 150 |
public function page_to_controller($page, $frontend = false) { |
| 151 |
|
| 152 |
$controller = array( |
| 153 |
'Controller' |
| 154 |
); |
| 155 |
|
| 156 |
if ($frontend) { |
| 157 |
$controller[] = 'Frontend'; |
| 158 |
} |
| 159 |
|
| 160 |
$controller = array_merge($controller, explode('-', $page)); |
| 161 |
$controller = array_map('ucfirst', $controller); |
| 162 |
|
| 163 |
return implode("_", $controller); |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Redirect to page |
| 168 |
* |
| 169 |
* @param string $location - Full url where to redirect |
| 170 |
*/ |
| 171 |
public function redirect($location) { |
| 172 |
if (headers_sent()) { |
| 173 |
die("<script>window.location='{$location}';</script>"); |
| 174 |
} else { |
| 175 |
wp_redirect($location); |
| 176 |
exit(); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Add notification |
| 182 |
* |
| 183 |
* @param string $type - Type of notification error|update |
| 184 |
* @param string $text - Text of notification |
| 185 |
*/ |
| 186 |
public function add_notification($type, $text) { |
| 187 |
$this->notification->add_notification($type, $text); |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* Get notifications |
| 192 |
* |
| 193 |
* @return array - List of notifications |
| 194 |
*/ |
| 195 |
public function get_notifications() { |
| 196 |
$notifications = $this->notification->get_notifications(); |
| 197 |
return $notifications; |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Force Json response |
| 202 |
* |
| 203 |
* @param array $data - Array of data |
| 204 |
* |
| 205 |
* @return json |
| 206 |
*/ |
| 207 |
public function json_response($data = array()) { |
| 208 |
@header('Content-Type: application/json; charset=' . get_option('blog_charset')); |
| 209 |
echo wp_json_encode($data, JSON_FORCE_OBJECT); |
| 210 |
wp_die(); |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* Force close browser tab from PHP |
| 215 |
*/ |
| 216 |
public function close_tab_response() { |
| 217 |
echo " |
| 218 |
<script> |
| 219 |
self.close(); |
| 220 |
</script> |
| 221 |
"; |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* Force Download response |
| 226 |
* |
| 227 |
* @param string $format - Format of file |
| 228 |
* @param string $file - Base64 Encoded File |
| 229 |
* @param string $name - Name of file when download |
| 230 |
* @param string $disposition - Disposition of file inline|attachment |
| 231 |
* |
| 232 |
* @return string |
| 233 |
*/ |
| 234 |
public function download_response($format = 'pdf', $file, $name = false, $disposition = false) { |
| 235 |
ob_end_clean(); |
| 236 |
|
| 237 |
if (!$name) { |
| 238 |
$name = time(); |
| 239 |
} |
| 240 |
|
| 241 |
header('Content-Transfer-Encoding: binary'); |
| 242 |
header('Content-Length: ' . strlen(base64_decode($file))); |
| 243 |
switch ($format) { |
| 244 |
case 'pdf': |
| 245 |
if (!$disposition) { |
| 246 |
$disposition = 'inline'; |
| 247 |
} |
| 248 |
header("Content-Disposition: {$disposition}; filename=\"{$name}.pdf\""); |
| 249 |
header("Content-type: application/pdf"); |
| 250 |
break; |
| 251 |
case 'zip': |
| 252 |
if (!$disposition) { |
| 253 |
$disposition = 'attachment'; |
| 254 |
} |
| 255 |
header("Content-Disposition: {$disposition}; filename=\"{$name}.zip\""); |
| 256 |
header('Content-type: application/zip'); |
| 257 |
break; |
| 258 |
|
| 259 |
case 'xml': |
| 260 |
if (!$disposition) { |
| 261 |
$disposition = 'attachment'; |
| 262 |
} |
| 263 |
header("Content-Disposition: {$disposition}; filename=\"{$name}.xml\""); |
| 264 |
header('Content-type: text/xml'); |
| 265 |
break; |
| 266 |
|
| 267 |
case 'txt': |
| 268 |
if (!$disposition) { |
| 269 |
$disposition = 'attachment'; |
| 270 |
} |
| 271 |
header("Content-Disposition: {$disposition}; filename=\"{$name}.txt\""); |
| 272 |
header('Content-type: text/html'); |
| 273 |
break; |
| 274 |
|
| 275 |
case 'php': |
| 276 |
if (!$disposition) { |
| 277 |
$disposition = 'attachment'; |
| 278 |
} |
| 279 |
header("Content-Disposition: {$disposition}; filename=\"{$name}.php\""); |
| 280 |
header('Content-type: text/html'); |
| 281 |
break; |
| 282 |
|
| 283 |
default: |
| 284 |
break; |
| 285 |
} |
| 286 |
echo base64_decode($file); |
| 287 |
die(); |
| 288 |
} |
| 289 |
|
| 290 |
/** |
| 291 |
* Check if exists in array |
| 292 |
* |
| 293 |
* @param string $value - Array Key |
| 294 |
* @param mixed $compare |
| 295 |
* @param array $data - Array of data |
| 296 |
* |
| 297 |
* @return bool |
| 298 |
*/ |
| 299 |
public function exist($value, $compare = false, $data = array()) { |
| 300 |
if (isset($data[$value]) && $value === $compare) { |
| 301 |
return true; |
| 302 |
} else { |
| 303 |
return false; |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
/** |
| 308 |
* Force Error |
| 309 |
* |
| 310 |
* @param string $code - Error Code |
| 311 |
*/ |
| 312 |
public function error($code = '404') { |
| 313 |
global $wp_query; |
| 314 |
if ($code === '404') { |
| 315 |
$wp_query->set_404(); |
| 316 |
status_header(404); |
| 317 |
$this->redirect(site_url('wp-admin')); |
| 318 |
wp_die('404'); |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
/** |
| 323 |
* Set var available to view template |
| 324 |
* |
| 325 |
* @param string $key - Key of value |
| 326 |
* @param mixed $value - Value |
| 327 |
*/ |
| 328 |
public function view($key, $value) { |
| 329 |
$this->view->$key = $value; |
| 330 |
} |
| 331 |
|
| 332 |
/** |
| 333 |
* Check if Array/Object Empty |
| 334 |
* |
| 335 |
* @param mixed $data - Object/Array to check |
| 336 |
* |
| 337 |
* @return bool |
| 338 |
*/ |
| 339 |
public function is_empty($data) { |
| 340 |
if (is_object($data) || is_array($data)) { |
| 341 |
|
| 342 |
if (is_object($data)) { |
| 343 |
$data = (array) $data; |
| 344 |
} |
| 345 |
if (count($data) > 0) { |
| 346 |
return false; |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
return true; |
| 351 |
} |
| 352 |
|
| 353 |
/** |
| 354 |
* Verify nonce |
| 355 |
* |
| 356 |
* @param string $nonce - Nonce value |
| 357 |
* @param string $key - Nonce Key |
| 358 |
*/ |
| 359 |
public function check_nonce($nonce, $key) { |
| 360 |
$message = __('Security Issue: Nonce Incorrect', 'e2pdf'); |
| 361 |
if ($key === 'e2pdf_ajax') { |
| 362 |
if (!wp_verify_nonce($nonce, $key)) { |
| 363 |
$data['error'] = $message; |
| 364 |
$this->json_response($data); |
| 365 |
} |
| 366 |
} else { |
| 367 |
if (!wp_verify_nonce($nonce, $key)) { |
| 368 |
die('<div id="message" class="error e2pdf-notice">' . $message . '</div>'); |
| 369 |
} |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
} |
| 374 |
|