PluginProbe
Import WP – CSV & XML Import Export for WordPress / 2.15.0
Import WP – CSV & XML Import Export for WordPress v2.15.0
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.15.0, at class/Common/Exporter/Mapper/MapperData.php

126 lines 3.0 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 MapperInterface
9 */
10 private $_mapper;
11
12 private $template_data;
13
14 /**
15 * @param MapperInterface $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 get_total_records()
30 {
31 return count($this->_mapper->records);
32 }
33
34 public function data($args, $index = 0)
35 {
36 $record = $this->_mapper->record($index);
37
38 if (empty($args)) {
39 return [$record];
40 }
41
42 if (strpos($args['loop'], 'tax_') === 0) {
43 $taxonomy = substr($args['loop'], strlen('tax_'));
44 $terms = wp_get_object_terms($record['ID'], $taxonomy);
45 $tmp = [];
46 if (!empty($terms)) {
47 foreach ($terms as $term) {
48
49 /**
50 * @var \WP_Term $term
51 */
52 $tmp[] = [
53 'id' => $term->term_id,
54 'slug' => $term->slug,
55 'name' => $term->name,
56 ];
57 }
58 }
59
60 return $tmp;
61 }
62
63 switch ($args['loop']) {
64 case 'custom_fields':
65 $tmp = [];
66 foreach ($record['custom_fields'] as $key => $value) {
67 $tmp[] = ['meta_key' => $key, 'meta_value' => implode("|", $value)];
68 }
69
70 return $tmp;
71 default:
72 if (isset($record[$args['loop']])) {
73 return $record[$args['loop']];
74 }
75 break;
76 }
77
78
79 return [];
80 }
81
82
83
84 public function template($template, $data)
85 {
86 $this->template_data = $data;
87 // Allow for settings to be present in the template variable e.g. {custom_fields.price | {"seperator": ","}}
88 $template = preg_replace_callback('/{(.*?)(?:\s+\|\s+)?({.*?})?}/', array($this, 'query_matches'), $template);
89 return $template;
90 }
91
92 public function query_matches($matches)
93 {
94 if (isset($matches[0]) && isset($matches[1])) {
95
96 $args = [];
97 if (!empty($matches[2])) {
98 $args = json_decode($matches[2], true);
99 }
100
101 $defaults = [
102 'escape' => false,
103 'seperator' => '|'
104 ];
105
106 $args = wp_parse_args($args, $defaults);
107
108 $value = $this->get_value($matches[1]);
109 $value = is_array($value) ? implode($args['seperator'], $value) : $value;
110
111 if ($args['escape'] == true) {
112 $value = esc_attr($value);
113 }
114
115 return $value;
116 }
117
118 return '';
119 }
120
121 public function get_value($field, $data = null)
122 {
123 return $this->_mapper->get_value($field, is_null($data) ? $this->template_data : $data);
124 }
125 }
126