PluginProbe
E2Pdf – Export Pdf Tool for WordPress / 1.08.06
E2Pdf – Export Pdf Tool for WordPress v1.08.06
1.32.49 1.32.48 1.32.43 1.32.40 1.32.34 1.32.32 1.32.31 1.32.26 1.32.22 1.32.23 1.32.18 1.32.17 1.32.15 trunk 1.00.00 1.00.13 1.01.01 1.02.02 1.03.07 1.04.07 1.05.03 1.06.02 1.07.11 1.08.00 1.08.06 All 72 releases
e2pdf / classes / model / e2pdf-shortcode.php

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

1,689 lines 68.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * E2pdf Shortcode 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
11 */
12 if (!defined('ABSPATH')) {
13 die('Access denied.');
14 }
15
16 class Model_E2pdf_Shortcode extends Model_E2pdf_Model {
17
18 function __construct() {
19 parent::__construct();
20 }
21
22 /**
23 * [e2pdf-attachment] shortcode
24 *
25 * @param array $atts - Attributes
26 */
27 function e2pdf_attachment($atts = array()) {
28
29 $response = '';
30
31 $template_id = isset($atts['id']) ? (int) $atts['id'] : 0;
32 $pdf = isset($atts['pdf']) ? $atts['pdf'] : false;
33 $apply = isset($atts['apply']) ? true : false;
34 $dataset = isset($atts['dataset']) ? $atts['dataset'] : false;
35 $uid = isset($atts['uid']) ? $atts['uid'] : false;
36
37 $args = array();
38 foreach ($atts as $att_key => $att_value) {
39 if (substr($att_key, 0, 3) === "arg") {
40 $args[$att_key] = $att_value;
41 }
42 }
43
44 if ($uid) {
45 $entry = new Model_E2pdf_Entry();
46 if ($entry->load_by_uid($uid)) {
47 $uid_params = $entry->get('entry');
48 $template_id = isset($uid_params['template_id']) ? (int) $uid_params['template_id'] : 0;
49 $dataset = isset($uid_params['dataset']) ? $uid_params['dataset'] : false;
50 $pdf = isset($uid_params['pdf']) ? $uid_params['pdf'] : false;
51 } else {
52 return $response;
53 }
54 }
55
56 if ($pdf) {
57 if ($apply && !$this->helper->load('filter')->is_stream($pdf) && file_exists($pdf)) {
58 return $pdf;
59 } else {
60 return $response;
61 }
62 }
63
64 if (!$apply || !$dataset || !$template_id) {
65 return $response;
66 }
67
68 $template = new Model_E2pdf_Template();
69 if ($template->load($template_id)) {
70
71 $uid_params = array();
72 $uid_params['template_id'] = $template_id;
73 $uid_params['dataset'] = $dataset;
74
75 $template->extension()->set('dataset', $dataset);
76
77 $options = array();
78 $options = apply_filters('e2pdf_model_shortcode_extension_options', $options, $template);
79 $options = apply_filters('e2pdf_model_shortcode_e2pdf_attachment_extension_options', $options, $template);
80
81 foreach ($options as $option_key => $option_value) {
82 $template->extension()->set($option_key, $option_value);
83 }
84
85 if ($template->extension()->verify()) {
86
87 if (array_key_exists('inline', $atts)) {
88 $inline = $atts['inline'] == 'true' ? 1 : 0;
89 $uid_params['inline'] = $inline;
90 }
91
92 if (array_key_exists('flatten', $atts)) {
93 $flatten = (int) $atts['flatten'];
94 $uid_params['flatten'] = $flatten;
95 $template->set('flatten', $flatten);
96 }
97
98 if (array_key_exists('format', $atts)) {
99 $format = $atts['format'];
100 $uid_params['format'] = $format;
101 $template->set('format', $format);
102 }
103
104 if (array_key_exists('password', $atts)) {
105 if (!array_key_exists('filter', $atts)) {
106 $password = $template->extension()->render($atts['password'], 'value');
107 } else {
108 $password = $atts['password'];
109 }
110 $uid_params['password'] = $password;
111 $template->set('password', $password);
112 } else {
113 $template->set('password', $template->extension()->render($template->get('password'), 'value'));
114 }
115
116 if (array_key_exists('meta_title', $atts)) {
117 if (!array_key_exists('filter', $atts)) {
118 $meta_title = $template->extension()->render($atts['meta_title'], 'value');
119 } else {
120 $meta_title = $atts['meta_title'];
121 }
122 $uid_params['meta_title'] = $meta_title;
123 $template->set('meta_title', $meta_title);
124 } else {
125 $template->set('meta_title', $template->extension()->render($template->get('meta_title'), 'value'));
126 }
127
128 if (array_key_exists('meta_subject', $atts)) {
129 if (!array_key_exists('filter', $atts)) {
130 $meta_subject = $template->extension()->render($atts['meta_subject'], 'value');
131 } else {
132 $meta_subject = $atts['meta_subject'];
133 }
134 $uid_params['meta_subject'] = $meta_subject;
135 $template->set('meta_subject', $meta_subject);
136 } else {
137 $template->set('meta_subject', $template->extension()->render($template->get('meta_subject'), 'value'));
138 }
139
140 if (array_key_exists('meta_author', $atts)) {
141 if (!array_key_exists('filter', $atts)) {
142 $meta_author = $template->extension()->render($atts['meta_author'], 'value');
143 } else {
144 $meta_author = $atts['meta_author'];
145 }
146 $uid_params['meta_author'] = $meta_author;
147 $template->set('meta_author', $meta_author);
148 } else {
149 $template->set('meta_author', $template->extension()->render($template->get('meta_author'), 'value'));
150 }
151
152 if (array_key_exists('meta_keywords', $atts)) {
153 if (!array_key_exists('filter', $atts)) {
154 $meta_keywords = $template->extension()->render($atts['meta_keywords'], 'value');
155 } else {
156 $meta_keywords = $atts['meta_keywords'];
157 }
158 $uid_params['meta_keywords'] = $meta_keywords;
159 $template->set('meta_keywords', $meta_keywords);
160 } else {
161 $template->set('meta_keywords', $template->extension()->render($template->get('meta_keywords'), 'value'));
162 }
163
164 if (array_key_exists('name', $atts)) {
165 if (!array_key_exists('filter', $atts)) {
166 $name = $template->extension()->render($atts['name'], 'value');
167 } else {
168 $name = $atts['name'];
169 }
170 $uid_params['name'] = $name;
171 $template->set('name', $name);
172 } else {
173 $template->set('name', $template->extension()->render($template->get('name'), 'value'));
174 }
175
176 if (array_key_exists('user_id', $atts)) {
177 $uid_params['user_id'] = (int) $atts['user_id'];
178 } else {
179 $uid_params['user_id'] = get_current_user_id();
180 }
181
182 $uid_params['args'] = $args;
183
184 $entry = new Model_E2pdf_Entry();
185 $entry->set('entry', $uid_params);
186 if (!$entry->load_by_uid($entry->get('uid'))) {
187 $entry->save();
188 }
189
190 $template->fill($dataset, $entry->get('uid'));
191 $request = $template->render();
192
193 if (!isset($request['error']) && $entry->get('ID')) {
194 $tmp_dir = $this->helper->get('tmp_dir') . 'e2pdf' . md5($entry->get('uid')) . '/';
195
196 $this->helper->create_dir($tmp_dir);
197
198 if ($template->get('name')) {
199 $name = $template->get('name');
200 } else {
201 $name = $template->extension()->render($template->get_filename(), 'value');
202 }
203
204 $file_name = $name . '.pdf';
205 $file_name = $this->helper->load('convert')->to_file_name($file_name);
206 $file_path = $tmp_dir . $file_name;
207 file_put_contents($file_path, base64_decode($request['file']));
208
209 if (file_exists($file_path)) {
210 $entry->set('pdf_num', $entry->get('pdf_num') + 1);
211 $entry->save();
212 return $file_path;
213 }
214 }
215 }
216 }
217 return $response;
218 }
219
220 /**
221 * [e2pdf-download] shortcode
222 *
223 * @param array $atts - Attributes
224 */
225 function e2pdf_download($atts = array()) {
226
227 $response = '';
228
229 $template_id = isset($atts['id']) ? (int) $atts['id'] : 0;
230 $dataset = isset($atts['dataset']) ? $atts['dataset'] : false;
231 $uid = isset($atts['uid']) ? $atts['uid'] : false;
232 $output = isset($atts['output']) ? $atts['output'] : false;
233 $pdf = isset($atts['pdf']) ? $atts['pdf'] : false;
234
235 $args = array();
236 foreach ($atts as $att_key => $att_value) {
237 if (substr($att_key, 0, 3) === "arg") {
238 $args[$att_key] = $att_value;
239 }
240 }
241
242 if ($uid) {
243 $entry = new Model_E2pdf_Entry();
244 if ($entry->load_by_uid($uid)) {
245 $uid_params = $entry->get('entry');
246 $template_id = isset($uid_params['template_id']) ? (int) $uid_params['template_id'] : 0;
247 $dataset = isset($uid_params['dataset']) ? $uid_params['dataset'] : false;
248 $pdf = isset($uid_params['pdf']) ? $uid_params['pdf'] : false;
249 } else {
250 return $response;
251 }
252 }
253
254 if ($pdf) {
255 if (!$this->helper->load('filter')->is_stream($pdf) && file_exists($pdf)) {
256
257 $uid_params = array();
258 $uid_params['pdf'] = $pdf;
259
260 if (array_key_exists('class', $atts)) {
261 $classes = explode(" ", $atts['class']);
262 } else {
263 $classes = array();
264 }
265 $classes[] = 'e2pdf-download';
266
267 if (array_key_exists('auto', $atts)) {
268 $auto = $atts['auto'] == 'true' ? 1 : 0;
269 if ($auto) {
270 $classes[] = 'e2pdf-auto';
271 }
272 }
273
274 if (array_key_exists('inline', $atts)) {
275 $inline = $atts['inline'] == 'true' ? 1 : 0;
276 $uid_params['inline'] = $inline;
277 if ($inline) {
278 $classes[] = 'e2pdf-inline';
279 }
280 }
281
282 if (array_key_exists('name', $atts)) {
283 $name = $atts['name'];
284 $uid_params['name'] = $name;
285 }
286
287 if (array_key_exists('button-title', $atts)) {
288 $button_title = $atts['button-title'];
289 } else {
290 $button_title = __('Download', 'e2pdf');
291 }
292
293 $entry = new Model_E2pdf_Entry();
294 $entry->set('entry', $uid_params);
295 if (!$entry->load_by_uid($entry->get('uid'))) {
296 $entry->save();
297 }
298
299 if ($entry->get('ID')) {
300 $url = esc_url(add_query_arg(array(
301 'page' => 'e2pdf-download',
302 'uid' => $entry->get('uid'),
303 ), site_url('/')
304 ));
305
306 if ($output && $output == 'url') {
307 $response = $url;
308 } else {
309 $response = "<a id='e2pdf-download' class='" . implode(" ", $classes) . "' target='_blank' href='{$url}'>{$button_title}</a>";
310 }
311 }
312 }
313 return $response;
314 }
315
316 if (!$dataset || !$template_id) {
317 return $response;
318 }
319
320 $template = new Model_E2pdf_Template();
321 if ($template->load($template_id, false)) {
322
323 $uid_params = array();
324 $uid_params['template_id'] = $template_id;
325 $uid_params['dataset'] = $dataset;
326
327 if (array_key_exists('class', $atts)) {
328 $classes = explode(" ", $atts['class']);
329 } else {
330 $classes = array();
331 }
332 $classes[] = 'e2pdf-download';
333
334 $template->extension()->set('dataset', $dataset);
335
336 $options = array();
337 $options = apply_filters('e2pdf_model_shortcode_extension_options', $options, $template);
338 $options = apply_filters('e2pdf_model_shortcode_e2pdf_download_extension_options', $options, $template);
339
340 foreach ($options as $option_key => $option_value) {
341 $template->extension()->set($option_key, $option_value);
342 }
343
344 if ($template->extension()->verify()) {
345
346 if (array_key_exists('auto', $atts)) {
347 $auto = $atts['auto'] == 'true' ? 1 : 0;
348 } else {
349 $auto = $template->get('auto');
350 }
351
352 if ($auto) {
353 $classes[] = 'e2pdf-auto';
354 }
355
356 if (array_key_exists('inline', $atts)) {
357 $inline = $atts['inline'] == 'true' ? 1 : 0;
358 $uid_params['inline'] = $inline;
359 } else {
360 $inline = $template->get('inline');
361 }
362
363 if ($inline) {
364 $classes[] = 'e2pdf-inline';
365 }
366
367 if (array_key_exists('flatten', $atts)) {
368 $flatten = (int) $atts['flatten'];
369 $uid_params['flatten'] = $flatten;
370 }
371
372 if (array_key_exists('format', $atts)) {
373 $format = $atts['format'];
374 $uid_params['format'] = $format;
375 }
376
377 if (array_key_exists('button-title', $atts)) {
378 if (!array_key_exists('filter', $atts)) {
379 $button_title = $template->extension()->render($atts['button-title'], 'value');
380 } else {
381 $button_title = $atts['button-title'];
382 }
383 } elseif ($template->extension()->render($template->get('button_title'), 'value') !== '') {
384 $button_title = $template->extension()->render($template->get('button_title'), 'value');
385 } else {
386 $button_title = __('Download', 'e2pdf');
387 }
388
389 if (array_key_exists('password', $atts)) {
390 if (!array_key_exists('filter', $atts)) {
391 $password = $template->extension()->render($atts['password'], 'value');
392 } else {
393 $password = $atts['password'];
394 }
395 $uid_params['password'] = $password;
396 }
397
398 if (array_key_exists('meta_title', $atts)) {
399 if (!array_key_exists('filter', $atts)) {
400 $meta_title = $template->extension()->render($atts['meta_title'], 'value');
401 } else {
402 $meta_title = $atts['meta_title'];
403 }
404 $uid_params['meta_title'] = $meta_title;
405 }
406
407 if (array_key_exists('meta_subject', $atts)) {
408 if (!array_key_exists('filter', $atts)) {
409 $meta_subject = $template->extension()->render($atts['meta_subject'], 'value');
410 } else {
411 $meta_subject = $atts['meta_subject'];
412 }
413 $uid_params['meta_subject'] = $meta_subject;
414 }
415
416 if (array_key_exists('meta_author', $atts)) {
417 if (!array_key_exists('filter', $atts)) {
418 $meta_author = $template->extension()->render($atts['meta_author'], 'value');
419 } else {
420 $meta_author = $atts['meta_author'];
421 }
422 $uid_params['meta_author'] = $meta_author;
423 }
424
425 if (array_key_exists('meta_keywords', $atts)) {
426 if (!array_key_exists('filter', $atts)) {
427 $meta_keywords = $template->extension()->render($atts['meta_keywords'], 'value');
428 } else {
429 $meta_keywords = $atts['meta_keywords'];
430 }
431 $uid_params['meta_keywords'] = $meta_keywords;
432 }
433
434 if (array_key_exists('name', $atts)) {
435 if (!array_key_exists('filter', $atts)) {
436 $name = $template->extension()->render($atts['name'], 'value');
437 } else {
438 $name = $atts['name'];
439 }
440 $uid_params['name'] = $name;
441 }
442
443 if (array_key_exists('user_id', $atts)) {
444 $uid_params['user_id'] = (int) $atts['user_id'];
445 } else {
446 $uid_params['user_id'] = get_current_user_id();
447 }
448
449 $uid_params['args'] = $args;
450
451 $entry = new Model_E2pdf_Entry();
452 $entry->set('entry', $uid_params);
453 if (!$entry->load_by_uid($entry->get('uid'))) {
454 $entry->save();
455 }
456
457 if ($entry->get('ID')) {
458 $url = esc_url(add_query_arg(array(
459 'page' => 'e2pdf-download',
460 'uid' => $entry->get('uid'),
461 ), site_url('/')
462 ));
463
464 if ($output && $output == 'url') {
465 $response = $url;
466 } else {
467 $response = "<a id='e2pdf-download' class='" . implode(" ", $classes) . "' target='_blank' href='{$url}'>{$button_title}</a>";
468 }
469 }
470 }
471 }
472 return $response;
473 }
474
475 /**
476 * @since 0.01.44
477 *
478 * [e2pdf-save] shortcode
479 *
480 * @param array $atts - Attributes
481 */
482 function e2pdf_save($atts = array()) {
483
484 $response = '';
485
486 $template_id = isset($atts['id']) ? (int) $atts['id'] : 0;
487 $dataset = isset($atts['dataset']) ? $atts['dataset'] : false;
488 $download = isset($atts['download']) && $atts['download'] == 'true' ? true : false;
489 $view = isset($atts['view']) && $atts['view'] == 'true' ? true : false;
490 $attachment = isset($atts['attachment']) && $atts['attachment'] == 'true' ? true : false;
491 $overwrite = isset($atts['overwrite']) && $atts['overwrite'] == 'false' ? false : true;
492 $output = isset($atts['output']) ? $atts['output'] : false;
493 $apply = isset($atts['apply']) ? true : false;
494
495 $args = array();
496 foreach ($atts as $att_key => $att_value) {
497 if (substr($att_key, 0, 3) === "arg") {
498 $args[$att_key] = $att_value;
499 }
500 }
501
502 if (!$apply || !$dataset || !$template_id) {
503 return $response;
504 }
505
506 $template = new Model_E2pdf_Template();
507
508 if ($template->load($template_id)) {
509
510 $uid_params = array();
511 $uid_params['template_id'] = $template_id;
512 $uid_params['dataset'] = $dataset;
513
514 $template->extension()->set('dataset', $dataset);
515
516 $options = array();
517 $options = apply_filters('e2pdf_model_shortcode_extension_options', $options, $template);
518 $options = apply_filters('e2pdf_model_shortcode_e2pdf_save_extension_options', $options, $template);
519
520 foreach ($options as $option_key => $option_value) {
521 $template->extension()->set($option_key, $option_value);
522 }
523
524 if ($template->extension()->verify()) {
525
526 if (array_key_exists('inline', $atts)) {
527 $inline = $atts['inline'] == 'true' ? 1 : 0;
528 $uid_params['inline'] = $inline;
529 }
530
531 if (array_key_exists('flatten', $atts)) {
532 $flatten = (int) $atts['flatten'];
533 $uid_params['flatten'] = $flatten;
534 $template->set('flatten', $flatten);
535 }
536
537 if (array_key_exists('format', $atts)) {
538 $format = $atts['format'];
539 $uid_params['format'] = $format;
540 $template->set('format', $format);
541 }
542
543 if (array_key_exists('password', $atts)) {
544 if (!array_key_exists('filter', $atts)) {
545 $password = $template->extension()->render($atts['password'], 'value');
546 } else {
547 $password = $atts['password'];
548 }
549 $uid_params['password'] = $password;
550 $template->set('password', $password);
551 } else {
552 $template->set('password', $template->extension()->render($template->get('password'), 'value'));
553 }
554
555 if (array_key_exists('meta_title', $atts)) {
556 if (!array_key_exists('filter', $atts)) {
557 $meta_title = $template->extension()->render($atts['meta_title'], 'value');
558 } else {
559 $meta_title = $atts['meta_title'];
560 }
561 $uid_params['meta_title'] = $meta_title;
562 $template->set('meta_title', $meta_title);
563 } else {
564 $template->set('meta_title', $template->extension()->render($template->get('meta_title'), 'value'));
565 }
566
567 if (array_key_exists('meta_subject', $atts)) {
568 if (!array_key_exists('filter', $atts)) {
569 $meta_subject = $template->extension()->render($atts['meta_subject'], 'value');
570 } else {
571 $meta_subject = $atts['meta_subject'];
572 }
573 $uid_params['meta_subject'] = $meta_subject;
574 $template->set('meta_subject', $meta_subject);
575 } else {
576 $template->set('meta_subject', $template->extension()->render($template->get('meta_subject'), 'value'));
577 }
578
579 if (array_key_exists('meta_author', $atts)) {
580 if (!array_key_exists('filter', $atts)) {
581 $meta_author = $template->extension()->render($atts['meta_author'], 'value');
582 } else {
583 $meta_author = $atts['meta_author'];
584 }
585 $uid_params['meta_author'] = $meta_author;
586 $template->set('meta_author', $meta_author);
587 } else {
588 $template->set('meta_author', $template->extension()->render($template->get('meta_author'), 'value'));
589 }
590
591 if (array_key_exists('meta_keywords', $atts)) {
592 if (!array_key_exists('filter', $atts)) {
593 $meta_keywords = $template->extension()->render($atts['meta_keywords'], 'value');
594 } else {
595 $meta_keywords = $atts['meta_keywords'];
596 }
597 $uid_params['meta_keywords'] = $meta_keywords;
598 $template->set('meta_keywords', $meta_keywords);
599 } else {
600 $template->set('meta_keywords', $template->extension()->render($template->get('meta_keywords'), 'value'));
601 }
602
603 if (array_key_exists('name', $atts)) {
604 if (!array_key_exists('filter', $atts)) {
605 $name = $template->extension()->render($atts['name'], 'value');
606 } else {
607 $name = $atts['name'];
608 }
609 $uid_params['name'] = $name;
610 $template->set('name', $name);
611 } else {
612 $template->set('name', $template->extension()->render($template->get('name'), 'value'));
613 }
614
615 if ($download || $view || $attachment) {
616 $uid_params['pdf'] = false;
617 }
618
619 if (array_key_exists('user_id', $atts)) {
620 $uid_params['user_id'] = (int) $atts['user_id'];
621 } else {
622 $uid_params['user_id'] = get_current_user_id();
623 }
624
625 $uid_params['args'] = $args;
626
627 $entry = new Model_E2pdf_Entry();
628 $entry->set('entry', $uid_params);
629 if (!$entry->load_by_uid($entry->get('uid'))) {
630 $entry->save();
631 }
632
633 if (array_key_exists('dir', $atts)) {
634 $save_dir = $atts['dir'];
635 } else {
636 $tpl_dir = $this->helper->get('tpl_dir') . $template->get('ID') . "/";
637 $save_dir = $tpl_dir . "save/";
638 $this->helper->create_dir($tpl_dir);
639 $this->helper->create_dir($save_dir);
640 }
641
642 if ($template->get('name')) {
643 $name = $template->get('name');
644 } else {
645 $name = $template->extension()->render($template->get_filename(), 'value');
646 }
647
648 $file_name = $name . '.pdf';
649 $file_name = $this->helper->load('convert')->to_file_name($file_name);
650 $file_path = $save_dir . $file_name;
651
652 if ($overwrite || !file_exists($file_path)) {
653 $template->fill($dataset, $entry->get('uid'));
654 $request = $template->render();
655 }
656
657 if (isset($request['error'])) {
658 return false;
659 } else {
660 if ($entry->get('ID')) {
661
662 if (is_dir($save_dir) && is_writable($save_dir)) {
663
664 if ($overwrite || !file_exists($file_path)) {
665 file_put_contents($file_path, base64_decode($request['file']));
666 }
667
668 if (!$this->helper->load('filter')->is_stream($file_path) && file_exists($file_path)) {
669 $entry->set('pdf_num', $entry->get('pdf_num') + 1);
670 if ($download || $view || $attachment) {
671 $uid_params['pdf'] = $file_path;
672 $entry->set('entry', $uid_params);
673 $atts['uid'] = $entry->get('uid');
674 }
675 $entry->save();
676 if ($download) {
677 $response = $this->e2pdf_download($atts);
678 } elseif ($view) {
679 $response = $this->e2pdf_view($atts);
680 } elseif ($attachment) {
681 $response = $this->e2pdf_attachment($atts);
682 } elseif ($output && $output == 'path') {
683 $response = $file_path;
684 }
685 }
686 }
687 }
688 }
689 }
690 }
691 return $response;
692 }
693
694 /**
695 * [e2pdf-view] shortcode
696 *
697 * @param array $atts - Attributes
698 */
699 function e2pdf_view($atts = array()) {
700
701 $response = '';
702 $name = '';
703
704 $template_id = isset($atts['id']) ? (int) $atts['id'] : 0;
705 $dataset = isset($atts['dataset']) ? $atts['dataset'] : false;
706 $uid = isset($atts['uid']) ? $atts['uid'] : false;
707 $width = isset($atts['width']) ? $atts['width'] : '100%';
708 $height = isset($atts['height']) ? $atts['height'] : '500';
709 $pdf = isset($atts['pdf']) ? $atts['pdf'] : false;
710 $page = isset($atts['page']) ? $atts['page'] : false;
711 $zoom = isset($atts['zoom']) ? $atts['zoom'] : false;
712 $nameddest = isset($atts['nameddest']) ? $atts['nameddest'] : false;
713 $pagemode = isset($atts['pagemode']) ? $atts['pagemode'] : false;
714
715 $args = array();
716 foreach ($atts as $att_key => $att_value) {
717 if (substr($att_key, 0, 3) === "arg") {
718 $args[$att_key] = $att_value;
719 }
720 }
721
722 $viewer_options = array();
723 if ($page) {
724 $viewer_options[] = 'page=' . $page;
725 }
726 if ($zoom) {
727 $viewer_options[] = 'zoom=' . $zoom;
728 }
729 if ($nameddest) {
730 $viewer_options[] = 'nameddest=' . $nameddest;
731 }
732 if ($pagemode) {
733 $viewer_options[] = 'pagemode=' . $pagemode;
734 }
735
736 if (array_key_exists('class', $atts)) {
737 $classes = explode(" ", $atts['class']);
738 } else {
739 $classes = array();
740 }
741 $classes[] = 'e2pdf-view';
742
743 if ($uid) {
744 $entry = new Model_E2pdf_Entry();
745 if ($entry->load_by_uid($uid)) {
746
747 $uid_params = $entry->get('entry');
748
749 $template_id = isset($uid_params['template_id']) ? (int) $uid_params['template_id'] : 0;
750 $dataset = isset($uid_params['dataset']) ? $uid_params['dataset'] : false;
751
752 $template = new Model_E2pdf_Template();
753 if ($uid_params['pdf'] && file_exists($uid_params['pdf']) && isset($uid_params['template_id']) && isset($uid_params['dataset']) && $template->load($uid_params['template_id'])) {
754
755 if (isset($uid_params['name'])) {
756 $template->set('name', $uid_params['name']);
757 } else {
758 $template->set('name', $template->extension()->render($template->get('name'), 'value'));
759 }
760 if ($template->get('name')) {
761 $name = $template->get('name');
762 } else {
763 $name = $template->extension()->render($template->get_filename(), 'value');
764 }
765
766 if (!$name) {
767 $name = basename($uid_params['pdf'], ".pdf");
768 }
769
770 $pdf = esc_url_raw(add_query_arg(array(
771 'page' => 'e2pdf-download',
772 'uid' => $entry->get('uid'),
773 'saveName' => $name
774 ), site_url('/')
775 ));
776 }
777 } else {
778 return $response;
779 }
780 }
781
782 if ($pdf) {
783 if (filter_var($pdf, FILTER_VALIDATE_URL)) {
784 $file = urlencode($pdf);
785 if (!empty($viewer_options)) {
786 $file .= '#' . implode('&', $viewer_options);
787 }
788
789 $url = plugins_url('assets/pdf.js/web/viewer.html?file=' . $file, $this->helper->get('plugin_file_path'));
790 $response = "<iframe class='" . implode(" ", $classes) . "' width='{$width}' height='{$height}' src='{$url}'></iframe>";
791 } else if (!$this->helper->load('filter')->is_stream($pdf) && file_exists($pdf)) {
792
793 $uid_params['pdf'] = $pdf;
794
795 if (array_key_exists('name', $atts)) {
796 $name = $atts['name'];
797 $uid_params['name'] = $name;
798 }
799
800 $entry = new Model_E2pdf_Entry();
801 $entry->set('entry', $uid_params);
802 if (!$entry->load_by_uid($entry->get('uid'))) {
803 $entry->save();
804 }
805
806 if ($entry->get('ID')) {
807 if (!$name) {
808 $name = basename($uid_params['pdf'], ".pdf");
809 }
810
811 $pdf_url = esc_url_raw(add_query_arg(array(
812 'page' => 'e2pdf-download',
813 'uid' => $entry->get('uid'),
814 'saveName' => $name
815 ), site_url('/')
816 ));
817
818 $file = urlencode($pdf_url);
819 if (!empty($viewer_options)) {
820 $file .= '#' . implode('&', $viewer_options);
821 }
822
823 $url = plugins_url('assets/pdf.js/web/viewer.html?file=' . $file, $this->helper->get('plugin_file_path'));
824 $response = "<iframe class='" . implode(" ", $classes) . "' width='{$width}' height='{$height}' src='{$url}'></iframe>";
825 }
826 }
827 return $response;
828 }
829
830 if (!$template_id || !$dataset) {
831 return $response;
832 }
833
834 $template = new Model_E2pdf_Template();
835 if ($template->load($template_id, false)) {
836
837 $uid_params = array();
838 $uid_params['template_id'] = $template_id;
839 $uid_params['dataset'] = $dataset;
840
841 $template->extension()->set('dataset', $dataset);
842
843 $options = array();
844 $options = apply_filters('e2pdf_model_shortcode_extension_options', $options, $template);
845 $options = apply_filters('e2pdf_model_shortcode_e2pdf_view_extension_options', $options, $template);
846
847 foreach ($options as $option_key => $option_value) {
848 $template->extension()->set($option_key, $option_value);
849 }
850
851 if ($template->extension()->verify()) {
852
853 if (array_key_exists('inline', $atts)) {
854 $inline = $atts['inline'] == 'true' ? 1 : 0;
855 $uid_params['inline'] = $inline;
856 }
857
858 if (array_key_exists('flatten', $atts)) {
859 $flatten = (int) $atts['flatten'];
860 $uid_params['flatten'] = $flatten;
861 }
862
863 if (array_key_exists('format', $atts)) {
864 $format = $atts['format'];
865 $uid_params['format'] = $format;
866 }
867
868 if (array_key_exists('password', $atts)) {
869 if (!array_key_exists('filter', $atts)) {
870 $password = $template->extension()->render($atts['password']);
871 } else {
872 $password = $atts['password'];
873 }
874 $uid_params['password'] = $password;
875 }
876
877 if (array_key_exists('meta_title', $atts)) {
878 if (!array_key_exists('filter', $atts)) {
879 $meta_title = $template->extension()->render($atts['meta_title'], 'value');
880 } else {
881 $meta_title = $atts['meta_title'];
882 }
883 $uid_params['meta_title'] = $meta_title;
884 }
885
886 if (array_key_exists('meta_subject', $atts)) {
887 if (!array_key_exists('filter', $atts)) {
888 $meta_subject = $template->extension()->render($atts['meta_subject'], 'value');
889 } else {
890 $meta_subject = $atts['meta_subject'];
891 }
892 $uid_params['meta_subject'] = $meta_subject;
893 }
894
895 if (array_key_exists('meta_author', $atts)) {
896 if (!array_key_exists('filter', $atts)) {
897 $meta_author = $template->extension()->render($atts['meta_author'], 'value');
898 } else {
899 $meta_author = $atts['meta_author'];
900 }
901 $uid_params['meta_author'] = $meta_author;
902 }
903
904 if (array_key_exists('meta_keywords', $atts)) {
905 if (!array_key_exists('filter', $atts)) {
906 $meta_keywords = $template->extension()->render($atts['meta_keywords'], 'value');
907 } else {
908 $meta_keywords = $atts['meta_keywords'];
909 }
910 $uid_params['meta_keywords'] = $meta_keywords;
911 }
912
913 if (array_key_exists('name', $atts)) {
914 if (!array_key_exists('filter', $atts)) {
915 $name = $template->extension()->render($atts['name'], 'value');
916 } else {
917 $name = $atts['name'];
918 }
919 $uid_params['name'] = $name;
920 $template->set('name', $name);
921 } else {
922 $template->set('name', $template->extension()->render($template->get('name'), 'value'));
923 }
924
925 if (array_key_exists('user_id', $atts)) {
926 $uid_params['user_id'] = (int) $atts['user_id'];
927 } else {
928 $uid_params['user_id'] = get_current_user_id();
929 }
930
931 $uid_params['args'] = $args;
932
933 $entry = new Model_E2pdf_Entry();
934 $entry->set('entry', $uid_params);
935 if (!$entry->load_by_uid($entry->get('uid'))) {
936 $entry->save();
937 }
938
939 if ($entry->get('ID')) {
940
941 $pdf_url = esc_url_raw(add_query_arg(array(
942 'page' => 'e2pdf-download',
943 'uid' => $entry->get('uid'),
944 'saveName' => $template->get('name') ? $template->get('name') . ".pdf" : $template->extension()->render($template->get_filename(), 'value') . ".pdf"
945 ), site_url('/')
946 ));
947
948 $file = urlencode($pdf_url);
949 if (!empty($viewer_options)) {
950 $file .= '#' . implode('&', $viewer_options);
951 }
952
953 $url = plugins_url('assets/pdf.js/web/viewer.html?file=' . $file, $this->helper->get('plugin_file_path'));
954 $response = "<iframe class='" . implode(" ", $classes) . "' width='{$width}' height='{$height}' src='{$url}'></iframe>";
955 }
956 }
957 }
958 return $response;
959 }
960
961 /**
962 * [e2pdf-adobesign] shortcode
963 *
964 * @param array $atts - Attributes
965 */
966 function e2pdf_adobesign($atts = array()) {
967
968 $template_id = isset($atts['id']) ? (int) $atts['id'] : 0;
969
970 $args = array();
971 foreach ($atts as $att_key => $att_value) {
972 if (substr($att_key, 0, 3) === "arg") {
973 $args[$att_key] = $att_value;
974 }
975 }
976
977 $response = '';
978
979 if (!array_key_exists('apply', $atts) || !array_key_exists('dataset', $atts) || !$template_id) {
980 return $response;
981 }
982
983 $dataset = $atts['dataset'];
984 $template = new Model_E2pdf_Template();
985 if ($template->load($template_id)) {
986 $uid_params = array();
987 $uid_params['template_id'] = $template_id;
988 $uid_params['dataset'] = $dataset;
989
990 $template->extension()->set('dataset', $dataset);
991
992 $options = array();
993 $options = apply_filters('e2pdf_model_shortcode_extension_options', $options, $template);
994 $options = apply_filters('e2pdf_model_shortcode_e2pdf_adobesign_extension_options', $options, $template);
995
996 foreach ($options as $option_key => $option_value) {
997 $template->extension()->set($option_key, $option_value);
998 }
999
1000 if ($template->extension()->verify()) {
1001
1002 if (array_key_exists('inline', $atts)) {
1003 $inline = $atts['inline'] == 'true' ? 1 : 0;
1004 $uid_params['inline'] = $inline;
1005 }
1006
1007 if (array_key_exists('flatten', $atts)) {
1008 $flatten = (int) $atts['flatten'];
1009 $uid_params['flatten'] = $flatten;
1010 $template->set('flatten', $flatten);
1011 }
1012
1013 if (array_key_exists('format', $atts)) {
1014 $format = $atts['format'];
1015 $uid_params['format'] = $format;
1016 $template->set('format', $format);
1017 }
1018
1019 if (array_key_exists('password', $atts)) {
1020 if (!array_key_exists('filter', $atts)) {
1021 $password = $template->extension()->render($atts['password'], 'value');
1022 } else {
1023 $password = $atts['password'];
1024 }
1025 $uid_params['password'] = $password;
1026 $template->set('password', $password);
1027 } else {
1028 $template->set('password', $template->extension()->render($template->get('password'), 'value'));
1029 }
1030
1031 if (array_key_exists('meta_title', $atts)) {
1032 if (!array_key_exists('filter', $atts)) {
1033 $meta_title = $template->extension()->render($atts['meta_title'], 'value');
1034 } else {
1035 $meta_title = $atts['meta_title'];
1036 }
1037 $uid_params['meta_title'] = $meta_title;
1038 $template->set('meta_title', $meta_title);
1039 } else {
1040 $template->set('meta_title', $template->extension()->render($template->get('meta_title'), 'value'));
1041 }
1042
1043 if (array_key_exists('meta_subject', $atts)) {
1044 if (!array_key_exists('filter', $atts)) {
1045 $meta_subject = $template->extension()->render($atts['meta_subject'], 'value');
1046 } else {
1047 $meta_subject = $atts['meta_subject'];
1048 }
1049 $uid_params['meta_subject'] = $meta_subject;
1050 $template->set('meta_subject', $meta_subject);
1051 } else {
1052 $template->set('meta_subject', $template->extension()->render($template->get('meta_subject'), 'value'));
1053 }
1054
1055 if (array_key_exists('meta_author', $atts)) {
1056 if (!array_key_exists('filter', $atts)) {
1057 $meta_author = $template->extension()->render($atts['meta_author'], 'value');
1058 } else {
1059 $meta_author = $atts['meta_author'];
1060 }
1061 $uid_params['meta_author'] = $meta_author;
1062 $template->set('meta_author', $meta_author);
1063 } else {
1064 $template->set('meta_author', $template->extension()->render($template->get('meta_author'), 'value'));
1065 }
1066
1067 if (array_key_exists('meta_keywords', $atts)) {
1068 if (!array_key_exists('filter', $atts)) {
1069 $meta_keywords = $template->extension()->render($atts['meta_keywords'], 'value');
1070 } else {
1071 $meta_keywords = $atts['meta_keywords'];
1072 }
1073 $uid_params['meta_keywords'] = $meta_keywords;
1074 $template->set('meta_keywords', $meta_keywords);
1075 } else {
1076 $template->set('meta_keywords', $template->extension()->render($template->get('meta_keywords'), 'value'));
1077 }
1078
1079 if (array_key_exists('name', $atts)) {
1080 if (!array_key_exists('filter', $atts)) {
1081 $name = $template->extension()->render($atts['name'], 'value');
1082 } else {
1083 $name = $atts['name'];
1084 }
1085 $uid_params['name'] = $name;
1086 $template->set('name', $name);
1087 } else {
1088 $template->set('name', $template->extension()->render($template->get('name'), 'value'));
1089 }
1090
1091 $disable = array();
1092 if (array_key_exists('disable', $atts)) {
1093 $disable = explode(',', $atts['disable']);
1094 }
1095
1096 if (array_key_exists('user_id', $atts)) {
1097 $uid_params['user_id'] = (int) $atts['user_id'];
1098 } else {
1099 $uid_params['user_id'] = get_current_user_id();
1100 }
1101
1102 $uid_params['args'] = $args;
1103
1104 $entry = new Model_E2pdf_Entry();
1105 $entry->set('entry', $uid_params);
1106 if (!$entry->load_by_uid($entry->get('uid'))) {
1107 $entry->save();
1108 }
1109
1110 $template->fill($dataset, $entry->get('uid'));
1111 $request = $template->render();
1112
1113 if (!isset($request['error']) && $entry->get('ID')) {
1114 $tmp_dir = $this->helper->get('tmp_dir') . 'e2pdf' . md5($entry->get('uid')) . '/';
1115 $this->helper->create_dir($tmp_dir);
1116
1117 if ($template->get('name')) {
1118 $name = $template->get('name');
1119 } else {
1120 $name = $template->extension()->render($template->get_filename(), 'value');
1121 }
1122
1123 $file_name = $name . '.pdf';
1124 $file_name = $this->helper->load('convert')->to_file_name($file_name);
1125 $file_path = $tmp_dir . $file_name;
1126 file_put_contents($file_path, base64_decode($request['file']));
1127
1128 if (file_exists($file_path)) {
1129
1130 $agreement_id = false;
1131 $documents = array();
1132 if (!in_array('post_transientDocuments', $disable)) {
1133 $model_e2pdf_adobesign = new Model_E2pdf_AdobeSign();
1134 $model_e2pdf_adobesign->set(array(
1135 'action' => 'api/rest/v5/transientDocuments',
1136 'headers' => array(
1137 'Content-Type: multipart/form-data',
1138 ),
1139 'data' => array(
1140 'File-Name' => $file_name,
1141 'Mime-Type' => 'application/pdf',
1142 'File' => class_exists('cURLFile') ? new cURLFile($file_path) : '@' . $file_path
1143 ),
1144 ));
1145
1146 if ($transientDocumentId = $model_e2pdf_adobesign->request('transientDocumentId')) {
1147 $documents[] = array(
1148 'transientDocumentId' => $transientDocumentId
1149 );
1150 }
1151 $model_e2pdf_adobesign->flush();
1152 }
1153
1154 $documents = apply_filters('e2pdf_model_shortcode_e2pdf_adobesign_fileInfos', $documents, $atts, $template, $entry, $template->extension(), $file_path);
1155
1156 if (!in_array('post_agreements', $disable) && !empty($documents)) {
1157
1158 $output = false;
1159 if (array_key_exists('output', $atts)) {
1160 $output = $atts['output'];
1161 }
1162
1163 $recipients = array();
1164 if (array_key_exists('recipients', $atts)) {
1165 $atts['recipients'] = $template->extension()->render($atts['recipients']);
1166 $recipients_list = explode(',', $atts['recipients']);
1167
1168 foreach ($recipients_list as $recipient_info) {
1169 $recipients[] = array(
1170 'recipientSetMemberInfos' => array(
1171 'email' => trim($recipient_info)
1172 ),
1173 'recipientSetRole' => 'SIGNER'
1174 );
1175 }
1176 }
1177
1178 $data = array(
1179 'documentCreationInfo' => array(
1180 'signatureType' => 'ESIGN',
1181 'recipientSetInfos' => $recipients,
1182 'signatureFlow' => 'SENDER_SIGNATURE_NOT_REQUIRED',
1183 'fileInfos' => $documents,
1184 'name' => $name
1185 )
1186 );
1187
1188 $data = apply_filters('e2pdf_model_shortcode_e2pdf_adobesign_post_agreements_data', $data, $atts, $template, $entry, $template->extension(), $file_path, $documents);
1189
1190 $model_e2pdf_adobesign = new Model_E2pdf_AdobeSign();
1191 $model_e2pdf_adobesign->set(array(
1192 'action' => 'api/rest/v5/agreements',
1193 'data' => $data,
1194 ));
1195
1196 $agreement_id = $model_e2pdf_adobesign->request('agreementId');
1197 $model_e2pdf_adobesign->flush();
1198 }
1199
1200 $response = apply_filters('e2pdf_model_shortcode_e2pdf_adobesign_response', $response, $atts, $template, $entry, $template->extension(), $file_path, $documents, $agreement_id);
1201 }
1202
1203 $this->helper->delete_dir($tmp_dir);
1204 return $response;
1205 }
1206 }
1207 }
1208 return $response;
1209 }
1210
1211 /**
1212 * [e2pdf-format-number] shortcode
1213 *
1214 * @param array $atts - Attributes
1215 * @param string $value - Content
1216 */
1217 function e2pdf_format_number($atts = array(), $value = '') {
1218
1219 $dec_point = isset($atts['dec_point']) ? $atts['dec_point'] : '.';
1220 $thousands_sep = isset($atts['thousands_sep']) ? $atts['thousands_sep'] : '';
1221 $decimal = isset($atts['decimal']) ? $atts['decimal'] : false;
1222 $explode = isset($atts['explode']) ? $atts['explode'] : '';
1223 $implode = isset($atts['implode']) ? $atts['implode'] : '';
1224
1225 $new_value = array();
1226 $value = array_filter((array) $value, 'strlen');
1227 foreach ($value as $v) {
1228 if ($explode && strpos($v, $explode)) {
1229 $v = explode($explode, $v);
1230 }
1231 foreach ((array) $v as $n) {
1232 $n = str_replace(array(" ", ","), array("", "."), $n);
1233 $n = preg_replace('/\.(?=.*\.)/', '', $n);
1234 $n = floatval($n);
1235
1236 if (!$decimal) {
1237 $num = explode('.', $n);
1238 $decimal = isset($num[1]) ? strlen($num[1]) : 0;
1239 }
1240
1241 $n = number_format($n, $decimal, $dec_point, $thousands_sep);
1242 $new_value[] = $n;
1243 }
1244 unset($v);
1245 }
1246 $new_value = array_filter((array) $new_value, 'strlen');
1247 return implode($implode, $new_value);
1248 }
1249
1250 /**
1251 * [e2pdf-format-date] shortcode
1252 *
1253 * @param array $atts - Attributes
1254 * @param string $value - Content
1255 */
1256 function e2pdf_format_date($atts = array(), $value = '') {
1257 $format = isset($atts['format']) ? $atts['format'] : get_option('date_format');
1258 if (!$value) {
1259 return '';
1260 }
1261 $value = date($format, strtotime($value));
1262 return $value;
1263 }
1264
1265 /**
1266 * [e2pdf-format-output] shortcode
1267 *
1268 * @param array $atts - Attributes
1269 * @param string $value - Content
1270 */
1271 function e2pdf_format_output($atts = array(), $value = '') {
1272
1273 $explode = isset($atts['explode']) ? $atts['explode'] : false;
1274 $implode = isset($atts['implode']) ? $atts['implode'] : '';
1275 $output = isset($atts['output']) ? $atts['output'] : false;
1276 $filter = isset($atts['filter']) ? $atts['filter'] : false;
1277 $search = isset($atts['search']) ? explode("|||", $atts['search']) : array();
1278 $ireplace = isset($atts['ireplace']) ? explode("|||", $atts['ireplace']) : array();
1279 $replace = isset($atts['replace']) ? explode("|||", $atts['replace']) : array();
1280
1281 $filters = array();
1282 if ($filter) {
1283 if (strpos($filter, ',')) {
1284 $filters = explode(',', $filter);
1285 } else {
1286 $filters = array_filter((array) $filter, 'strlen');
1287 }
1288 }
1289
1290 $new_value = array();
1291 if (!empty($ireplace)) {
1292 $value = str_ireplace($search, $ireplace, $value);
1293 } elseif (!empty($replace)) {
1294 $value = str_replace($search, $replace, $value);
1295 }
1296
1297 $value = array_filter((array) $value, 'strlen');
1298
1299 foreach ($value as $v) {
1300 if ($explode && strpos($v, $explode)) {
1301 $v = explode($explode, $v);
1302 }
1303 foreach ((array) $v as $n) {
1304 if (!empty($filters)) {
1305 foreach ((array) $filters as $sub_filter) {
1306 switch ($sub_filter) {
1307 case 'trim':
1308 $n = trim($n);
1309 break;
1310
1311 case 'strip_tags':
1312 $n = strip_tags($n);
1313 break;
1314
1315 case 'strtolower':
1316 if (function_exists('mb_strtolower')) {
1317 $n = mb_strtolower($n);
1318 } elseif (function_exists('strtolower')) {
1319 $n = strtolower($n);
1320 }
1321 break;
1322
1323 case 'ucfirst':
1324 if (function_exists('mb_strtoupper') && function_exists('mb_strtolower')) {
1325 $fc = mb_strtoupper(mb_substr($n, 0, 1));
1326 $n = $fc . mb_substr($n, 1);
1327 } else if (function_exists('ucfirst') && function_exists('strtolower')) {
1328 $n = ucfirst($n);
1329 }
1330 break;
1331
1332 case 'strtoupper':
1333 if (function_exists('mb_strtoupper')) {
1334 $n = mb_strtoupper($n);
1335 } elseif (function_exists('strtoupper')) {
1336 $n = strtoupper($n);
1337 }
1338 break;
1339
1340 case 'lines':
1341 $n = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $n);
1342 break;
1343
1344 case 'nl2br':
1345 $n = nl2br($n);
1346 break;
1347
1348 default:
1349 break;
1350 }
1351 }
1352 }
1353 $new_value[] = $n;
1354 }
1355 unset($v);
1356 }
1357
1358 if ($output) {
1359 $o_search = array();
1360 $o_replace = array();
1361
1362 foreach ($new_value as $key => $value) {
1363 $o_search[] = "{" . $key . "}";
1364 $o_replace[] = $value;
1365 }
1366 $output = str_replace($o_search, $o_replace, $output);
1367 return preg_replace('~(?:{/?)[^/}]+/?}~s', "", $output);
1368 } else {
1369 return implode($implode, $new_value);
1370 }
1371 }
1372
1373 /**
1374 * [e2pdf-user] shortcode
1375 *
1376 * @param array $atts - Attributes
1377 */
1378 function e2pdf_user($atts = array()) {
1379
1380 $id = isset($atts['id']) ? $atts['id'] : false;
1381 $key = isset($atts['key']) ? $atts['key'] : false;
1382 $meta = isset($atts['meta']) && $atts['meta'] == 'true' ? true : false;
1383
1384 $response = '';
1385
1386 if ($id) {
1387 $data_fields = array(
1388 'ID',
1389 'user_login',
1390 'user_nicename',
1391 'user_email',
1392 'user_url',
1393 'user_registered',
1394 'display_name'
1395 );
1396
1397 $data_fields = apply_filters('e2pdf_model_shortcode_user_data_fields', $data_fields);
1398
1399 if ($key) {
1400 if (in_array($key, $data_fields) && !$meta) {
1401 $user = get_userdata($id);
1402 if (isset($user->$key)) {
1403 $response = $user->$key;
1404 }
1405 } else {
1406 $response = get_user_meta($id, $key, true);
1407 }
1408 } else {
1409 $response = $id;
1410 }
1411 }
1412 return $response;
1413 }
1414
1415 /**
1416 * [e2pdf-content] shortcode
1417 *
1418 * @param array $atts - Attributes
1419 * @param string $value - Content
1420 */
1421 function e2pdf_content($atts = array(), $value = '') {
1422 $response = '';
1423
1424 $id = isset($atts['id']) ? $atts['id'] : false;
1425 $key = isset($atts['key']) ? $atts['key'] : false;
1426
1427 if ($id && $key) {
1428 $post = get_post($id);
1429 if ($post) {
1430 if (isset($post->post_content) && $post->post_content) {
1431 $content = $this->helper->load('convert')->to_content_key($key, $post->post_content);
1432 remove_filter('the_content', 'wpautop');
1433 $content = apply_filters('the_content', $content);
1434 add_filter('the_content', 'wpautop');
1435 $content = str_replace("</p>", "</p>\r\n", $content);
1436 $response = $content;
1437 }
1438 }
1439 } elseif ($value) {
1440 $response = apply_filters('the_content', $value);
1441 }
1442 return $response;
1443 }
1444
1445 /**
1446 * [e2pdf-exclude] shortcode
1447 *
1448 * @param array $atts - Attributes
1449 * @param string $value - Content
1450 */
1451 function e2pdf_exclude($atts = array(), $value = '') {
1452
1453 $apply = isset($atts['apply']) ? true : false;
1454
1455 if ($apply) {
1456 $response = '';
1457 } else {
1458 $response = apply_filters('the_content', $value);
1459 }
1460
1461 return $response;
1462 }
1463
1464 /**
1465 * [e2pdf-wp] shortcode
1466 *
1467 * @param array $atts - Attributes
1468 * @param string $value - Content
1469 */
1470 function e2pdf_wp($atts = array(), $value = '') {
1471
1472 $response = '';
1473
1474 $id = isset($atts['id']) ? $atts['id'] : false;
1475 $key = isset($atts['key']) ? $atts['key'] : false;
1476 $path = isset($atts['path']) ? $atts['path'] : false;
1477 $names = isset($atts['names']) && $atts['names'] == 'true' ? true : false;
1478 $explode = isset($atts['explode']) ? $atts['explode'] : '.';
1479 $implode = isset($atts['implode']) ? $atts['implode'] : ', ';
1480 $attachment_url = isset($atts['attachment_url']) && $atts['attachment_url'] == 'true' ? true : false;
1481 $attachment_image_url = isset($atts['attachment_image_url']) && $atts['attachment_image_url'] == 'true' ? true : false;
1482 $size = isset($atts['size']) ? $atts['size'] : 'thumbnail';
1483 $meta = isset($atts['meta']) && $atts['meta'] == 'true' ? true : false;
1484 $terms = isset($atts['terms']) && $atts['terms'] == 'true' ? true : false;
1485
1486 $data_fields = array(
1487 'id',
1488 'post_author',
1489 'post_date',
1490 'post_date_gmt',
1491 'post_content',
1492 'post_title',
1493 'post_excerpt',
1494 'post_status',
1495 'comment_status',
1496 'ping_status',
1497 'post_password',
1498 'post_name',
1499 'to_ping',
1500 'pinged',
1501 'post_modified',
1502 'post_modified_gmt',
1503 'post_content_filtered',
1504 'post_parent',
1505 'guid',
1506 'menu_order',
1507 'post_type',
1508 'post_mime_type',
1509 'comment_count',
1510 'filter',
1511 'post_thumbnail'
1512 );
1513
1514 $data_fields = apply_filters('e2pdf_model_shortcode_wp_data_fields', $data_fields);
1515
1516 if ($id && $key) {
1517 $post = get_post($id);
1518 if ($post) {
1519 if (in_array($key, $data_fields) && !$meta && !$terms) {
1520 if ($key == 'post_author') {
1521 $response = isset($post->post_author) && $post->post_author ? get_userdata($post->post_author)->user_nicename : '';
1522 } elseif ($key == 'id' && isset($post->ID)) {
1523 $response = $post->ID;
1524 } elseif ($key == 'post_thumbnail' && isset($post->ID)) {
1525 $response = get_the_post_thumbnail_url($post->ID, $size);
1526 } elseif ($key == 'post_content' && isset($post->post_content)) {
1527 $content = $post->post_content;
1528
1529 if (false !== strpos($content, '[')) {
1530 $shortcode_tags = array(
1531 'e2pdf-exclude',
1532 );
1533
1534 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $content, $matches);
1535 $tagnames = array_intersect($shortcode_tags, $matches[1]);
1536
1537 if (!empty($tagnames)) {
1538
1539 $pattern = $this->helper->load('shortcode')->get_shortcode_regex($tagnames);
1540 preg_match_all("/$pattern/", $content, $shortcodes);
1541 foreach ($shortcodes[0] as $key => $shortcode_value) {
1542 $shortcode = array();
1543 $shortcode[1] = $shortcodes[1][$key];
1544 $shortcode[2] = $shortcodes[2][$key];
1545 $shortcode[3] = $shortcodes[3][$key];
1546 $shortcode[4] = $shortcodes[4][$key];
1547 $shortcode[5] = $shortcodes[5][$key];
1548 $shortcode[6] = $shortcodes[6][$key];
1549
1550 $shortcode[3] .= " apply=\"true\"";
1551 $content = str_replace($shortcode_value, "[" . $shortcode['2'] . $shortcode['3'] . "]" . $shortcode['5'] . "[/" . $shortcode['2'] . "]", $content);
1552 }
1553 }
1554 }
1555 $content = apply_filters('the_content', $content);
1556 $content = str_replace("</p>", "</p>\r\n", $content);
1557 $response = $content;
1558 } elseif (isset($post->$key)) {
1559 $response = $post->$key;
1560 }
1561 } elseif ($terms) {
1562 if ($names) {
1563 $post_terms = wp_get_post_terms($id, $key, array('fields' => 'names'));
1564 if (!is_wp_error($post_terms) && is_array($post_terms)) {
1565 $response = implode($implode, $post_terms);
1566 }
1567 } else {
1568 $post_terms = wp_get_post_terms($id, $key);
1569 if (!is_wp_error($post_terms)) {
1570 $post_terms = json_decode(json_encode($post_terms), true);
1571 if ($path !== false) {
1572 $path_parts = explode($explode, $path);
1573 $path_value = &$post_terms;
1574 $found = true;
1575 foreach ($path_parts as $path_part) {
1576 if (isset($path_value[$path_part])) {
1577 $path_value = &$path_value[$path_part];
1578 } else {
1579 $found = false;
1580 break;
1581 }
1582 }
1583
1584 if ($found) {
1585 if ($attachment_url) {
1586 if (!is_array($path_value)) {
1587 $response = wp_get_attachment_url($path_value);
1588 }
1589 } elseif ($attachment_image_url) {
1590 if (!is_array($path_value)) {
1591 $image = wp_get_attachment_image_url($path_value, $size);
1592 if ($image) {
1593 $response = $image;
1594 }
1595 }
1596 } else {
1597 if (is_array($path_value)) {
1598 $response = serialize($path_value);
1599 } else {
1600 $response = $path_value;
1601 }
1602 }
1603 }
1604 } else {
1605 $response = serialize($post_terms);
1606 }
1607 }
1608 }
1609 } else {
1610 $post_meta = get_post_meta($id, $key, true);
1611 if ($post_meta) {
1612 if (is_array($post_meta)) {
1613 if ($path !== false) {
1614 $path_parts = explode($explode, $path);
1615 $path_value = &$post_meta;
1616 $found = true;
1617 foreach ($path_parts as $path_part) {
1618 if (isset($path_value[$path_part])) {
1619 $path_value = &$path_value[$path_part];
1620 } else {
1621 $found = false;
1622 break;
1623 }
1624 }
1625
1626 if ($found) {
1627 if ($attachment_url) {
1628 if (!is_array($path_value)) {
1629 $response = wp_get_attachment_url($path_value);
1630 }
1631 } elseif ($attachment_image_url) {
1632 if (!is_array($path_value)) {
1633 $image = wp_get_attachment_image_url($path_value, $size);
1634 if ($image) {
1635 $response = $image;
1636 }
1637 }
1638 } else {
1639 if (is_array($path_value)) {
1640 $response = serialize($path_value);
1641 } else {
1642 $response = $path_value;
1643 }
1644 }
1645 }
1646 } else {
1647 $response = serialize($post_meta);
1648 }
1649 } else {
1650 if ($attachment_url) {
1651 $response = wp_get_attachment_url($post_meta);
1652 } elseif ($attachment_image_url) {
1653 $image = wp_get_attachment_image_url($post_meta, $size);
1654 if ($image) {
1655 $response = $image;
1656 }
1657 } else {
1658 $response = $post_meta;
1659 }
1660 }
1661 }
1662 }
1663 }
1664 }
1665 return $response;
1666 }
1667
1668 /**
1669 * [e2pdf-filter] shortcode
1670 *
1671 * @param array $atts - Attributes
1672 * @param string $value - Content
1673 */
1674 function e2pdf_filter($atts = array(), $value = '') {
1675 if ($value) {
1676 $search = array(
1677 '[', ']', '\'', '"', '&#091;', '&#093;'
1678 );
1679
1680 $replace = array(
1681 '&#91;', '&#93;', '&#39;', '&#34;', '&#91;', '&#93;'
1682 );
1683 $value = str_replace($search, $replace, $value);
1684 }
1685 return $value;
1686 }
1687
1688 }
1689