PluginProbe
Import WP – CSV & XML Import Export for WordPress / 2.7.3
Import WP – CSV & XML Import Export for WordPress v2.7.3
2.15.1 2.15.0 2.14.24 2.14.23 2.7.0 2.7.1 2.7.10 2.7.11 2.7.12 2.7.13 2.7.14 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 2.8.0 2.8.1 2.8.2 2.8.3 2.9.0 2.9.1 All 144 releases
jc-importer / class / Common / Exporter / Mapper / MapperData.php

MapperData.php in Import WP – CSV & XML Import Export for WordPress 2.7.3, at class/Common/Exporter/Mapper/MapperData.php

121 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace ImportWP\Common\Exporter\Mapper;
4
5 class MapperData
6 {
7 /**
8 * @var PostMapper
9 */
10 private $_mapper;
11
12 private $template_data;
13
14 /**
15 * @param PostMapper $mapper
16 * @param int $i
17 */
18 public function __construct($mapper, $i)
19 {
20 $this->_mapper = $mapper;
21 $this->_mapper->setup($i);
22 }
23
24 public function skip()
25 {
26 return $this->_mapper->filter();
27 }
28
29 public function data($args)
30 {
31 $record = $this->_mapper->record();
32
33 if (empty($args)) {
34 return [$record];
35 }
36
37 if (strpos($args['loop'], 'tax_') === 0) {
38 $taxonomy = substr($args['loop'], strlen('tax_'));
39 $terms = wp_get_object_terms($record['ID'], $taxonomy);
40 $tmp = [];
41 if (!empty($terms)) {
42 foreach ($terms as $term) {
43
44 /**
45 * @var \WP_Term $term
46 */
47 $tmp[] = [
48 'id' => $term->term_id,
49 'slug' => $term->slug,
50 'name' => $term->name,
51 ];
52 }
53 }
54
55 return $tmp;
56 }
57
58 switch ($args['loop']) {
59 case 'custom_fields':
60 $tmp = [];
61 foreach ($record['custom_fields'] as $key => $value) {
62 $tmp[] = ['meta_key' => $key, 'meta_value' => implode("|", $value)];
63 }
64
65 return $tmp;
66 default:
67 if (isset($record[$args['loop']])) {
68 return $record[$args['loop']];
69 }
70 break;
71 }
72
73
74 return [];
75 }
76
77
78
79 public function template($template, $data)
80 {
81 $this->template_data = $data;
82 // Allow for settings to be present in the template variable e.g. {custom_fields.price | {"seperator": ","}}
83 $template = preg_replace_callback('/{(.*?)(?:\s+\|\s+)?({.*?})?}/', array($this, 'query_matches'), $template);
84 return $template;
85 }
86
87 public function query_matches($matches)
88 {
89 if (isset($matches[0]) && isset($matches[1])) {
90
91 $args = [];
92 if (!empty($matches[2])) {
93 $args = json_decode($matches[2], true);
94 }
95
96 $defaults = [
97 'escape' => false,
98 'seperator' => '|'
99 ];
100
101 $args = wp_parse_args($args, $defaults);
102
103 $value = $this->get_value($matches[1]);
104 $value = is_array($value) ? implode($args['seperator'], $value) : $value;
105
106 if ($args['escape'] == true) {
107 $value = esc_attr($value);
108 }
109
110 return $value;
111 }
112
113 return '';
114 }
115
116 public function get_value($field, $data = null)
117 {
118 return $this->_mapper->get_value($field, is_null($data) ? $this->template_data : $data);
119 }
120 }
121