PluginProbe
E2Pdf – Export Pdf Tool for WordPress / 1.32.26
E2Pdf – Export Pdf Tool for WordPress v1.32.26
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.26, at classes/controller/e2pdf-templates.php

2,624 lines 119.5 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="%1$s">View</a> | <a target="_blank" href="%2$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(
849 [
850 'page' => 'e2pdf-templates',
851 'action' => 'edit',
852 'id' => $template->get('ID')
853 ]
854 )
855 )
856 );
857 } else {
858 $this->add_notification('error', __('Something went wrong!', 'e2pdf'));
859 }
860 } else {
861 foreach ($errors as $key => $error) {
862 $this->add_notification('error', $error);
863 }
864 }
865 }
866 unlink($import['tmp_name']);
867 }
868 }
869
870 $this->view('import_disabled', false);
871 if (!$this->helper->load('xml')->check()) {
872 $this->add_notification('error', sprintf(__('The PHP %s extension is required', 'e2pdf'), 'SimpleXml'));
873 $this->view('import_disabled', true);
874 }
875
876 global $wpdb;
877
878 $order_condition = array(
879 'orderby' => 'ID',
880 'order' => 'desc',
881 );
882 $orderby = $this->helper->load('db')->prepare_orderby($order_condition);
883 $tpls = $wpdb->get_results($wpdb->prepare('SELECT `ID`, `title` FROM `' . (new Model_E2pdf_Template())->get_table() . '`' . $orderby . ''));
884 $templates = [
885 '' => __('--- Select ---', 'e2pdf')
886 ];
887 if (!empty($tpls)) {
888 foreach ($tpls as $tpl) {
889 $templates[$tpl->ID] = $tpl->ID . ' | ' . $tpl->title;
890 }
891 }
892
893 $options = array(
894 'overwrite' => [
895 'name' => __('Overwrite Options', 'e2pdf'),
896 'options' => [
897 [
898 'name' => __('Overwrite Template by ID', 'e2pdf'),
899 'key' => 'options[overwrite]',
900 'value' => '',
901 'default_value' => '',
902 'type' => 'select',
903 'options' => [
904 '' => __('Create New E2Pdf Template ID', 'e2pdf'),
905 '1' => __('Existing Template ID', 'e2pdf'),
906 '2' => __('Custom Template ID', 'e2pdf'),
907 '3' => __('Backup Template ID', 'e2pdf'),
908 ]
909 ],
910 [
911 'name' => __('Template ID', 'e2pdf'),
912 'key' => 'options[template_id]',
913 'value' => '',
914 'default_value' => '',
915 'type' => 'select',
916 'options' => $templates,
917 'li' => array(
918 'class' => 'e2pdf-import-overwrite-option e2pdf-hide',
919 ),
920 ],
921 [
922 'name' => __('Template ID', 'e2pdf'),
923 'key' => 'options[custom_template_id]',
924 'value' => '',
925 'default_value' => '',
926 'type' => 'text',
927 'class' => 'e2pdf-numbers',
928 'placeholder' => __('Template ID', 'e2pdf'),
929 'li' => array(
930 'class' => 'e2pdf-import-overwrite-option e2pdf-hide',
931 ),
932 ],
933 ],
934 ],
935 'template' => array(
936 'name' => __('Template Options', 'e2pdf'),
937 'options' => array(
938 array(
939 'name' => __('Import Images', 'e2pdf'),
940 'key' => 'options[images]',
941 'value' => '1',
942 'default_value' => '0',
943 'type' => 'checkbox',
944 'placeholder' => '',
945 ),
946 array(
947 'name' => __('Import Fonts', 'e2pdf'),
948 'key' => 'options[fonts]',
949 'value' => '1',
950 'default_value' => '0',
951 'type' => 'checkbox',
952 'placeholder' => '',
953 ),
954 ),
955 ),
956 'item' => array(
957 'name' => __('Connection Options', 'e2pdf'),
958 'options' => array(
959 array(
960 'name' => __('Import Connection', 'e2pdf'),
961 'key' => 'options[item]',
962 'value' => '0',
963 'default_value' => '0',
964 'type' => 'checkbox',
965 'placeholder' => '',
966 'class' => 'e2pdf-collapse',
967 'data-collapse' => 'e2pdf-import-extension-option',
968 ),
969 ),
970 ),
971 );
972
973 $options = apply_filters('e2pdf_controller_templates_import_options', $options);
974 $this->view('options', $options);
975 $this->view('upload_max_filesize', $this->helper->load('files')->get_upload_max_filesize());
976 }
977
978 /**
979 * @url admin.php?page=e2pdf-templates&action=backup&id=$id
980 */
981 public function download_action() {
982 if ($this->post->get('_wpnonce')) {
983 if (wp_verify_nonce($this->post->get('_wpnonce'), 'e2pdf_templates')) {
984 if ($this->helper->load('xml')->check()) {
985 $options = $this->post->get('options');
986 $xml = $this->helper->load('xml')->create('backup');
987 $template = new Model_E2pdf_Template();
988 if ($template->load($this->post->get('id'))) {
989 if ($template->get('title') != '') {
990 $filename = $template->get('ID') . ' - ' . $template->get('title') . '.' . date('Y-m-d.H-i-s') . '.e2pdf';
991 } else {
992 $filename = date('Y-m-d.H-i-s') . '.e2pdf';
993 }
994 $pages = $template->get('pages');
995 if ($options['images']) {
996 foreach ($pages as $page_key => $page) {
997 if (isset($page['elements']) && !empty($page['elements'])) {
998 foreach ($page['elements'] as $element_key => $element) {
999 if ($element['type'] === 'e2pdf-image') {
1000 $file = false;
1001 if ($this->helper->load('pdf')->get_extension($element['value'])) {
1002 $file = $this->helper->load('pdf')->get_pdf($element['value']);
1003 } else {
1004 $file = $this->helper->load('image')->get_image($element['value']);
1005 }
1006 if ($file) {
1007 $pages[$page_key]['elements'][$element_key]['base64'] = $file;
1008 }
1009 }
1010 }
1011 }
1012 }
1013 }
1014
1015 /*
1016 * Build Xml
1017 */
1018 $xml->addChildCData('version', $this->helper->get('version'));
1019 $xml->addChildCData('date', date('Y-m-d H:i:s'));
1020
1021 $options_xml = $xml->addChild('options');
1022 foreach ($options as $option_key => $option) {
1023 $options_xml->addChildCData($option_key, $option);
1024 }
1025 $template_xml = $xml->addChild('template');
1026 $template_xml->addChildCData('ID', $template->get('ID'));
1027 $template_xml->addChildCData('title', $template->get('title'));
1028 $template_xml->addChildCData('created_at', $template->get('created_at'));
1029 $template_xml->addChildCData('updated_at', $template->get('updated_at'));
1030 $template_xml->addChildCData('flatten', $template->get('flatten'));
1031 $template_xml->addChildCData('tab_order', $template->get('tab_order'));
1032 $template_xml->addChildCData('compression', $template->get('compression'));
1033 $template_xml->addChildCData('optimization', $template->get('optimization'));
1034 $template_xml->addChildCData('appearance', $template->get('appearance'));
1035 $template_xml->addChildCData('width', $template->get('width'));
1036 $template_xml->addChildCData('height', $template->get('height'));
1037 $template_xml->addChildCData('extension', $template->get('extension'));
1038 $template_xml->addChildCData('item', $template->get('item'));
1039 $template_xml->addChildCData('item1', $template->get('item1'));
1040 $template_xml->addChildCData('item2', $template->get('item2'));
1041 $template_xml->addChildCData('format', $template->get('format'));
1042 $template_xml->addChildCData('resample', $template->get('resample'));
1043 $template_xml->addChildCData(
1044 'dataset_title',
1045 apply_filters('e2pdf_controller_templates_backup_replace_shortcodes', $template->get('dataset_title'), $options, $template, $template->extension())
1046 );
1047 $template_xml->addChildCData(
1048 'dataset_title1',
1049 apply_filters('e2pdf_controller_templates_backup_replace_shortcodes', $template->get('dataset_title1'), $options, $template, $template->extension())
1050 );
1051 $template_xml->addChildCData(
1052 'dataset_title2',
1053 apply_filters('e2pdf_controller_templates_backup_replace_shortcodes', $template->get('dataset_title2'), $options, $template, $template->extension())
1054 );
1055 $template_xml->addChildCData(
1056 'button_title',
1057 apply_filters('e2pdf_controller_templates_backup_replace_shortcodes', $template->get('button_title'), $options, $template, $template->extension())
1058 );
1059 $template_xml->addChildCData(
1060 'dpdf',
1061 apply_filters('e2pdf_controller_templates_backup_replace_shortcodes', $template->get('dpdf'), $options, $template, $template->extension())
1062 );
1063 $template_xml->addChildCData(
1064 'attachments',
1065 apply_filters('e2pdf_controller_templates_backup_replace_shortcodes', $template->get('attachments'), $options, $template, $template->extension())
1066 );
1067 $template_xml->addChildCData('inline', $template->get('inline'));
1068 $template_xml->addChildCData('auto', $template->get('auto'));
1069 $template_xml->addChildCData('rtl', $template->get('rtl'));
1070 $template_xml->addChildCData('font_processor', $template->get('font_processor'));
1071 $template_xml->addChildCData(
1072 'name',
1073 apply_filters('e2pdf_controller_templates_backup_replace_shortcodes', $template->get('name'), $options, $template, $template->extension())
1074 );
1075 $template_xml->addChildCData(
1076 'savename',
1077 apply_filters('e2pdf_controller_templates_backup_replace_shortcodes', $template->get('savename'), $options, $template, $template->extension())
1078 );
1079 $template_xml->addChildCData(
1080 'password',
1081 apply_filters('e2pdf_controller_templates_backup_replace_shortcodes', $template->get('password'), $options, $template, $template->extension())
1082 );
1083 $template_xml->addChildCData(
1084 'owner_password',
1085 apply_filters('e2pdf_controller_templates_backup_replace_shortcodes', $template->get('owner_password'), $options, $template, $template->extension())
1086 );
1087 $template_xml->addChildCData('permissions', base64_encode(serialize($template->get('permissions'))));
1088 $template_xml->addChildCData('hooks', $template->get('hooks'));
1089 $template_xml->addChildCData(
1090 'meta_title',
1091 apply_filters('e2pdf_controller_templates_backup_replace_shortcodes', $template->get('meta_title'), $options, $template, $template->extension())
1092 );
1093 $template_xml->addChildCData(
1094 'meta_subject',
1095 apply_filters('e2pdf_controller_templates_backup_replace_shortcodes', $template->get('meta_subject'), $options, $template, $template->extension())
1096 );
1097 $template_xml->addChildCData(
1098 'meta_author',
1099 apply_filters('e2pdf_controller_templates_backup_replace_shortcodes', $template->get('meta_author'), $options, $template, $template->extension())
1100 );
1101 $template_xml->addChildCData(
1102 'meta_keywords',
1103 apply_filters('e2pdf_controller_templates_backup_replace_shortcodes', $template->get('meta_keywords'), $options, $template, $template->extension())
1104 );
1105 $template_xml->addChildCData('lang_code', $template->get('lang_code'));
1106 $template_xml->addChildCData('font', $template->get('font'));
1107 $template_xml->addChildCData('font_size', $template->get('font_size'));
1108 $template_xml->addChildCData('font_color', $template->get('font_color'));
1109 $template_xml->addChildCData('line_height', $template->get('line_height'));
1110 $template_xml->addChildCData('text_align', $template->get('text_align'));
1111 $template_xml->addChildCData('fonts', base64_encode(serialize($template->get('fonts'))));
1112 $template_xml->addChildCData(
1113 'properties', base64_encode(
1114 serialize(
1115 apply_filters('e2pdf_controller_templates_backup_properties', $template->get('properties'), $options, $template, $template->extension())
1116 )
1117 )
1118 );
1119 $template_xml->addChildCData(
1120 'actions', base64_encode(
1121 serialize(
1122 apply_filters('e2pdf_controller_templates_backup_actions', $template->get('actions'), $options, $template, $template->extension())
1123 )
1124 )
1125 );
1126 $template_xml->addChildCData('pages', base64_encode(
1127 serialize(
1128 apply_filters('e2pdf_controller_templates_backup_pages', $pages, $options, $template, $template->extension())
1129 )
1130 )
1131 );
1132
1133 // include pdf
1134 if ($template->get('pdf')) {
1135 $pdf_xml = $xml->addChild('pdf');
1136 $pdf_xml->addChildCData('source', base64_encode(file_get_contents($this->helper->get('pdf_dir') . $template->get('pdf') . '/' . $template->get('pdf') . '.pdf')));
1137 $pdf_xml_images = $pdf_xml->addChild('images');
1138 foreach ($template->get('pages') as $key => $page) {
1139 $pdf_xml_image = $pdf_xml_images->addChild('image');
1140 $pdf_xml_image->addChildCData('page_id', $page['page_id']);
1141 $pdf_xml_image->addChildCData('source', base64_encode(file_get_contents($this->helper->get('pdf_dir') . $template->get('pdf') . '/images/' . $page['page_id'] . '.png')));
1142 }
1143 }
1144
1145 // include fonts
1146 if ($options['fonts']) {
1147 $fonts_xml = $xml->addChild('fonts');
1148 if ($template->get('fonts')) {
1149 $model_e2pdf_font = new Model_E2pdf_Font();
1150 foreach ($template->get('fonts') as $key => $value) {
1151 $font = $fonts_xml->addChild('font');
1152 $font->addChildCData('title', $value);
1153 $font->addChildCData('name', $key);
1154 $font->addChildCData('value', $model_e2pdf_font->get_font($key));
1155 }
1156 }
1157 }
1158
1159 // include item
1160 if ($options['item']) {
1161 if ($template->extension()->method('backup')) {
1162 $item = $xml->addChild('item');
1163 $template->extension()->backup($item);
1164 }
1165 }
1166 $this->download_response('xml', $this->helper->load('xml')->get_xml(), $filename);
1167 exit;
1168 } else {
1169 $this->add_notification('error', __('Something went wrong!', 'e2pdf'));
1170 $this->render('blocks', 'notifications');
1171 }
1172 }
1173 } else {
1174 wp_die($this->message('wp_verify_nonce_error'));
1175 }
1176 }
1177
1178 $this->view('export_disabled', false);
1179
1180 if (!$this->helper->load('xml')->check()) {
1181 $this->add_notification('error', sprintf(__('The PHP %s extension is required', 'e2pdf'), 'SimpleXml'));
1182 $this->view('export_disabled', true);
1183 }
1184
1185 $template = new Model_E2pdf_Template();
1186 if ($template->load($this->get->get('id'))) {
1187
1188 $this->view('template', $template);
1189
1190 $options = array(
1191 'common' => array(
1192 'name' => __('Options', 'e2pdf'),
1193 'options' => array(
1194 array(
1195 'name' => __('Include Connection', 'e2pdf'),
1196 'key' => 'options[item]',
1197 'value' => $template->extension()->method('backup') ? '1' : '0',
1198 'default_value' => 0,
1199 'type' => 'checkbox',
1200 'placeholder' => '',
1201 'disabled' => $template->extension()->method('backup') ? false : 'disabled',
1202 ),
1203 array(
1204 'name' => __('Include Images', 'e2pdf'),
1205 'key' => 'options[images]',
1206 'value' => 1,
1207 'default_value' => 0,
1208 'type' => 'checkbox',
1209 'placeholder' => '',
1210 ),
1211 array(
1212 'name' => __('Include Fonts', 'e2pdf'),
1213 'key' => 'options[fonts]',
1214 'value' => 1,
1215 'default_value' => 0,
1216 'type' => 'checkbox',
1217 'placeholder' => '',
1218 ),
1219 ),
1220 ),
1221 );
1222
1223 $options = apply_filters('e2pdf_controller_templates_backup_options', $options, $template, $template->extension());
1224 $this->view('options', $options);
1225 } else {
1226 $this->add_notification('error', __('Something went wrong!', 'e2pdf'));
1227 $this->render('blocks', 'notifications');
1228 }
1229 }
1230
1231 // screen action
1232 public function screen_action() {
1233 $option = $this->post->get('wp_screen_options');
1234 if (is_array($option) && isset($option['option']) && isset($option['value']) && $option['value']) {
1235 update_option($option['option'], $option['value']);
1236 }
1237 $this->redirect(
1238 $this->helper->get_url(
1239 [
1240 'page' => 'e2pdf-templates',
1241 ]
1242 )
1243 );
1244 }
1245
1246 // ajax save form
1247 public function ajax_save_form() {
1248 global $wpdb;
1249 if (wp_verify_nonce($this->get->get('_wpnonce'), 'e2pdf_templates')) {
1250 $data = json_decode($this->post->get('data'), true);
1251 $template = new Model_E2pdf_Template();
1252 if (isset($data['ID'])) {
1253 $template->load($data['ID']);
1254 }
1255 if (!isset($data['permissions'])) {
1256 $data['permissions'] = array();
1257 }
1258 foreach ($data as $key => $value) {
1259 if ($key == 'pdf' && $value) {
1260 if (file_exists($this->helper->get('pdf_dir') . $value . '/' . $value . '.pdf')) {
1261 $template->set($key, $value);
1262 }
1263 } else {
1264 $template->set($key, $value);
1265 }
1266 }
1267 $template_id = $template->save(true);
1268 if (!$template_id) {
1269 if ($wpdb->last_error) {
1270 $response = array(
1271 'error' => $wpdb->last_error,
1272 );
1273 } else {
1274 $response = array(
1275 'error' => __('Something went wrong!', 'e2pdf'),
1276 );
1277 }
1278 } else {
1279 if (isset($data['item']) && $data['item'] == '-1') {
1280 $template->load($template_id);
1281 if ($template->extension()->method('auto_form')) {
1282 $template = $template->extension()->auto_form($template, $data);
1283 } else {
1284 $template->set('item', '');
1285 }
1286 $template->save(true);
1287 }
1288 if (!isset($data['ID'])) {
1289 $template->activate();
1290 }
1291 $response = array(
1292 'redirect' => $this->helper->get_url(
1293 array(
1294 'page' => 'e2pdf-templates',
1295 'action' => 'edit',
1296 'id' => $template->get('ID'),
1297 'notification' => $this->add_notification('update', __('Template Saved', 'e2pdf')),
1298 )
1299 ),
1300 );
1301 }
1302 } else {
1303 $response['error'] = $this->message('wp_verify_nonce_error');
1304 }
1305 $this->json_response($response);
1306 }
1307
1308 public function ajax_get_styles() {
1309
1310 if (wp_verify_nonce($this->get->get('_wpnonce'), 'e2pdf_templates')) {
1311 $data = $this->post->get('data');
1312 $content = array();
1313 if (isset($data['extension']) && isset($data['item'])) {
1314 $extension = new Model_E2pdf_Extension();
1315 if ($extension->load($data['extension'])) {
1316 $content = $extension->styles($data['item']);
1317 }
1318 }
1319 $response = array(
1320 'content' => $content,
1321 );
1322 } else {
1323 $response['error'] = $this->message('wp_verify_nonce_error');
1324 }
1325 $this->json_response($response);
1326 }
1327
1328 // ajax extension
1329 public function ajax_extension() {
1330 if (wp_verify_nonce($this->get->get('_wpnonce'), 'e2pdf_templates')) {
1331 $data = $this->post->get('data');
1332 $extension = new Model_E2pdf_Extension();
1333 $content = array();
1334 if ($extension->load($data)) {
1335 $content = $extension->items();
1336 if ($extension->method('merged_items')) {
1337 $select = new stdClass();
1338 $select->id = '-2';
1339 $select->url = '';
1340 $select->name = __('Merged Connections', 'e2pdf');
1341 array_unshift($content, $select);
1342 }
1343 if ($extension->method('auto_form')) {
1344 $select = new stdClass();
1345 $select->id = '-1';
1346 $select->url = '';
1347 $select->name = __('Auto Form from PDF', 'e2pdf');
1348 array_unshift($content, $select);
1349 }
1350 }
1351 $select = new stdClass();
1352 $select->id = '';
1353 $select->url = '';
1354 $select->name = __('--- Select ---', 'e2pdf');
1355 array_unshift($content, $select);
1356 $response = array(
1357 'content' => $content,
1358 );
1359 } else {
1360 $response['error'] = $this->message('wp_verify_nonce_error');
1361 }
1362 $this->json_response($response);
1363 }
1364
1365 // ajax visual mapper
1366 public function ajax_visual_mapper() {
1367 if (wp_verify_nonce($this->get->get('_wpnonce'), 'e2pdf_templates')) {
1368 $data = $this->post->get('data');
1369 $item = isset($data['item']) ? $data['item'] : '';
1370 $item1 = isset($data['item1']) ? $data['item1'] : '';
1371 $item2 = isset($data['item2']) ? $data['item2'] : '';
1372 $extension = new Model_E2pdf_Extension();
1373 $extension->load($data['extension']);
1374 $extension->set('item', $item);
1375 if ($item == '-2') {
1376 $extension->set('item1', $item1);
1377 $extension->set('item2', $item2);
1378 }
1379 $visual_mapper = $extension->visual_mapper();
1380 if ($visual_mapper) {
1381 $response = array(
1382 'content' => $visual_mapper,
1383 );
1384 } elseif (!$item || ($item == '-2' && !$item1 && !$item2)) {
1385 $response = array(
1386 'content' => __('You must set the Connection to use the Visual Mapper', 'e2pdf'),
1387 );
1388 } else {
1389 $response = array(
1390 'content' => __("Sorry, this Extension doesn't support the Visual Mapper", 'e2pdf'),
1391 );
1392 }
1393 } else {
1394 $response['error'] = $this->message('wp_verify_nonce_error');
1395 }
1396 $this->json_response($response);
1397 }
1398
1399 // ajax upload
1400 public function ajax_upload() {
1401
1402 if (!wp_verify_nonce($this->get->get('_wpnonce'), 'e2pdf_templates')) {
1403 $this->json_response(
1404 [
1405 'error' => $this->message('wp_verify_nonce_error'),
1406 ]
1407 );
1408 }
1409
1410 $data = $this->post->get();
1411 $post_extension = $data['extension'];
1412 $item = isset($data['item']) ? $data['item'] : '';
1413 $template_id = isset($data['template_id']) && $data['template_id'] ? $data['template_id'] : false;
1414 $font = isset($data['font']) ? $data['font'] : false;
1415 $font_size = isset($data['font_size']) ? $data['font_size'] : false;
1416 $line_height = isset($data['line_height']) ? $data['line_height'] : false;
1417 $title = isset($data['title']) ? $data['title'] : __('(no title)', 'e2pdf');
1418 $rtl = isset($data['rtl']) && $data['rtl'] ? '1' : '0';
1419 $text_align = isset($data['text_align']) ? $data['text_align'] : 'left';
1420 $pdf = $this->files->get('pdf');
1421 $name = strtolower($pdf['name']);
1422 $ext = strtolower(pathinfo($name, PATHINFO_EXTENSION));
1423
1424 if (!empty($pdf['error'])) {
1425 $this->json_response(
1426 [
1427 'error' => $pdf['error'],
1428 ]
1429 );
1430 } elseif (!in_array($ext, array('pdf'))) {
1431 $this->json_response(
1432 [
1433 'error' => sprintf(__('Only %s files allowed', 'e2pdf'), '.pdf'),
1434 ]
1435 );
1436 } elseif ($pdf['type'] != 'application/pdf') {
1437 $this->json_response(
1438 [
1439 'error' => __('Invalid Type', 'e2pdf'),
1440 ]
1441 );
1442 }
1443
1444 wp_raise_memory_limit('admin');
1445 if (get_option('e2pdf_api_protocol', '0') == '1') {
1446 $upload = class_exists('CURLFile') ? new CURLFile(realpath($pdf['tmp_name'])) : '@' . realpath($pdf['tmp_name']);
1447 } else {
1448 $upload = base64_encode(file_get_contents($pdf['tmp_name']));
1449 }
1450
1451 $model_e2pdf_api = new Model_E2pdf_Api();
1452 $model_e2pdf_api->set(
1453 array(
1454 'action' => 'template/upload2',
1455 'data' => array(
1456 'title' => $name,
1457 'pdf' => $upload,
1458 ),
1459 )
1460 );
1461 $request = $model_e2pdf_api->request();
1462
1463 if (isset($request['error'])) {
1464 $this->json_response(
1465 [
1466 'error' => $request['error'],
1467 ]
1468 );
1469 }
1470
1471 $extension = new Model_E2pdf_Extension();
1472 if ($post_extension) {
1473 $extension->load($post_extension);
1474 if ($item && $item != '-1') {
1475 $extension->set('item', $item);
1476 }
1477 }
1478
1479 $pdf_name = md5(time());
1480 $pdf_dir = $this->helper->get('pdf_dir') . $pdf_name . '/';
1481 $pdf_images_dir = $pdf_dir . 'images/';
1482 $this->helper->create_dir($pdf_dir);
1483 $this->helper->create_dir($pdf_images_dir);
1484 move_uploaded_file($pdf['tmp_name'], $pdf_dir . $pdf_name . '.pdf');
1485
1486 try {
1487 $xml_template = [];
1488 $pages = [];
1489 if (class_exists('XMLReader')) {
1490 $reader = new XMLReader();
1491 if (!$reader->XML($request['file'])) {
1492 throw new Exception(__('Failed to parse PDF', 'e2pdf'));
1493 }
1494 while ($reader->read()) {
1495 if ($reader->nodeType == XMLReader::ELEMENT) {
1496 switch ($reader->name) {
1497 case 'template':
1498 $template_node = new SimpleXMLElement($reader->readOuterXML(), LIBXML_PARSEHUGE);
1499 $xml_template = [
1500 'width' => (string) $template_node->width,
1501 'height' => (string) $template_node->height,
1502 'format' => (string) $template_node->format,
1503 'compression' => (string) $template_node->compression,
1504 'appearance' => (string) $template_node->appearance,
1505 'meta_title' => (string) $template_node->meta_title,
1506 'meta_subject' => (string) $template_node->meta_subject,
1507 'meta_author' => (string) $template_node->meta_author,
1508 'meta_keywords' => (string) $template_node->meta_keywords,
1509 'lang_code' => (string) $template_node->lang_code,
1510 'font' => (string) $template_node->font,
1511 'font_size' => (string) $template_node->font_size,
1512 'font_color' => (string) $template_node->font_color,
1513 ];
1514 break;
1515 case 'page':
1516 $page_node = new SimpleXMLElement($reader->readOuterXML(), LIBXML_PARSEHUGE);
1517 $this->helper->load('xml')->parse_xml_page($page_node, $pages, $pdf_images_dir, $extension);
1518 break;
1519 case 'fonts':
1520 $fonts_node = new SimpleXMLElement($reader->readOuterXML(), LIBXML_PARSEHUGE);
1521 foreach ($fonts_node->children() as $xml_font) {
1522 $this->helper->load('xml')->parse_xml_font($xml_font);
1523 }
1524 break;
1525 default:
1526 break;
1527 }
1528 }
1529 }
1530
1531 $reader->close();
1532 unset($reader);
1533 } elseif (is_callable('simplexml_load_string')) {
1534 $xml = simplexml_load_string($request['file'], 'SimpleXMLElement', LIBXML_PARSEHUGE);
1535 if (isset($xml->pages)) {
1536 foreach ($xml->pages->page as $page_node) {
1537 $this->helper->load('xml')->parse_xml_page($page_node, $pages, $pdf_images_dir, $extension);
1538 }
1539 }
1540 if (isset($xml->template)) {
1541 $xml_template = [
1542 'width' => (string) $xml->template->width,
1543 'height' => (string) $xml->template->height,
1544 'format' => (string) $xml->template->format,
1545 'compression' => (string) $xml->template->compression,
1546 'appearance' => (string) $xml->template->appearance,
1547 'meta_title' => (string) $xml->template->meta_title,
1548 'meta_subject' => (string) $xml->template->meta_subject,
1549 'meta_author' => (string) $xml->template->meta_author,
1550 'meta_keywords' => (string) $xml->template->meta_keywords,
1551 'lang_code' => (string) $xml->template->lang_code,
1552 'font' => (string) $xml->template->font,
1553 'font_size' => (string) $xml->template->font_size,
1554 'font_color' => (string) $xml->template->font_color,
1555 ];
1556 }
1557 if (isset($xml->fonts) && $xml->fonts) {
1558 foreach ($xml->fonts->children() as $key => $xml_font) {
1559 $this->helper->load('xml')->parse_xml_font($xml_font);
1560 }
1561 }
1562 }
1563
1564 if (empty($pages) || !is_array($pages)) {
1565 throw new Exception(__('Failed to parse PDF pages', 'e2pdf'));
1566 }
1567
1568 $this->helper->load('sort')->stable_uasort($pages, 'sort_by_pageid');
1569
1570 $template = new Model_E2pdf_Template();
1571 if ($template_id) {
1572 $template->load($template_id);
1573 $template->set('title', $title);
1574 $template->set('pages', $pages);
1575 $template->set('pdf', $pdf_name);
1576 $template->set('width', $xml_template['width']);
1577 $template->set('height', $xml_template['height']);
1578 $template->set('extension', $post_extension);
1579 $template->set('item', $item);
1580 $template->set('rtl', $rtl);
1581 $template->set('font', $font ? $font : $xml_template['font']);
1582 $template->set('font_size', $font_size ? $font_size : $xml_template['font_size']);
1583 $template->set('line_height', $line_height ? $line_height : $xml_template['font_size']);
1584 $template->set('text_align', $text_align);
1585 } else {
1586 $template->set('title', $title);
1587 $template->set('flatten', '1');
1588 $template->set('format', $xml_template['format']);
1589 $template->set('compression', $xml_template['compression']);
1590 $template->set('appearance', $xml_template['appearance']);
1591 $template->set('width', $xml_template['width']);
1592 $template->set('height', $xml_template['height']);
1593 $template->set('extension', $post_extension);
1594 $template->set('item', $item);
1595 $template->set('dataset_title', '');
1596 $template->set('button_title', '');
1597 $template->set('dpdf', '');
1598 $template->set('attachments', '');
1599 $template->set('inline', '');
1600 $template->set('auto', '');
1601 $template->set('rtl', $rtl);
1602 $template->set('name', '[e2pdf-dataset]');
1603 $template->set('savename', '[e2pdf-dataset]');
1604 $template->set('password', '');
1605 $template->set('meta_title', $xml_template['meta_title']);
1606 $template->set('meta_subject', $xml_template['meta_subject']);
1607 $template->set('meta_author', $xml_template['meta_author']);
1608 $template->set('meta_keywords', $xml_template['meta_keywords']);
1609 $template->set('lang_code', $xml_template['lang_code']);
1610 $template->set('font', $font ? $font : $xml_template['font']);
1611 $template->set('font_size', $font_size ? $font_size : $xml_template['font_size']);
1612 $template->set('line_height', $line_height ? $line_height : $xml_template['font_size']);
1613 $template->set('text_align', $text_align);
1614 $template->set('font_color', $xml_template['font_color']);
1615 $template->set('pages', $pages);
1616 $template->set('pdf', $pdf_name);
1617 }
1618 $template->save(true);
1619
1620 if ($item == '-1' && $extension->method('auto_form')) {
1621 $template = $extension->auto_form($template, $data);
1622 $template->save(true);
1623 }
1624
1625 if (!$template_id) {
1626 $template->activate();
1627 }
1628
1629 $this->json_response(
1630 [
1631 'redirect' => $this->helper->get_url(
1632 [
1633 'page' => 'e2pdf-templates',
1634 'action' => 'edit',
1635 'id' => $template->get('ID'),
1636 ]
1637 ),
1638 ]
1639 );
1640 } catch (Exception $ex) {
1641 if (file_exists($pdf_dir)) {
1642 $this->helper->delete_dir($pdf_dir);
1643 }
1644 $this->json_response(
1645 [
1646 'error' => $ex->getMessage(),
1647 ]
1648 );
1649 }
1650 }
1651
1652 // ajax reupload
1653 public function ajax_reupload() {
1654
1655 if (!wp_verify_nonce($this->get->get('_wpnonce'), 'e2pdf_templates')) {
1656 $this->json_response(
1657 [
1658 'error' => $this->message('wp_verify_nonce_error'),
1659 ]
1660 );
1661 }
1662
1663 $data = $this->post->get();
1664 $new = isset($data['new']) ? $data['new'] : array();
1665 $flush = isset($data['flush']) ? $data['flush'] : array();
1666 $positions = isset($data['positions']) ? $data['positions'] : array();
1667 $template_id = isset($data['template_id']) && $data['template_id'] ? $data['template_id'] : false;
1668 if (!$template_id) {
1669 return false;
1670 }
1671
1672 $pdf = $this->files->get('pdf');
1673 $name = strtolower($pdf['name']);
1674 $ext = strtolower(pathinfo($name, PATHINFO_EXTENSION));
1675
1676 if (!empty($pdf['error'])) {
1677 $this->json_response(
1678 [
1679 'error' => $pdf['error'],
1680 ]
1681 );
1682 } elseif (!in_array($ext, array('pdf'))) {
1683 $this->json_response(
1684 [
1685 'error' => sprintf(__('Only %s files allowed', 'e2pdf'), '.pdf'),
1686 ]
1687 );
1688 } elseif ($pdf['type'] != 'application/pdf') {
1689 $this->json_response(
1690 [
1691 'error' => __('Invalid Type', 'e2pdf'),
1692 ]
1693 );
1694 }
1695
1696 wp_raise_memory_limit('admin');
1697 if (get_option('e2pdf_api_protocol', '0') == '1') {
1698 $upload = class_exists('CURLFile') ? new CURLFile(realpath($pdf['tmp_name'])) : '@' . realpath($pdf['tmp_name']);
1699 } else {
1700 $upload = base64_encode(file_get_contents($pdf['tmp_name']));
1701 }
1702
1703 $model_e2pdf_api = new Model_E2pdf_Api();
1704 $model_e2pdf_api->set(
1705 array(
1706 'action' => 'template/upload2',
1707 'data' => array(
1708 'title' => $name,
1709 'pdf' => $upload,
1710 ),
1711 )
1712 );
1713 $request = $model_e2pdf_api->request();
1714
1715 if (isset($request['error'])) {
1716 $this->json_response(
1717 [
1718 'error' => $request['error'],
1719 ]
1720 );
1721 }
1722
1723 $template = new Model_E2pdf_Template();
1724 if (!$template->load($template_id)) {
1725 $this->json_response(
1726 [
1727 'error' => __('Failed to load E2Pdf Template', 'e2pdf'),
1728 ]
1729 );
1730 }
1731
1732 $extension = new Model_E2pdf_Extension();
1733 if ($template->get('extension') && $template->get('item')) {
1734 $extension->load($template->get('extension'));
1735 $extension->set('item', $template->get('item'));
1736 }
1737
1738 $pdf_name = md5(time());
1739 $pdf_dir = $this->helper->get('pdf_dir') . $pdf_name . '/';
1740 $pdf_images_dir = $pdf_dir . 'images/';
1741 $this->helper->create_dir($pdf_dir);
1742 $this->helper->create_dir($pdf_images_dir);
1743 move_uploaded_file($pdf['tmp_name'], $pdf_dir . $pdf_name . '.pdf');
1744
1745 try {
1746 $pages = [];
1747 $pos_pages = [];
1748 if (class_exists('XMLReader')) {
1749 $reader = new XMLReader();
1750 if (!$reader->XML($request['file'])) {
1751 throw new Exception(__('Failed to parse PDF', 'e2pdf'));
1752 }
1753 while ($reader->read()) {
1754 if ($reader->nodeType == XMLReader::ELEMENT) {
1755 if ($reader->name == 'page') {
1756 $page_node = new SimpleXMLElement($reader->readOuterXML(), LIBXML_PARSEHUGE);
1757 $pos_pages[] = (string) $page_node['number'];
1758 }
1759 }
1760 }
1761 $reader->close();
1762 unset($reader);
1763 foreach ($positions as $pos_key => $pos_page) {
1764 if (!in_array($pos_key, $flush)) {
1765 if ($pos_page == '0') {
1766 $flush[] = $pos_key;
1767 } elseif (!in_array($pos_page, $pos_pages, false)) {
1768 throw new Exception(sprintf(__('Invalid position of %s page', 'e2pdf'), $pos_key));
1769 }
1770 }
1771 }
1772 $reader = new XMLReader();
1773 $reader->XML($request['file']);
1774 while ($reader->read()) {
1775 if ($reader->nodeType == XMLReader::ELEMENT) {
1776 switch ($reader->name) {
1777 case 'page':
1778 $page_node = new SimpleXMLElement($reader->readOuterXML(), LIBXML_PARSEHUGE);
1779 $this->helper->load('xml')->parse_xml_page($page_node, $pages, $pdf_images_dir, $extension);
1780 break;
1781 default:
1782 break;
1783 }
1784 }
1785 }
1786 $reader->close();
1787 unset($reader);
1788 } elseif (is_callable('simplexml_load_string')) {
1789 $xml = simplexml_load_string($request['file'], 'SimpleXMLElement', LIBXML_PARSEHUGE);
1790 if (isset($xml->pages)) {
1791 foreach ($xml->pages->page as $page_node) {
1792 $pos_pages[] = (string) $page_node['number'];
1793 }
1794 }
1795 foreach ($positions as $pos_key => $pos_page) {
1796 if (!in_array($pos_key, $flush)) {
1797 if ($pos_page == '0') {
1798 $flush[] = $pos_key;
1799 } elseif (!in_array($pos_page, $pos_pages, false)) {
1800 throw new Exception(sprintf(__('Invalid position of %s page', 'e2pdf'), $pos_key));
1801 }
1802 }
1803 }
1804 if (isset($xml->pages)) {
1805 foreach ($xml->pages->page as $page_node) {
1806 $this->helper->load('xml')->parse_xml_page($page_node, $pages, $pdf_images_dir, $extension);
1807 }
1808 }
1809 }
1810
1811 if (empty($pages) || !is_array($pages)) {
1812 throw new Exception(__('Failed to parse PDF pages', 'e2pdf'));
1813 }
1814
1815 $this->helper->load('sort')->stable_uasort($pages, 'sort_by_pageid');
1816
1817 $model_e2pdf_element = new Model_E2pdf_Element();
1818 $last_element_id = $model_e2pdf_element->get_last_element_id($template_id);
1819 $original_pages = $template->get('pages');
1820
1821 foreach ($pages as $page_key => $page) {
1822 if (isset($page['elements']) && !empty($page['elements'])) {
1823 foreach ($page['elements'] as $element_key => $element) {
1824 $last_element_id++;
1825 $pages[$page_key]['elements'][$element_key]['element_id'] = $last_element_id;
1826 }
1827 }
1828 foreach ($positions as $pos_key => $pos_page) {
1829 if ($pos_page == $page_key) {
1830 if (in_array($pos_key, $new)) {
1831 if (isset($original_pages[$pos_key]['elements']) && !in_array($pos_key, $flush)) {
1832 $elements = $original_pages[$pos_key]['elements'];
1833 $pages[$page_key]['elements'] = array_merge($pages[$page_key]['elements'], $elements);
1834 }
1835 } else {
1836 if (isset($original_pages[$pos_key]['elements']) && !in_array($pos_key, $flush)) {
1837 $elements = $original_pages[$pos_key]['elements'];
1838 $pages[$page_key]['elements'] = $elements;
1839 } else {
1840 $pages[$page_key]['elements'] = array();
1841 }
1842 }
1843 if (isset($original_pages[$pos_key]['actions'])) {
1844 $pages[$page_key]['actions'] = $original_pages[$pos_key]['actions'];
1845 }
1846 }
1847 }
1848 }
1849
1850 $template->set('pdf', $pdf_name);
1851 $template->set('pages', $pages);
1852 $template->save(true);
1853
1854 $this->json_response(
1855 [
1856 'redirect' => $this->helper->get_url(
1857 array(
1858 'page' => 'e2pdf-templates',
1859 'action' => 'edit',
1860 'id' => $template->get('ID'),
1861 )
1862 ),
1863 ]
1864 );
1865 } catch (Exception $ex) {
1866 if (file_exists($pdf_dir)) {
1867 $this->helper->delete_dir($pdf_dir);
1868 }
1869 $this->json_response(
1870 [
1871 'error' => $ex->getMessage(),
1872 ]
1873 );
1874 }
1875 }
1876
1877 // ajax auto
1878 public function ajax_auto() {
1879 if (wp_verify_nonce($this->get->get('_wpnonce'), 'e2pdf_templates')) {
1880 $data = $this->post->get('data');
1881
1882 $extension = $data['extension'];
1883 $item = isset($data['item']) ? $data['item'] : '';
1884 $item1 = isset($data['item1']) ? $data['item1'] : '';
1885 $item2 = isset($data['item2']) ? $data['item2'] : '';
1886 $font_size = isset($data['font_size']) ? $data['font_size'] : '14';
1887 $line_height = isset($data['line_height']) ? $data['line_height'] : '14';
1888
1889 if (!$extension || !$item) {
1890 return;
1891 }
1892
1893 $model_e2pdf_extension = new Model_E2pdf_Extension();
1894 $model_e2pdf_extension->load($extension);
1895 $model_e2pdf_extension->set('item', $item);
1896 $model_e2pdf_extension->set('item1', $item1);
1897 $model_e2pdf_extension->set('item2', $item2);
1898 $model_e2pdf_extension->set('font_size', $font_size);
1899 $model_e2pdf_extension->set('line_height', $line_height);
1900
1901 if (method_exists($model_e2pdf_extension->extension(), 'auto')) {
1902 $content = $model_e2pdf_extension->auto();
1903 } else {
1904 $content = array();
1905 }
1906
1907 $response = array(
1908 'content' => $content,
1909 );
1910 } else {
1911 $response['error'] = $this->message('wp_verify_nonce_error');
1912 }
1913 $this->json_response($response);
1914 }
1915
1916 // ajax activate template
1917 public function ajax_activate_template() {
1918 if (wp_verify_nonce($this->get->get('_wpnonce'), 'e2pdf_templates')) {
1919 $data = $this->post->get('data');
1920 $id = $data['id'];
1921 if (!$id) {
1922 return;
1923 }
1924 $request = $this->activate_template($id);
1925 if (isset($request['error'])) {
1926 $response = array(
1927 'error' => $request['error'],
1928 );
1929 } else {
1930 $response = array(
1931 'content' => 'activated',
1932 );
1933 }
1934 } else {
1935 $response['error'] = $this->message('wp_verify_nonce_error');
1936 }
1937 $this->json_response($response);
1938 }
1939
1940 // ajax deactivaate template
1941 public function ajax_deactivate_template() {
1942 if (wp_verify_nonce($this->get->get('_wpnonce'), 'e2pdf_templates')) {
1943 $data = $this->post->get('data');
1944 $id = $data['id'];
1945 if (!$id) {
1946 return;
1947 }
1948 $request = $this->deactivate_template($id);
1949 if (isset($request['error'])) {
1950 $response = array(
1951 'error' => $request['error'],
1952 );
1953 } else {
1954 $response = array(
1955 'content' => 'deactivated',
1956 );
1957 }
1958 } else {
1959 $response['error'] = $this->message('wp_verify_nonce_error');
1960 }
1961 $this->json_response($response);
1962 }
1963
1964 // ajax email
1965 public function ajax_email() {
1966 if (wp_verify_nonce($this->get->get('_wpnonce'), 'e2pdf_templates')) {
1967 $data = $this->post->get('data');
1968 $email = isset($data['email']) ? trim($data['email']) : '';
1969 $email_code = isset($data['email_code']) ? trim($data['email_code']) : '';
1970 $model_e2pdf_api = new Model_E2pdf_Api();
1971 $model_e2pdf_api->set(
1972 array(
1973 'action' => 'common/email',
1974 'data' => array(
1975 'email' => $email,
1976 'email_code' => $email_code,
1977 'email_confirm' => isset($data['email_code']),
1978 ),
1979 )
1980 );
1981 $request = $model_e2pdf_api->request();
1982 if (isset($request['error'])) {
1983 if ($request['error'] === 'incorrect_email') {
1984 $response = array(
1985 'error' => __('Invalid E-mail address format', 'e2pdf'),
1986 );
1987 } elseif ($request['error'] === 'incorrect_code') {
1988 $response = array(
1989 'error' => __('Invalid Confirmation Code', 'e2pdf'),
1990 );
1991 } else {
1992 $response = array(
1993 'error' => $request['error'],
1994 );
1995 }
1996 } elseif (isset($request['success'])) {
1997 $response = array(
1998 'content' => $request['success'],
1999 );
2000 if ($request['success'] == 'subscribed') {
2001 update_option('e2pdf_email', $email);
2002 }
2003 }
2004 } else {
2005 $response['error'] = $this->message('wp_verify_nonce_error');
2006 }
2007 $this->json_response($response);
2008 }
2009
2010 // trash template
2011 public function trash_template($id = false) {
2012 $id = (int) $id;
2013 $template = new Model_E2pdf_Template();
2014 if ($id && $template->load($id, false)) {
2015 $template->set('trash', '1');
2016 $template->update(array('trash'));
2017 $this->deactivate_template($template->get('ID'));
2018 return true;
2019 }
2020 return false;
2021 }
2022
2023 // delete template
2024 public function delete_template($id = false) {
2025 $id = (int) $id;
2026 $template = new Model_E2pdf_Template();
2027 if ($id && $template->load($id)) {
2028 $this->deactivate_template($template->get('ID'));
2029 $template->delete();
2030 return true;
2031 }
2032 return false;
2033 }
2034
2035 // restore template
2036 public function restore_template($id = false) {
2037 $id = (int) $id;
2038 $template = new Model_E2pdf_Template();
2039 if ($id && $template->load($id, false)) {
2040 $template->set('trash', '0');
2041 $template->update(array('trash'));
2042 return true;
2043 }
2044 return false;
2045 }
2046
2047 // duplicate template
2048 public function duplicate_template($id = false) {
2049
2050 $id = (int) $id;
2051
2052 $template = new Model_E2pdf_Template();
2053 if ($id && $template->load($id)) {
2054 $title = $template->get('title') . ' ' . __('(Copy)', 'e2pdf');
2055 $template->set('ID', false);
2056 $template->set('title', $title);
2057 $template->set('uid', '');
2058 $template->set('activated', '0');
2059 if ($template->get('pdf')) {
2060 $pdf_name = md5(time());
2061 $pdf_dir = $this->helper->get('pdf_dir') . $pdf_name . '/';
2062 $pdf_images_dir = $pdf_dir . 'images/';
2063
2064 $this->helper->create_dir($pdf_dir);
2065 $this->helper->create_dir($pdf_images_dir);
2066
2067 if (file_exists($this->helper->get('pdf_dir') . $template->get('pdf') . '/' . $template->get('pdf') . '.pdf')) {
2068 $images = glob($this->helper->get('pdf_dir') . $template->get('pdf') . '/images/*');
2069 foreach ($images as $image) {
2070 copy($image, $pdf_images_dir . pathinfo($image, PATHINFO_BASENAME));
2071 }
2072 copy($this->helper->get('pdf_dir') . $template->get('pdf') . '/' . $template->get('pdf') . '.pdf', $this->helper->get('pdf_dir') . $pdf_name . '/' . $pdf_name . '.pdf');
2073 $template->set('pdf', $pdf_name);
2074 }
2075 }
2076 $template->save(true);
2077 return true;
2078 }
2079 return false;
2080 }
2081
2082 // activate template
2083 public function activate_template($id = false) {
2084 $id = (int) $id;
2085 $template = new Model_E2pdf_Template();
2086 if ($id && $template->load($id, false)) {
2087 return $template->activate();
2088 } else {
2089 return array(
2090 'error' => __('Something went wrong!', 'e2pdf'),
2091 );
2092 }
2093 }
2094
2095 // deactivate template
2096 public function deactivate_template($id = false) {
2097 $id = (int) $id;
2098 $template = new Model_E2pdf_Template();
2099 if ($id && $template->load($id, false)) {
2100 return $template->deactivate();
2101 } else {
2102 return array(
2103 'error' => __('Something went wrong!', 'e2pdf'),
2104 );
2105 }
2106 }
2107
2108 // get templates list
2109 public function get_templates_list($filters = array(), $count = false) {
2110 global $wpdb;
2111
2112 $model_e2pdf_template = new Model_E2pdf_Template();
2113 $condition = array();
2114 if (isset($filters['status']) && $filters['status'] == 'trash') {
2115 $condition = array(
2116 'trash' => array(
2117 'condition' => '=',
2118 'value' => '1',
2119 'type' => '%d',
2120 ),
2121 );
2122 } else {
2123 $condition = array(
2124 'trash' => array(
2125 'condition' => '<>',
2126 'value' => '1',
2127 'type' => '%d',
2128 ),
2129 );
2130 }
2131
2132 if (isset($filters['s']) && $filters['s']) {
2133 $condition['title'] = array(
2134 'condition' => 'LIKE',
2135 'value' => '%%' . $filters['s'] . '%%',
2136 'type' => '%s',
2137 'or' => array(
2138 array(
2139 'ID' => array(
2140 'condition' => 'LIKE',
2141 'value' => '%%' . $filters['s'] . '%%',
2142 'type' => '%s',
2143 ),
2144 ),
2145 ),
2146 );
2147 }
2148
2149 $order_condition = array();
2150 $sortable = array('id', 'title', 'updated_at');
2151 if (isset($filters['orderby']) && in_array($filters['orderby'], $sortable) && isset($filters['order'])) {
2152 $order_condition = array(
2153 'orderby' => $filters['orderby'],
2154 'order' => $filters['order'] == 'asc' ? 'asc' : 'desc',
2155 );
2156 } else {
2157 $order_condition = array(
2158 'orderby' => 'id',
2159 'order' => 'desc',
2160 );
2161 }
2162
2163 $limit_condition = array();
2164 if (!$count) {
2165 $paged = isset($filters['paged']) && $filters['paged'] ? $filters['paged'] : '0';
2166 $paged = (int) $paged <= 0 ? 1 : (int) $paged;
2167 $per_page = get_option('e2pdf_templates_screen_per_page', '20');
2168
2169 $limit_condition = array(
2170 'limit' => (int) $per_page,
2171 'offset' => (int) ($paged - 1) * $per_page,
2172 );
2173 }
2174
2175 $where = $this->helper->load('db')->prepare_where($condition);
2176 $orderby = $this->helper->load('db')->prepare_orderby($order_condition);
2177 $limit = $this->helper->load('db')->prepare_limit($limit_condition);
2178 // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
2179 $tpls = $wpdb->get_results($wpdb->prepare('SELECT `ID` FROM `' . $model_e2pdf_template->get_table() . '`' . $where['sql'] . $orderby . $limit . '', $where['filter']));
2180
2181 if ($count) {
2182 return count($tpls);
2183 }
2184
2185 $templates = array();
2186 foreach ($tpls as $key => $tpl) {
2187 $template = new Model_E2pdf_Template();
2188 $template->load($tpl->ID, false);
2189 if (is_array($this->helper->get('license')->get('activated_templates'))) {
2190 if ($template->get('activated') && !in_array($template->get('uid'), $this->helper->get('license')->get('activated_templates'))) {
2191 $template->deactivate();
2192 } elseif (!$template->get('activated') && in_array($template->get('uid'), $this->helper->get('license')->get('activated_templates'))) {
2193 $template->activate();
2194 }
2195 }
2196 $templates[] = $template;
2197 }
2198 return $templates;
2199 }
2200
2201 // get sizes list
2202 public function get_sizes_list($size = false, $attr = false) {
2203
2204 $sizes = array(
2205 'A4 (PORTRAIT)' => array(
2206 'width' => '595',
2207 'height' => '842',
2208 ),
2209 'A4 (LANDSCAPE)' => array(
2210 'width' => '842',
2211 'height' => '595',
2212 ),
2213 'LETTER' => array(
2214 'width' => '612',
2215 'height' => '792',
2216 ),
2217 'NOTE' => array(
2218 'width' => '540',
2219 'height' => '720',
2220 ),
2221 'LEGAL' => array(
2222 'width' => '612',
2223 'height' => '1008',
2224 ),
2225 'TABLOID' => array(
2226 'width' => '792',
2227 'height' => '1224',
2228 ),
2229 'EXECUTIVE' => array(
2230 'width' => '522',
2231 'height' => '756',
2232 ),
2233 'POSTCARD' => array(
2234 'width' => '283',
2235 'height' => '416',
2236 ),
2237 );
2238
2239 if (!$size) {
2240 return $sizes;
2241 } else {
2242 if (isset($sizes[$size][$attr])) {
2243 return $sizes[$size][$attr];
2244 }
2245 return false;
2246 }
2247 }
2248
2249 // get fonts
2250 public function get_fonts($with_path = false) {
2251 $model_e2pdf_font = new Model_E2pdf_Font();
2252 $fonts = $model_e2pdf_font->get_fonts();
2253
2254 if ($with_path) {
2255 foreach ($fonts as $key => $value) {
2256 $fonts[$key] = array(
2257 'key' => $value,
2258 'subfield' => array(
2259 'path' => $key,
2260 ),
2261 'value' => $value,
2262 );
2263 }
2264 }
2265
2266 return $fonts;
2267 }
2268
2269 // get font sizes
2270 public function get_font_sizes() {
2271 $max_font_size = apply_filters('e2pdf_controller_templates_max_font_size', 512);
2272 $sizes = array();
2273 for ($i = 1; $i <= $max_font_size; $i++) {
2274 $sizes[$i] = $i;
2275 }
2276 return $sizes;
2277 }
2278
2279 // get line heights
2280 public function get_line_heights() {
2281
2282 $max_line_height = apply_filters('e2pdf_controller_templates_max_line_height', 512);
2283 $line_heights = array();
2284 for ($i = 1; $i <= $max_line_height; $i++) {
2285 $line_heights[$i] = $i;
2286 }
2287 return $line_heights;
2288 }
2289
2290 // get lang codes
2291 public function get_lang_codes() {
2292 return array(
2293 '' => '',
2294 'aa' => 'aa',
2295 'ab' => 'ab',
2296 'ae' => 'ae',
2297 'af' => 'af',
2298 'ak' => 'ak',
2299 'am' => 'am',
2300 'an' => 'an',
2301 'ar' => 'ar',
2302 'ar-AE' => 'ar-AE',
2303 'ar-BH' => 'ar-BH',
2304 'ar-DZ' => 'ar-DZ',
2305 'ar-EG' => 'ar-EG',
2306 'ar-IQ' => 'ar-IQ',
2307 'ar-JO' => 'ar-JO',
2308 'ar-KW' => 'ar-KW',
2309 'ar-LB' => 'ar-LB',
2310 'ar-LY' => 'ar-LY',
2311 'ar-MA' => 'ar-MA',
2312 'ar-OM' => 'ar-OM',
2313 'ar-QA' => 'ar-QA',
2314 'ar-SA' => 'ar-SA',
2315 'ar-SY' => 'ar-SY',
2316 'ar-TN' => 'ar-TN',
2317 'ar-YE' => 'ar-YE',
2318 'as' => 'as',
2319 'av' => 'av',
2320 'ay' => 'ay',
2321 'az' => 'az',
2322 'ba' => 'ba',
2323 'be' => 'be',
2324 'bg' => 'bg',
2325 'bh' => 'bh',
2326 'bi' => 'bi',
2327 'bm' => 'bm',
2328 'bn' => 'bn',
2329 'bo' => 'bo',
2330 'br' => 'br',
2331 'bs' => 'bs',
2332 'ca' => 'ca',
2333 'ce' => 'ce',
2334 'ch' => 'ch',
2335 'co' => 'co',
2336 'cr' => 'cr',
2337 'cs' => 'cs',
2338 'cu' => 'cu',
2339 'cv' => 'cv',
2340 'cy' => 'cy',
2341 'da' => 'da',
2342 'de' => 'de',
2343 'de-AT' => 'de-AT',
2344 'de-CH' => 'de-CH',
2345 'de-DE' => 'de-DE',
2346 'de-LI' => 'de-LI',
2347 'de-LU' => 'de-LU',
2348 'div' => 'div',
2349 'dv' => 'dv',
2350 'dz' => 'dz',
2351 'ee' => 'ee',
2352 'el' => 'el',
2353 'en' => 'en',
2354 'en-AU' => 'en-AU',
2355 'en-BZ' => 'en-BZ',
2356 'en-CA' => 'en-CA',
2357 'en-CB' => 'en-CB',
2358 'en-GB' => 'en-GB',
2359 'en-IE' => 'en-IE',
2360 'en-JM' => 'en-JM',
2361 'en-NZ' => 'en-NZ',
2362 'en-PH' => 'en-PH',
2363 'en-TT' => 'en-TT',
2364 'en-US' => 'en-US',
2365 'en-ZA' => 'en-ZA',
2366 'en-ZW' => 'en-ZW',
2367 'eo' => 'eo',
2368 'es' => 'es',
2369 'es-AR' => 'es-AR',
2370 'es-BO' => 'es-BO',
2371 'es-CL' => 'es-CL',
2372 'es-CO' => 'es-CO',
2373 'es-CR' => 'es-CR',
2374 'es-DO' => 'es-DO',
2375 'es-EC' => 'es-EC',
2376 'es-ES' => 'es-ES',
2377 'es-GT' => 'es-GT',
2378 'es-HN' => 'es-HN',
2379 'es-MX' => 'es-MX',
2380 'es-NI' => 'es-NI',
2381 'es-PA' => 'es-PA',
2382 'es-PE' => 'es-PE',
2383 'es-PR' => 'es-PR',
2384 'es-PY' => 'es-PY',
2385 'es-SV' => 'es-SV',
2386 'es-US' => 'es-US',
2387 'es-UY' => 'es-UY',
2388 'es-VE' => 'es-VE',
2389 'et' => 'et',
2390 'eu' => 'eu',
2391 'fa' => 'fa',
2392 'ff' => 'ff',
2393 'fi' => 'fi',
2394 'fj' => 'fj',
2395 'fo' => 'fo',
2396 'fr' => 'fr',
2397 'fr-BE' => 'fr-BE',
2398 'fr-CA' => 'fr-CA',
2399 'fr-CH' => 'fr-CH',
2400 'fr-FR' => 'fr-FR',
2401 'fr-LU' => 'fr-LU',
2402 'fr-MC' => 'fr-MC',
2403 'fy' => 'fy',
2404 'ga' => 'ga',
2405 'gd' => 'gd',
2406 'gl' => 'gl',
2407 'gn' => 'gn',
2408 'gu' => 'gu',
2409 'gv' => 'gv',
2410 'ha' => 'ha',
2411 'he' => 'he',
2412 'hi' => 'hi',
2413 'ho' => 'ho',
2414 'hr' => 'hr',
2415 'hr-BA' => 'hr-BA',
2416 'hr-HR' => 'hr-HR',
2417 'ht' => 'ht',
2418 'hu' => 'hu',
2419 'hy' => 'hy',
2420 'hz' => 'hz',
2421 'ia' => 'ia',
2422 'id' => 'id',
2423 'ie' => 'ie',
2424 'ig' => 'ig',
2425 'ii' => 'ii',
2426 'ik' => 'ik',
2427 'in' => 'in',
2428 'io' => 'io',
2429 'is' => 'is',
2430 'it' => 'it',
2431 'it-CH' => 'it-CH',
2432 'it-IT' => 'it-IT',
2433 'iu' => 'iu',
2434 'iw' => 'iw',
2435 'ja' => 'ja',
2436 'ji' => 'ji',
2437 'jv' => 'jv',
2438 'jw' => 'jw',
2439 'ka' => 'ka',
2440 'kg' => 'kg',
2441 'ki' => 'ki',
2442 'kj' => 'kj',
2443 'kk' => 'kk',
2444 'kl' => 'kl',
2445 'km' => 'km',
2446 'kn' => 'kn',
2447 'ko' => 'ko',
2448 'kok' => 'kok',
2449 'kr' => 'kr',
2450 'ks' => 'ks',
2451 'ku' => 'ku',
2452 'kv' => 'kv',
2453 'kw' => 'kw',
2454 'ky' => 'ky',
2455 'kz' => 'kz',
2456 'la' => 'la',
2457 'lb' => 'lb',
2458 'lg' => 'lg',
2459 'li' => 'li',
2460 'ln' => 'ln',
2461 'lo' => 'lo',
2462 'ls' => 'ls',
2463 'lt' => 'lt',
2464 'lu' => 'lu',
2465 'lv' => 'lv',
2466 'mg' => 'mg',
2467 'mh' => 'mh',
2468 'mi' => 'mi',
2469 'mk' => 'mk',
2470 'ml' => 'ml',
2471 'mn' => 'mn',
2472 'mo' => 'mo',
2473 'mr' => 'mr',
2474 'ms' => 'ms',
2475 'ms-BN' => 'ms-BN',
2476 'ms-MY' => 'ms-MY',
2477 'mt' => 'mt',
2478 'my' => 'my',
2479 'na' => 'na',
2480 'nb' => 'nb',
2481 'nd' => 'nd',
2482 'ne' => 'ne',
2483 'ng' => 'ng',
2484 'nl' => 'nl',
2485 'nl-BE' => 'nl-BE',
2486 'nl-NL' => 'nl-NL',
2487 'nn' => 'nn',
2488 'no' => 'no',
2489 'nr' => 'nr',
2490 'ns' => 'ns',
2491 'nv' => 'nv',
2492 'ny' => 'ny',
2493 'oc' => 'oc',
2494 'oj' => 'oj',
2495 'om' => 'om',
2496 'or' => 'or',
2497 'os' => 'os',
2498 'pa' => 'pa',
2499 'pi' => 'pi',
2500 'pl' => 'pl',
2501 'ps' => 'ps',
2502 'pt' => 'pt',
2503 'pt-BR' => 'pt-BR',
2504 'pt-PT' => 'pt-PT',
2505 'qu' => 'qu',
2506 'qu-BO' => 'qu-BO',
2507 'qu-EC' => 'qu-EC',
2508 'qu-PE' => 'qu-PE',
2509 'rm' => 'rm',
2510 'rn' => 'rn',
2511 'ro' => 'ro',
2512 'ru' => 'ru',
2513 'rw' => 'rw',
2514 'sa' => 'sa',
2515 'sb' => 'sb',
2516 'sc' => 'sc',
2517 'sd' => 'sd',
2518 'se' => 'se',
2519 'se-FI' => 'se-FI',
2520 'se-NO' => 'se-NO',
2521 'se-SE' => 'se-SE',
2522 'sg' => 'sg',
2523 'sh' => 'sh',
2524 'si' => 'si',
2525 'sk' => 'sk',
2526 'sl' => 'sl',
2527 'sm' => 'sm',
2528 'sn' => 'sn',
2529 'so' => 'so',
2530 'sq' => 'sq',
2531 'sr' => 'sr',
2532 'sr-BA' => 'sr-BA',
2533 'sr-SP' => 'sr-SP',
2534 'ss' => 'ss',
2535 'st' => 'st',
2536 'su' => 'su',
2537 'sv' => 'sv',
2538 'sv-FI' => 'sv-FI',
2539 'sv-SE' => 'sv-SE',
2540 'sw' => 'sw',
2541 'sx' => 'sx',
2542 'syr' => 'syr',
2543 'ta' => 'ta',
2544 'te' => 'te',
2545 'tg' => 'tg',
2546 'th' => 'th',
2547 'ti' => 'ti',
2548 'tk' => 'tk',
2549 'tl' => 'tl',
2550 'tn' => 'tn',
2551 'to' => 'to',
2552 'tr' => 'tr',
2553 'ts' => 'ts',
2554 'tt' => 'tt',
2555 'tw' => 'tw',
2556 'ty' => 'ty',
2557 'ug' => 'ug',
2558 'uk' => 'uk',
2559 'ur' => 'ur',
2560 'us' => 'us',
2561 'uz' => 'uz',
2562 've' => 've',
2563 'vi' => 'vi',
2564 'vo' => 'vo',
2565 'wa' => 'wa',
2566 'wo' => 'wo',
2567 'xh' => 'xh',
2568 'yi' => 'yi',
2569 'yo' => 'yo',
2570 'za' => 'za',
2571 'zh' => 'zh',
2572 'zh-CN' => 'zh-CN',
2573 'zh-HK' => 'zh-HK',
2574 'zh-MO' => 'zh-MO',
2575 'zh-SG' => 'zh-SG',
2576 'zh-TW' => 'zh-TW',
2577 'zu' => 'zu',
2578 );
2579 }
2580
2581 // load metaboxes
2582 public function load_metaboxes() {
2583 add_meta_box(
2584 'e2pdf_templates_save', __('Preset', 'e2pdf'), array($this, 'render_metabox'), null, 'side', 'default', array('tpl' => 'e2pdf_templates_save')
2585 );
2586 add_meta_box(
2587 'e2pdf_templates_builder', __('PDF Builder', 'e2pdf'), array($this, 'render_metabox'), null, 'side', 'default', array('tpl' => 'e2pdf_templates_builder')
2588 );
2589 add_meta_box(
2590 'e2pdf_templates_settings', __('Settings', 'e2pdf'), array($this, 'render_metabox'), null, 'side', 'default', array('tpl' => 'e2pdf_templates_settings')
2591 );
2592 }
2593
2594 // load scripts
2595 public function load_scripts() {
2596 wp_enqueue_script('postbox');
2597 wp_enqueue_script('jquery-ui-droppable');
2598 wp_enqueue_script('jquery-ui-resizable');
2599 wp_enqueue_script('jquery-ui-selectable');
2600 wp_enqueue_script('jquery-ui-dialog');
2601 wp_enqueue_script('jquery-ui-autocomplete');
2602 wp_enqueue_script('wp-color-picker');
2603 wp_enqueue_media();
2604 }
2605
2606 // load styles
2607 public function load_styles($ext = false) {
2608 $version = get_option('e2pdf_debug', '0') === '1' ? strtotime('now') : $this->helper->get('version');
2609 wp_enqueue_style('css/e2pdf.jquery-ui', plugins_url('css/jquery-ui.css', $this->helper->get('plugin_file_path')), false, $version, false);
2610 wp_enqueue_style('wp-color-picker');
2611 if ($ext) {
2612 $extension = new Model_E2pdf_Extension();
2613 if ($extension->load($ext)) {
2614 $styles = $extension->styles();
2615 if ($styles && is_array($styles)) {
2616 foreach ($styles as $key => $value) {
2617 wp_enqueue_style('e2pdf-dynamic-style-' . $key, $value, false, $version, false);
2618 }
2619 }
2620 }
2621 }
2622 }
2623 }
2624