| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImportWP\Common\Exporter\Mapper; |
| 4 |
|
| 5 |
use ImportWP\Common\Exporter\MapperInterface; |
| 6 |
|
| 7 |
class TaxMapper extends AbstractMapper implements MapperInterface |
| 8 |
{ |
| 9 |
|
| 10 |
/** |
| 11 |
* @var \WP_Term_Query |
| 12 |
*/ |
| 13 |
private $query; |
| 14 |
private $taxonomy; |
| 15 |
|
| 16 |
public function get_core_fields() |
| 17 |
{ |
| 18 |
return array( |
| 19 |
'term_id', |
| 20 |
'name', |
| 21 |
'slug', |
| 22 |
'description', |
| 23 |
); |
| 24 |
} |
| 25 |
|
| 26 |
public function __construct($taxonomy) |
| 27 |
{ |
| 28 |
$this->taxonomy = $taxonomy; |
| 29 |
} |
| 30 |
|
| 31 |
public function get_fields() |
| 32 |
{ |
| 33 |
/** |
| 34 |
* @var \WPDB |
| 35 |
*/ |
| 36 |
global $wpdb; |
| 37 |
|
| 38 |
$fields = [ |
| 39 |
'key' => 'main', |
| 40 |
'label' => $this->taxonomy, |
| 41 |
'loop' => true, |
| 42 |
'fields' => [], |
| 43 |
'children' => [ |
| 44 |
'parent' => [ |
| 45 |
'key' => 'parent', |
| 46 |
'label' => 'Parent', |
| 47 |
'loop' => false, |
| 48 |
'fields' => [], |
| 49 |
'children' => [] |
| 50 |
], |
| 51 |
'anscestors' => [ |
| 52 |
'key' => 'anscestors', |
| 53 |
'label' => 'Anscestors', |
| 54 |
'loop' => true, |
| 55 |
'fields' => [], |
| 56 |
'children' => [] |
| 57 |
], |
| 58 |
'custom_fields' => [ |
| 59 |
'key' => 'custom_fields', |
| 60 |
'label' => 'Custom Fields', |
| 61 |
'loop' => true, |
| 62 |
'loop_fields' => ['meta_key', 'meta_value'], |
| 63 |
'fields' => [], |
| 64 |
'children' => [] |
| 65 |
] |
| 66 |
] |
| 67 |
|
| 68 |
]; |
| 69 |
|
| 70 |
$fields['fields'] = $this->get_core_fields(); |
| 71 |
|
| 72 |
// get taxonomy custom fields |
| 73 |
global $wpdb; |
| 74 |
$meta_fields = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT meta_key FROM " . $wpdb->termmeta . " as tm INNER JOIN " . $wpdb->term_taxonomy . " as tt ON tm.term_id = tt.term_id WHERE tt.taxonomy = %s", [$this->taxonomy])); |
| 75 |
$custom_file_fields = apply_filters('iwp/exporter/taxonomy/custom_file_id_fields', []); |
| 76 |
foreach ($meta_fields as $field) { |
| 77 |
$fields['children']['custom_fields']['fields'][] = $field; |
| 78 |
|
| 79 |
if (in_array($field, $custom_file_fields)) { |
| 80 |
$fields['children']['custom_fields']['fields'][] = $field . '::id'; |
| 81 |
$fields['children']['custom_fields']['fields'][] = $field . '::url'; |
| 82 |
$fields['children']['custom_fields']['fields'][] = $field . '::title'; |
| 83 |
$fields['children']['custom_fields']['fields'][] = $field . '::alt'; |
| 84 |
$fields['children']['custom_fields']['fields'][] = $field . '::caption'; |
| 85 |
$fields['children']['custom_fields']['fields'][] = $field . '::description'; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
$fields['children']['parent']['fields'] = [ |
| 90 |
'term_id', |
| 91 |
'slug', |
| 92 |
'name', |
| 93 |
'parent' |
| 94 |
]; |
| 95 |
|
| 96 |
$fields['children']['anscestors']['fields'] = [ |
| 97 |
'term_id', |
| 98 |
'slug', |
| 99 |
'name', |
| 100 |
'parent' |
| 101 |
]; |
| 102 |
|
| 103 |
$fields['children']['custom_fields']['fields'] = apply_filters('iwp/exporter/taxonomy/custom_field_list', $fields['children']['custom_fields']['fields'], $this->taxonomy); |
| 104 |
$fields = apply_filters('iwp/exporter/taxonomy/fields', $fields, $this->taxonomy); |
| 105 |
|
| 106 |
return $fields; |
| 107 |
} |
| 108 |
|
| 109 |
public function have_records($exporter_id) |
| 110 |
{ |
| 111 |
$query_args = []; |
| 112 |
$query_args = apply_filters('iwp/exporter/tax_query', $query_args); |
| 113 |
$query_args = apply_filters(sprintf('iwp/exporter/%d/tax_query', $exporter_id), $query_args); |
| 114 |
$query_args = apply_filters(sprintf('iwp/exporter/tax_query/%s', $this->taxonomy), $query_args); |
| 115 |
$query_args = apply_filters(sprintf('iwp/exporter/%d/tax_query/%s', $exporter_id, $this->taxonomy), $query_args); |
| 116 |
|
| 117 |
$query_args = wp_parse_args($query_args, [ |
| 118 |
'taxonomy' => $this->taxonomy, |
| 119 |
'hide_empty' => false, |
| 120 |
'fields' => 'ids' |
| 121 |
]); |
| 122 |
|
| 123 |
$this->query = new \WP_Term_Query($query_args); |
| 124 |
$this->items = (array)$this->query->terms; |
| 125 |
|
| 126 |
return $this->found_records() > 0; |
| 127 |
} |
| 128 |
|
| 129 |
public function found_records() |
| 130 |
{ |
| 131 |
return count($this->items); |
| 132 |
} |
| 133 |
|
| 134 |
public function get_records() |
| 135 |
{ |
| 136 |
return $this->items; |
| 137 |
} |
| 138 |
|
| 139 |
public function setup($i) |
| 140 |
{ |
| 141 |
$this->record = get_term($this->items[$i], '', ARRAY_A); |
| 142 |
$this->record['custom_fields'] = get_term_meta($this->record['term_id']); |
| 143 |
$this->record = $this->modify_custom_field_data($this->record, 'taxonomy'); |
| 144 |
|
| 145 |
$parent = get_term($this->record['parent'], $this->taxonomy); |
| 146 |
if (!is_wp_error($parent)) { |
| 147 |
$this->record['parent'] = [ |
| 148 |
'term_id' => $parent->term_id, |
| 149 |
'slug' => $parent->slug, |
| 150 |
'name' => $parent->name, |
| 151 |
'parent' => $parent->parent |
| 152 |
]; |
| 153 |
} else { |
| 154 |
$this->record['parent'] = [ |
| 155 |
'term_id' => '', |
| 156 |
'slug' => '', |
| 157 |
'name' => '', |
| 158 |
'parent' => '' |
| 159 |
]; |
| 160 |
} |
| 161 |
|
| 162 |
$ancestor_ids = get_ancestors($this->record['term_id'], $this->taxonomy, 'taxonomy'); |
| 163 |
$ancestor_ids = array_reverse($ancestor_ids); |
| 164 |
|
| 165 |
$this->record['anscestors'] = []; |
| 166 |
|
| 167 |
if ($ancestor_ids) { |
| 168 |
foreach ($ancestor_ids as $ancestor_id) { |
| 169 |
|
| 170 |
$ancestor = get_term($ancestor_id, $this->taxonomy); |
| 171 |
if (!is_wp_error($ancestor)) { |
| 172 |
$this->record['anscestors'][] = [ |
| 173 |
'term_id' => $ancestor->term_id, |
| 174 |
'slug' => $ancestor->slug, |
| 175 |
'name' => $ancestor->name, |
| 176 |
'parent' => $ancestor->parent |
| 177 |
]; |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
$this->record = apply_filters('iwp/exporter/taxonomy/setup_data', $this->record, $this->taxonomy); |
| 183 |
return true; |
| 184 |
} |
| 185 |
|
| 186 |
public function get_field($column, $record, $meta) |
| 187 |
{ |
| 188 |
// Core fields |
| 189 |
$core = $this->get_core_fields(); |
| 190 |
|
| 191 |
$output = ''; |
| 192 |
|
| 193 |
if (preg_match('/^ewp_cf_(.*?)$/', $column, $matches) == 1) { |
| 194 |
|
| 195 |
$meta_key = $matches[1]; |
| 196 |
if (isset($meta[$meta_key])) { |
| 197 |
$output = $meta[$meta_key]; |
| 198 |
} |
| 199 |
} else { |
| 200 |
|
| 201 |
if (in_array($column, $core, true)) { |
| 202 |
$output = $record->{$column}; |
| 203 |
} else { |
| 204 |
switch ($column) { |
| 205 |
case 'parent_id': |
| 206 |
/** |
| 207 |
* @var \WP_Term $record |
| 208 |
*/ |
| 209 |
$parent = get_term($record->parent, $this->taxonomy); |
| 210 |
if (!is_wp_error($parent)) { |
| 211 |
$output = $parent->term_id; |
| 212 |
} |
| 213 |
break; |
| 214 |
case 'parent_slug': |
| 215 |
$parent = get_term($record->parent, $this->taxonomy); |
| 216 |
if (!is_wp_error($parent)) { |
| 217 |
$output = $parent->slug; |
| 218 |
} |
| 219 |
break; |
| 220 |
case 'parent_name': |
| 221 |
$parent = get_term($record->parent, $this->taxonomy); |
| 222 |
if (!is_wp_error($parent)) { |
| 223 |
$output = $parent->name; |
| 224 |
} |
| 225 |
break; |
| 226 |
case 'parent_anscestors_id': |
| 227 |
$parents = get_ancestors($record->term_id, $this->taxonomy, 'taxonomy'); |
| 228 |
$output = implode(' > ', $parents); |
| 229 |
break; |
| 230 |
case 'parent_anscestors_slug': |
| 231 |
$parents = get_ancestors($record->term_id, $this->taxonomy, 'taxonomy'); |
| 232 |
$parents = array_filter(array_map(function ($item) { |
| 233 |
$term = get_term($item, $this->taxonomy); |
| 234 |
return !is_wp_error($term) ? $term->slug : ''; |
| 235 |
}, $parents)); |
| 236 |
$output = implode(' > ', $parents); |
| 237 |
break; |
| 238 |
case 'parent_anscestors_name': |
| 239 |
$parents = get_ancestors($record->term_id, $this->taxonomy, 'taxonomy'); |
| 240 |
$parents = array_filter(array_map(function ($item) { |
| 241 |
$term = get_term($item, $this->taxonomy); |
| 242 |
return !is_wp_error($term) ? $term->name : ''; |
| 243 |
}, $parents)); |
| 244 |
|
| 245 |
$output = implode(' > ', $parents); |
| 246 |
break; |
| 247 |
} |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
$output = apply_filters('iwp/exporter/taxonomy/value', $output, $column, $record, $meta); |
| 252 |
return $output; |
| 253 |
} |
| 254 |
} |
| 255 |
|