PluginProbe
E2Pdf – Export Pdf Tool for WordPress / 1.32.22
E2Pdf – Export Pdf Tool for WordPress v1.32.22
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 / controller / e2pdf-templates.php

e2pdf-templates.php in E2Pdf – Export Pdf Tool for WordPress 1.32.22, at classes/controller/e2pdf-templates.php

2,618 lines 119.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 Templates Controller
5 * @copyright Copyright 2017 https://e2pdf.com
6 * @license GPLv3
7 * @version 1
8 * @link https://e2pdf.com
9 * @since 0.00.01
10 */
11 if (!defined('ABSPATH')) {
12 die('Access denied.');
13 }
14
15 class Controller_E2pdf_Templates extends Helper_E2pdf_View {
16
17 /**
18 * @url admin.php?page=e2pdf-templates
19 */
20 public function index_action() {
21 if ($this->post->get('screenoptionnonce')) {
22 if (wp_verify_nonce($this->post->get('screenoptionnonce'), 'screen-options-nonce')) {
23 $this->screen_action();
24 } else {
25 wp_die($this->message('wp_verify_nonce_error'));
26 }
27 } elseif ($this->post->get('_wpnonce')) {
28 if (wp_verify_nonce($this->post->get('_wpnonce'), 'e2pdf_templates')) {
29 if ($this->post->get('action') == '-1' && $this->post->get('action2') == '-1') {
30 $redirect_url = [
31 'page' => 'e2pdf-templates',
32 'status' => $this->get->get('status'),
33 'orderby' => $this->get->get('orderby'),
34 'order' => $this->get->get('order'),
35 ];
36 if ($this->post->get('s')) {
37 $redirect_url['s'] = $this->post->get('s');
38 }
39 $this->redirect(
40 $this->helper->get_url($redirect_url)
41 );
42 } else {
43 $action = $this->post->get('action') != '-1' ? $this->post->get('action') : $this->post->get('action2');
44 if ($this->post->get('post')) {
45 if ($action == 'trash') {
46 foreach ($this->post->get('post') as $key => $value) {
47 $this->trash_template($value);
48 }
49 $this->add_notification('update', sprintf(__('Templates Trashed: %d', 'e2pdf'), count($this->post->get('post'))));
50 } elseif ($action == 'activate') {
51 foreach ($this->post->get('post') as $key => $value) {
52 $this->activate_template($value);
53 }
54 $this->add_notification('update', sprintf(__('Templates Activated: %d', 'e2pdf'), count($this->post->get('post'))));
55 } elseif ($action == 'deactivate') {
56 foreach ($this->post->get('post') as $key => $value) {
57 $this->deactivate_template($value);
58 }
59 $this->add_notification('update', sprintf(__('Templates Deactivated: %d', 'e2pdf'), count($this->post->get('post'))));
60 } elseif ($action == 'restore') {
61 foreach ($this->post->get('post') as $key => $value) {
62 $this->restore_template($value);
63 }
64 $this->add_notification('update', sprintf(__('Templates Restored: %d', 'e2pdf'), count($this->post->get('post'))));
65 } elseif ($action == 'delete') {
66 foreach ($this->post->get('post') as $key => $value) {
67 $this->delete_template($value);
68 }
69 $this->add_notification('update', sprintf(__('Templates Deleted: %d', 'e2pdf'), count($this->post->get('post'))));
70 }
71 $this->helper->load('license')->load();
72 }
73 }
74 } else {
75 wp_die($this->message('wp_verify_nonce_error'));
76 }
77 }
78 }
79
80 /**
81 * @url admin.php?page=e2pdf-templates&action=view&id=$id
82 * @return file
83 */
84 public function view_action() {
85 if ($this->get->get('id')) {
86 $template = new Model_E2pdf_Template();
87 if ($template->load($this->get->get('id'))) {
88 $pages = $template->get('pages');
89 foreach ($pages as $page_key => $page) {
90 if (!empty($page['elements'])) {
91 foreach ($page['elements'] as $element_key => $element) {
92 if (isset($element['type'])) {
93 switch ($element['type']) {
94 case 'e2pdf-image':
95 if (isset($element['properties']['quality']) && $element['properties']['quality']) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedIf
96 } else {
97 $element['properties']['quality'] = $template->get('optimization');
98 $pages[$page_key]['elements'][$element_key]['properties']['quality'] = $template->get('optimization');
99 }
100 if ($image = $this->helper->load('image')->get_image($element['value'], null, $element)) {
101 $pages[$page_key]['elements'][$element_key]['value'] = $image;
102 } else {
103 $pages[$page_key]['elements'][$element_key]['value'] = $this->helper->load('image')->get_image($this->helper->get('plugin_dir') . 'img/upload.svg');
104 $pages[$page_key]['elements'][$element_key]['properties']['preview'] = '1';
105 }
106 break;
107 case 'e2pdf-signature':
108 $esig = isset($element['properties']['esig']) && $element['properties']['esig'] ? true : false;
109 if (!$esig) {
110 if ($image = $this->helper->load('image')->get_image($element['value'])) {
111 $pages[$page_key]['elements'][$element_key]['value'] = $image;
112 } else {
113 $pages[$page_key]['elements'][$element_key]['value'] = $this->helper->load('image')->get_image($this->helper->get('plugin_dir') . 'img/signature.svg');
114 $pages[$page_key]['elements'][$element_key]['properties']['preview'] = '1';
115 }
116 }
117 break;
118 case 'e2pdf-qrcode':
119 $pages[$page_key]['elements'][$element_key]['type'] = 'e2pdf-image';
120 $pages[$page_key]['elements'][$element_key]['value'] = $this->helper->load('image')->get_image($this->helper->get('plugin_dir') . 'img/qrcode.svg');
121 $pages[$page_key]['elements'][$element_key]['properties']['preview'] = '1';
122 break;
123 case 'e2pdf-barcode':
124 $pages[$page_key]['elements'][$element_key]['type'] = 'e2pdf-image';
125 $pages[$page_key]['elements'][$element_key]['value'] = $this->helper->load('image')->get_image($this->helper->get('plugin_dir') . 'img/barcode.svg');
126 $pages[$page_key]['elements'][$element_key]['properties']['preview'] = '1';
127 break;
128 case 'e2pdf-graph':
129 $pages[$page_key]['elements'][$element_key]['type'] = 'e2pdf-image';
130 $pages[$page_key]['elements'][$element_key]['value'] = $this->helper->load('image')->get_image($this->helper->get('plugin_dir') . 'img/graph.svg');
131 $pages[$page_key]['elements'][$element_key]['properties']['preview'] = '1';
132 break;
133 case 'e2pdf-html':
134 if (isset($pages[$page_key]['elements'][$element_key]['properties']['hide_page_if_empty'])) {
135 unset($pages[$page_key]['elements'][$element_key]['properties']['hide_page_if_empty']);
136 }
137 if (isset($pages[$page_key]['elements'][$element_key]['properties']['hide_if_empty'])) {
138 unset($pages[$page_key]['elements'][$element_key]['properties']['hide_if_empty']);
139 }
140 if (isset($element['properties']['nl2br']) && $element['properties']['nl2br']) {
141 $element['value'] = nl2br($element['value']);
142 }
143 if (isset($element['properties']['html_worker']) && $element['properties']['html_worker']) {
144 $element['value'] = $this->helper->load('properties')->html_worker(
145 $element['value']
146 );
147 }
148 $pages[$page_key]['elements'][$element_key]['value'] = $this->helper->load('filter')->filter_html_tags(
149 $element['value']
150 );
151 break;
152 case 'e2pdf-page-number':
153 if (isset($element['properties']['html_worker']) && $element['properties']['html_worker']) {
154 $element['value'] = $this->helper->load('properties')->html_worker(
155 $element['value']
156 );
157 }
158 $pages[$page_key]['elements'][$element_key]['value'] = $this->helper->load('filter')->filter_html_tags(
159 $element['value']
160 );
161 break;
162 default:
163 break;
164 }
165 }
166 }
167 }
168 }
169
170 $template->set('pages', $pages);
171 $request = $template->render();
172
173 if (isset($request['error'])) {
174 $this->add_notification('error', $request['error']);
175 $this->render('blocks', 'notifications');
176 } else {
177 $this->download_response($template->get('format'), $request['file'], $template->get_name(), 'inline');
178 exit;
179 }
180 } else {
181 $this->add_notification('error', __('Something went wrong!', 'e2pdf'));
182 $this->render('blocks', 'notifications');
183 }
184 } else {
185 $this->redirect(
186 $this->helper->get_url(
187 [
188 'page' => 'e2pdf',
189 ]
190 )
191 );
192 }
193 }
194
195 /**
196 * @url admin.php?page=e2pdf-templates&action=preview
197 */
198 public function preview_action() {
199 $preview = array();
200 if ($this->post->get('preview')) {
201 $preview = json_decode(stripslashes($this->post->get('preview')), true);
202 }
203 if (empty($preview)) {
204 $this->close_tab_response();
205 } else {
206 $data = $preview;
207 $pages = $preview['pages'];
208 unset($preview['pages']);
209 if (wp_verify_nonce(isset($preview['_wpnonce']) ? $preview['_wpnonce'] : '', 'e2pdf_templates')) {
210 $template = new Model_E2pdf_Template();
211 foreach ($data as $key => $value) {
212 $template->set($key, $value);
213 }
214 $template->set('uid', $template->get('uid', true));
215 $template->set('activated', $template->get('activated', true));
216
217 $preview_pages = array();
218 foreach ($pages as $page_key => $page) {
219 $new_page = new Model_E2pdf_Page();
220 foreach ($page as $page_set_key => $page_set_value) {
221 $new_page->set($page_set_key, $page_set_value);
222 }
223 $new_page->set('page_id', $page_key);
224
225 $elements = array();
226 foreach ($page['elements'] as $element_key => $element) {
227 if (isset($element['type'])) {
228 switch ($element['type']) {
229 case 'e2pdf-image':
230 if (isset($element['properties']['quality']) && $element['properties']['quality']) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedIf
231 } else {
232 $element['properties']['quality'] = $template->get('optimization');
233 $pages[$page_key]['elements'][$element_key]['properties']['quality'] = $template->get('optimization');
234 }
235 if ($image = $this->helper->load('image')->get_image($element['value'], null, $element)) {
236 $element['value'] = $image;
237 } else {
238 $element['value'] = $this->helper->load('image')->get_image($this->helper->get('plugin_dir') . 'img/upload.svg');
239 $element['properties']['preview'] = '1';
240 }
241 break;
242 case 'e2pdf-signature':
243 $esig = isset($element['properties']['esig']) && $element['properties']['esig'] ? true : false;
244 if (!$esig) {
245 if ($image = $this->helper->load('image')->get_image($element['value'])) {
246 $element['value'] = $image;
247 } else {
248 $element['value'] = $this->helper->load('image')->get_image($this->helper->get('plugin_dir') . 'img/signature.svg');
249 $element['properties']['preview'] = '1';
250 }
251 }
252 break;
253 case 'e2pdf-qrcode':
254 $element['type'] = 'e2pdf-image';
255 $element['value'] = $this->helper->load('image')->get_image($this->helper->get('plugin_dir') . 'img/qrcode.svg');
256 $element['properties']['preview'] = '1';
257 break;
258 case 'e2pdf-barcode':
259 $element['type'] = 'e2pdf-image';
260 $element['value'] = $this->helper->load('image')->get_image($this->helper->get('plugin_dir') . 'img/barcode.svg');
261 $element['properties']['preview'] = '1';
262 break;
263 case 'e2pdf-graph':
264 $element['type'] = 'e2pdf-image';
265 $element['value'] = $this->helper->load('image')->get_image($this->helper->get('plugin_dir') . 'img/graph.svg');
266 $element['properties']['preview'] = '1';
267 break;
268 case 'e2pdf-html':
269 if (isset($element['properties']['hide_page_if_empty'])) {
270 unset($element['properties']['hide_page_if_empty']);
271 }
272 if (isset($element['properties']['hide_if_empty'])) {
273 unset($element['properties']['hide_if_empty']);
274 }
275 if (isset($element['properties']['nl2br']) && $element['properties']['nl2br']) {
276 $element['value'] = nl2br($element['value']);
277 }
278 if (isset($element['properties']['html_worker']) && $element['properties']['html_worker']) {
279 $element['value'] = $this->helper->load('properties')->html_worker(
280 $element['value']
281 );
282 }
283 $element['value'] = $this->helper->load('filter')->filter_html_tags(
284 $element['value']
285 );
286 break;
287 case 'e2pdf-page-number':
288 $element['value'] = str_replace(array('[e2pdf-page-number]', '[e2pdf-page-total]'), array('e2pdf-page-number', 'e2pdf-page-total'), $element['value']);
289 if (isset($element['properties']['html_worker']) && $element['properties']['html_worker']) {
290 $element['value'] = $this->helper->load('properties')->html_worker(
291 $element['value']
292 );
293 }
294 $element['value'] = $this->helper->load('filter')->filter_html_tags(
295 $element['value']
296 );
297 break;
298 default:
299 break;
300 }
301 }
302
303 $new_element = new Model_E2pdf_Element();
304 foreach ($element as $element_set_key => $element_set_value) {
305 $new_element->set($element_set_key, $element_set_value);
306 }
307
308 if (isset($element['properties'])) {
309 $element_properties = $element['properties'];
310 } else {
311 $element_properties = array();
312 }
313 $new_element->set('properties', $element_properties);
314 $elements[] = $new_element->get_element();
315 }
316
317 $new_page->set('elements', $elements);
318 $preview_pages[] = $new_page->get_page();
319 }
320
321 $template->set('pages', $preview_pages);
322 $template->load_fonts();
323
324 $type = isset($_GET['type']) ? sanitize_text_field(wp_unslash($_GET['type'])) : false;
325 if ($type == 'php') {
326 $request = $template->render('php');
327 if (isset($request['error'])) {
328 $this->add_notification('error', $request['error']);
329 $this->render('blocks', 'notifications');
330 } else {
331 $this->download_response('php', $request['file'], 'template');
332 exit;
333 }
334 } else {
335 $request = $template->render();
336 if (isset($request['error'])) {
337 $this->add_notification('error', $request['error']);
338 $this->render('blocks', 'notifications');
339 } else {
340 $this->download_response($template->get('format'), $request['file'], $template->get_name(), 'inline', false, true);
341 exit;
342 }
343 }
344 } else {
345 wp_die($this->message('wp_verify_nonce_error'));
346 }
347 }
348 }
349
350 /**
351 * @url admin.php?page=e2pdf-templates&action=create
352 */
353 public function create_action() {
354 $template = new Model_E2pdf_Template();
355 $this->load_metaboxes();
356 $this->load_scripts();
357 $this->load_styles();
358 $this->view('license', $this->helper->get('license'));
359 $this->view('template', $template);
360 }
361
362 /**
363 * @url admin.php?page=e2pdf-templates&action=edit&id=$id
364 */
365 public function edit_action() {
366 if ($this->get->get('id')) {
367 $template = new Model_E2pdf_Template();
368 $revision_id = (int) $this->get->get('revision_id');
369 if ($template->load($this->get->get('id'), true, $revision_id)) {
370 if (is_array($this->helper->get('license')->get('activated_templates'))) {
371 if ($template->get('activated') && !in_array($template->get('uid'), $this->helper->get('license')->get('activated_templates'))) {
372 $template->deactivate();
373 } elseif (!$template->get('activated') && in_array($template->get('uid'), $this->helper->get('license')->get('activated_templates'))) {
374 $template->activate();
375 }
376 }
377 $this->view('license', $this->helper->get('license'));
378 $this->view('template', $template);
379 $this->load_metaboxes();
380 $this->load_scripts();
381 $this->load_styles($template->get('extension'));
382 } else {
383 $this->add_notification('error', __('Something went wrong!', 'e2pdf'));
384 $this->render('blocks', 'notifications');
385 }
386 } else {
387 $this->redirect(
388 $this->helper->get_url(
389 [
390 'page' => 'e2pdf-templates',
391 ]
392 )
393 );
394 }
395 }
396
397 /**
398 * @url admin.php?page=e2pdf-templates&action=trash&id=$id
399 */
400 public function trash_action() {
401 $notification = '';
402 if (wp_verify_nonce($this->get->get('_wpnonce'), 'e2pdf_templates')) {
403 if ($this->get->get('id')) {
404 $this->trash_template($this->get->get('id'));
405 $notification = $this->add_notification('update', __('Template Trashed', 'e2pdf'));
406 }
407 } else {
408 wp_die($this->message('wp_verify_nonce_error'));
409 }
410 $this->redirect(
411 $this->helper->get_url(
412 [
413 'page' => 'e2pdf-templates',
414 'status' => $this->get->get('status'),
415 'orderby' => $this->get->get('orderby'),
416 'order' => $this->get->get('order'),
417 's' => $this->get->get('s'),
418 'notification' => $notification,
419 ]
420 )
421 );
422 }
423
424 /**
425 * @url admin.php?page=e2pdf-templates&action=restore&id=$id&status=$status
426 */
427 public function restore_action() {
428 $notification = '';
429 if (wp_verify_nonce($this->get->get('_wpnonce'), 'e2pdf_templates')) {
430 if ($this->get->get('id')) {
431 $this->restore_template($this->get->get('id'));
432 $notification = $this->add_notification('update', __('Template Restored', 'e2pdf'));
433 }
434 } else {
435 wp_die($this->message('wp_verify_nonce_error'));
436 }
437 $this->redirect(
438 $this->helper->get_url(
439 [
440 'page' => 'e2pdf-templates',
441 'status' => $this->get->get('status'),
442 'orderby' => $this->get->get('orderby'),
443 'order' => $this->get->get('order'),
444 's' => $this->get->get('s'),
445 'notification' => $notification,
446 ]
447 )
448 );
449 }
450
451 /**
452 * @url admin.php?page=e2pdf-templates&action=delete&id=$id&status=$status
453 */
454 public function delete_action() {
455 $notification = '';
456 if (wp_verify_nonce($this->get->get('_wpnonce'), 'e2pdf_templates')) {
457 if ($this->get->get('id')) {
458 $this->delete_template($this->get->get('id'));
459 $notification = $this->add_notification('update', __('Template Deleted', 'e2pdf'));
460 }
461 } else {
462 wp_die($this->message('wp_verify_nonce_error'));
463 }
464
465 $this->redirect(
466 $this->helper->get_url(
467 [
468 'page' => 'e2pdf-templates',
469 'status' => $this->get->get('status'),
470 'orderby' => $this->get->get('orderby'),
471 'order' => $this->get->get('order'),
472 's' => $this->get->get('s'),
473 'notification' => $notification,
474 ]
475 )
476 );
477 }
478
479 /**
480 * @url admin.php?page=e2pdf-templates&action=duplicate&id=$id
481 */
482 public function duplicate_action() {
483 $notification = '';
484 if (wp_verify_nonce($this->get->get('_wpnonce'), 'e2pdf_templates')) {
485 if ($this->get->get('id')) {
486 $this->duplicate_template($this->get->get('id'));
487 $notification = $this->add_notification('update', __('Template Duplicated', 'e2pdf'));
488 }
489 } else {
490 wp_die($this->message('wp_verify_nonce_error'));
491 }
492 $this->redirect(
493 $this->helper->get_url(
494 [
495 'page' => 'e2pdf-templates',
496 'status' => $this->get->get('status'),
497 'orderby' => $this->get->get('orderby'),
498 'order' => $this->get->get('order'),
499 's' => $this->get->get('s'),
500 'notification' => $notification,
501 ]
502 )
503 );
504 }
505
506 /**
507 * @url admin.php?page=e2pdf-templates&action=import
508 */
509 public function import_action() {
510 if ($this->post->get('_wpnonce')) {
511
512 if (!wp_verify_nonce($this->post->get('_wpnonce'), 'e2pdf_templates')) {
513 wp_die($this->message('wp_verify_nonce_error'));
514 }
515
516 $errors = array();
517 $import = $this->files->get('template');
518 $name = $import['name'];
519 $tmp = $import['tmp_name'];
520 $ext = strtolower(pathinfo($name, PATHINFO_EXTENSION));
521 if (!$tmp) {
522 $this->add_notification('error', __('Choose Template file to upload', 'e2pdf'));
523 } elseif ($import['error']) {
524 $this->add_notification('error', $import['error']);
525 } elseif (!in_array($ext, array('xml'))) {
526 $this->add_notification('error', sprintf(__('Only %s files allowed', 'e2pdf'), '.xml'));
527 } elseif ($import['type'] != 'text/xml') {
528 $this->add_notification('error', __('Invalid Type', 'e2pdf'));
529 } else {
530 $options = $this->post->get('options');
531 $xml = simplexml_load_file($import['tmp_name'], 'SimpleXMLElement', LIBXML_PARSEHUGE);
532 if (!isset($xml->template->pages)) {
533 $this->add_notification('error', __('The file is not a valid E2Pdf Template', 'e2pdf'));
534 } else {
535 $pages = $this->helper->load('convert')->unserialize(base64_decode((string) $xml->template->pages));
536 if (is_array($pages)) {
537 foreach ($pages as $page_key => $page) {
538 if (isset($page['elements']) && !empty($page['elements'])) {
539 foreach ($page['elements'] as $element_key => $element) {
540 if ($element['type'] === 'e2pdf-image' && isset($element['base64']) && $element['base64']) {
541 if ($options['images']) {
542 $image = base64_decode($element['base64']);
543 $file_name = basename($element['value']);
544 $upload_file = wp_upload_bits($file_name, null, $image);
545 if (!$upload_file['error']) {
546 $wp_filetype = wp_check_filetype($file_name, null);
547 $attachment = array(
548 'post_mime_type' => $wp_filetype['type'],
549 'post_parent' => 0,
550 'post_title' => preg_replace('/\.[^.]+$/', '', $file_name),
551 'post_content' => '',
552 'post_status' => 'inherit',
553 );
554 $attachment_id = wp_insert_attachment($attachment, $upload_file['file'], 0);
555 if (!is_wp_error($attachment_id)) {
556 require_once ABSPATH . 'wp-admin/includes/image.php';
557 $attachment_data = wp_generate_attachment_metadata($attachment_id, $upload_file['file']);
558 wp_update_attachment_metadata($attachment_id, $attachment_data);
559 $pages[$page_key]['elements'][$element_key]['value'] = $upload_file['url'];
560 }
561 }
562 }
563 unset($pages[$page_key]['elements'][$element_key]['base64']);
564 }
565 }
566 }
567 }
568 }
569
570 $template = new Model_E2pdf_Template();
571 if ($options['overwrite']) {
572 if ($options['overwrite'] == '1' && $options['template_id']) {
573 $template->load((int) $options['template_id']);
574 } elseif ($options['overwrite'] == '2' && $options['custom_template_id']) {
575 if (!$template->load((int) $options['custom_template_id'])) {
576 $template->set('TMP_ID', (int) $options['custom_template_id']);
577 }
578 } elseif ($options['overwrite'] == '3') {
579 if (!$template->load((int) $xml->template->ID)) {
580 $template->set('TMP_ID', (int) $xml->template->ID);
581 }
582 }
583 }
584 $template->set('title', (string) $xml->template->title);
585 $template->set('flatten', (string) $xml->template->flatten);
586 if (isset($xml->template->tab_order)) {
587 $template->set('tab_order', (string) $xml->template->tab_order);
588 }
589 $template->set('compression', (string) $xml->template->compression);
590 if (isset($xml->template->optimization)) {
591 $template->set('optimization', (string) $xml->template->optimization);
592 }
593 $template->set('appearance', (string) $xml->template->appearance);
594 $template->set('width', (string) $xml->template->width);
595 $template->set('height', (string) $xml->template->height);
596 $template->set('extension', (string) $xml->template->extension);
597 if (isset($xml->template->item)) {
598 $template->set('item', (string) $xml->template->item);
599 }
600 if (isset($xml->template->item1)) {
601 $template->set('item1', (string) $xml->template->item1);
602 }
603 if (isset($xml->template->item2)) {
604 $template->set('item2', (string) $xml->template->item2);
605 }
606
607 $fpro2pdf_backup = false;
608 if ($xml->source) {
609 if ((string) $xml->source == 'fpro2pdf') {
610 $fpro2pdf_backup = true;
611 }
612 }
613
614 $extension = new Model_E2pdf_Extension();
615 $extension->load($template->get('extension'));
616 if ($options['item'] && $xml->item && !$fpro2pdf_backup) {
617 $extension->set('item', $template->get('item'));
618 if ($template->get('item') == '-2') {
619 $extension->set('item1', $template->get('item1'));
620 $extension->set('item2', $template->get('item2'));
621 }
622
623 $updated_items = $extension->import($xml->item, $options);
624 if ($updated_items) {
625 if (isset($updated_items['errors'])) {
626 $errors = array_merge($errors, $updated_items['errors']);
627 } else {
628 if ($template->get('item') == '-2') {
629 if ($template->get('item1') && isset($updated_items[$template->get('item1')])) {
630 $item1 = $updated_items[$template->get('item1')];
631 $template->set('item1', $item1);
632 $extension->set('item1', $item1);
633 } else {
634 $template->set('item1', '');
635 $extension->set('item1', '');
636 }
637 if ($template->get('item2') && isset($updated_items[$template->get('item2')])) {
638 $item2 = $updated_items[$template->get('item2')];
639 $template->set('item2', $item2);
640 $extension->set('item2', $item2);
641 } else {
642 $template->set('item2', '');
643 $extension->set('item2', '');
644 }
645 } else {
646 if (isset($updated_items[$template->get('item')])) {
647 $item = $updated_items[$template->get('item')];
648 $template->set('item', $item);
649 $extension->set('item', $item);
650 } else {
651 $template->set('item', '');
652 $extension->set('item', '');
653 }
654 }
655 }
656 }
657 }
658
659 $template->set('format', (string) $xml->template->format);
660 if (isset($xml->template->resample)) {
661 $template->set('resample', (string) $xml->template->resample);
662 }
663 $template->set(
664 'dataset_title',
665 apply_filters('e2pdf_controller_templates_import_replace_shortcodes', (string) $xml->template->dataset_title, $options, $xml, $template, $extension)
666 );
667 if (isset($xml->template->dataset_title1)) {
668 $template->set(
669 'dataset_title1',
670 apply_filters('e2pdf_controller_templates_import_replace_shortcodes', (string) $xml->template->dataset_title1, $options, $xml, $template, $extension)
671 );
672 }
673 if (isset($xml->template->dataset_title2)) {
674 $template->set(
675 'dataset_title2',
676 apply_filters('e2pdf_controller_templates_import_replace_shortcodes', (string) $xml->template->dataset_title2, $options, $xml, $template, $extension)
677 );
678 }
679 $template->set(
680 'button_title',
681 apply_filters('e2pdf_controller_templates_import_replace_shortcodes', (string) $xml->template->button_title, $options, $xml, $template, $extension)
682 );
683 if (isset($xml->template->dpdf)) {
684 $template->set(
685 'dpdf',
686 apply_filters('e2pdf_controller_templates_import_replace_shortcodes', (string) $xml->template->dpdf, $options, $xml, $template, $extension)
687 );
688 }
689 if (isset($xml->template->attachments)) {
690 $template->set(
691 'attachments',
692 apply_filters('e2pdf_controller_templates_import_replace_shortcodes', (string) $xml->template->attachments, $options, $xml, $template, $extension)
693 );
694 }
695 $template->set('inline', (string) $xml->template->inline);
696 $template->set('auto', (string) $xml->template->auto);
697 if (isset($xml->template->rtl)) {
698 $template->set('rtl', (string) $xml->template->rtl);
699 }
700 if (isset($xml->template->font_processor)) {
701 $template->set('font_processor', (string) $xml->template->font_processor);
702 }
703 $template->set(
704 'name',
705 apply_filters('e2pdf_controller_templates_import_replace_shortcodes', (string) $xml->template->name, $options, $xml, $template, $extension)
706 );
707 if (isset($xml->template->savename)) {
708 $template->set(
709 'savename',
710 apply_filters('e2pdf_controller_templates_import_replace_shortcodes', (string) $xml->template->savename, $options, $xml, $template, $extension)
711 );
712 }
713 $template->set(
714 'password',
715 apply_filters('e2pdf_controller_templates_import_replace_shortcodes', (string) $xml->template->password, $options, $xml, $template, $extension)
716 );
717 if (isset($xml->template->owner_password)) {
718 $template->set(
719 'owner_password',
720 apply_filters('e2pdf_controller_templates_import_replace_shortcodes', (string) $xml->template->owner_password, $options, $xml, $template, $extension)
721 );
722 }
723 if (isset($xml->template->permissions)) {
724 $template->set('permissions', $this->helper->load('convert')->unserialize(base64_decode((string) $xml->template->permissions)));
725 } else {
726 $template->set('permissions', array('printing'));
727 }
728 if (isset($xml->template->hooks)) {
729 $template->set(
730 'hooks',
731 (string) $xml->template->hooks
732 );
733 }
734
735 $template->set(
736 'meta_title',
737 apply_filters('e2pdf_controller_templates_import_replace_shortcodes', (string) $xml->template->meta_title, $options, $xml, $template, $extension)
738 );
739 $template->set(
740 'meta_subject',
741 apply_filters('e2pdf_controller_templates_import_replace_shortcodes', (string) $xml->template->meta_subject, $options, $xml, $template, $extension)
742 );
743 $template->set(
744 'meta_author',
745 apply_filters('e2pdf_controller_templates_import_replace_shortcodes', (string) $xml->template->meta_author, $options, $xml, $template, $extension)
746 );
747 $template->set(
748 'meta_keywords',
749 apply_filters('e2pdf_controller_templates_import_replace_shortcodes', (string) $xml->template->meta_keywords, $options, $xml, $template, $extension)
750 );
751 if (isset($xml->template->lang_code)) {
752 $template->set('lang_code', (string) $xml->template->lang_code);
753 }
754 $template->set('font', (string) $xml->template->font);
755 $template->set('font_size', (string) $xml->template->font_size);
756 $template->set('font_color', (string) $xml->template->font_color);
757 $template->set('line_height', (string) $xml->template->line_height);
758 if (isset($xml->template->text_align)) {
759 $template->set('text_align', (string) $xml->template->text_align);
760 }
761 $template->set('fonts', $this->helper->load('convert')->unserialize(base64_decode((string) $xml->template->fonts)));
762 $template->set(
763 'properties',
764 apply_filters('e2pdf_controller_templates_import_properties', $this->helper->load('convert')->unserialize(base64_decode((string) $xml->template->properties)), $options, $xml, $template, $extension)
765 );
766 $template->set(
767 'actions',
768 apply_filters('e2pdf_controller_templates_import_actions', $this->helper->load('convert')->unserialize(base64_decode((string) $xml->template->actions)), $options, $xml, $template, $extension)
769 );
770 $template->set(
771 'pages',
772 apply_filters('e2pdf_controller_templates_import_pages', $pages, $options, $xml, $template, $extension)
773 );
774
775 if ($options['fonts'] && $xml->fonts) {
776 $model_e2pdf_font = new Model_E2pdf_Font();
777 $fonts = $model_e2pdf_font->get_fonts();
778
779 if ($xml->fonts) {
780 foreach ($xml->fonts->children() as $key => $font) {
781 $title = (string) $font->title;
782 $name = (string) $font->name;
783 $value = (string) $font->value;
784
785 if ($title == 'Noto Sans' && $name == 'NotoSans-Regular.ttf') {
786 $title = 'Noto Sans Regular';
787 }
788
789 $exist = array_search($title, $fonts);
790 if (!$exist) {
791 if (!file_exists($this->helper->get('fonts_dir') . $name)) {
792 $f_name = $name;
793 } else {
794 $i = 0;
795 do {
796 $f_name = $i . '_' . $name;
797 $i++;
798 } while (file_exists($this->helper->get('fonts_dir') . $f_name));
799 }
800 $font_ext = strtolower(pathinfo($f_name, PATHINFO_EXTENSION));
801 if (in_array($font_ext, $model_e2pdf_font->get_allowed_extensions())) {
802 file_put_contents($this->helper->get('fonts_dir') . $f_name, base64_decode($value), LOCK_EX);
803 }
804 }
805 }
806 }
807 }
808
809 if ($xml->pdf && $xml->pdf->source) {
810
811 $pdf_name = md5(time());
812 $pdf_dir = $this->helper->get('pdf_dir') . $pdf_name . '/';
813 $pdf_images_dir = $pdf_dir . 'images/';
814
815 $this->helper->create_dir($pdf_dir);
816 $this->helper->create_dir($pdf_images_dir);
817
818 $pdf_source = (string) $xml->pdf->source;
819 file_put_contents($pdf_dir . $pdf_name . '.pdf', base64_decode($pdf_source), LOCK_EX);
820
821 if ($xml->pdf->images) {
822 foreach ($xml->pdf->images->children() as $key => $image) {
823 $page_id = (string) $image->page_id;
824 $image_source = (string) $image->source;
825 file_put_contents($pdf_dir . 'images/' . $page_id . '.png', base64_decode($image_source), LOCK_EX);
826 }
827 }
828 $template->set('pdf', $pdf_name);
829 }
830
831 if (empty($errors)) {
832 $template->save(true);
833 if ($options['item']) {
834 $extension->after_import((string) $xml->template->ID, $template->get('ID'));
835 }
836 $template->activate();
837 if ($template->get('ID')) {
838 $this->add_notification(
839 'update', sprintf(
840 __('Template Imported: <a target="_blank" href="%2$s">View</a> | <a target="_blank" href="%3$s">Edit</a>', 'e2pdf'),
841 $this->helper->get_url(
842 [
843 'page' => 'e2pdf-templates',
844 'action' => 'view',
845 'id' => $template->get('ID')
846 ]
847 ),
848 $this->helper->get_url(array('page' => 'e2pdf-templates', 'action' => 'edit', 'id' => $template->get('ID')))
849 )
850 );
851 } else {
852 $this->add_notification('error', __('Something went wrong!', 'e2pdf'));
853 }
854 } else {
855 foreach ($errors as $key => $error) {
856 $this->add_notification('error', $error);
857 }
858 }
859 }
860 unlink($import['tmp_name']);
861 }
862 }
863
864 $this->view('import_disabled', false);
865 if (!$this->helper->load('xml')->check()) {
866 $this->add_notification('error', sprintf(__('The PHP %s extension is required', 'e2pdf'), 'SimpleXml'));
867 $this->view('import_disabled', true);
868 }
869
870 global $wpdb;
871
872 $order_condition = array(
873 'orderby' => 'ID',
874 'order' => 'desc',
875 );
876 $orderby = $this->helper->load('db')->prepare_orderby($order_condition);
877 $tpls = $wpdb->get_results($wpdb->prepare('SELECT `ID`, `title` FROM `' . (new Model_E2pdf_Template())->get_table() . '`' . $orderby . ''));
878 $templates = [
879 '' => __('--- Select ---', 'e2pdf')
880 ];
881 if (!empty($tpls)) {
882 foreach ($tpls as $tpl) {
883 $templates[$tpl->ID] = $tpl->ID . ' | ' . $tpl->title;
884 }
885 }
886
887 $options = array(
888 'overwrite' => [
889 'name' => __('Overwrite Options', 'e2pdf'),
890 'options' => [
891 [
892 'name' => __('Overwrite Template by ID', 'e2pdf'),
893 'key' => 'options[overwrite]',
894 'value' => '',
895 'default_value' => '',
896 'type' => 'select',
897 'options' => [
898 '' => __('Create New E2Pdf Template ID', 'e2pdf'),
899 '1' => __('Existing Template ID', 'e2pdf'),
900 '2' => __('Custom Template ID', 'e2pdf'),
901 '3' => __('Backup Template ID', 'e2pdf'),
902 ]
903 ],
904 [
905 'name' => __('Template ID', 'e2pdf'),
906 'key' => 'options[template_id]',
907 'value' => '',
908 'default_value' => '',
909 'type' => 'select',
910 'options' => $templates,
911 'li' => array(
912 'class' => 'e2pdf-import-overwrite-option e2pdf-hide',
913 ),
914 ],
915 [
916 'name' => __('Template ID', 'e2pdf'),
917 'key' => 'options[custom_template_id]',
918 'value' => '',
919 'default_value' => '',
920 'type' => 'text',
921 'class' => 'e2pdf-numbers',
922 'placeholder' => __('Template ID', 'e2pdf'),
923 'li' => array(
924 'class' => 'e2pdf-import-overwrite-option e2pdf-hide',
925 ),
926 ],
927 ],
928 ],
929 'template' => array(
930 'name' => __('Template Options', 'e2pdf'),
931 'options' => array(
932 array(
933 'name' => __('Import Images', 'e2pdf'),
934 'key' => 'options[images]',
935 'value' => '1',
936 'default_value' => '0',
937 'type' => 'checkbox',
938 'placeholder' => '',
939 ),
940 array(
941 'name' => __('Import Fonts', 'e2pdf'),
942 'key' => 'options[fonts]',
943 'value' => '1',
944 'default_value' => '0',
945 'type' => 'checkbox',
946 'placeholder' => '',
947 ),
948 ),
949 ),
950 'item' => array(
951 'name' => __('Connection Options', 'e2pdf'),
952 'options' => array(
953 array(
954 'name' => __('Import Connection', 'e2pdf'),
955 'key' => 'options[item]',
956 'value' => '0',
957 'default_value' => '0',
958 'type' => 'checkbox',
959 'placeholder' => '',
960 'class' => 'e2pdf-collapse',
961 'data-collapse' => 'e2pdf-import-extension-option',
962 ),
963 ),
964 ),
965 );
966
967 $options = apply_filters('e2pdf_controller_templates_import_options', $options);
968 $this->view('options', $options);
969 $this->view('upload_max_filesize', $this->helper->load('files')->get_upload_max_filesize());
970 }
971
972 /**
973 * @url admin.php?page=e2pdf-templates&action=backup&id=$id
974 */
975 public function download_action() {
976 if ($this->post->get('_wpnonce')) {
977 if (wp_verify_nonce($this->post->get('_wpnonce'), 'e2pdf_templates')) {
978 if ($this->helper->load('xml')->check()) {
979 $options = $this->post->get('options');
980 $xml = $this->helper->load('xml')->create('backup');
981 $template = new Model_E2pdf_Template();
982 if ($template->load($this->post->get('id'))) {
983 if ($template->get('title') != '') {
984 $filename = $template->get('ID') . ' - ' . $template->get('title') . '.' . date('Y-m-d.H-i-s') . '.e2pdf';
985 } else {
986 $filename = date('Y-m-d.H-i-s') . '.e2pdf';
987 }
988 $pages = $template->get('pages');
989 if ($options['images']) {
990 foreach ($pages as $page_key => $page) {
991 if (isset($page['elements']) && !empty($page['elements'])) {
992 foreach ($page['elements'] as $element_key => $element) {
993 if ($element['type'] === 'e2pdf-image') {
994 $file = false;
995 if ($this->helper->load('pdf')->get_extension($element['value'])) {
996 $file = $this->helper->load('pdf')->get_pdf($element['value']);
997 } else {
998 $file = $this->helper->load('image')->get_image($element['value']);
999 }
1000 if ($file) {
1001 $pages[$page_key]['elements'][$element_key]['base64'] = $file;
1002 }
1003 }
1004 }
1005 }
1006 }
1007 }
1008
1009 /*
1010 * Build Xml
1011 */
1012 $xml->addChildCData('version', $this->helper->get('version'));
1013 $xml->addChildCData('date', date('Y-m-d H:i:s'));
1014
1015 $options_xml = $xml->addChild('options');
1016 foreach ($options as $option_key => $option) {
1017 $options_xml->addChildCData($option_key, $option);
1018 }
1019 $template_xml = $xml->addChild('template');
1020 $template_xml->addChildCData('ID', $template->get('ID'));
1021 $template_xml->addChildCData('title', $template->get('title'));
1022 $template_xml->addChildCData('created_at', $template->get('created_at'));
1023 $template_xml->addChildCData('updated_at', $template->get('updated_at'));
1024 $template_xml->addChildCData('flatten', $template->get('flatten'));
1025 $template_xml->addChildCData('tab_order', $template->get('tab_order'));
1026 $template_xml->addChildCData('compression', $template->get('compression'));
1027 $template_xml->addChildCData('optimization', $template->get('optimization'));
1028 $template_xml->addChildCData('appearance', $template->get('appearance'));
1029 $template_xml->addChildCData('width', $template->get('width'));
1030 $template_xml->addChildCData('height', $template->get('height'));
1031 $template_xml->addChildCData('extension', $template->get('extension'));
1032 $template_xml->addChildCData('item', $template->get('item'));
1033 $template_xml->addChildCData('item1', $template->get('item1'));
1034 $template_xml->addChildCData('item2', $template->get('item2'));
1035 $template_xml->addChildCData('format', $template->get('format'));
1036 $template_xml->addChildCData('resample', $template->get('resample'));
1037 $template_xml->addChildCData(
1038 'dataset_title',
1039 apply_filters('e2pdf_controller_templates_backup_replace_shortcodes', $template->get('dataset_title'), $options, $template, $template->extension())
1040 );
1041 $template_xml->addChildCData(
1042 'dataset_title1',
1043 apply_filters('e2pdf_controller_templates_backup_replace_shortcodes', $template->get('dataset_title1'), $options, $template, $template->extension())
1044 );
1045 $template_xml->addChildCData(
1046 'dataset_title2',
1047 apply_filters('e2pdf_controller_templates_backup_replace_shortcodes', $template->get('dataset_title2'), $options, $template, $template->extension())
1048 );
1049 $template_xml->addChildCData(
1050 'button_title',
1051 apply_filters('e2pdf_controller_templates_backup_replace_shortcodes', $template->get('button_title'), $options, $template, $template->extension())
1052 );
1053 $template_xml->addChildCData(
1054 'dpdf',
1055 apply_filters('e2pdf_controller_templates_backup_replace_shortcodes', $template->get('dpdf'), $options, $template, $template->extension())
1056 );
1057 $template_xml->addChildCData(
1058 'attachments',
1059 apply_filters('e2pdf_controller_templates_backup_replace_shortcodes', $template->get('attachments'), $options, $template, $template->extension())
1060 );
1061 $template_xml->addChildCData('inline', $template->get('inline'));
1062 $template_xml->addChildCData('auto', $template->get('auto'));
1063 $template_xml->addChildCData('rtl', $template->get('rtl'));
1064 $template_xml->addChildCData('font_processor', $template->get('font_processor'));
1065 $template_xml->addChildCData(
1066 'name',
1067 apply_filters('e2pdf_controller_templates_backup_replace_shortcodes', $template->get('name'), $options, $template, $template->extension())
1068 );
1069 $template_xml->addChildCData(
1070 'savename',
1071 apply_filters('e2pdf_controller_templates_backup_replace_shortcodes', $template->get('savename'), $options, $template, $template->extension())
1072 );
1073 $template_xml->addChildCData(
1074 'password',
1075 apply_filters('e2pdf_controller_templates_backup_replace_shortcodes', $template->get('password'), $options, $template, $template->extension())
1076 );
1077 $template_xml->addChildCData(
1078 'owner_password',
1079 apply_filters('e2pdf_controller_templates_backup_replace_shortcodes', $template->get('owner_password'), $options, $template, $template->extension())
1080 );
1081 $template_xml->addChildCData('permissions', base64_encode(serialize($template->get('permissions'))));
1082 $template_xml->addChildCData('hooks', $template->get('hooks'));
1083 $template_xml->addChildCData(
1084 'meta_title',
1085 apply_filters('e2pdf_controller_templates_backup_replace_shortcodes', $template->get('meta_title'), $options, $template, $template->extension())
1086 );
1087 $template_xml->addChildCData(
1088 'meta_subject',
1089 apply_filters('e2pdf_controller_templates_backup_replace_shortcodes', $template->get('meta_subject'), $options, $template, $template->extension())
1090 );
1091 $template_xml->addChildCData(
1092 'meta_author',
1093 apply_filters('e2pdf_controller_templates_backup_replace_shortcodes', $template->get('meta_author'), $options, $template, $template->extension())
1094 );
1095 $template_xml->addChildCData(
1096 'meta_keywords',
1097 apply_filters('e2pdf_controller_templates_backup_replace_shortcodes', $template->get('meta_keywords'), $options, $template, $template->extension())
1098 );
1099 $template_xml->addChildCData('lang_code', $template->get('lang_code'));
1100 $template_xml->addChildCData('font', $template->get('font'));
1101 $template_xml->addChildCData('font_size', $template->get('font_size'));
1102 $template_xml->addChildCData('font_color', $template->get('font_color'));
1103 $template_xml->addChildCData('line_height', $template->get('line_height'));
1104 $template_xml->addChildCData('text_align', $template->get('text_align'));
1105 $template_xml->addChildCData('fonts', base64_encode(serialize($template->get('fonts'))));
1106 $template_xml->addChildCData(
1107 'properties', base64_encode(
1108 serialize(
1109 apply_filters('e2pdf_controller_templates_backup_properties', $template->get('properties'), $options, $template, $template->extension())
1110 )
1111 )
1112 );
1113 $template_xml->addChildCData(
1114 'actions', base64_encode(
1115 serialize(
1116 apply_filters('e2pdf_controller_templates_backup_actions', $template->get('actions'), $options, $template, $template->extension())
1117 )
1118 )
1119 );
1120 $template_xml->addChildCData('pages', base64_encode(
1121 serialize(
1122 apply_filters('e2pdf_controller_templates_backup_pages', $pages, $options, $template, $template->extension())
1123 )
1124 )
1125 );
1126
1127 // include pdf
1128 if ($template->get('pdf')) {
1129 $pdf_xml = $xml->addChild('pdf');
1130 $pdf_xml->addChildCData('source', base64_encode(file_get_contents($this->helper->get('pdf_dir') . $template->get('pdf') . '/' . $template->get('pdf') . '.pdf')));
1131 $pdf_xml_images = $pdf_xml->addChild('images');
1132 foreach ($template->get('pages') as $key => $page) {
1133 $pdf_xml_image = $pdf_xml_images->addChild('image');
1134 $pdf_xml_image->addChildCData('page_id', $page['page_id']);
1135 $pdf_xml_image->addChildCData('source', base64_encode(file_get_contents($this->helper->get('pdf_dir') . $template->get('pdf') . '/images/' . $page['page_id'] . '.png')));
1136 }
1137 }
1138
1139 // include fonts
1140 if ($options['fonts']) {
1141 $fonts_xml = $xml->addChild('fonts');
1142 if ($template->get('fonts')) {
1143 $model_e2pdf_font = new Model_E2pdf_Font();
1144 foreach ($template->get('fonts') as $key => $value) {
1145 $font = $fonts_xml->addChild('font');
1146 $font->addChildCData('title', $value);
1147 $font->addChildCData('name', $key);
1148 $font->addChildCData('value', $model_e2pdf_font->get_font($key));
1149 }
1150 }
1151 }
1152
1153 // include item
1154 if ($options['item']) {
1155 if ($template->extension()->method('backup')) {
1156 $item = $xml->addChild('item');
1157 $template->extension()->backup($item);
1158 }
1159 }
1160 $this->download_response('xml', $this->helper->load('xml')->get_xml(), $filename);
1161 exit;
1162 } else {
1163 $this->add_notification('error', __('Something went wrong!', 'e2pdf'));
1164 $this->render('blocks', 'notifications');
1165 }
1166 }
1167 } else {
1168 wp_die($this->message('wp_verify_nonce_error'));
1169 }
1170 }
1171
1172 $this->view('export_disabled', false);
1173
1174 if (!$this->helper->load('xml')->check()) {
1175 $this->add_notification('error', sprintf(__('The PHP %s extension is required', 'e2pdf'), 'SimpleXml'));
1176 $this->view('export_disabled', true);
1177 }
1178
1179 $template = new Model_E2pdf_Template();
1180 if ($template->load($this->get->get('id'))) {
1181
1182 $this->view('template', $template);
1183
1184 $options = array(
1185 'common' => array(
1186 'name' => __('Options', 'e2pdf'),
1187 'options' => array(
1188 array(
1189 'name' => __('Include Connection', 'e2pdf'),
1190 'key' => 'options[item]',
1191 'value' => $template->extension()->method('backup') ? '1' : '0',
1192 'default_value' => 0,
1193 'type' => 'checkbox',
1194 'placeholder' => '',
1195 'disabled' => $template->extension()->method('backup') ? false : 'disabled',
1196 ),
1197 array(
1198 'name' => __('Include Images', 'e2pdf'),
1199 'key' => 'options[images]',
1200 'value' => 1,
1201 'default_value' => 0,
1202 'type' => 'checkbox',
1203 'placeholder' => '',
1204 ),
1205 array(
1206 'name' => __('Include Fonts', 'e2pdf'),
1207 'key' => 'options[fonts]',
1208 'value' => 1,
1209 'default_value' => 0,
1210 'type' => 'checkbox',
1211 'placeholder' => '',
1212 ),
1213 ),
1214 ),
1215 );
1216
1217 $options = apply_filters('e2pdf_controller_templates_backup_options', $options, $template, $template->extension());
1218 $this->view('options', $options);
1219 } else {
1220 $this->add_notification('error', __('Something went wrong!', 'e2pdf'));
1221 $this->render('blocks', 'notifications');
1222 }
1223 }
1224
1225 // screen action
1226 public function screen_action() {
1227 $option = $this->post->get('wp_screen_options');
1228 if (is_array($option) && isset($option['option']) && isset($option['value']) && $option['value']) {
1229 update_option($option['option'], $option['value']);
1230 }
1231 $this->redirect(
1232 $this->helper->get_url(
1233 [
1234 'page' => 'e2pdf-templates',
1235 ]
1236 )
1237 );
1238 }
1239
1240 // ajax save form
1241 public function ajax_save_form() {
1242 global $wpdb;
1243 if (wp_verify_nonce($this->get->get('_wpnonce'), 'e2pdf_templates')) {
1244 $data = json_decode($this->post->get('data'), true);
1245 $template = new Model_E2pdf_Template();
1246 if (isset($data['ID'])) {
1247 $template->load($data['ID']);
1248 }
1249 if (!isset($data['permissions'])) {
1250 $data['permissions'] = array();
1251 }
1252 foreach ($data as $key => $value) {
1253 if ($key == 'pdf' && $value) {
1254 if (file_exists($this->helper->get('pdf_dir') . $value . '/' . $value . '.pdf')) {
1255 $template->set($key, $value);
1256 }
1257 } else {
1258 $template->set($key, $value);
1259 }
1260 }
1261 $template_id = $template->save(true);
1262 if (!$template_id) {
1263 if ($wpdb->last_error) {
1264 $response = array(
1265 'error' => $wpdb->last_error,
1266 );
1267 } else {
1268 $response = array(
1269 'error' => __('Something went wrong!', 'e2pdf'),
1270 );
1271 }
1272 } else {
1273 if (isset($data['item']) && $data['item'] == '-1') {
1274 $template->load($template_id);
1275 if ($template->extension()->method('auto_form')) {
1276 $template = $template->extension()->auto_form($template, $data);
1277 } else {
1278 $template->set('item', '');
1279 }
1280 $template->save(true);
1281 }
1282 if (!isset($data['ID'])) {
1283 $template->activate();
1284 }
1285 $response = array(
1286 'redirect' => $this->helper->get_url(
1287 array(
1288 'page' => 'e2pdf-templates',
1289 'action' => 'edit',
1290 'id' => $template->get('ID'),
1291 'notification' => $this->add_notification('update', __('Template Saved', 'e2pdf')),
1292 )
1293 ),
1294 );
1295 }
1296 } else {
1297 $response['error'] = $this->message('wp_verify_nonce_error');
1298 }
1299 $this->json_response($response);
1300 }
1301
1302 public function ajax_get_styles() {
1303
1304 if (wp_verify_nonce($this->get->get('_wpnonce'), 'e2pdf_templates')) {
1305 $data = $this->post->get('data');
1306 $content = array();
1307 if (isset($data['extension']) && isset($data['item'])) {
1308 $extension = new Model_E2pdf_Extension();
1309 if ($extension->load($data['extension'])) {
1310 $content = $extension->styles($data['item']);
1311 }
1312 }
1313 $response = array(
1314 'content' => $content,
1315 );
1316 } else {
1317 $response['error'] = $this->message('wp_verify_nonce_error');
1318 }
1319 $this->json_response($response);
1320 }
1321
1322 // ajax extension
1323 public function ajax_extension() {
1324 if (wp_verify_nonce($this->get->get('_wpnonce'), 'e2pdf_templates')) {
1325 $data = $this->post->get('data');
1326 $extension = new Model_E2pdf_Extension();
1327 $content = array();
1328 if ($extension->load($data)) {
1329 $content = $extension->items();
1330 if ($extension->method('merged_items')) {
1331 $select = new stdClass();
1332 $select->id = '-2';
1333 $select->url = '';
1334 $select->name = __('Merged Connections', 'e2pdf');
1335 array_unshift($content, $select);
1336 }
1337 if ($extension->method('auto_form')) {
1338 $select = new stdClass();
1339 $select->id = '-1';
1340 $select->url = '';
1341 $select->name = __('Auto Form from PDF', 'e2pdf');
1342 array_unshift($content, $select);
1343 }
1344 }
1345 $select = new stdClass();
1346 $select->id = '';
1347 $select->url = '';
1348 $select->name = __('--- Select ---', 'e2pdf');
1349 array_unshift($content, $select);
1350 $response = array(
1351 'content' => $content,
1352 );
1353 } else {
1354 $response['error'] = $this->message('wp_verify_nonce_error');
1355 }
1356 $this->json_response($response);
1357 }
1358
1359 // ajax visual mapper
1360 public function ajax_visual_mapper() {
1361 if (wp_verify_nonce($this->get->get('_wpnonce'), 'e2pdf_templates')) {
1362 $data = $this->post->get('data');
1363 $item = isset($data['item']) ? $data['item'] : '';
1364 $item1 = isset($data['item1']) ? $data['item1'] : '';
1365 $item2 = isset($data['item2']) ? $data['item2'] : '';
1366 $extension = new Model_E2pdf_Extension();
1367 $extension->load($data['extension']);
1368 $extension->set('item', $item);
1369 if ($item == '-2') {
1370 $extension->set('item1', $item1);
1371 $extension->set('item2', $item2);
1372 }
1373 $visual_mapper = $extension->visual_mapper();
1374 if ($visual_mapper) {
1375 $response = array(
1376 'content' => $visual_mapper,
1377 );
1378 } elseif (!$item || ($item == '-2' && !$item1 && !$item2)) {
1379 $response = array(
1380 'content' => __('You must set the Connection to use the Visual Mapper', 'e2pdf'),
1381 );
1382 } else {
1383 $response = array(
1384 'content' => __("Sorry, this Extension doesn't support the Visual Mapper", 'e2pdf'),
1385 );
1386 }
1387 } else {
1388 $response['error'] = $this->message('wp_verify_nonce_error');
1389 }
1390 $this->json_response($response);
1391 }
1392
1393 // ajax upload
1394 public function ajax_upload() {
1395
1396 if (!wp_verify_nonce($this->get->get('_wpnonce'), 'e2pdf_templates')) {
1397 $this->json_response(
1398 [
1399 'error' => $this->message('wp_verify_nonce_error'),
1400 ]
1401 );
1402 }
1403
1404 $data = $this->post->get();
1405 $post_extension = $data['extension'];
1406 $item = isset($data['item']) ? $data['item'] : '';
1407 $template_id = isset($data['template_id']) && $data['template_id'] ? $data['template_id'] : false;
1408 $font = isset($data['font']) ? $data['font'] : false;
1409 $font_size = isset($data['font_size']) ? $data['font_size'] : false;
1410 $line_height = isset($data['line_height']) ? $data['line_height'] : false;
1411 $title = isset($data['title']) ? $data['title'] : __('(no title)', 'e2pdf');
1412 $rtl = isset($data['rtl']) && $data['rtl'] ? '1' : '0';
1413 $text_align = isset($data['text_align']) ? $data['text_align'] : 'left';
1414 $pdf = $this->files->get('pdf');
1415 $name = strtolower($pdf['name']);
1416 $ext = strtolower(pathinfo($name, PATHINFO_EXTENSION));
1417
1418 if (!empty($pdf['error'])) {
1419 $this->json_response(
1420 [
1421 'error' => $pdf['error'],
1422 ]
1423 );
1424 } elseif (!in_array($ext, array('pdf'))) {
1425 $this->json_response(
1426 [
1427 'error' => sprintf(__('Only %s files allowed', 'e2pdf'), '.pdf'),
1428 ]
1429 );
1430 } elseif ($pdf['type'] != 'application/pdf') {
1431 $this->json_response(
1432 [
1433 'error' => __('Invalid Type', 'e2pdf'),
1434 ]
1435 );
1436 }
1437
1438 wp_raise_memory_limit('admin');
1439 if (get_option('e2pdf_api_protocol', '0') == '1') {
1440 $upload = class_exists('CURLFile') ? new CURLFile(realpath($pdf['tmp_name'])) : '@' . realpath($pdf['tmp_name']);
1441 } else {
1442 $upload = base64_encode(file_get_contents($pdf['tmp_name']));
1443 }
1444
1445 $model_e2pdf_api = new Model_E2pdf_Api();
1446 $model_e2pdf_api->set(
1447 array(
1448 'action' => 'template/upload2',
1449 'data' => array(
1450 'title' => $name,
1451 'pdf' => $upload,
1452 ),
1453 )
1454 );
1455 $request = $model_e2pdf_api->request();
1456
1457 if (isset($request['error'])) {
1458 $this->json_response(
1459 [
1460 'error' => $request['error'],
1461 ]
1462 );
1463 }
1464
1465 $extension = new Model_E2pdf_Extension();
1466 if ($post_extension) {
1467 $extension->load($post_extension);
1468 if ($item && $item != '-1') {
1469 $extension->set('item', $item);
1470 }
1471 }
1472
1473 $pdf_name = md5(time());
1474 $pdf_dir = $this->helper->get('pdf_dir') . $pdf_name . '/';
1475 $pdf_images_dir = $pdf_dir . 'images/';
1476 $this->helper->create_dir($pdf_dir);
1477 $this->helper->create_dir($pdf_images_dir);
1478 move_uploaded_file($pdf['tmp_name'], $pdf_dir . $pdf_name . '.pdf');
1479
1480 try {
1481 $xml_template = [];
1482 $pages = [];
1483 if (class_exists('XMLReader')) {
1484 $reader = new XMLReader();
1485 if (!$reader->XML($request['file'])) {
1486 throw new Exception(__('Failed to parse PDF', 'e2pdf'));
1487 }
1488 while ($reader->read()) {
1489 if ($reader->nodeType == XMLReader::ELEMENT) {
1490 switch ($reader->name) {
1491 case 'template':
1492 $template_node = new SimpleXMLElement($reader->readOuterXML(), LIBXML_PARSEHUGE);
1493 $xml_template = [
1494 'width' => (string) $template_node->width,
1495 'height' => (string) $template_node->height,
1496 'format' => (string) $template_node->format,
1497 'compression' => (string) $template_node->compression,
1498 'appearance' => (string) $template_node->appearance,
1499 'meta_title' => (string) $template_node->meta_title,
1500 'meta_subject' => (string) $template_node->meta_subject,
1501 'meta_author' => (string) $template_node->meta_author,
1502 'meta_keywords' => (string) $template_node->meta_keywords,
1503 'lang_code' => (string) $template_node->lang_code,
1504 'font' => (string) $template_node->font,
1505 'font_size' => (string) $template_node->font_size,
1506 'font_color' => (string) $template_node->font_color,
1507 ];
1508 break;
1509 case 'page':
1510 $page_node = new SimpleXMLElement($reader->readOuterXML(), LIBXML_PARSEHUGE);
1511 $this->helper->load('xml')->parse_xml_page($page_node, $pages, $pdf_images_dir, $extension);
1512 break;
1513 case 'fonts':
1514 $fonts_node = new SimpleXMLElement($reader->readOuterXML(), LIBXML_PARSEHUGE);
1515 foreach ($fonts_node->children() as $font) {
1516 $this->helper->load('xml')->parse_xml_font($font);
1517 }
1518 break;
1519 default:
1520 break;
1521 }
1522 }
1523 }
1524
1525 $reader->close();
1526 unset($reader);
1527 } elseif (is_callable('simplexml_load_string')) {
1528 $xml = simplexml_load_string($request['file'], 'SimpleXMLElement', LIBXML_PARSEHUGE);
1529 if (isset($xml->pages)) {
1530 foreach ($xml->pages->page as $page_node) {
1531 $this->helper->load('xml')->parse_xml_page($page_node, $pages, $pdf_images_dir, $extension);
1532 }
1533 }
1534 if (isset($xml->template)) {
1535 $xml_template = [
1536 'width' => (string) $xml->template->width,
1537 'height' => (string) $xml->template->height,
1538 'format' => (string) $xml->template->format,
1539 'compression' => (string) $xml->template->compression,
1540 'appearance' => (string) $xml->template->appearance,
1541 'meta_title' => (string) $xml->template->meta_title,
1542 'meta_subject' => (string) $xml->template->meta_subject,
1543 'meta_author' => (string) $xml->template->meta_author,
1544 'meta_keywords' => (string) $xml->template->meta_keywords,
1545 'lang_code' => (string) $xml->template->lang_code,
1546 'font' => (string) $xml->template->font,
1547 'font_size' => (string) $xml->template->font_size,
1548 'font_color' => (string) $xml->template->font_color,
1549 ];
1550 }
1551 if (isset($xml->fonts) && $xml->fonts) {
1552 foreach ($xml->fonts->children() as $key => $font) {
1553 $this->helper->load('xml')->parse_xml_font($font);
1554 }
1555 }
1556 }
1557
1558 if (empty($pages) || !is_array($pages)) {
1559 throw new Exception(__('Failed to parse PDF pages', 'e2pdf'));
1560 }
1561
1562 $this->helper->load('sort')->stable_uasort($pages, 'sort_by_pageid');
1563
1564 $template = new Model_E2pdf_Template();
1565 if ($template_id) {
1566 $template->load($template_id);
1567 $template->set('title', $title);
1568 $template->set('pages', $pages);
1569 $template->set('pdf', $pdf_name);
1570 $template->set('width', $xml_template['width']);
1571 $template->set('height', $xml_template['height']);
1572 $template->set('extension', $post_extension);
1573 $template->set('item', $item);
1574 $template->set('rtl', $rtl);
1575 $template->set('font', $font ? $font : $xml_template['font']);
1576 $template->set('font_size', $font_size ? $font_size : $xml_template['font_size']);
1577 $template->set('line_height', $line_height ? $line_height : $xml_template['font_size']);
1578 $template->set('text_align', $text_align);
1579 } else {
1580 $template->set('title', $title);
1581 $template->set('flatten', '1');
1582 $template->set('format', $xml_template['format']);
1583 $template->set('compression', $xml_template['compression']);
1584 $template->set('appearance', $xml_template['appearance']);
1585 $template->set('width', $xml_template['width']);
1586 $template->set('height', $xml_template['height']);
1587 $template->set('extension', $post_extension);
1588 $template->set('item', $item);
1589 $template->set('dataset_title', '');
1590 $template->set('button_title', '');
1591 $template->set('dpdf', '');
1592 $template->set('attachments', '');
1593 $template->set('inline', '');
1594 $template->set('auto', '');
1595 $template->set('rtl', $rtl);
1596 $template->set('name', '[e2pdf-dataset]');
1597 $template->set('savename', '[e2pdf-dataset]');
1598 $template->set('password', '');
1599 $template->set('meta_title', $xml_template['meta_title']);
1600 $template->set('meta_subject', $xml_template['meta_subject']);
1601 $template->set('meta_author', $xml_template['meta_author']);
1602 $template->set('meta_keywords', $xml_template['meta_keywords']);
1603 $template->set('lang_code', $xml_template['lang_code']);
1604 $template->set('font', $font ? $font : $xml_template['font']);
1605 $template->set('font_size', $font_size ? $font_size : $xml_template['font_size']);
1606 $template->set('line_height', $line_height ? $line_height : $xml_template['font_size']);
1607 $template->set('text_align', $text_align);
1608 $template->set('font_color', $xml_template['font_color']);
1609 $template->set('pages', $pages);
1610 $template->set('pdf', $pdf_name);
1611 }
1612 $template->save(true);
1613
1614 if ($item == '-1' && $extension->method('auto_form')) {
1615 $template = $extension->auto_form($template, $data);
1616 $template->save(true);
1617 }
1618
1619 if (!$template_id) {
1620 $template->activate();
1621 }
1622
1623 $this->json_response(
1624 [
1625 'redirect' => $this->helper->get_url(
1626 [
1627 'page' => 'e2pdf-templates',
1628 'action' => 'edit',
1629 'id' => $template->get('ID'),
1630 ]
1631 ),
1632 ]
1633 );
1634 } catch (Exception $ex) {
1635 if (file_exists($pdf_dir)) {
1636 $this->helper->delete_dir($pdf_dir);
1637 }
1638 $this->json_response(
1639 [
1640 'error' => $ex->getMessage(),
1641 ]
1642 );
1643 }
1644 }
1645
1646 // ajax reupload
1647 public function ajax_reupload() {
1648
1649 if (!wp_verify_nonce($this->get->get('_wpnonce'), 'e2pdf_templates')) {
1650 $this->json_response(
1651 [
1652 'error' => $this->message('wp_verify_nonce_error'),
1653 ]
1654 );
1655 }
1656
1657 $data = $this->post->get();
1658 $new = isset($data['new']) ? $data['new'] : array();
1659 $flush = isset($data['flush']) ? $data['flush'] : array();
1660 $positions = isset($data['positions']) ? $data['positions'] : array();
1661 $template_id = isset($data['template_id']) && $data['template_id'] ? $data['template_id'] : false;
1662 if (!$template_id) {
1663 return false;
1664 }
1665
1666 $pdf = $this->files->get('pdf');
1667 $name = strtolower($pdf['name']);
1668 $ext = strtolower(pathinfo($name, PATHINFO_EXTENSION));
1669
1670 if (!empty($pdf['error'])) {
1671 $this->json_response(
1672 [
1673 'error' => $pdf['error'],
1674 ]
1675 );
1676 } elseif (!in_array($ext, array('pdf'))) {
1677 $this->json_response(
1678 [
1679 'error' => sprintf(__('Only %s files allowed', 'e2pdf'), '.pdf'),
1680 ]
1681 );
1682 } elseif ($pdf['type'] != 'application/pdf') {
1683 $this->json_response(
1684 [
1685 'error' => __('Invalid Type', 'e2pdf'),
1686 ]
1687 );
1688 }
1689
1690 wp_raise_memory_limit('admin');
1691 if (get_option('e2pdf_api_protocol', '0') == '1') {
1692 $upload = class_exists('CURLFile') ? new CURLFile(realpath($pdf['tmp_name'])) : '@' . realpath($pdf['tmp_name']);
1693 } else {
1694 $upload = base64_encode(file_get_contents($pdf['tmp_name']));
1695 }
1696
1697 $model_e2pdf_api = new Model_E2pdf_Api();
1698 $model_e2pdf_api->set(
1699 array(
1700 'action' => 'template/upload2',
1701 'data' => array(
1702 'title' => $name,
1703 'pdf' => $upload,
1704 ),
1705 )
1706 );
1707 $request = $model_e2pdf_api->request();
1708
1709 if (isset($request['error'])) {
1710 $this->json_response(
1711 [
1712 'error' => $request['error'],
1713 ]
1714 );
1715 }
1716
1717 $template = new Model_E2pdf_Template();
1718 if (!$template->load($template_id)) {
1719 $this->json_response(
1720 [
1721 'error' => __('Failed to load E2Pdf Template', 'e2pdf'),
1722 ]
1723 );
1724 }
1725
1726 $extension = new Model_E2pdf_Extension();
1727 if ($template->get('extension') && $template->get('item')) {
1728 $extension->load($template->get('extension'));
1729 $extension->set('item', $template->get('item'));
1730 }
1731
1732 $pdf_name = md5(time());
1733 $pdf_dir = $this->helper->get('pdf_dir') . $pdf_name . '/';
1734 $pdf_images_dir = $pdf_dir . 'images/';
1735 $this->helper->create_dir($pdf_dir);
1736 $this->helper->create_dir($pdf_images_dir);
1737 move_uploaded_file($pdf['tmp_name'], $pdf_dir . $pdf_name . '.pdf');
1738
1739 try {
1740 $pages = [];
1741 $pos_pages = [];
1742 if (class_exists('XMLReader')) {
1743 $reader = new XMLReader();
1744 if (!$reader->XML($request['file'])) {
1745 throw new Exception(__('Failed to parse PDF', 'e2pdf'));
1746 }
1747 while ($reader->read()) {
1748 if ($reader->nodeType == XMLReader::ELEMENT) {
1749 if ($reader->name == 'page') {
1750 $page_node = new SimpleXMLElement($reader->readOuterXML(), LIBXML_PARSEHUGE);
1751 $pos_pages[] = (string) $page_node['number'];
1752 }
1753 }
1754 }
1755 $reader->close();
1756 unset($reader);
1757 foreach ($positions as $pos_key => $pos_page) {
1758 if (!in_array($pos_key, $flush)) {
1759 if ($pos_page == '0') {
1760 $flush[] = $pos_key;
1761 } elseif (!in_array($pos_page, $pos_pages, false)) {
1762 throw new Exception(sprintf(__('Invalid position of %s page', 'e2pdf'), $pos_key));
1763 }
1764 }
1765 }
1766 $reader = new XMLReader();
1767 $reader->XML($request['file']);
1768 while ($reader->read()) {
1769 if ($reader->nodeType == XMLReader::ELEMENT) {
1770 switch ($reader->name) {
1771 case 'page':
1772 $page_node = new SimpleXMLElement($reader->readOuterXML(), LIBXML_PARSEHUGE);
1773 $this->helper->load('xml')->parse_xml_page($page_node, $pages, $pdf_images_dir, $extension);
1774 break;
1775 default:
1776 break;
1777 }
1778 }
1779 }
1780 $reader->close();
1781 unset($reader);
1782 } elseif (is_callable('simplexml_load_string')) {
1783 $xml = simplexml_load_string($request['file'], 'SimpleXMLElement', LIBXML_PARSEHUGE);
1784 if (isset($xml->pages)) {
1785 foreach ($xml->pages->page as $page_node) {
1786 $pos_pages[] = (string) $page_node['number'];
1787 }
1788 }
1789 foreach ($positions as $pos_key => $pos_page) {
1790 if (!in_array($pos_key, $flush)) {
1791 if ($pos_page == '0') {
1792 $flush[] = $pos_key;
1793 } elseif (!in_array($pos_page, $pos_pages, false)) {
1794 throw new Exception(sprintf(__('Invalid position of %s page', 'e2pdf'), $pos_key));
1795 }
1796 }
1797 }
1798 if (isset($xml->pages)) {
1799 foreach ($xml->pages->page as $page_node) {
1800 $this->helper->load('xml')->parse_xml_page($page_node, $pages, $pdf_images_dir, $extension);
1801 }
1802 }
1803 }
1804
1805 if (empty($pages) || !is_array($pages)) {
1806 throw new Exception(__('Failed to parse PDF pages', 'e2pdf'));
1807 }
1808
1809 $this->helper->load('sort')->stable_uasort($pages, 'sort_by_pageid');
1810
1811 $model_e2pdf_element = new Model_E2pdf_Element();
1812 $last_element_id = $model_e2pdf_element->get_last_element_id($template_id);
1813 $original_pages = $template->get('pages');
1814
1815 foreach ($pages as $page_key => $page) {
1816 if (isset($page['elements']) && !empty($page['elements'])) {
1817 foreach ($page['elements'] as $element_key => $element) {
1818 $last_element_id++;
1819 $pages[$page_key]['elements'][$element_key]['element_id'] = $last_element_id;
1820 }
1821 }
1822 foreach ($positions as $pos_key => $pos_page) {
1823 if ($pos_page == $page_key) {
1824 if (in_array($pos_key, $new)) {
1825 if (isset($original_pages[$pos_key]['elements']) && !in_array($pos_key, $flush)) {
1826 $elements = $original_pages[$pos_key]['elements'];
1827 $pages[$page_key]['elements'] = array_merge($pages[$page_key]['elements'], $elements);
1828 }
1829 } else {
1830 if (isset($original_pages[$pos_key]['elements']) && !in_array($pos_key, $flush)) {
1831 $elements = $original_pages[$pos_key]['elements'];
1832 $pages[$page_key]['elements'] = $elements;
1833 } else {
1834 $pages[$page_key]['elements'] = array();
1835 }
1836 }
1837 if (isset($original_pages[$pos_key]['actions'])) {
1838 $pages[$page_key]['actions'] = $original_pages[$pos_key]['actions'];
1839 }
1840 }
1841 }
1842 }
1843
1844 $template->set('pdf', $pdf_name);
1845 $template->set('pages', $pages);
1846 $template->save(true);
1847
1848 $this->json_response(
1849 [
1850 'redirect' => $this->helper->get_url(
1851 array(
1852 'page' => 'e2pdf-templates',
1853 'action' => 'edit',
1854 'id' => $template->get('ID'),
1855 )
1856 ),
1857 ]
1858 );
1859 } catch (Exception $ex) {
1860 if (file_exists($pdf_dir)) {
1861 $this->helper->delete_dir($pdf_dir);
1862 }
1863 $this->json_response(
1864 [
1865 'error' => $ex->getMessage(),
1866 ]
1867 );
1868 }
1869 }
1870
1871 // ajax auto
1872 public function ajax_auto() {
1873 if (wp_verify_nonce($this->get->get('_wpnonce'), 'e2pdf_templates')) {
1874 $data = $this->post->get('data');
1875
1876 $extension = $data['extension'];
1877 $item = isset($data['item']) ? $data['item'] : '';
1878 $item1 = isset($data['item1']) ? $data['item1'] : '';
1879 $item2 = isset($data['item2']) ? $data['item2'] : '';
1880 $font_size = isset($data['font_size']) ? $data['font_size'] : '14';
1881 $line_height = isset($data['line_height']) ? $data['line_height'] : '14';
1882
1883 if (!$extension || !$item) {
1884 return;
1885 }
1886
1887 $model_e2pdf_extension = new Model_E2pdf_Extension();
1888 $model_e2pdf_extension->load($extension);
1889 $model_e2pdf_extension->set('item', $item);
1890 $model_e2pdf_extension->set('item1', $item1);
1891 $model_e2pdf_extension->set('item2', $item2);
1892 $model_e2pdf_extension->set('font_size', $font_size);
1893 $model_e2pdf_extension->set('line_height', $line_height);
1894
1895 if (method_exists($model_e2pdf_extension->extension(), 'auto')) {
1896 $content = $model_e2pdf_extension->auto();
1897 } else {
1898 $content = array();
1899 }
1900
1901 $response = array(
1902 'content' => $content,
1903 );
1904 } else {
1905 $response['error'] = $this->message('wp_verify_nonce_error');
1906 }
1907 $this->json_response($response);
1908 }
1909
1910 // ajax activate template
1911 public function ajax_activate_template() {
1912 if (wp_verify_nonce($this->get->get('_wpnonce'), 'e2pdf_templates')) {
1913 $data = $this->post->get('data');
1914 $id = $data['id'];
1915 if (!$id) {
1916 return;
1917 }
1918 $request = $this->activate_template($id);
1919 if (isset($request['error'])) {
1920 $response = array(
1921 'error' => $request['error'],
1922 );
1923 } else {
1924 $response = array(
1925 'content' => 'activated',
1926 );
1927 }
1928 } else {
1929 $response['error'] = $this->message('wp_verify_nonce_error');
1930 }
1931 $this->json_response($response);
1932 }
1933
1934 // ajax deactivaate template
1935 public function ajax_deactivate_template() {
1936 if (wp_verify_nonce($this->get->get('_wpnonce'), 'e2pdf_templates')) {
1937 $data = $this->post->get('data');
1938 $id = $data['id'];
1939 if (!$id) {
1940 return;
1941 }
1942 $request = $this->deactivate_template($id);
1943 if (isset($request['error'])) {
1944 $response = array(
1945 'error' => $request['error'],
1946 );
1947 } else {
1948 $response = array(
1949 'content' => 'deactivated',
1950 );
1951 }
1952 } else {
1953 $response['error'] = $this->message('wp_verify_nonce_error');
1954 }
1955 $this->json_response($response);
1956 }
1957
1958 // ajax email
1959 public function ajax_email() {
1960 if (wp_verify_nonce($this->get->get('_wpnonce'), 'e2pdf_templates')) {
1961 $data = $this->post->get('data');
1962 $email = isset($data['email']) ? trim($data['email']) : '';
1963 $email_code = isset($data['email_code']) ? trim($data['email_code']) : '';
1964 $model_e2pdf_api = new Model_E2pdf_Api();
1965 $model_e2pdf_api->set(
1966 array(
1967 'action' => 'common/email',
1968 'data' => array(
1969 'email' => $email,
1970 'email_code' => $email_code,
1971 'email_confirm' => isset($data['email_code']),
1972 ),
1973 )
1974 );
1975 $request = $model_e2pdf_api->request();
1976 if (isset($request['error'])) {
1977 if ($request['error'] === 'incorrect_email') {
1978 $response = array(
1979 'error' => __('Invalid E-mail address format', 'e2pdf'),
1980 );
1981 } elseif ($request['error'] === 'incorrect_code') {
1982 $response = array(
1983 'error' => __('Invalid Confirmation Code', 'e2pdf'),
1984 );
1985 } else {
1986 $response = array(
1987 'error' => $request['error'],
1988 );
1989 }
1990 } elseif (isset($request['success'])) {
1991 $response = array(
1992 'content' => $request['success'],
1993 );
1994 if ($request['success'] == 'subscribed') {
1995 update_option('e2pdf_email', $email);
1996 }
1997 }
1998 } else {
1999 $response['error'] = $this->message('wp_verify_nonce_error');
2000 }
2001 $this->json_response($response);
2002 }
2003
2004 // trash template
2005 public function trash_template($id = false) {
2006 $id = (int) $id;
2007 $template = new Model_E2pdf_Template();
2008 if ($id && $template->load($id, false)) {
2009 $template->set('trash', '1');
2010 $template->update(array('trash'));
2011 $this->deactivate_template($template->get('ID'));
2012 return true;
2013 }
2014 return false;
2015 }
2016
2017 // delete template
2018 public function delete_template($id = false) {
2019 $id = (int) $id;
2020 $template = new Model_E2pdf_Template();
2021 if ($id && $template->load($id)) {
2022 $this->deactivate_template($template->get('ID'));
2023 $template->delete();
2024 return true;
2025 }
2026 return false;
2027 }
2028
2029 // restore template
2030 public function restore_template($id = false) {
2031 $id = (int) $id;
2032 $template = new Model_E2pdf_Template();
2033 if ($id && $template->load($id, false)) {
2034 $template->set('trash', '0');
2035 $template->update(array('trash'));
2036 return true;
2037 }
2038 return false;
2039 }
2040
2041 // duplicate template
2042 public function duplicate_template($id = false) {
2043
2044 $id = (int) $id;
2045
2046 $template = new Model_E2pdf_Template();
2047 if ($id && $template->load($id)) {
2048 $title = $template->get('title') . ' ' . __('(Copy)', 'e2pdf');
2049 $template->set('ID', false);
2050 $template->set('title', $title);
2051 $template->set('uid', '');
2052 $template->set('activated', '0');
2053 if ($template->get('pdf')) {
2054 $pdf_name = md5(time());
2055 $pdf_dir = $this->helper->get('pdf_dir') . $pdf_name . '/';
2056 $pdf_images_dir = $pdf_dir . 'images/';
2057
2058 $this->helper->create_dir($pdf_dir);
2059 $this->helper->create_dir($pdf_images_dir);
2060
2061 if (file_exists($this->helper->get('pdf_dir') . $template->get('pdf') . '/' . $template->get('pdf') . '.pdf')) {
2062 $images = glob($this->helper->get('pdf_dir') . $template->get('pdf') . '/images/*');
2063 foreach ($images as $image) {
2064 copy($image, $pdf_images_dir . pathinfo($image, PATHINFO_BASENAME));
2065 }
2066 copy($this->helper->get('pdf_dir') . $template->get('pdf') . '/' . $template->get('pdf') . '.pdf', $this->helper->get('pdf_dir') . $pdf_name . '/' . $pdf_name . '.pdf');
2067 $template->set('pdf', $pdf_name);
2068 }
2069 }
2070 $template->save(true);
2071 return true;
2072 }
2073 return false;
2074 }
2075
2076 // activate template
2077 public function activate_template($id = false) {
2078 $id = (int) $id;
2079 $template = new Model_E2pdf_Template();
2080 if ($id && $template->load($id, false)) {
2081 return $template->activate();
2082 } else {
2083 return array(
2084 'error' => __('Something went wrong!', 'e2pdf'),
2085 );
2086 }
2087 }
2088
2089 // deactivate template
2090 public function deactivate_template($id = false) {
2091 $id = (int) $id;
2092 $template = new Model_E2pdf_Template();
2093 if ($id && $template->load($id, false)) {
2094 return $template->deactivate();
2095 } else {
2096 return array(
2097 'error' => __('Something went wrong!', 'e2pdf'),
2098 );
2099 }
2100 }
2101
2102 // get templates list
2103 public function get_templates_list($filters = array(), $count = false) {
2104 global $wpdb;
2105
2106 $model_e2pdf_template = new Model_E2pdf_Template();
2107 $condition = array();
2108 if (isset($filters['status']) && $filters['status'] == 'trash') {
2109 $condition = array(
2110 'trash' => array(
2111 'condition' => '=',
2112 'value' => '1',
2113 'type' => '%d',
2114 ),
2115 );
2116 } else {
2117 $condition = array(
2118 'trash' => array(
2119 'condition' => '<>',
2120 'value' => '1',
2121 'type' => '%d',
2122 ),
2123 );
2124 }
2125
2126 if (isset($filters['s']) && $filters['s']) {
2127 $condition['title'] = array(
2128 'condition' => 'LIKE',
2129 'value' => '%%' . $filters['s'] . '%%',
2130 'type' => '%s',
2131 'or' => array(
2132 array(
2133 'ID' => array(
2134 'condition' => 'LIKE',
2135 'value' => '%%' . $filters['s'] . '%%',
2136 'type' => '%s',
2137 ),
2138 ),
2139 ),
2140 );
2141 }
2142
2143 $order_condition = array();
2144 $sortable = array('id', 'title', 'updated_at');
2145 if (isset($filters['orderby']) && in_array($filters['orderby'], $sortable) && isset($filters['order'])) {
2146 $order_condition = array(
2147 'orderby' => $filters['orderby'],
2148 'order' => $filters['order'] == 'asc' ? 'asc' : 'desc',
2149 );
2150 } else {
2151 $order_condition = array(
2152 'orderby' => 'id',
2153 'order' => 'desc',
2154 );
2155 }
2156
2157 $limit_condition = array();
2158 if (!$count) {
2159 $paged = isset($filters['paged']) && $filters['paged'] ? $filters['paged'] : '0';
2160 $paged = (int) $paged <= 0 ? 1 : (int) $paged;
2161 $per_page = get_option('e2pdf_templates_screen_per_page', '20');
2162
2163 $limit_condition = array(
2164 'limit' => (int) $per_page,
2165 'offset' => (int) ($paged - 1) * $per_page,
2166 );
2167 }
2168
2169 $where = $this->helper->load('db')->prepare_where($condition);
2170 $orderby = $this->helper->load('db')->prepare_orderby($order_condition);
2171 $limit = $this->helper->load('db')->prepare_limit($limit_condition);
2172 // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
2173 $tpls = $wpdb->get_results($wpdb->prepare('SELECT `ID` FROM `' . $model_e2pdf_template->get_table() . '`' . $where['sql'] . $orderby . $limit . '', $where['filter']));
2174
2175 if ($count) {
2176 return count($tpls);
2177 }
2178
2179 $templates = array();
2180 foreach ($tpls as $key => $tpl) {
2181 $template = new Model_E2pdf_Template();
2182 $template->load($tpl->ID, false);
2183 if (is_array($this->helper->get('license')->get('activated_templates'))) {
2184 if ($template->get('activated') && !in_array($template->get('uid'), $this->helper->get('license')->get('activated_templates'))) {
2185 $template->deactivate();
2186 } elseif (!$template->get('activated') && in_array($template->get('uid'), $this->helper->get('license')->get('activated_templates'))) {
2187 $template->activate();
2188 }
2189 }
2190 $templates[] = $template;
2191 }
2192 return $templates;
2193 }
2194
2195 // get sizes list
2196 public function get_sizes_list($size = false, $attr = false) {
2197
2198 $sizes = array(
2199 'A4 (PORTRAIT)' => array(
2200 'width' => '595',
2201 'height' => '842',
2202 ),
2203 'A4 (LANDSCAPE)' => array(
2204 'width' => '842',
2205 'height' => '595',
2206 ),
2207 'LETTER' => array(
2208 'width' => '612',
2209 'height' => '792',
2210 ),
2211 'NOTE' => array(
2212 'width' => '540',
2213 'height' => '720',
2214 ),
2215 'LEGAL' => array(
2216 'width' => '612',
2217 'height' => '1008',
2218 ),
2219 'TABLOID' => array(
2220 'width' => '792',
2221 'height' => '1224',
2222 ),
2223 'EXECUTIVE' => array(
2224 'width' => '522',
2225 'height' => '756',
2226 ),
2227 'POSTCARD' => array(
2228 'width' => '283',
2229 'height' => '416',
2230 ),
2231 );
2232
2233 if (!$size) {
2234 return $sizes;
2235 } else {
2236 if (isset($sizes[$size][$attr])) {
2237 return $sizes[$size][$attr];
2238 }
2239 return false;
2240 }
2241 }
2242
2243 // get fonts
2244 public function get_fonts($with_path = false) {
2245 $model_e2pdf_font = new Model_E2pdf_Font();
2246 $fonts = $model_e2pdf_font->get_fonts();
2247
2248 if ($with_path) {
2249 foreach ($fonts as $key => $value) {
2250 $fonts[$key] = array(
2251 'key' => $value,
2252 'subfield' => array(
2253 'path' => $key,
2254 ),
2255 'value' => $value,
2256 );
2257 }
2258 }
2259
2260 return $fonts;
2261 }
2262
2263 // get font sizes
2264 public function get_font_sizes() {
2265 $max_font_size = apply_filters('e2pdf_controller_templates_max_font_size', 512);
2266 $sizes = array();
2267 for ($i = 1; $i <= $max_font_size; $i++) {
2268 $sizes[$i] = $i;
2269 }
2270 return $sizes;
2271 }
2272
2273 // get line heights
2274 public function get_line_heights() {
2275
2276 $max_line_height = apply_filters('e2pdf_controller_templates_max_line_height', 512);
2277 $line_heights = array();
2278 for ($i = 1; $i <= $max_line_height; $i++) {
2279 $line_heights[$i] = $i;
2280 }
2281 return $line_heights;
2282 }
2283
2284 // get lang codes
2285 public function get_lang_codes() {
2286 return array(
2287 '' => '',
2288 'aa' => 'aa',
2289 'ab' => 'ab',
2290 'ae' => 'ae',
2291 'af' => 'af',
2292 'ak' => 'ak',
2293 'am' => 'am',
2294 'an' => 'an',
2295 'ar' => 'ar',
2296 'ar-AE' => 'ar-AE',
2297 'ar-BH' => 'ar-BH',
2298 'ar-DZ' => 'ar-DZ',
2299 'ar-EG' => 'ar-EG',
2300 'ar-IQ' => 'ar-IQ',
2301 'ar-JO' => 'ar-JO',
2302 'ar-KW' => 'ar-KW',
2303 'ar-LB' => 'ar-LB',
2304 'ar-LY' => 'ar-LY',
2305 'ar-MA' => 'ar-MA',
2306 'ar-OM' => 'ar-OM',
2307 'ar-QA' => 'ar-QA',
2308 'ar-SA' => 'ar-SA',
2309 'ar-SY' => 'ar-SY',
2310 'ar-TN' => 'ar-TN',
2311 'ar-YE' => 'ar-YE',
2312 'as' => 'as',
2313 'av' => 'av',
2314 'ay' => 'ay',
2315 'az' => 'az',
2316 'ba' => 'ba',
2317 'be' => 'be',
2318 'bg' => 'bg',
2319 'bh' => 'bh',
2320 'bi' => 'bi',
2321 'bm' => 'bm',
2322 'bn' => 'bn',
2323 'bo' => 'bo',
2324 'br' => 'br',
2325 'bs' => 'bs',
2326 'ca' => 'ca',
2327 'ce' => 'ce',
2328 'ch' => 'ch',
2329 'co' => 'co',
2330 'cr' => 'cr',
2331 'cs' => 'cs',
2332 'cu' => 'cu',
2333 'cv' => 'cv',
2334 'cy' => 'cy',
2335 'da' => 'da',
2336 'de' => 'de',
2337 'de-AT' => 'de-AT',
2338 'de-CH' => 'de-CH',
2339 'de-DE' => 'de-DE',
2340 'de-LI' => 'de-LI',
2341 'de-LU' => 'de-LU',
2342 'div' => 'div',
2343 'dv' => 'dv',
2344 'dz' => 'dz',
2345 'ee' => 'ee',
2346 'el' => 'el',
2347 'en' => 'en',
2348 'en-AU' => 'en-AU',
2349 'en-BZ' => 'en-BZ',
2350 'en-CA' => 'en-CA',
2351 'en-CB' => 'en-CB',
2352 'en-GB' => 'en-GB',
2353 'en-IE' => 'en-IE',
2354 'en-JM' => 'en-JM',
2355 'en-NZ' => 'en-NZ',
2356 'en-PH' => 'en-PH',
2357 'en-TT' => 'en-TT',
2358 'en-US' => 'en-US',
2359 'en-ZA' => 'en-ZA',
2360 'en-ZW' => 'en-ZW',
2361 'eo' => 'eo',
2362 'es' => 'es',
2363 'es-AR' => 'es-AR',
2364 'es-BO' => 'es-BO',
2365 'es-CL' => 'es-CL',
2366 'es-CO' => 'es-CO',
2367 'es-CR' => 'es-CR',
2368 'es-DO' => 'es-DO',
2369 'es-EC' => 'es-EC',
2370 'es-ES' => 'es-ES',
2371 'es-GT' => 'es-GT',
2372 'es-HN' => 'es-HN',
2373 'es-MX' => 'es-MX',
2374 'es-NI' => 'es-NI',
2375 'es-PA' => 'es-PA',
2376 'es-PE' => 'es-PE',
2377 'es-PR' => 'es-PR',
2378 'es-PY' => 'es-PY',
2379 'es-SV' => 'es-SV',
2380 'es-US' => 'es-US',
2381 'es-UY' => 'es-UY',
2382 'es-VE' => 'es-VE',
2383 'et' => 'et',
2384 'eu' => 'eu',
2385 'fa' => 'fa',
2386 'ff' => 'ff',
2387 'fi' => 'fi',
2388 'fj' => 'fj',
2389 'fo' => 'fo',
2390 'fr' => 'fr',
2391 'fr-BE' => 'fr-BE',
2392 'fr-CA' => 'fr-CA',
2393 'fr-CH' => 'fr-CH',
2394 'fr-FR' => 'fr-FR',
2395 'fr-LU' => 'fr-LU',
2396 'fr-MC' => 'fr-MC',
2397 'fy' => 'fy',
2398 'ga' => 'ga',
2399 'gd' => 'gd',
2400 'gl' => 'gl',
2401 'gn' => 'gn',
2402 'gu' => 'gu',
2403 'gv' => 'gv',
2404 'ha' => 'ha',
2405 'he' => 'he',
2406 'hi' => 'hi',
2407 'ho' => 'ho',
2408 'hr' => 'hr',
2409 'hr-BA' => 'hr-BA',
2410 'hr-HR' => 'hr-HR',
2411 'ht' => 'ht',
2412 'hu' => 'hu',
2413 'hy' => 'hy',
2414 'hz' => 'hz',
2415 'ia' => 'ia',
2416 'id' => 'id',
2417 'ie' => 'ie',
2418 'ig' => 'ig',
2419 'ii' => 'ii',
2420 'ik' => 'ik',
2421 'in' => 'in',
2422 'io' => 'io',
2423 'is' => 'is',
2424 'it' => 'it',
2425 'it-CH' => 'it-CH',
2426 'it-IT' => 'it-IT',
2427 'iu' => 'iu',
2428 'iw' => 'iw',
2429 'ja' => 'ja',
2430 'ji' => 'ji',
2431 'jv' => 'jv',
2432 'jw' => 'jw',
2433 'ka' => 'ka',
2434 'kg' => 'kg',
2435 'ki' => 'ki',
2436 'kj' => 'kj',
2437 'kk' => 'kk',
2438 'kl' => 'kl',
2439 'km' => 'km',
2440 'kn' => 'kn',
2441 'ko' => 'ko',
2442 'kok' => 'kok',
2443 'kr' => 'kr',
2444 'ks' => 'ks',
2445 'ku' => 'ku',
2446 'kv' => 'kv',
2447 'kw' => 'kw',
2448 'ky' => 'ky',
2449 'kz' => 'kz',
2450 'la' => 'la',
2451 'lb' => 'lb',
2452 'lg' => 'lg',
2453 'li' => 'li',
2454 'ln' => 'ln',
2455 'lo' => 'lo',
2456 'ls' => 'ls',
2457 'lt' => 'lt',
2458 'lu' => 'lu',
2459 'lv' => 'lv',
2460 'mg' => 'mg',
2461 'mh' => 'mh',
2462 'mi' => 'mi',
2463 'mk' => 'mk',
2464 'ml' => 'ml',
2465 'mn' => 'mn',
2466 'mo' => 'mo',
2467 'mr' => 'mr',
2468 'ms' => 'ms',
2469 'ms-BN' => 'ms-BN',
2470 'ms-MY' => 'ms-MY',
2471 'mt' => 'mt',
2472 'my' => 'my',
2473 'na' => 'na',
2474 'nb' => 'nb',
2475 'nd' => 'nd',
2476 'ne' => 'ne',
2477 'ng' => 'ng',
2478 'nl' => 'nl',
2479 'nl-BE' => 'nl-BE',
2480 'nl-NL' => 'nl-NL',
2481 'nn' => 'nn',
2482 'no' => 'no',
2483 'nr' => 'nr',
2484 'ns' => 'ns',
2485 'nv' => 'nv',
2486 'ny' => 'ny',
2487 'oc' => 'oc',
2488 'oj' => 'oj',
2489 'om' => 'om',
2490 'or' => 'or',
2491 'os' => 'os',
2492 'pa' => 'pa',
2493 'pi' => 'pi',
2494 'pl' => 'pl',
2495 'ps' => 'ps',
2496 'pt' => 'pt',
2497 'pt-BR' => 'pt-BR',
2498 'pt-PT' => 'pt-PT',
2499 'qu' => 'qu',
2500 'qu-BO' => 'qu-BO',
2501 'qu-EC' => 'qu-EC',
2502 'qu-PE' => 'qu-PE',
2503 'rm' => 'rm',
2504 'rn' => 'rn',
2505 'ro' => 'ro',
2506 'ru' => 'ru',
2507 'rw' => 'rw',
2508 'sa' => 'sa',
2509 'sb' => 'sb',
2510 'sc' => 'sc',
2511 'sd' => 'sd',
2512 'se' => 'se',
2513 'se-FI' => 'se-FI',
2514 'se-NO' => 'se-NO',
2515 'se-SE' => 'se-SE',
2516 'sg' => 'sg',
2517 'sh' => 'sh',
2518 'si' => 'si',
2519 'sk' => 'sk',
2520 'sl' => 'sl',
2521 'sm' => 'sm',
2522 'sn' => 'sn',
2523 'so' => 'so',
2524 'sq' => 'sq',
2525 'sr' => 'sr',
2526 'sr-BA' => 'sr-BA',
2527 'sr-SP' => 'sr-SP',
2528 'ss' => 'ss',
2529 'st' => 'st',
2530 'su' => 'su',
2531 'sv' => 'sv',
2532 'sv-FI' => 'sv-FI',
2533 'sv-SE' => 'sv-SE',
2534 'sw' => 'sw',
2535 'sx' => 'sx',
2536 'syr' => 'syr',
2537 'ta' => 'ta',
2538 'te' => 'te',
2539 'tg' => 'tg',
2540 'th' => 'th',
2541 'ti' => 'ti',
2542 'tk' => 'tk',
2543 'tl' => 'tl',
2544 'tn' => 'tn',
2545 'to' => 'to',
2546 'tr' => 'tr',
2547 'ts' => 'ts',
2548 'tt' => 'tt',
2549 'tw' => 'tw',
2550 'ty' => 'ty',
2551 'ug' => 'ug',
2552 'uk' => 'uk',
2553 'ur' => 'ur',
2554 'us' => 'us',
2555 'uz' => 'uz',
2556 've' => 've',
2557 'vi' => 'vi',
2558 'vo' => 'vo',
2559 'wa' => 'wa',
2560 'wo' => 'wo',
2561 'xh' => 'xh',
2562 'yi' => 'yi',
2563 'yo' => 'yo',
2564 'za' => 'za',
2565 'zh' => 'zh',
2566 'zh-CN' => 'zh-CN',
2567 'zh-HK' => 'zh-HK',
2568 'zh-MO' => 'zh-MO',
2569 'zh-SG' => 'zh-SG',
2570 'zh-TW' => 'zh-TW',
2571 'zu' => 'zu',
2572 );
2573 }
2574
2575 // load metaboxes
2576 public function load_metaboxes() {
2577 add_meta_box(
2578 'e2pdf_templates_save', __('Preset', 'e2pdf'), array($this, 'render_metabox'), null, 'side', 'default', array('tpl' => 'e2pdf_templates_save')
2579 );
2580 add_meta_box(
2581 'e2pdf_templates_builder', __('PDF Builder', 'e2pdf'), array($this, 'render_metabox'), null, 'side', 'default', array('tpl' => 'e2pdf_templates_builder')
2582 );
2583 add_meta_box(
2584 'e2pdf_templates_settings', __('Settings', 'e2pdf'), array($this, 'render_metabox'), null, 'side', 'default', array('tpl' => 'e2pdf_templates_settings')
2585 );
2586 }
2587
2588 // load scripts
2589 public function load_scripts() {
2590 wp_enqueue_script('postbox');
2591 wp_enqueue_script('jquery-ui-droppable');
2592 wp_enqueue_script('jquery-ui-resizable');
2593 wp_enqueue_script('jquery-ui-selectable');
2594 wp_enqueue_script('jquery-ui-dialog');
2595 wp_enqueue_script('jquery-ui-autocomplete');
2596 wp_enqueue_script('wp-color-picker');
2597 wp_enqueue_media();
2598 }
2599
2600 // load styles
2601 public function load_styles($ext = false) {
2602 $version = get_option('e2pdf_debug', '0') === '1' ? strtotime('now') : $this->helper->get('version');
2603 wp_enqueue_style('css/e2pdf.jquery-ui', plugins_url('css/jquery-ui.css', $this->helper->get('plugin_file_path')), false, $version, false);
2604 wp_enqueue_style('wp-color-picker');
2605 if ($ext) {
2606 $extension = new Model_E2pdf_Extension();
2607 if ($extension->load($ext)) {
2608 $styles = $extension->styles();
2609 if ($styles && is_array($styles)) {
2610 foreach ($styles as $key => $value) {
2611 wp_enqueue_style('e2pdf-dynamic-style-' . $key, $value, false, $version, false);
2612 }
2613 }
2614 }
2615 }
2616 }
2617 }
2618