PluginProbe
E2Pdf – Export Pdf Tool for WordPress / 1.32.49
E2Pdf – Export Pdf Tool for WordPress v1.32.49
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
← All changes | classes/model/e2pdf-template.php +802 -314 1.04.071.32.49 View file →
@@ -1,14 +1,12 @@
1 1 <?php
2 2
3 3 /**
4 - * E2pdf Template Model
5 - *
6 - * @copyright Copyright 2017 https://e2pdf.com
7 - * @license GPL v2
8 - * @version 1
9 - * @link https://e2pdf.com
10 - * @since 0.00.01
4 + * File: /model/e2pdf-template.php
5 + *
6 + * @package E2Pdf
7 + * @license GPLv3
8 + * @link https://e2pdf.com
11 9 */
12 10 if (!defined('ABSPATH')) {
13 11 die('Access denied.');
14 12 }
@@ -14,33 +12,48 @@
14 12 }
15 13
16 14 class Model_E2pdf_Template extends Model_E2pdf_Model {
17 15
18 - private $template = array();
19 - private $extension = null;
16 + private $template = [];
17 + private $extension;
20 18 private $table;
21 19
22 - /*
23 - * On Template init
24 - */
25 -
26 - function __construct() {
20 + public function __construct() {
27 21 global $wpdb;
28 22 parent::__construct();
29 23 $this->table = $wpdb->prefix . 'e2pdf_templates';
30 24 }
31 25
32 - /**
33 - * Load Template by ID
34 - *
35 - * @param int $template_id - ID of template
36 - */
37 - public function load($template_id, $pages = true) {
26 + // load
27 + public function load($template_id, $full = true, $revision_id = 0) {
38 28 global $wpdb;
39 - $template = $wpdb->get_row($wpdb->prepare("SELECT * FROM `{$this->get_table()}` WHERE ID = %d", $template_id), ARRAY_A);
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 +
40 53 if ($template) {
54 + $template = apply_filters('e2pdf_load_template', $template, $this);
41 55 $this->template = $template;
42 -
43 56 $extension = new Model_E2pdf_Extension();
44 57 if ($this->get('extension')) {
45 58 $extension->load($this->get('extension'));
46 59 }
@@ -45,15 +58,52 @@
45 58 $extension->load($this->get('extension'));
46 59 }
47 60 if ($this->get('item')) {
48 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 + }
49 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);
50 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']));
51 77
52 - if ($pages) {
53 - $this->set('fonts', unserialize($template['fonts']));
54 - $model_e2pdf_page = new Model_E2pdf_Page();
55 - $this->set('pages', $model_e2pdf_page->get_pages($this->get('ID')));
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);
56 106 }
57 107 return true;
58 108 }
59 109 return false;
@@ -58,51 +108,84 @@
58 108 }
59 109 return false;
60 110 }
61 111
62 - /**
63 - * Get loaded Template
64 - *
65 - * @return object
66 - */
112 + // template
67 113 public function template() {
68 114 return $this->template;
69 115 }
70 116
117 + // extension
71 118 public function extension() {
72 119 return $this->extension;
73 120 }
74 121
75 - /**
76 - * Set Template attribute
77 - *
78 - * @param string $key - Attribute Key
79 - * @param string $value - Attribute Value
80 - */
122 + // set
81 123 public function set($key, $value) {
82 - $this->template[$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;
83 154 }
84 155
85 - /**
86 - * Get Template attribute by Key
87 - *
88 - * @param string $key - Attribute Key
89 - *
90 - * @return mixed
91 - */
92 - public function get($key) {
93 - if (isset($this->template[$key])) {
94 - $value = $this->template[$key];
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 + }
95 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;
96 183 } else {
97 -
98 184 switch ($key) {
99 -
100 185 case 'title':
101 - $value = __("(no title)", 'e2pdf');
186 + $value = __('(no title)', 'e2pdf');
102 187 break;
103 -
104 - case 'flatten':
105 188 case 'width':
106 189 case 'height':
107 190 case 'inline':
108 191 case 'auto':
@@ -108,27 +191,54 @@
108 191 case 'auto':
109 192 case 'trash':
110 193 case 'locked':
111 194 case 'activated':
112 - $value = 0;
195 + case 'revision_id':
196 + case 'rtl':
197 + case 'tab_order':
198 + $value = '0';
113 199 break;
114 -
200 + case 'flatten':
115 201 case 'appearance':
116 - $value = 1;
202 + $value = '1';
117 203 break;
118 -
119 204 case 'compression':
120 - $value = -1;
205 + $value = '-1';
121 206 break;
122 -
207 + case 'optimization':
208 + $value = '-1';
209 + break;
123 210 case 'format':
124 211 $value = 'pdf';
125 212 break;
126 -
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;
127 224 case 'fonts':
128 - $value = array();
225 + case 'properties':
226 + case 'actions':
227 + case 'pages':
228 + case 'revisions':
229 + $value = [];
129 230 break;
130 -
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;
131 241 default:
132 242 $value = '';
133 243 break;
134 244 }
@@ -135,34 +245,37 @@
135 245 return $value;
136 246 }
137 247 }
138 248
139 - /**
140 - * Get Templates table
141 - *
142 - * @return string
143 - */
249 + // get table
144 250 public function get_table() {
145 251 return $this->table;
146 252 }
147 253
148 - /**
149 - * Delete loaded template
150 - */
254 + // delete
151 255 public function delete() {
152 256 global $wpdb;
153 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 +
154 267 if ($this->get('pdf')) {
155 - $pdf_dir = $this->helper->get('pdf_dir') . $this->get('pdf') . "/";
268 + $pdf_dir = $this->helper->get('pdf_dir') . $this->get('pdf') . '/';
156 269 $this->helper->delete_dir($pdf_dir);
157 270 }
158 271
159 - $tpl_dir = $this->helper->get('tpl_dir') . $this->get('ID') . "/";
272 + $tpl_dir = $this->helper->get('tpl_dir') . $this->get('ID') . '/';
160 273 $this->helper->delete_dir($tpl_dir);
161 274
162 - $where = array(
163 - 'ID' => $this->get('ID')
164 - );
275 + $where = [
276 + 'ID' => $this->get('ID'),
277 + ];
165 278 $wpdb->delete($this->get_table(), $where);
166 279
167 280 foreach ($this->get('pages') as $page) {
168 281 $model_e2pdf_page = new Model_E2pdf_Page();
@@ -168,98 +281,194 @@
168 281 $model_e2pdf_page = new Model_E2pdf_Page();
169 282 $model_e2pdf_page->load($page['page_id'], $page['template_id']);
170 283 $model_e2pdf_page->delete();
171 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 + }
172 296 }
173 297 }
174 298
175 - /**
176 - * Save template
177 - */
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
178 329 public function save($rebuild = false) {
179 330 global $wpdb;
180 331 $template = $this->pre_save();
332 + $show_errors = false;
333 + if ($wpdb->show_errors) {
334 + $wpdb->show_errors(false);
335 + $show_errors = true;
336 + }
181 337
182 338 if ($this->get('ID')) {
183 - $where = array(
184 - 'ID' => $this->get('ID')
185 - );
186 - $wpdb->update($this->get_table(), $template, $where);
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 + }
187 369 } else {
188 - $wpdb->insert($this->get_table(), $template);
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 + }
189 378 $this->set('ID', $wpdb->insert_id);
190 379 }
191 380
381 + if ($show_errors) {
382 + $wpdb->show_errors();
383 + }
384 +
192 385 if ($this->get('ID') && $rebuild) {
193 - $this->flush();
194 - foreach ($this->get('pages') as $page_key => $page_value) {
195 - $page = new Model_E2pdf_Page();
196 - foreach ($page_value as $page_value_key => $page_value_value) {
197 - $page->set($page_value_key, $page_value_value);
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);
198 402 }
199 -
200 - if (!$page_value['actions']) {
201 - $page_actions = array();
403 + if (!isset($page['actions'])) {
404 + $page_actions = [];
202 405 } else {
203 - $page_actions = $page_value['actions'];
406 + $page_actions = $page['actions'];
204 407 }
205 - $page->set('actions', $page_actions);
206 -
207 - $page->set('template_id', $this->get('ID'));
208 - $page->set('page_id', $page_key);
209 - $page->save();
210 -
211 - if (isset($page_value['elements'])) {
212 -
213 - $this->helper->load('sort')->uasort($page_value['elements'], "sort_by_elementid");
214 -
215 - foreach ($page_value['elements'] as $element_key => $element_value) {
216 - $element = new Model_E2pdf_Element();
217 - foreach ($element_value as $element_value_key => $element_value_value) {
218 - $element->set($element_value_key, $element_value_value);
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 + }
219 422 }
220 423
221 - if (!$element_value['properties']) {
222 - $element_properties = array();
223 - } else {
224 - $element_properties = $element_value['properties'];
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 + }
225 438 }
226 - $element->set('properties', $element_properties);
227 -
228 - if (!$element_value['actions']) {
229 - $element_actions = array();
230 - } else {
231 - $element_actions = $element_value['actions'];
232 - }
233 - $element->set('actions', $element_actions);
234 - $element->set('page_id', $page->get('page_id'));
235 - $element->set('template_id', $this->get('ID'));
236 - $element->save();
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();
237 444 }
238 445 }
239 446 }
447 + if ($translator) {
448 + $translator->flush($this->get('ID'));
449 + }
240 450 }
241 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 +
242 458 return $this->get('ID');
243 459 }
244 460
245 - /**
246 - * Before save template
247 - */
248 - public function pre_save() {
249 -
250 - $fonts = array();
461 + // pre save
462 + public function load_fonts() {
463 + $fonts = [];
251 464 $model_e2pdf_font = new Model_E2pdf_Font();
252 -
253 465 $all_fonts = $model_e2pdf_font->get_fonts();
254 -
255 - $c_font = array_search($this->get('font'), $all_fonts);
466 + $c_font = array_search($this->get('font'), $all_fonts, true);
256 467 if ($c_font) {
257 468 $fonts[$c_font] = $this->get('font');
258 469 }
259 -
260 - $pages = $this->get('pages');
261 - foreach ($pages as $key => $page) {
470 + foreach ($this->get('pages') as $key => $page) {
262 471 if (!empty($page['elements'])) {
263 472 foreach ($page['elements'] as $element_key => $element) {
264 473 $tmp_fonts = $model_e2pdf_font->get_element_fonts($element, $all_fonts);
265 474 if (!empty($tmp_fonts)) {
@@ -268,40 +477,73 @@
268 477 }
269 478 }
270 479 }
271 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 +
272 488 $this->set('fonts', $fonts);
489 + }
273 490
274 - $template = array(
491 + // pre save
492 + public function pre_save() {
493 + $this->load_fonts();
494 + $template = [
275 495 'uid' => $this->get('uid'),
496 + 'activated' => $this->get('activated'),
276 497 'title' => $this->get('title'),
277 498 'pdf' => $this->get('pdf'),
278 499 'updated_at' => current_time('mysql', 1),
279 500 'flatten' => $this->get('flatten'),
501 + 'tab_order' => $this->get('tab_order'),
280 502 'format' => $this->get('format'),
503 + 'resample' => $this->get('resample'),
281 504 'compression' => $this->get('compression'),
505 + 'optimization' => $this->get('optimization'),
282 506 'appearance' => $this->get('appearance'),
283 507 'width' => $this->get('width'),
284 508 'height' => $this->get('height'),
285 509 'extension' => $this->get('extension'),
286 510 'item' => $this->get('item'),
287 - 'format' => $this->get('format'),
511 + 'item1' => $this->get('item1'),
512 + 'item2' => $this->get('item2'),
288 513 'dataset_title' => $this->get('dataset_title'),
514 + 'dataset_title1' => $this->get('dataset_title1'),
515 + 'dataset_title2' => $this->get('dataset_title2'),
289 516 'button_title' => $this->get('button_title'),
517 + 'dpdf' => $this->get('dpdf'),
290 518 'inline' => $this->get('inline'),
291 519 'auto' => $this->get('auto'),
520 + 'rtl' => $this->get('rtl'),
521 + 'font_processor' => $this->get('font_processor'),
292 522 'name' => $this->get('name'),
523 + 'savename' => $this->get('savename'),
293 524 'password' => $this->get('password'),
294 - 'fonts' => serialize($this->get('fonts')),
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
295 534 'font' => $this->get('font'),
296 535 'font_size' => $this->get('font_size'),
297 536 'font_color' => $this->get('font_color'),
298 537 'line_height' => $this->get('line_height'),
538 + 'text_align' => $this->get('text_align'),
299 539 'trash' => $this->get('trash'),
300 540 'locked' => $this->get('locked'),
301 - 'activated' => $this->get('activated'),
302 541 'author' => get_current_user_id(),
303 - );
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 + ];
304 546
305 547 if (!$this->get('ID')) {
306 548 $template['created_at'] = current_time('mysql', 1);
307 549 }
@@ -308,30 +550,23 @@
308 550
309 551 return $template;
310 552 }
311 553
312 - public function fill($dataset = false, $uid = false) {
554 + // fill
555 + public function fill() {
313 556
314 - $this->set('dataset', $dataset);
315 - $this->extension()->set('dataset', $this->get('dataset'));
557 + add_filter('e2pdf_pdf_fill', [$this->helper, '__return_true'], 999);
558 + do_action('e2pdf_model_template_fill_pre', $this, $this->extension);
316 559
317 - if ($uid) {
318 - $this->extension()->set('uid', $uid);
319 - }
320 -
321 - do_action('e2pdf_model_template_fill_pre', $this, $this->extension());
322 -
323 560 $action = new Model_E2pdf_Action();
324 - $action->load($this->extension());
325 -
561 + $action->load($this->extension);
326 562 $pages = $this->get('pages');
327 -
328 - $changed_elements = array();
563 + $changed_elements = [];
329 564 foreach ($pages as $key => $value) {
330 565 $changed_elements = array_merge($changed_elements, $action->process_page_id($value));
331 566 }
332 567
333 - $changed_pages = array();
568 + $changed_pages = [];
334 569 if (!empty($changed_elements)) {
335 570 foreach ($changed_elements as $element) {
336 571 $changed_pages[$element['page_id']][] = $element;
337 572 }
@@ -336,80 +571,145 @@
336 571 $changed_pages[$element['page_id']][] = $element;
337 572 }
338 573 }
339 574
340 - foreach ($pages as $key => $value) {
341 -
342 - if (isset($changed_pages[$value['page_id']])) {
343 - $value['elements'] = array_merge($value['elements'], $changed_pages[$value['page_id']]);
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']]);
344 578 }
345 -
346 - $pages[$key] = $value = $action->process_actions($value);
347 -
348 - if ($value) {
349 - if (!empty($value['elements'])) {
350 -
351 - $this->helper->load('sort')->stable_uasort($value['elements'], "sort_by_zindex");
352 - $pages[$key]['elements'] = $value['elements'];
353 -
354 - foreach ($value['elements'] as $el_key => $el_value) {
355 - if ($el_value['type'] === 'e2pdf-checkbox') {
356 - //e2pdf-checkbox render
357 - $pages[$key]['elements'][$el_key]['properties']['option'] = $this->extension()->render(
358 - $el_value['properties']['option'], false, $el_value
359 - );
360 - $pages[$key]['elements'][$el_key]['value'] = $this->extension()->render(
361 - $el_value['value'], 'value', $el_value
362 - );
363 - } elseif ($el_value['type'] === 'e2pdf-radio') {
364 - //e2pdf-radio render
365 - $pages[$key]['elements'][$el_key]['properties']['option'] = $this->extension()->render(
366 - $el_value['properties']['option'], false, $el_value
367 - );
368 - $pages[$key]['elements'][$el_key]['value'] = $this->extension()->render(
369 - $el_value['value'], 'value', $el_value
370 - );
371 - } elseif ($el_value['type'] === 'e2pdf-select') {
372 - //e2pdf-select render
373 - $pages[$key]['elements'][$el_key]['properties']['options'] = $this->extension()->render(
374 - $el_value['properties']['options'], false, $el_value
375 - );
376 - $pages[$key]['elements'][$el_key]['value'] = $this->extension()->render(
377 - $el_value['value'], 'value', $el_value
378 - );
379 - } elseif ($el_value['type'] === 'e2pdf-html') {
380 - //e2pdf-html render
381 - $pages[$key]['elements'][$el_key]['value'] = $this->extension()->render(
382 - $el_value['value'], 'value', $el_value
383 - );
384 - } elseif ($el_value['type'] === 'e2pdf-image') {
385 - //e2pdf-image render
386 - $pages[$key]['elements'][$el_key]['value'] = $this->extension()->render(
387 - $el_value['value'], 'value', $el_value
388 - );
389 - } elseif ($el_value['type'] === 'e2pdf-signature') {
390 - //e2pdf-signature render
391 - $pages[$key]['elements'][$el_key]['value'] = $this->extension()->render(
392 - $el_value['value'], 'value', $el_value
393 - );
394 - } else {
395 - //another e2pdf fields render
396 - $pages[$key]['elements'][$el_key]['value'] = $this->extension()->render(
397 - $el_value['value'], 'value', $el_value
398 - );
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 + }
399 700 }
400 701 }
401 702 }
402 - } else {
403 - unset($pages[$key]);
404 703 }
405 704 }
406 -
407 705 $this->set('pages', $pages);
408 706
409 - do_action('e2pdf_model_template_fill_after', $this, $this->extension());
707 + do_action('e2pdf_model_template_fill_after', $this, $this->extension);
708 + remove_filter('e2pdf_pdf_fill', [$this->helper, '__return_true'], 999);
410 709 }
411 710
711 + // pre render
412 712 public function pre_render() {
413 713 $pages = $this->get('pages');
414 714 foreach ($pages as $key => $page) {
415 715 $page = (array) $page;
@@ -414,150 +714,205 @@
414 714 foreach ($pages as $key => $page) {
415 715 $page = (array) $page;
416 716 $pages[$key] = $page;
417 717 if (!empty($page['elements'])) {
418 - foreach ($page['elements'] as $el_key => $el_value) {
419 - $el_value = (array) $el_value;
420 - $pages[$key]['elements'][$el_key] = $el_value;
421 - $esig = isset($el_value['properties']['esig']) && $el_value['properties']['esig'] ? true : false;
422 - if ($el_value['type'] == 'e2pdf-image' || ($el_value['type'] == 'e2pdf-signature' && !$esig)) {
423 - if ($image = $this->helper->load('image')->get_image($el_value['value'])) {
424 - $pages[$key]['elements'][$el_key]['value'] = $image;
425 - } else {
426 - $pages[$key]['elements'][$el_key]['value'] = '';
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 + );
427 728 }
428 729 }
429 730 }
430 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');
431 734 }
432 -
433 735 return $pages;
434 736 }
435 737
436 - public function convert($pre_save, $type = false) {
738 + // render
739 + public function render($type = false) {
437 740
438 - $model_e2pdf_convert = new Model_E2pdf_Convert();
741 + $model_e2pdf_api = new Model_E2pdf_Api();
439 742 $model_e2pdf_font = new Model_E2pdf_Font();
440 743
441 - if ($pre_save) {
442 - $pages = $this->get('pages');
443 - $this->template = $this->pre_save();
444 - $this->set('pages', $pages);
445 - $this->set('fonts', unserialize($this->get('fonts')));
446 - }
447 -
448 - $fonts = array();
449 - if ($this->get('fonts')) {
450 - foreach ($this->get('fonts') as $key => $value) {
451 - $fonts[] = array(
452 - 'name' => $value,
453 - 'value' => $model_e2pdf_font->get_font($key)
454 - );
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 + }
455 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 + }
456 791 }
457 792
458 793 $pages = $this->pre_render();
459 794
460 - $settings = array(
461 - 'title' => $this->get_filename(),
795 + $settings = [
796 + 'uid' => $this->get('uid'),
797 + 'activated' => $this->get('activated'),
798 + 'title' => $this->get_name(),
462 799 'flatten' => $this->get('flatten'),
800 + 'tab_order' => $this->get('tab_order'),
463 801 'format' => $this->get('format'),
802 + 'resample' => $this->get('resample'),
464 803 'compression' => $this->get('compression'),
465 804 'appearance' => $this->get('appearance'),
466 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'] : '',
467 814 'font' => $this->get('font'),
468 815 'font_size' => $this->get('font_size'),
469 816 'font_color' => $this->get('font_color'),
470 817 'line_height' => $this->get('line_height'),
471 - 'uid' => $this->get('uid'),
472 - 'dataset' => $this->get('dataset'),
473 - );
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 + ];
474 824
475 - if ($this->get('pdf')) {
476 - $pdf = $this->helper->get('pdf_dir') . $this->get('pdf') . "/" . $this->get('pdf') . ".pdf";
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';
477 833 if (file_exists($pdf)) {
834 + // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode, WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
478 835 $settings['pdf'] = base64_encode(file_get_contents($pdf));
479 836 }
480 837 }
481 838
482 - $model_e2pdf_convert->set(array(
483 - 'type' => $type,
484 - 'data' => array(
485 - 'settings' => $settings,
486 - 'pages' => $pages,
487 - 'fonts' => $fonts
488 - ),
489 - ));
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 + ];
490 845
491 - return $model_e2pdf_convert->convert();
492 - }
493 -
494 - public function render($pre_save = false) {
495 - $model_e2pdf_api = new Model_E2pdf_Api();
496 - $model_e2pdf_font = new Model_E2pdf_Font();
497 -
498 - if ($pre_save) {
499 - $pages = $this->get('pages');
500 - $this->template = $this->pre_save();
501 - $this->set('pages', $pages);
502 - $this->set('fonts', unserialize($this->get('fonts')));
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;
503 861 }
504 862
505 - $fonts = array();
506 - if ($this->get('fonts')) {
507 - foreach ($this->get('fonts') as $key => $value) {
508 - $fonts[] = array(
509 - 'name' => $value,
510 - 'value' => $model_e2pdf_font->get_font($key)
511 - );
512 - }
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();
513 874 }
514 875
515 - $pages = $this->pre_render();
516 -
517 - $settings = array(
518 - 'title' => $this->get_filename(),
519 - 'flatten' => $this->get('flatten'),
520 - 'format' => $this->get('format'),
521 - 'compression' => $this->get('compression'),
522 - 'appearance' => $this->get('appearance'),
523 - 'password' => $this->get('password'),
524 - 'font' => $this->get('font'),
525 - 'font_size' => $this->get('font_size'),
526 - 'font_color' => $this->get('font_color'),
527 - 'line_height' => $this->get('line_height'),
528 - 'uid' => $this->get('uid'),
529 - 'dataset' => $this->get('dataset')
530 - );
531 -
532 - if ($this->get('pdf')) {
533 - $pdf = $this->helper->get('pdf_dir') . $this->get('pdf') . "/" . $this->get('pdf') . ".pdf";
534 - if (file_exists($pdf)) {
535 - $settings['pdf'] = base64_encode(file_get_contents($pdf));
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 + }
536 886 }
537 - }
538 -
539 - $model_e2pdf_api->set(array(
540 - 'action' => 'template/build',
541 - 'data' => array(
887 + $data = [
542 888 'settings' => $settings,
543 889 'pages' => $pages,
544 - 'fonts' => $fonts
545 - ),
546 - ));
547 -
548 - $request = $model_e2pdf_api->request();
549 -
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);
550 906 return $request;
551 907 }
552 908
553 - public function get_filename() {
909 + public function get_name() {
910 + $name = __('(no title)', 'e2pdf');
554 911 if ($this->get('name')) {
555 912 $name = $this->get('name');
556 913 } elseif ($this->get('title')) {
557 914 $name = $this->get('title');
558 - } else {
559 - $name = __('(no title)', 'e2pdf');
560 915 }
561 916 return $name;
562 917 }
563 918
@@ -564,13 +919,146 @@
564 919 public function flush() {
565 920 if ($this->get('ID')) {
566 921 $model_e2pdf_page = new Model_E2pdf_Page();
567 922 $pages = $model_e2pdf_page->get_pages($this->get('ID'));
568 - foreach ($pages as $page_value) {
569 - $page = new Model_E2pdf_Page();
570 - $page->load($page_value['page_id'], $page_value['template_id']);
571 - $page->delete();
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();
572 927 }
573 928 }
574 929 }
575 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 + }
576 1064 }