| 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 |
|