PluginProbe
E2Pdf – Export Pdf Tool for WordPress / 1.08.00
E2Pdf – Export Pdf Tool for WordPress v1.08.00
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-view.php

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

380 lines 10.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 if (function_exists('wp_json_encode')) {
210 echo wp_json_encode($data, JSON_FORCE_OBJECT);
211 } else {
212 echo json_encode($data, JSON_FORCE_OBJECT);
213 }
214 wp_die();
215 }
216
217 /**
218 * Force close browser tab from PHP
219 */
220 public function close_tab_response() {
221 echo "
222 <script>
223 self.close();
224 </script>
225 ";
226 }
227
228 /**
229 * Force Download response
230 *
231 * @param string $format - Format of file
232 * @param string $file - Base64 Encoded File
233 * @param string $name - Name of file when download
234 * @param string $disposition - Disposition of file inline|attachment
235 *
236 * @return string
237 */
238 public function download_response($format = 'pdf', $file, $name = false, $disposition = false) {
239 ob_end_clean();
240
241 if (!$name) {
242 $name = time();
243 }
244
245 $name = $this->helper->load('convert')->to_file_name($name);
246
247 header('Content-Transfer-Encoding: binary');
248 header('Content-Length: ' . strlen(base64_decode($file)));
249 switch ($format) {
250 case 'pdf':
251 if (!$disposition) {
252 $disposition = 'inline';
253 }
254 header("Content-Disposition: {$disposition}; filename=\"{$name}.pdf\"");
255 header("Content-type: application/pdf");
256 break;
257 case 'zip':
258 if (!$disposition) {
259 $disposition = 'attachment';
260 }
261 header("Content-Disposition: {$disposition}; filename=\"{$name}.zip\"");
262 header('Content-type: application/zip');
263 break;
264
265 case 'xml':
266 if (!$disposition) {
267 $disposition = 'attachment';
268 }
269 header("Content-Disposition: {$disposition}; filename=\"{$name}.xml\"");
270 header('Content-type: text/xml');
271 break;
272
273 case 'txt':
274 if (!$disposition) {
275 $disposition = 'attachment';
276 }
277 header("Content-Disposition: {$disposition}; filename=\"{$name}.txt\"");
278 header('Content-type: text/html');
279 break;
280
281 case 'php':
282 if (!$disposition) {
283 $disposition = 'attachment';
284 }
285 header("Content-Disposition: {$disposition}; filename=\"{$name}.php\"");
286 header('Content-type: text/html');
287 break;
288
289 default:
290 break;
291 }
292 echo base64_decode($file);
293 die();
294 }
295
296 /**
297 * Check if exists in array
298 *
299 * @param string $value - Array Key
300 * @param mixed $compare
301 * @param array $data - Array of data
302 *
303 * @return bool
304 */
305 public function exist($value, $compare = false, $data = array()) {
306 if (isset($data[$value]) && $value === $compare) {
307 return true;
308 } else {
309 return false;
310 }
311 }
312
313 /**
314 * Force Error
315 *
316 * @param string $code - Error Code
317 */
318 public function error($code = '404') {
319 global $wp_query;
320 if ($code === '404') {
321 $wp_query->set_404();
322 status_header(404);
323 $this->redirect(site_url('wp-admin'));
324 wp_die('404');
325 }
326 }
327
328 /**
329 * Set var available to view template
330 *
331 * @param string $key - Key of value
332 * @param mixed $value - Value
333 */
334 public function view($key, $value) {
335 $this->view->$key = $value;
336 }
337
338 /**
339 * Check if Array/Object Empty
340 *
341 * @param mixed $data - Object/Array to check
342 *
343 * @return bool
344 */
345 public function is_empty($data) {
346 if (is_object($data) || is_array($data)) {
347
348 if (is_object($data)) {
349 $data = (array) $data;
350 }
351 if (count($data) > 0) {
352 return false;
353 }
354 }
355
356 return true;
357 }
358
359 /**
360 * Verify nonce
361 *
362 * @param string $nonce - Nonce value
363 * @param string $key - Nonce Key
364 */
365 public function check_nonce($nonce, $key) {
366 $message = __('Security Issue: Nonce Incorrect', 'e2pdf');
367 if ($key === 'e2pdf_ajax') {
368 if (!wp_verify_nonce($nonce, $key)) {
369 $data['error'] = $message;
370 $this->json_response($data);
371 }
372 } else {
373 if (!wp_verify_nonce($nonce, $key)) {
374 die('<div id="message" class="error e2pdf-notice">' . $message . '</div>');
375 }
376 }
377 }
378
379 }
380