PluginProbe
E2Pdf – Export Pdf Tool for WordPress / 1.00.00
E2Pdf – Export Pdf Tool for WordPress v1.00.00
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 / extension / e2pdf-wordpress.php

e2pdf-wordpress.php in E2Pdf – Export Pdf Tool for WordPress 1.00.00, at classes/extension/e2pdf-wordpress.php

656 lines 21.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * E2pdf Wordpress Extension
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 Extension_E2pdf_Wordpress extends Model_E2pdf_Model {
17
18 private $options;
19
20 function __construct() {
21 parent::__construct();
22 }
23
24 /**
25 * Get info about extension
26 *
27 * @return array() - Extension key and name
28 */
29 public function info() {
30 return array(
31 'wordpress' => __('Wordpress', 'e2pdf')
32 );
33 }
34
35 /**
36 * Check if needed plugin active
37 *
38 * @return bool - Activated/Not Activated plugin
39 */
40 public function active() {
41 return true;
42 }
43
44 /**
45 * Check if export item function available
46 *
47 * @return bool - Item export available/not available
48 */
49 public function export() {
50 return false;
51 }
52
53 /**
54 * Set option
55 *
56 * @param string $attr - Key of option
57 * @param string $value - Value of option
58 *
59 * @return bool - Status of setting option
60 */
61 public function set($key, $value) {
62 if (!isset($this->options)) {
63 $this->options = new stdClass();
64 }
65
66 $this->options->$key = $value;
67 }
68
69 /**
70 * Get option by key
71 *
72 * @param string $key - Key to get assigned option value
73 *
74 * @return mixed
75 */
76 public function get($key) {
77 if (isset($this->options->$key)) {
78 $value = $this->options->$key;
79 return $value;
80 } else {
81 return false;
82 }
83 }
84
85 /**
86 * Get items to work with
87 *
88 * @return array() - List of available items
89 */
90 public function get_items() {
91 $content = array();
92
93 $content[] = array(
94 'key' => 'post',
95 'value' => __('Wordpress Posts', 'e2pdf'),
96 );
97 $content[] = array(
98 'key' => 'page',
99 'value' => __('Wordpress Pages', 'e2pdf'),
100 );
101
102 return $content;
103 }
104
105 /**
106 * Get entries for export
107 *
108 * @param string $item - Item
109 * @param string $name - Entries names
110 *
111 * @return array() - Entries list
112 */
113 public function get_datasets($item = false, $name = false) {
114
115 $datasets = array();
116 if ($item == 'page') {
117 $datasets_tmp = get_pages();
118 } elseif ($item == 'post') {
119 $datasets_tmp = get_posts();
120 }
121
122 if ($datasets_tmp) {
123 foreach ($datasets_tmp as $key => $dataset) {
124 $this->set('item', $item);
125 $this->set('dataset', $dataset->ID);
126 $dataset_title = $this->render($name);
127 if (!$dataset_title) {
128 $dataset_title = $dataset->post_title;
129 }
130 $datasets[] = array(
131 'key' => $dataset->ID,
132 'value' => $dataset_title
133 );
134 }
135 }
136
137 return $datasets;
138 }
139
140 /**
141 * Get dataset
142 *
143 * @param int $dataset - Dataset ID
144 *
145 * @return object - Dataset
146 */
147 public function get_dataset($dataset = false) {
148
149 $dataset = (int) $dataset;
150
151 if (!$dataset) {
152 return;
153 }
154
155 $data = new stdClass();
156 $data->url = $this->helper->get_url(array('post' => $dataset, 'action' => 'edit'), 'post.php?');
157
158 return $data;
159 }
160
161 /**
162 * Get item
163 *
164 * @param string $item - Item
165 *
166 * @return object - Item
167 */
168 public function get_item($item = false) {
169
170 $form = new stdClass();
171 if ($item == 'post') {
172 $form->name = __('Wordpress Posts', 'e2pdf');
173 $form->url = $this->helper->get_url(array('post_type' => $item), 'edit.php?');
174 } elseif ($item == 'page') {
175 $form->name = __('Wordpress Pages', 'e2pdf');
176 $form->url = $this->helper->get_url(array('post_type' => $item), 'edit.php?');
177 } else {
178 $form->name = '';
179 $form->url = 'javascript:void(0);';
180 }
181
182 return $form;
183 }
184
185 public function load_filters() {
186 add_filter('the_content', array($this, 'filter_content'), 10, 2);
187 }
188
189 /**
190 * Render value according to content
191 *
192 * @param string $value - Content
193 * @param string $type - Type of rendering value
194 * @param string $field - Field type for rendering
195 * @param string $info - Additional value to render
196 *
197 * @return string - Fully rendered value
198 */
199 public function render($value, $type = false, $field = false, $info = false) {
200
201 $value = $this->render_shortcodes($value, $type, $field);
202 $value = $this->strip_shortcodes($value);
203
204 if ($type === 'value' && $field === 'e2pdf-checkbox') {
205 $option = $this->render($info);
206 $options = explode(', ', $value);
207 if (is_array($options) && in_array($option, $options)) {
208 return $option;
209 } else {
210 return "";
211 }
212 }
213 return $value;
214 }
215
216 /**
217 * Render shortcodes which available in this extension
218 *
219 * @param string $value - Content
220 *
221 * @return string - Value with rendered shortcodes
222 */
223 public function render_shortcodes($value, $type = false, $field = false) {
224
225 $dataset_id = $this->get('dataset');
226 $item_id = $this->get('item');
227 $post = get_post($dataset_id);
228
229 $ext_shortcodes = array(
230 "[id]" => $post->ID,
231 "[post_author]" => get_userdata($post->post_author)->user_nicename,
232 "[post_date]" => $post->post_date,
233 "[post_date_gmt]" => $post->post_date_gmt,
234 "[post_content]" => $post->post_content,
235 "[post_title]" => $post->post_title,
236 "[post_excerpt]" => $post->post_excerpt,
237 "[post_status]" => $post->post_status,
238 "[comment_status]" => $post->comment_status,
239 "[ping_status]" => $post->ping_status,
240 "[post_password]" => $post->post_password,
241 "[post_name]" => $post->post_name,
242 "[to_ping]" => $post->to_ping,
243 "[pinged]" => $post->pinged,
244 "[post_modified]" => $post->post_modified,
245 "[post_modified_gmt]" => $post->post_modified_gmt,
246 "[post_content_filtered]" => $post->post_content_filtered,
247 "[post_parent]" => $post->post_parent,
248 "[guid]" => $post->guid,
249 "[menu_order]" => $post->menu_order,
250 "[post_type]" => $post->post_type,
251 "[post_mime_type]" => $post->post_mime_type,
252 "[comment_count]" => $post->comment_count,
253 "[filter]" => $post->filter
254 );
255
256
257 $post_content = false;
258
259 if (false !== strpos($value, '[')) {
260
261 $shortcode_tags = array(
262 'meta',
263 );
264 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $value, $matches);
265 $tagnames = array_intersect($shortcode_tags, $matches[1]);
266
267 if (!empty($tagnames)) {
268 $pattern = get_shortcode_regex($tagnames);
269
270 preg_match_all("/$pattern/", $value, $shortcodes);
271
272 foreach ($shortcodes[0] as $key => $shortcode_value) {
273 $shortcode = array();
274 $shortcode[1] = $shortcodes[1][$key];
275 $shortcode[2] = $shortcodes[2][$key];
276 $shortcode[3] = $shortcodes[3][$key];
277 $shortcode[4] = $shortcodes[4][$key];
278 $shortcode[5] = $shortcodes[5][$key];
279 $shortcode[6] = $shortcodes[6][$key];
280
281 $atts = shortcode_parse_atts($shortcode[3]);
282
283 if (array_key_exists('key', $atts)) {
284 $value = str_replace($shortcode_value, get_post_meta($post->ID, $atts['key'], true), $value);
285 }
286 }
287 }
288
289 $shortcode_tags = array(
290 'e2pdf-format-number',
291 'e2pdf-format-date',
292 'e2pdf-format-output',
293 );
294 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $value, $matches);
295 $tagnames = array_intersect($shortcode_tags, $matches[1]);
296
297 if (!empty($tagnames)) {
298
299 $pattern = get_shortcode_regex($tagnames);
300 preg_match_all("/$pattern/", $value, $shortcodes);
301 foreach ($shortcodes[0] as $key => $shortcode_value) {
302 $shortcode = array();
303 $shortcode[1] = $shortcodes[1][$key];
304 $shortcode[2] = $shortcodes[2][$key];
305 $shortcode[3] = $shortcodes[3][$key];
306 $shortcode[4] = $shortcodes[4][$key];
307 $shortcode[5] = $shortcodes[5][$key];
308 $shortcode[6] = $shortcodes[6][$key];
309
310 if (isset($shortcode['5'])) {
311
312 if (strpos($shortcode['5'], "[post_content]") !== FALSE && !$this->get('no_filter') && !$post_content) {
313 $ext_shortcodes['[post_content]'] = $this->before_render_post_content($ext_shortcodes['[post_content]']);
314 $ext_shortcodes['[post_content]'] = apply_filters('the_content', $ext_shortcodes['[post_content]']);
315 $ext_shortcodes['[post_content]'] = str_replace("</p>", "</p>&nbsp;\r\n", $ext_shortcodes['[post_content]']);
316 $post_content = true;
317 }
318
319 $sub_value = str_replace(array_keys($ext_shortcodes), $ext_shortcodes, $shortcode['5']);
320 $value = str_replace($shortcode_value, "[" . $shortcode['2'] . $shortcode['3'] . "]" . $sub_value . "[/" . $shortcode['2'] . "]", $value);
321 }
322 }
323 }
324 }
325
326 $value = do_shortcode($value);
327
328 if (strpos($value, "[post_content]") !== FALSE && !$this->get('no_filter') && !$post_content) {
329 $ext_shortcodes['[post_content]'] = $this->before_render_post_content($ext_shortcodes['[post_content]']);
330 $ext_shortcodes['[post_content]'] = apply_filters('the_content', $ext_shortcodes['[post_content]']);
331 $ext_shortcodes['[post_content]'] = str_replace("</p>", "</p>&nbsp;\r\n", $ext_shortcodes['[post_content]']);
332 $post_content = true;
333 }
334
335 $value = str_replace(array_keys($ext_shortcodes), $ext_shortcodes, $value);
336
337 return $value;
338 }
339
340 public function filter_custom_shortcodes() {
341
342 }
343
344 /**
345 * Strip unused shortcodes
346 *
347 * @param string $value - Content
348 *
349 * @return string - Value with removed unused shortcodes
350 */
351 public function strip_shortcodes($value) {
352 $value = preg_replace('~(?:\[/?)[^/\]]+/?\]~s', "", $value);
353 return $value;
354 }
355
356 public function auto() {
357 $response = array();
358 $elements = array();
359
360
361 $post = $this->get('item');
362
363 $elements[] = array(
364 'type' => 'e2pdf-html',
365 'top' => '20',
366 'left' => '20',
367 'right' => '20',
368 'block' => true,
369 'properties' => array(
370 'value' => "<h1>[post_title]</h1>",
371 )
372 );
373
374
375 $elements[] = array(
376 'type' => 'e2pdf-html',
377 'top' => '20',
378 'left' => '20',
379 'right' => '20',
380 'block' => true,
381 'properties' => array(
382 'value' => __("Post name", 'e2pdf') . ": [post_name]",
383 )
384 );
385
386 $elements[] = array(
387 'type' => 'e2pdf-html',
388 'top' => '20',
389 'left' => '20',
390 'right' => '20',
391 'block' => true,
392 'properties' => array(
393 'value' => __("Post type", 'e2pdf') . ": [post_type]",
394 )
395 );
396
397
398 $elements[] = array(
399 'type' => 'e2pdf-html',
400 'top' => '20',
401 'left' => '20',
402 'right' => '20',
403 'block' => true,
404 'properties' => array(
405 'value' => __("ID", 'e2pdf') . ": [id]",
406 )
407 );
408
409 $elements[] = array(
410 'type' => 'e2pdf-html',
411 'top' => '20',
412 'left' => '20',
413 'right' => '20',
414 'block' => true,
415 'properties' => array(
416 'value' => __("Author", 'e2pdf') . ": [post_author]",
417 )
418 );
419
420
421
422 $elements[] = array(
423 'type' => 'e2pdf-html',
424 'top' => '20',
425 'left' => '20',
426 'right' => '20',
427 'block' => true,
428 'properties' => array(
429 'value' => "[post_content]",
430 'dynamic_height' => '1',
431 'height' => '300',
432 )
433 );
434
435
436 $elements[] = array(
437 'type' => 'e2pdf-html',
438 'top' => '20',
439 'left' => '20',
440 'right' => '20',
441 'block' => true,
442 'properties' => array(
443 'value' => __("Created", 'e2pdf') . ": [post_date]",
444 )
445 );
446
447 $elements[] = array(
448 'type' => 'e2pdf-html',
449 'top' => '20',
450 'left' => '20',
451 'right' => '20',
452 'block' => true,
453 'properties' => array(
454 'value' => __("Modified", 'e2pdf') . ": [post_modified]",
455 )
456 );
457
458
459 $response['page'] = array(
460 'bottom' => '20',
461 'top' => '20',
462 );
463
464
465
466 $response['elements'] = $elements;
467 return $response;
468 }
469
470 /**
471 * Action before wordpress post/page render
472 * Force to apply e2pdf shortcodes
473 *
474 * @param string $content - Content
475 *
476 * @return string - Content with update shortcodes
477 */
478 public function before_render_post_content($content) {
479
480 if (false === strpos($content, '[')) {
481 return $content;
482 }
483
484
485 $shortcode_tags = array(
486 'e2pdf-exclude',
487 );
488
489
490 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $content, $matches);
491 $tagnames = array_intersect($shortcode_tags, $matches[1]);
492
493
494 if (!empty($tagnames)) {
495 $pattern = get_shortcode_regex($tagnames);
496
497 preg_match_all("/$pattern/", $content, $shortcodes);
498
499
500 foreach ($shortcodes[0] as $key => $shortcode_value) {
501
502 $shortcode = array();
503 $shortcode[1] = $shortcodes[1][$key];
504 $shortcode[2] = $shortcodes[2][$key];
505 $shortcode[3] = $shortcodes[3][$key];
506 $shortcode[4] = $shortcodes[4][$key];
507 $shortcode[5] = $shortcodes[5][$key];
508 $shortcode[6] = $shortcodes[6][$key];
509
510 $shortcode[3] .= " apply='true'";
511 $content = str_replace($shortcode_value, do_shortcode_tag($shortcode), $content);
512 }
513 }
514
515
516 return $content;
517 }
518
519 /**
520 * Search and update shortcodes for this extension inside content
521 * Auto set of dataset id
522 *
523 * @param string $content - Content
524 *
525 * @return string - Content with updated shortcodes
526 */
527 public function filter_content($content) {
528 global $post;
529
530 if (false === strpos($content, '[')) {
531 return $content;
532 }
533
534 $shortcode_tags = array(
535 'e2pdf-download',
536 'e2pdf-view'
537 );
538
539 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $content, $matches);
540 $tagnames = array_intersect($shortcode_tags, $matches[1]);
541
542 if (!empty($tagnames)) {
543 $pattern = get_shortcode_regex($tagnames);
544
545 preg_match_all("/$pattern/", $content, $shortcodes);
546
547 foreach ($shortcodes[0] as $key => $shortcode_value) {
548
549 wp_reset_postdata();
550 $extension = false;
551 $item = false;
552
553 $shortcode = array();
554 $shortcode[1] = $shortcodes[1][$key];
555 $shortcode[2] = $shortcodes[2][$key];
556 $shortcode[3] = $shortcodes[3][$key];
557 $shortcode[4] = $shortcodes[4][$key];
558 $shortcode[5] = $shortcodes[5][$key];
559 $shortcode[6] = $shortcodes[6][$key];
560
561 $atts = shortcode_parse_atts($shortcode[3]);
562
563 if (array_key_exists('id', $atts)) {
564 $template = new Model_E2pdf_Template();
565 $template->load($atts['id']);
566 if ($template->get('extension') === 'wordpress') {
567 $extension = $template->get('extension');
568 $item = $template->get('item');
569 }
570 }
571
572 if ($extension) {
573 if (!array_key_exists('dataset', $atts)) {
574 if (isset($post->ID)) {
575 $dataset_id = $post->ID;
576 $atts['dataset'] = $dataset_id;
577 $shortcode[3] .= " dataset='{$dataset_id}'";
578 $content = str_replace($shortcode_value, do_shortcode_tag($shortcode), $content);
579 }
580 } else {
581 $content = str_replace($shortcode_value, do_shortcode_tag($shortcode), $content);
582 }
583 } elseif ($shortcode[2] === 'e2pdf-view') {
584 $content = str_replace($shortcode_value, do_shortcode_tag($shortcode), $content);
585 }
586 }
587 }
588
589 return $content;
590 }
591
592 public function verify() {
593 $item_id = $this->get('item');
594 $dataset_id = $this->get('dataset');
595
596 if ($item_id && $dataset_id && $item_id === get_post_type($dataset_id)) {
597 return true;
598 }
599
600 return false;
601 }
602
603 public function visual_mapper() {
604
605 $vc = "<h3>Wordpress</h3>";
606 $vc .= "<div class='e2pdf-grid'>";
607 $vc .= "<div class='e2pdf-ib e2pdf-w50 e2pdf-pr10'>{$this->get_vm_element(__("ID", "e2pdf"), 'id')}</div>";
608 $vc .= "<div class='e2pdf-ib e2pdf-w50 e2pdf-pl10'>{$this->get_vm_element(__("Author", "e2pdf"), 'post_author')}</div>";
609 $vc .= "<div class='e2pdf-ib e2pdf-w50 e2pdf-pr10'>{$this->get_vm_element(__("Date", "e2pdf"), 'post_date')}</div>";
610 $vc .= "<div class='e2pdf-ib e2pdf-w50 e2pdf-pl10'>{$this->get_vm_element(__("Date (GMT)", "e2pdf"), 'post_date_gmt')}</div>";
611 $vc .= "<div class='e2pdf-ib e2pdf-w50 e2pdf-pr10'>{$this->get_vm_element(__("Content", "e2pdf"), 'post_content')}</div>";
612 $vc .= "<div class='e2pdf-ib e2pdf-w50 e2pdf-pl10'>{$this->get_vm_element(__("Title", "e2pdf"), 'post_title')}</div>";
613 $vc .= "<div class='e2pdf-ib e2pdf-w50 e2pdf-pr10'>{$this->get_vm_element(__("Excerpt", "e2pdf"), 'post_excerpt')}</div>";
614 $vc .= "<div class='e2pdf-ib e2pdf-w50 e2pdf-pl10'>{$this->get_vm_element(__("Status", "e2pdf"), 'post_status')}</div>";
615 $vc .= "<div class='e2pdf-ib e2pdf-w50 e2pdf-pr10'>{$this->get_vm_element(__("Comment Status", "e2pdf"), 'comment_status')}</div>";
616 $vc .= "<div class='e2pdf-ib e2pdf-w50 e2pdf-pl10'>{$this->get_vm_element(__("Ping Status", "e2pdf"), 'ping_status')}</div>";
617 $vc .= "<div class='e2pdf-ib e2pdf-w50 e2pdf-pr10'>{$this->get_vm_element(__("Post Password", "e2pdf"), 'post_password')}</div>";
618 $vc .= "<div class='e2pdf-ib e2pdf-w50 e2pdf-pl10'>{$this->get_vm_element(__("Post Name", "e2pdf"), 'post_name')}</div>";
619 $vc .= "<div class='e2pdf-ib e2pdf-w50 e2pdf-pr10'>{$this->get_vm_element(__("To Ping", "e2pdf"), 'to_ping')}</div>";
620 $vc .= "<div class='e2pdf-ib e2pdf-w50 e2pdf-pl10'>{$this->get_vm_element(__("Ping", "e2pdf"), 'pinged')}</div>";
621 $vc .= "<div class='e2pdf-ib e2pdf-w50 e2pdf-pr10'>{$this->get_vm_element(__("Modified Date", "e2pdf"), 'post_modified')}</div>";
622 $vc .= "<div class='e2pdf-ib e2pdf-w50 e2pdf-pl10'>{$this->get_vm_element(__("Modified Date (GMT)", "e2pdf"), 'post_modified_gmt')}</div>";
623 $vc .= "<div class='e2pdf-ib e2pdf-w50 e2pdf-pr10'>{$this->get_vm_element(__("Filtered Content", "e2pdf"), 'post_content_filtered')}</div>";
624 $vc .= "<div class='e2pdf-ib e2pdf-w50 e2pdf-pl10'>{$this->get_vm_element(__("Parent ID", "e2pdf"), 'post_parent')}</div>";
625 $vc .= "<div class='e2pdf-ib e2pdf-w50 e2pdf-pr10'>{$this->get_vm_element(__("GUID", "e2pdf"), 'guid')}</div>";
626 $vc .= "<div class='e2pdf-ib e2pdf-w50 e2pdf-pl10'>{$this->get_vm_element(__("Menu Order", "e2pdf"), 'menu_order')}</div>";
627 $vc .= "<div class='e2pdf-ib e2pdf-w50 e2pdf-pr10'>{$this->get_vm_element(__("Type", "e2pdf"), 'post_type')}</div>";
628 $vc .= "<div class='e2pdf-ib e2pdf-w50 e2pdf-pl10'>{$this->get_vm_element(__("Mime Type", "e2pdf"), 'post_mime_type')}</div>";
629 $vc .= "<div class='e2pdf-ib e2pdf-w50 e2pdf-pr10'>{$this->get_vm_element(__("Comments Count", "e2pdf"), 'comment_count')}</div>";
630 $vc .= "<div class='e2pdf-ib e2pdf-w50 e2pdf-pl10'>{$this->get_vm_element(__("Filter", "e2pdf"), 'filter')}</div>";
631 $vc .= "</div>";
632 return $vc;
633 }
634
635 private function get_vm_element($name, $id) {
636 $element = "<div>";
637 $element .= "<label>{$name}:</label>";
638 $element .= "<input type='text' name='[{$id}]' value='[{$id}]'>";
639 $element .= "</div>";
640 return $element;
641 }
642
643 public function load_shortcodes() {
644 add_shortcode('e2pdf-exclude', array($this, 'shortcode_e2pdf_exclude'));
645 }
646
647 function shortcode_e2pdf_exclude($atts = array(), $content = null) {
648 if (is_array($atts) && array_key_exists('apply', $atts)) {
649 return '';
650 } else {
651 return $content;
652 }
653 }
654
655 }
656