PluginProbe
E2Pdf – Export Pdf Tool for WordPress / 1.08.06
E2Pdf – Export Pdf Tool for WordPress v1.08.06
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.06, at classes/helper/e2pdf-view.php

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