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