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

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