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