PluginProbe
E2Pdf – Export Pdf Tool for WordPress / 1.32.18
E2Pdf – Export Pdf Tool for WordPress v1.32.18
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 1.08.09 All 71 releases
e2pdf / classes / model / e2pdf-template.php

e2pdf-template.php in E2Pdf – Export Pdf Tool for WordPress 1.32.18, at classes/model/e2pdf-template.php

1,065 lines 48.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * File: /model/e2pdf-template.php
5 *
6 * @package E2Pdf
7 * @license GPLv3
8 * @link https://e2pdf.com
9 */
10 if (!defined('ABSPATH')) {
11 die('Access denied.');
12 }
13
14 class Model_E2pdf_Template extends Model_E2pdf_Model {
15
16 private $template = [];
17 private $extension;
18 private $table;
19
20 public function __construct() {
21 global $wpdb;
22 parent::__construct();
23 $this->table = $wpdb->prefix . 'e2pdf_templates';
24 }
25
26 // load
27 public function load($template_id, $full = true, $revision_id = 0) {
28 global $wpdb;
29
30 $template = false;
31 if ($this->helper->get('cache') && !$revision_id) {
32 $template = wp_cache_get($template_id, 'e2pdf_templates');
33 }
34
35 if ($template === false) {
36 $this->helper->load('cache')->pre_objects_cache();
37 // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
38 $template = $wpdb->get_row($wpdb->prepare('SELECT * FROM `' . $this->get_table() . '` WHERE ID = %d', $template_id), ARRAY_A);
39 if ($this->helper->get('cache') && !$revision_id) {
40 wp_cache_set($template_id, $template, 'e2pdf_templates');
41 }
42 }
43
44 if ($revision_id) {
45 $revision = new Model_E2pdf_Revision();
46 if ($revision->load($template_id, false, $revision_id)) {
47 $template = array_replace($template, $revision->template());
48 } else {
49 $template = [];
50 }
51 }
52
53 if ($template) {
54 $template = apply_filters('e2pdf_load_template', $template, $this);
55 $this->template = $template;
56 $extension = new Model_E2pdf_Extension();
57 if ($this->get('extension')) {
58 $extension->load($this->get('extension'));
59 }
60 if ($this->get('item')) {
61 $extension->set('item', $this->get('item'));
62 if ($this->get('item') == '-2') {
63 $extension->set('item1', $this->get('item1'));
64 $extension->set('item2', $this->get('item2'));
65 }
66 }
67 if ($this->get('ID')) {
68 $extension->set('template_id', $this->get('ID'));
69 }
70 $extension = apply_filters('e2pdf_load_template_extension', $extension, $this);
71 $this->extension = $extension;
72 if ($full) {
73 $this->set('fonts', $this->helper->load('convert')->unserialize($template['fonts']));
74 $this->set('permissions', $this->helper->load('convert')->unserialize($template['permissions']));
75 $this->set('properties', $this->helper->load('convert')->unserialize($template['properties']));
76 $this->set('actions', $this->helper->load('convert')->unserialize($template['actions']));
77
78 $pages = false;
79 if ($this->helper->get('cache') && !$revision_id) {
80 $pages = wp_cache_get($this->get('ID'), 'e2pdf_pages');
81 }
82 if ($pages === false) {
83 $this->helper->load('cache')->pre_objects_cache();
84 $model_e2pdf_page = new Model_E2pdf_Page();
85 $pages = $model_e2pdf_page->get_pages($this->get('ID'), $revision_id);
86 if ($this->helper->get('cache') && !$revision_id) {
87 wp_cache_set($this->get('ID'), $pages, 'e2pdf_pages');
88 }
89 }
90 $this->set('pages', $pages);
91
92 $revisions = false;
93 if ($this->helper->get('cache')) {
94 $revisions = wp_cache_get($this->get('ID'), 'e2pdf_revisions');
95 }
96
97 if ($revisions === false) {
98 $this->helper->load('cache')->pre_objects_cache();
99 $model_e2pdf_revision = new Model_E2pdf_Revision();
100 $revisions = $model_e2pdf_revision->revisions($this->get('ID'));
101 if ($this->helper->get('cache')) {
102 wp_cache_set($this->get('ID'), $revisions, 'e2pdf_revisions');
103 }
104 }
105 $this->set('revisions', $revisions);
106 }
107 return true;
108 }
109 return false;
110 }
111
112 // template
113 public function template() {
114 return $this->template;
115 }
116
117 // extension
118 public function extension() {
119 return $this->extension;
120 }
121
122 // set
123 public function set($key, $value) {
124 switch ($key) {
125 case 'format':
126 if (in_array($value, ['pdf', 'jpg'], true)) {
127 $this->template[$key] = $value;
128 return true;
129 } else {
130 return false;
131 }
132 break;
133 case 'permissions':
134 if (!is_array($value)) {
135 $this->template[$key] = ['printing'];
136 } else {
137 $this->template[$key] = $value;
138 }
139 break;
140 case 'fonts':
141 case 'properties':
142 case 'actions':
143 if (!is_array($value)) {
144 $this->template[$key] = [];
145 } else {
146 $this->template[$key] = $value;
147 }
148 break;
149 default:
150 $this->template[$key] = $value;
151 break;
152 }
153 return true;
154 }
155
156 // get
157 public function get($key, $force = false) {
158 if ($force && $this->get('ID')) {
159 global $wpdb;
160 if ($key == 'activated') {
161 // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
162 $value = $wpdb->get_var($wpdb->prepare('SELECT `activated` FROM `' . $this->get_table() . '` WHERE ID = %d', $this->get('ID')));
163 } elseif ($key == 'uid') {
164 // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
165 $value = $wpdb->get_var($wpdb->prepare('SELECT `uid` FROM `' . $this->get_table() . '` WHERE ID = %d', $this->get('ID')));
166 }
167 return $value;
168 } elseif (isset($this->template[$key])) {
169 if ($key == 'revisions') {
170 $value = [
171 '0' => __('Latest', 'e2pdf'),
172 ];
173 if (is_array($this->template[$key])) {
174 foreach ($this->template[$key] as $revision) {
175 // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
176 $value[$revision['revision_id']] = date('d M Y H:i:s', strtotime($revision['updated_at']));
177 }
178 }
179 } else {
180 $value = $this->template[$key];
181 }
182 return $value;
183 } else {
184 switch ($key) {
185 case 'title':
186 $value = __('(no title)', 'e2pdf');
187 break;
188 case 'width':
189 case 'height':
190 case 'inline':
191 case 'auto':
192 case 'trash':
193 case 'locked':
194 case 'activated':
195 case 'revision_id':
196 case 'rtl':
197 case 'tab_order':
198 $value = '0';
199 break;
200 case 'flatten':
201 case 'appearance':
202 $value = '1';
203 break;
204 case 'compression':
205 $value = '-1';
206 break;
207 case 'optimization':
208 $value = '-1';
209 break;
210 case 'format':
211 $value = 'pdf';
212 break;
213 case 'resample':
214 $value = '100';
215 break;
216 case 'text_align':
217 $value = 'left';
218 break;
219 case 'permissions':
220 $value = [
221 'printing',
222 ];
223 break;
224 case 'fonts':
225 case 'properties':
226 case 'actions':
227 case 'pages':
228 case 'revisions':
229 $value = [];
230 break;
231 case 'name':
232 case 'savename':
233 $value = '[e2pdf-dataset]';
234 break;
235 case 'lang_code':
236 $value = 'en-US';
237 break;
238 case 'font_processor':
239 $value = get_option('e2pdf_font_processor', '0');
240 break;
241 default:
242 $value = '';
243 break;
244 }
245 return $value;
246 }
247 }
248
249 // get table
250 public function get_table() {
251 return $this->table;
252 }
253
254 // delete
255 public function delete() {
256 global $wpdb;
257 if ($this->get('ID')) {
258
259 $revision = new Model_E2pdf_Revision();
260 foreach ($this->get('revisions') as $revision_id => $revision_name) {
261 if ($revision_id != '0') {
262 $revision->load($this->get('ID'), true, $revision_id);
263 $revision->delete();
264 }
265 }
266
267 if ($this->get('pdf')) {
268 $pdf_dir = $this->helper->get('pdf_dir') . $this->get('pdf') . '/';
269 $this->helper->delete_dir($pdf_dir);
270 }
271
272 $tpl_dir = $this->helper->get('tpl_dir') . $this->get('ID') . '/';
273 $this->helper->delete_dir($tpl_dir);
274
275 $where = [
276 'ID' => $this->get('ID'),
277 ];
278 $wpdb->delete($this->get_table(), $where);
279
280 foreach ($this->get('pages') as $page) {
281 $model_e2pdf_page = new Model_E2pdf_Page();
282 $model_e2pdf_page->load($page['page_id'], $page['template_id']);
283 $model_e2pdf_page->delete();
284 }
285
286 if ($this->helper->get('cache')) {
287 wp_cache_delete($this->get('ID'), 'e2pdf_templates');
288 wp_cache_delete($this->get('ID'), 'e2pdf_pages');
289 wp_cache_delete($this->get('ID'), 'e2pdf_revisions');
290 }
291
292 $translator = $this->helper->load('translator');
293 if ($translator) {
294 $translator->flush($this->get('ID'));
295 }
296 }
297 }
298
299 // update
300 public function update($keys = []) {
301 global $wpdb;
302 if ($this->get('ID') && !empty($keys)) {
303 $show_errors = false;
304 if ($wpdb->show_errors) {
305 $wpdb->show_errors(false);
306 $show_errors = true;
307 }
308 foreach ($keys as $key) {
309 $template[$key] = $this->get($key);
310 }
311 $where = [
312 'ID' => $this->get('ID'),
313 ];
314 $success = $wpdb->update($this->get_table(), $template, $where);
315 if ($success === false) {
316 $this->helper->load('db')->db_init($wpdb->prefix);
317 $wpdb->update($this->get_table(), $template, $where);
318 }
319 if ($this->helper->get('cache')) {
320 wp_cache_delete($this->get('ID'), 'e2pdf_templates');
321 }
322 if ($show_errors) {
323 $wpdb->show_errors();
324 }
325 }
326 }
327
328 // save
329 public function save($rebuild = false) {
330 global $wpdb;
331 $template = $this->pre_save();
332 $show_errors = false;
333 if ($wpdb->show_errors) {
334 $wpdb->show_errors(false);
335 $show_errors = true;
336 }
337
338 if ($this->get('ID')) {
339 if ($rebuild) {
340 $revision = new Model_E2pdf_Revision();
341 $revision->revision($this->get('ID'));
342 if (isset($template['pdf']) && $template['pdf'] && ($template['pdf'] == $revision->get('pdf') || $this->get('revision_id'))) {
343 $pdf_name = md5(time());
344 $pdf_dir = $this->helper->get('pdf_dir') . $pdf_name . '/';
345 $pdf_images_dir = $pdf_dir . 'images/';
346
347 $this->helper->create_dir($pdf_dir);
348 $this->helper->create_dir($pdf_images_dir);
349
350 if (file_exists($this->helper->get('pdf_dir') . $template['pdf'] . '/' . $template['pdf'] . '.pdf')) {
351 $images = glob($this->helper->get('pdf_dir') . $template['pdf'] . '/images/*');
352 foreach ($images as $image) {
353 copy($image, $pdf_images_dir . pathinfo($image, PATHINFO_BASENAME));
354 }
355 copy($this->helper->get('pdf_dir') . $template['pdf'] . '/' . $template['pdf'] . '.pdf', $this->helper->get('pdf_dir') . $pdf_name . '/' . $pdf_name . '.pdf');
356 $template['pdf'] = $pdf_name;
357 }
358 }
359 $revision->flush();
360 }
361 $where = [
362 'ID' => $this->get('ID'),
363 ];
364 $success = $wpdb->update($this->get_table(), $template, $where);
365 if ($success === false) {
366 $this->helper->load('db')->db_init($wpdb->prefix);
367 $wpdb->update($this->get_table(), $template, $where);
368 }
369 } else {
370 if ($this->get('TMP_ID')) {
371 $template['ID'] = $this->get('TMP_ID');
372 }
373 $success = $wpdb->insert($this->get_table(), $template);
374 if ($success === false) {
375 $this->helper->load('db')->db_init($wpdb->prefix);
376 $wpdb->insert($this->get_table(), $template);
377 }
378 $this->set('ID', $wpdb->insert_id);
379 }
380
381 if ($show_errors) {
382 $wpdb->show_errors();
383 }
384
385 if ($this->get('ID') && $rebuild) {
386
387 $translator = $this->helper->load('translator');
388 if ($translator) {
389 $translator->update_translation($template['title'], $this->get('ID'), 'title', 'template');
390 $translator->update_translation($template['dataset_title'], $this->get('ID'), 'dataset_title', 'template');
391 $translator->update_translation($template['dataset_title1'], $this->get('ID'), 'dataset_title1', 'template');
392 $translator->update_translation($template['dataset_title2'], $this->get('ID'), 'dataset_title2', 'template');
393 $translator->update_translation($template['button_title'], $this->get('ID'), 'button_title', 'template');
394 $translator->update_translation($template['name'], $this->get('ID'), 'name', 'template');
395 $translator->update_translation($template['savename'], $this->get('ID'), 'savename', 'template');
396 }
397
398 foreach ($this->get('pages') as $page_key => $page) {
399 $new_page = new Model_E2pdf_Page();
400 foreach ($page as $page_set_key => $page_set_value) {
401 $new_page->set($page_set_key, $page_set_value);
402 }
403 if (!isset($page['actions'])) {
404 $page_actions = [];
405 } else {
406 $page_actions = $page['actions'];
407 }
408 $new_page->set('actions', $page_actions);
409 $new_page->set('template_id', $this->get('ID'));
410 $new_page->set('page_id', $page_key);
411 $new_page->save();
412 if (!empty($page['elements'])) {
413 foreach ($page['elements'] as $element_key => $element) {
414 $new_element = new Model_E2pdf_Element();
415 foreach ($element as $element_set_key => $element_set_value) {
416 $new_element->set($element_set_key, $element_set_value);
417 if ($translator) {
418 if ($element_set_key == 'value') {
419 $translator->update_translation($element_set_value, $this->get('ID'), $element['element_id'], $element_set_key);
420 }
421 }
422 }
423
424 if ($translator) {
425 if (!empty($element['properties']['option'])) {
426 $translator->update_translation($element['properties']['option'], $this->get('ID'), $element['element_id'], 'option');
427 }
428 if (!empty($element['properties']['options'])) {
429 $translator->update_translation($element['properties']['options'], $this->get('ID'), $element['element_id'], 'options');
430 }
431 if (!empty($element['actions'])) {
432 foreach ($element['actions'] as $action_key => $action) {
433 if ($action['action'] == 'change' && isset($action['property']) && $action['property'] == 'value' && isset($action['change'])) {
434 $translator->update_translation($action['change'], $this->get('ID'), $element['element_id'], 'value_action_' . $action_key . '_change');
435 }
436 }
437 }
438 }
439 $new_element->set('properties', isset($element['properties']) ? $element['properties'] : []);
440 $new_element->set('actions', isset($element['actions']) ? $element['actions'] : []);
441 $new_element->set('page_id', $new_page->get('page_id'));
442 $new_element->set('template_id', $this->get('ID'));
443 $new_element->save();
444 }
445 }
446 }
447 if ($translator) {
448 $translator->flush($this->get('ID'));
449 }
450 }
451
452 if ($this->helper->get('cache') && $this->get('ID')) {
453 wp_cache_delete($this->get('ID'), 'e2pdf_templates');
454 wp_cache_delete($this->get('ID'), 'e2pdf_pages');
455 wp_cache_delete($this->get('ID'), 'e2pdf_revisions');
456 }
457
458 return $this->get('ID');
459 }
460
461 // pre save
462 public function load_fonts() {
463 $fonts = [];
464 $model_e2pdf_font = new Model_E2pdf_Font();
465 $all_fonts = $model_e2pdf_font->get_fonts();
466 $c_font = array_search($this->get('font'), $all_fonts, true);
467 if ($c_font) {
468 $fonts[$c_font] = $this->get('font');
469 }
470 foreach ($this->get('pages') as $key => $page) {
471 if (!empty($page['elements'])) {
472 foreach ($page['elements'] as $element_key => $element) {
473 $tmp_fonts = $model_e2pdf_font->get_element_fonts($element, $all_fonts);
474 if (!empty($tmp_fonts)) {
475 $fonts = array_merge($fonts, $tmp_fonts);
476 }
477 }
478 }
479 }
480
481 if (isset($this->get('properties')['css']) && $this->get('properties')['css']) {
482 $tmp_fonts = $model_e2pdf_font->get_css_fonts($this->get('properties')['css'], $all_fonts);
483 if (!empty($tmp_fonts)) {
484 $fonts = array_merge($fonts, $tmp_fonts);
485 }
486 }
487
488 $this->set('fonts', $fonts);
489 }
490
491 // pre save
492 public function pre_save() {
493 $this->load_fonts();
494 $template = [
495 'uid' => $this->get('uid'),
496 'activated' => $this->get('activated'),
497 'title' => $this->get('title'),
498 'pdf' => $this->get('pdf'),
499 'updated_at' => current_time('mysql', 1),
500 'flatten' => $this->get('flatten'),
501 'tab_order' => $this->get('tab_order'),
502 'format' => $this->get('format'),
503 'resample' => $this->get('resample'),
504 'compression' => $this->get('compression'),
505 'optimization' => $this->get('optimization'),
506 'appearance' => $this->get('appearance'),
507 'width' => $this->get('width'),
508 'height' => $this->get('height'),
509 'extension' => $this->get('extension'),
510 'item' => $this->get('item'),
511 'item1' => $this->get('item1'),
512 'item2' => $this->get('item2'),
513 'dataset_title' => $this->get('dataset_title'),
514 'dataset_title1' => $this->get('dataset_title1'),
515 'dataset_title2' => $this->get('dataset_title2'),
516 'button_title' => $this->get('button_title'),
517 'dpdf' => $this->get('dpdf'),
518 'inline' => $this->get('inline'),
519 'auto' => $this->get('auto'),
520 'rtl' => $this->get('rtl'),
521 'font_processor' => $this->get('font_processor'),
522 'name' => $this->get('name'),
523 'savename' => $this->get('savename'),
524 'password' => $this->get('password'),
525 'owner_password' => $this->get('owner_password'),
526 'permissions' => serialize($this->get('permissions')), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize
527 'hooks' => $this->get('hooks'),
528 'meta_title' => $this->get('meta_title'),
529 'meta_subject' => $this->get('meta_subject'),
530 'meta_author' => $this->get('meta_author'),
531 'meta_keywords' => $this->get('meta_keywords'),
532 'lang_code' => $this->get('lang_code'),
533 'fonts' => serialize($this->get('fonts')), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize
534 'font' => $this->get('font'),
535 'font_size' => $this->get('font_size'),
536 'font_color' => $this->get('font_color'),
537 'line_height' => $this->get('line_height'),
538 'text_align' => $this->get('text_align'),
539 'trash' => $this->get('trash'),
540 'locked' => $this->get('locked'),
541 'author' => get_current_user_id(),
542 'properties' => serialize($this->get('properties')), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize
543 'actions' => serialize($this->get('actions')), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize
544 'attachments' => $this->get('attachments'),
545 ];
546
547 if (!$this->get('ID')) {
548 $template['created_at'] = current_time('mysql', 1);
549 }
550
551 return $template;
552 }
553
554 // fill
555 public function fill() {
556
557 add_filter('e2pdf_pdf_fill', [$this->helper, '__return_true'], 999);
558 do_action('e2pdf_model_template_fill_pre', $this, $this->extension);
559
560 $action = new Model_E2pdf_Action();
561 $action->load($this->extension);
562 $pages = $this->get('pages');
563 $changed_elements = [];
564 foreach ($pages as $key => $value) {
565 $changed_elements = array_merge($changed_elements, $action->process_page_id($value));
566 }
567
568 $changed_pages = [];
569 if (!empty($changed_elements)) {
570 foreach ($changed_elements as $element) {
571 $changed_pages[$element['page_id']][] = $element;
572 }
573 }
574
575 foreach ($pages as $page_key => $page) {
576 if (isset($changed_pages[$page['page_id']])) {
577 $page['elements'] = array_merge($page['elements'], $changed_pages[$page['page_id']]);
578 }
579 // phpcs:ignore Squiz.PHP.DisallowMultipleAssignments.Found
580 $pages[$page_key] = $page = $action->process_actions($page);
581 if (isset($page['hidden']) && $page['hidden']) {
582 $pages[$page_key]['page_id'] = '0';
583 $pages[$page_key]['elements'] = [];
584 } else {
585 if (!empty($page['elements'])) {
586 $pages[$page_key]['elements'] = $page['elements'];
587 foreach ($page['elements'] as $element_key => $element) {
588 if (isset($element['type'])) {
589 $rendered = isset($element['rendered']) ? $element['rendered'] : false;
590 switch ($element['type']) {
591 case 'e2pdf-checkbox':
592 case 'e2pdf-radio':
593 if (isset($element['properties']['option'])) {
594 $element['properties']['option'] = $this->helper->load('translator')->pre_translate($element['properties']['option'], $element['template_id'], $element['element_id'], 'option');
595 }
596 $pages[$page_key]['elements'][$element_key]['properties']['option'] = $this->extension()->render(
597 isset($element['properties']['option']) ? $element['properties']['option'] : ''
598 );
599 if (!$rendered) {
600 $element['value'] = $this->helper->load('translator')->pre_translate($element['value'], $element['template_id'], $element['element_id'], 'value');
601 $pages[$page_key]['elements'][$element_key]['value'] = $this->extension()->render(
602 $element['value'], $element
603 );
604 }
605 break;
606 case 'e2pdf-select':
607 if (isset($element['properties']['options'])) {
608 $element['properties']['options'] = $this->helper->load('translator')->pre_translate($element['properties']['options'], $element['template_id'], $element['element_id'], 'options');
609 }
610 $pages[$page_key]['elements'][$element_key]['properties']['options'] = $this->extension()->render(
611 isset($element['properties']['options']) ? $element['properties']['options'] : ''
612 );
613 if (!$rendered) {
614 $element['value'] = $this->helper->load('translator')->pre_translate($element['value'], $element['template_id'], $element['element_id'], 'value');
615 $pages[$page_key]['elements'][$element_key]['value'] = $this->extension()->render(
616 $element['value'], $element
617 );
618 }
619 break;
620 case 'e2pdf-html':
621 case 'e2pdf-page-number':
622 if (!$rendered) {
623 $element['value'] = $this->helper->load('translator')->pre_translate($element['value'], $element['template_id'], $element['element_id'], 'value');
624 $pages[$page_key]['elements'][$element_key]['value'] = $this->helper->load('filter')->filter_html_tags(
625 $this->extension()->render(
626 $element['value'], $element
627 )
628 );
629 }
630 break;
631 case 'e2pdf-image':
632 case 'e2pdf-signature':
633 if (isset($element['properties']['quality']) && $element['properties']['quality']) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedIf
634 } else {
635 $element['properties']['quality'] = $this->get('optimization');
636 $pages[$page_key]['elements'][$element_key]['properties']['quality'] = $this->get('optimization');
637 }
638 if (!$rendered) {
639 $element['value'] = $this->helper->load('translator')->pre_translate($element['value'], $element['template_id'], $element['element_id'], 'value');
640 $pages[$page_key]['elements'][$element_key]['value'] = $this->extension()->render(
641 $element['value'], $element
642 );
643 }
644 if (!empty($element['properties']['link_url'])) {
645 $pages[$page_key]['elements'][$element_key]['properties']['link_url'] = $this->extension()->render(
646 $element['properties']['link_url']
647 );
648 if (!empty($pages[$page_key]['elements'][$element_key]['properties']['link_url']) && !empty($element['properties']['link_type']) && $element['properties']['link_type'] == 'attachment') {
649 $source = $this->helper->load('attachments')->get_file(trim($pages[$page_key]['elements'][$element_key]['properties']['link_url']), $this->extension);
650 if ($source) {
651 $pages[$page_key]['elements'][$element_key]['attachment']['value'] = $source;
652 $pages[$page_key]['elements'][$element_key]['attachment']['name'] = $this->helper->load('attachments')->get_attachment_name($pages[$page_key]['elements'][$element_key]['properties']['link_url'], $this->extension, $element['value']);
653 $pages[$page_key]['elements'][$element_key]['attachment']['description'] = $this->helper->load('attachments')->get_attachment_description($pages[$page_key]['elements'][$element_key]['properties']['link_url'], $this->extension, $element['value']);
654 }
655 }
656 }
657 break;
658 case 'e2pdf-link':
659 $pages[$page_key]['elements'][$element_key]['value'] = $this->extension()->render(
660 $element['value'], $element
661 );
662 $pages[$page_key]['elements'][$element_key]['properties']['link_label'] = $this->extension()->render(
663 !empty($element['properties']['link_label']) ? $element['properties']['link_label'] : ''
664 );
665 if (!empty($pages[$page_key]['elements'][$element_key]['value']) && !empty($element['properties']['link_type']) && $element['properties']['link_type'] == 'attachment') {
666 $source = $this->helper->load('attachments')->get_file(trim($pages[$page_key]['elements'][$element_key]['value']), $this->extension);
667 if ($source) {
668 $pages[$page_key]['elements'][$element_key]['attachment']['value'] = $source;
669 $pages[$page_key]['elements'][$element_key]['attachment']['name'] = $this->helper->load('attachments')->get_attachment_name($pages[$page_key]['elements'][$element_key]['value'], $this->extension, $element['value']);
670 $pages[$page_key]['elements'][$element_key]['attachment']['description'] = $this->helper->load('attachments')->get_attachment_description($pages[$page_key]['elements'][$element_key]['value'], $this->extension, $element['value']);
671 }
672 }
673 break;
674 case 'e2pdf-graph':
675 if (!$rendered) {
676 $chart = 0 === strpos($element['value'], '[frm-graph');
677 if ($chart) {
678 $element['properties']['chart'] = '1';
679 $pages[$page_key]['elements'][$element_key]['properties']['chart'] = '1';
680 }
681 $element['value'] = $this->helper->load('translator')->pre_translate($element['value'], $element['template_id'], $element['element_id'], 'value');
682 $pages[$page_key]['elements'][$element_key]['value'] = $this->extension()->render(
683 $element['value'], $element
684 );
685 if ($chart) {
686 $data = $this->helper->load('convert')->unserialize($pages[$page_key]['elements'][$element_key]['value']);
687 $pages[$page_key]['elements'][$element_key]['value'] = is_array($data) ? json_encode($data) : '';
688 }
689 }
690 break;
691 default:
692 if (!$rendered) {
693 $element['value'] = $this->helper->load('translator')->pre_translate($element['value'], $element['template_id'], $element['element_id'], 'value');
694 $pages[$page_key]['elements'][$element_key]['value'] = $this->extension()->render(
695 $element['value'], $element
696 );
697 }
698 break;
699 }
700 }
701 }
702 }
703 }
704 }
705 $this->set('pages', $pages);
706
707 do_action('e2pdf_model_template_fill_after', $this, $this->extension);
708 remove_filter('e2pdf_pdf_fill', [$this->helper, '__return_true'], 999);
709 }
710
711 // pre render
712 public function pre_render() {
713 $pages = $this->get('pages');
714 foreach ($pages as $key => $page) {
715 $page = (array) $page;
716 $pages[$key] = $page;
717 if (!empty($page['elements'])) {
718 foreach ($page['elements'] as $element_key => $element) {
719 $element = (array) $element;
720 $pages[$key]['elements'][$element_key] = $element;
721 if ($element['type'] == 'e2pdf-qrcode' || $element['type'] == 'e2pdf-barcode' || $element['type'] == 'e2pdf-graph') {
722 $pages[$key]['elements'][$element_key]['type'] = 'e2pdf-image';
723 } elseif ($element['type'] == 'e2pdf-html') {
724 if (!empty($element['properties']['css_style'])) {
725 $pages[$key]['elements'][$element_key]['properties']['css'] = $this->helper->load('properties')->css_style(
726 isset($element['properties']['css']) ? $element['properties']['css'] : '', $element['properties']['css_style']
727 );
728 }
729 }
730 }
731 }
732 $this->helper->load('sort')->uasort($pages[$key]['elements'], 'sort_by_elementid');
733 $this->helper->load('sort')->stable_uasort($pages[$key]['elements'], 'sort_by_zindex');
734 }
735 return $pages;
736 }
737
738 // render
739 public function render($type = false) {
740
741 $model_e2pdf_api = new Model_E2pdf_Api();
742 $model_e2pdf_font = new Model_E2pdf_Font();
743
744 $fonts = [];
745 if (get_option('e2pdf_cache_fonts', '1') && $type != 'php') {
746 $cached_fonts = get_option('e2pdf_cached_fonts', []);
747 if ($this->get('fonts')) {
748 $uncached_fonts = [];
749 foreach ($this->get('fonts') as $key => $value) {
750 $md5 = $model_e2pdf_font->get_font($key, true);
751 if ($md5 && in_array($md5, $cached_fonts, true)) {
752 $fonts[] = [
753 'name' => $value,
754 'md5' => $md5,
755 'cache' => true,
756 ];
757 } else {
758 $uncached_fonts[] = $md5;
759 $fonts[] = [
760 'name' => $value,
761 'value' => $model_e2pdf_font->get_font($key),
762 'cache' => true,
763 ];
764 }
765 }
766 if (!empty($uncached_fonts)) {
767 $model_e2pdf_api->set(
768 [
769 'action' => 'cache/fonts',
770 'data' => [
771 'fonts' => $uncached_fonts,
772 ],
773 ]
774 );
775 $request = $model_e2pdf_api->request();
776 if (isset($request['fonts']) && is_array($request['fonts'])) {
777 $cached_fonts = array_merge($cached_fonts, $request['fonts']);
778 }
779 update_option('e2pdf_cached_fonts', $cached_fonts);
780 }
781 }
782 } else {
783 if ($this->get('fonts')) {
784 foreach ($this->get('fonts') as $key => $value) {
785 $fonts[] = [
786 'name' => $value,
787 'value' => $model_e2pdf_font->get_font($key),
788 ];
789 }
790 }
791 }
792
793 $pages = $this->pre_render();
794
795 $settings = [
796 'uid' => $this->get('uid'),
797 'activated' => $this->get('activated'),
798 'title' => $this->get_name(),
799 'flatten' => $this->get('flatten'),
800 'tab_order' => $this->get('tab_order'),
801 'format' => $this->get('format'),
802 'resample' => $this->get('resample'),
803 'compression' => $this->get('compression'),
804 'appearance' => $this->get('appearance'),
805 'password' => $this->get('password'),
806 'owner_password' => $this->get('owner_password'),
807 'permissions' => $this->get('permissions'),
808 'meta_title' => $this->get('meta_title'),
809 'meta_subject' => $this->get('meta_subject'),
810 'meta_author' => $this->get('meta_author'),
811 'meta_keywords' => $this->get('meta_keywords'),
812 'lang_code' => $this->get('lang_code'),
813 'css' => isset($this->get('properties')['css']) ? $this->get('properties')['css'] : '',
814 'font' => $this->get('font'),
815 'font_size' => $this->get('font_size'),
816 'font_color' => $this->get('font_color'),
817 'line_height' => $this->get('line_height'),
818 'text_align' => $this->get('text_align'),
819 'rtl' => $this->get('rtl'),
820 'font_processor' => apply_filters('e2pdf_font_processor', $this->get('font_processor'), $this->get('ID')),
821 'created_at' => $this->get('created_at'),
822 'updated_at' => $this->get('updated_at'),
823 ];
824
825 if ($this->get('dpdf')) {
826 $dpdf = $this->helper->load('pdf')->get_pdf($this->get('dpdf'));
827 if ($dpdf) {
828 $settings['pdf'] = $dpdf;
829 $settings['dpdf'] = '1';
830 }
831 } elseif ($this->get('pdf')) {
832 $pdf = $this->helper->get('pdf_dir') . $this->get('pdf') . '/' . $this->get('pdf') . '.pdf';
833 if (file_exists($pdf)) {
834 // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode, WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
835 $settings['pdf'] = base64_encode(file_get_contents($pdf));
836 }
837 }
838
839 $data = [
840 'settings' => $settings,
841 'pages' => $pages,
842 'fonts' => $fonts,
843 'attachments' => $this->helper->load('attachments')->get_attachments($this->get('attachments'), $this->extension),
844 ];
845
846 if ($type == 'php') {
847 $model_e2pdf_convert = new Model_E2pdf_Convert();
848 $file = '<?php' . PHP_EOL;
849 $file .= 'function e2pdf_template() {' . PHP_EOL;
850 $file .= '$template = [' . PHP_EOL;
851 $file .= $model_e2pdf_convert->toPHP($data);
852 $file .= '];' . PHP_EOL;
853 $file .= 'return $template;' . PHP_EOL;
854 $file .= '}';
855 if ($file) {
856 $response['file'] = $file;
857 } else {
858 $response['error'] = __('Something went wrong!', 'e2pdf');
859 }
860 return $response;
861 }
862
863 // phpcs:ignore WordPress.WP.AlternativeFunctions.json_encode_json_encode
864 $cached_pdf = md5(json_encode($data));
865 $request = $this->helper->load('cache')->get_cached_pdf($cached_pdf);
866 if (empty($request['file'])) {
867 $model_e2pdf_api->set(
868 [
869 'action' => 'template/build',
870 'data' => $data,
871 ]
872 );
873 $request = $model_e2pdf_api->request();
874 }
875
876 if (isset($request['error']) && $request['error'] == 'cache_error') {
877 update_option('e2pdf_cached_fonts', []);
878 $fonts = [];
879 if ($this->get('fonts')) {
880 foreach ($this->get('fonts') as $key => $value) {
881 $fonts[] = [
882 'name' => $value,
883 'value' => $model_e2pdf_font->get_font($key),
884 ];
885 }
886 }
887 $data = [
888 'settings' => $settings,
889 'pages' => $pages,
890 'fonts' => $fonts,
891 ];
892 // phpcs:ignore WordPress.WP.AlternativeFunctions.json_encode_json_encode
893 $cached_pdf = md5(json_encode($data));
894 $request = $this->helper->load('cache')->get_cached_pdf($cached_pdf);
895 if (empty($request['file'])) {
896 $model_e2pdf_api->set(
897 [
898 'action' => 'template/build',
899 'data' => $data,
900 ]
901 );
902 $request = $model_e2pdf_api->request();
903 }
904 }
905 $this->helper->load('cache')->cache_pdf($cached_pdf, $request);
906 return $request;
907 }
908
909 public function get_name() {
910 $name = __('(no title)', 'e2pdf');
911 if ($this->get('name')) {
912 $name = $this->get('name');
913 } elseif ($this->get('title')) {
914 $name = $this->get('title');
915 }
916 return $name;
917 }
918
919 public function flush() {
920 if ($this->get('ID')) {
921 $model_e2pdf_page = new Model_E2pdf_Page();
922 $pages = $model_e2pdf_page->get_pages($this->get('ID'));
923 foreach ($pages as $page) {
924 $new_page = new Model_E2pdf_Page();
925 $new_page->load($page['page_id'], $page['template_id']);
926 $new_page->delete();
927 }
928 }
929 }
930
931 public function activate() {
932 if ($this->get('ID')) {
933 if ($this->get('activated', true)) {
934 $request = [
935 'template_id' => $this->get('ID'),
936 'template_uid' => $this->get('uid'),
937 ];
938 } else {
939 $model_e2pdf_api = new Model_E2pdf_Api();
940 $model_e2pdf_api->set(
941 [
942 'action' => 'template/activate',
943 'data' => [
944 'template_id' => $this->get('ID'),
945 'template_uid' => $this->get('uid'),
946 'template_title' => $this->get('title'),
947 'template_extension' => $this->get('extension'),
948 ],
949 ]
950 );
951 $request = $model_e2pdf_api->request();
952 if (!isset($request['error'])) {
953 $this->set('activated', '1');
954 $this->set('uid', $request['template_uid']);
955 $this->update(['activated', 'uid']);
956 } else {
957 $this->set('activated', '0');
958 $this->update(['activated']);
959 }
960 if ($this->helper->get('cache')) {
961 wp_cache_delete($this->get('ID'), 'e2pdf_templates');
962 }
963 }
964 } else {
965 $request = [
966 'error' => __('Something went wrong!', 'e2pdf'),
967 ];
968 }
969 return $request;
970 }
971
972 public function deactivate() {
973 if ($this->get('ID')) {
974 if (!$this->get('activated', true)) {
975 $request = [
976 'success' => __('Template Deactivated', 'e2pdf'),
977 ];
978 } else {
979 $model_e2pdf_api = new Model_E2pdf_Api();
980 $model_e2pdf_api->set(
981 [
982 'action' => 'template/deactivate',
983 'data' => [
984 'template_id' => $this->get('ID'),
985 'template_uid' => $this->get('uid'),
986 ],
987 ]
988 );
989 $request = $model_e2pdf_api->request();
990 if (!isset($request['error'])) {
991 $this->set('activated', '0');
992 $this->update(['activated']);
993 }
994 if ($this->helper->get('cache')) {
995 wp_cache_delete($this->get('ID'), 'e2pdf_templates');
996 }
997 }
998 } else {
999 $request = [
1000 'error' => __('Something went wrong!', 'e2pdf'),
1001 ];
1002 }
1003 return $request;
1004 }
1005
1006 public function patch($key, $value = false, $entry = null, $filter = false) {
1007 if ($value !== false) {
1008 $this->set($key, $filter ? $this->extension()->convert_shortcodes($value, true) : $this->extension()->render($value));
1009 if ($entry) {
1010 $entry->set_data($key, $this->get($key));
1011 }
1012 } else {
1013 switch ($key) {
1014 case 'meta_title':
1015 case 'meta_subject':
1016 case 'meta_author':
1017 case 'meta_keywords':
1018 $value = $this->extension()->render(
1019 $this->helper->load('translator')->pre_translate(
1020 $this->get($key), $this->get('ID'), $key, 'template'
1021 )
1022 );
1023 $this->set($key, $value);
1024 break;
1025 case 'name':
1026 if ($this->get('name')) {
1027 $value = $this->extension()->render(
1028 $this->helper->load('translator')->pre_translate(
1029 $this->get('name'), $this->get('ID'), 'name', 'template'
1030 )
1031 );
1032 }
1033 if (!trim($value) && $this->get('title')) {
1034 $value = $this->extension()->render(
1035 $this->helper->load('translator')->pre_translate(
1036 $this->get('title'), $this->get('ID'), 'title', 'template'
1037 )
1038 );
1039 }
1040 if (!trim($value)) {
1041 $value = __('(no title)', 'e2pdf');
1042 }
1043 $this->set($key, $value);
1044 break;
1045 case 'savename':
1046 if ($this->get('savename')) {
1047 $value = $this->extension()->render(
1048 $this->helper->load('translator')->pre_translate(
1049 $this->get('savename'), $this->get('ID'), 'savename', 'template'
1050 )
1051 );
1052 }
1053 if (!trim($value)) {
1054 $value = $this->get('name');
1055 }
1056 $this->set($key, $value);
1057 break;
1058 default:
1059 $this->set($key, $this->extension()->render($this->get($key)));
1060 break;
1061 }
1062 }
1063 }
1064 }
1065