| @@ -1,8 +1,9 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace ImportWP\Common\Exporter\Mapper; |
| 4 | 4 | |
| 5 | +use ImportWP\Common\Exporter\ExporterRecord; | |
| 5 | 6 | use ImportWP\Common\Exporter\MapperInterface; |
| 6 | 7 | |
| 7 | 8 | class TaxMapper extends AbstractMapper implements MapperInterface |
| 8 | 9 | { |
| @@ -12,9 +13,9 @@ | ||
| 12 | 13 | */ |
| 13 | 14 | private $query; |
| 14 | 15 | private $taxonomy; |
| 15 | 16 | |
| 16 | - private function get_core_fields() | |
| 17 | + public function get_core_fields() | |
| 17 | 18 | { |
| 18 | 19 | return array( |
| 19 | 20 | 'term_id', |
| 20 | 21 | 'name', |
| @@ -25,8 +26,10 @@ | ||
| 25 | 26 | |
| 26 | 27 | public function __construct($taxonomy) |
| 27 | 28 | { |
| 28 | 29 | $this->taxonomy = $taxonomy; |
| 30 | + | |
| 31 | + add_filter('iwp/exporter_record/tax', [$this, 'get_record_data'], 10, 3); | |
| 29 | 32 | } |
| 30 | 33 | |
| 31 | 34 | public function get_fields() |
| 32 | 35 | { |
| @@ -42,9 +45,9 @@ | ||
| 42 | 45 | 'fields' => [], |
| 43 | 46 | 'children' => [ |
| 44 | 47 | 'parent' => [ |
| 45 | 48 | 'key' => 'parent', |
| 46 | - 'label' => 'Parent', | |
| 49 | + 'label' => __('Parent', 'jc-importer'), | |
| 47 | 50 | 'loop' => false, |
| 48 | 51 | 'fields' => [], |
| 49 | 52 | 'children' => [] |
| 50 | 53 | ], |
| @@ -49,9 +52,9 @@ | ||
| 49 | 52 | 'children' => [] |
| 50 | 53 | ], |
| 51 | 54 | 'anscestors' => [ |
| 52 | 55 | 'key' => 'anscestors', |
| 53 | - 'label' => 'Anscestors', | |
| 56 | + 'label' => __('Anscestors', 'jc-importer'), | |
| 54 | 57 | 'loop' => true, |
| 55 | 58 | 'fields' => [], |
| 56 | 59 | 'children' => [] |
| 57 | 60 | ], |
| @@ -56,9 +59,9 @@ | ||
| 56 | 59 | 'children' => [] |
| 57 | 60 | ], |
| 58 | 61 | 'custom_fields' => [ |
| 59 | 62 | 'key' => 'custom_fields', |
| 60 | - 'label' => 'Custom Fields', | |
| 63 | + 'label' => __('Custom Fields', 'jc-importer'), | |
| 61 | 64 | 'loop' => true, |
| 62 | 65 | 'loop_fields' => ['meta_key', 'meta_value'], |
| 63 | 66 | 'fields' => [], |
| 64 | 67 | 'children' => [] |
| @@ -71,10 +74,20 @@ | ||
| 71 | 74 | |
| 72 | 75 | // get taxonomy custom fields |
| 73 | 76 | global $wpdb; |
| 74 | 77 | $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])); |
| 78 | + $custom_file_fields = apply_filters('iwp/exporter/taxonomy/custom_file_id_fields', []); | |
| 75 | 79 | foreach ($meta_fields as $field) { |
| 76 | 80 | $fields['children']['custom_fields']['fields'][] = $field; |
| 81 | + | |
| 82 | + if (in_array($field, $custom_file_fields)) { | |
| 83 | + $fields['children']['custom_fields']['fields'][] = $field . '::id'; | |
| 84 | + $fields['children']['custom_fields']['fields'][] = $field . '::url'; | |
| 85 | + $fields['children']['custom_fields']['fields'][] = $field . '::title'; | |
| 86 | + $fields['children']['custom_fields']['fields'][] = $field . '::alt'; | |
| 87 | + $fields['children']['custom_fields']['fields'][] = $field . '::caption'; | |
| 88 | + $fields['children']['custom_fields']['fields'][] = $field . '::description'; | |
| 89 | + } | |
| 77 | 90 | } |
| 78 | 91 | |
| 79 | 92 | $fields['children']['parent']['fields'] = [ |
| 80 | 93 | 'term_id', |
| @@ -90,69 +103,53 @@ | ||
| 90 | 103 | 'parent' |
| 91 | 104 | ]; |
| 92 | 105 | |
| 93 | 106 | $fields['children']['custom_fields']['fields'] = apply_filters('iwp/exporter/taxonomy/custom_field_list', $fields['children']['custom_fields']['fields'], $this->taxonomy); |
| 107 | + $fields = apply_filters('iwp/exporter/taxonomy/fields', $fields, $this->taxonomy); | |
| 94 | 108 | |
| 95 | - return $fields; | |
| 109 | + return $this->parse_fields($fields); | |
| 96 | 110 | } |
| 97 | 111 | |
| 98 | - public function have_records() | |
| 112 | + public function have_records($exporter_id) | |
| 99 | 113 | { |
| 100 | - $this->query = new \WP_Term_Query(array( | |
| 114 | + $query_args = []; | |
| 115 | + $query_args = apply_filters('iwp/exporter/tax_query', $query_args); | |
| 116 | + $query_args = apply_filters(sprintf('iwp/exporter/%d/tax_query', $exporter_id), $query_args); | |
| 117 | + $query_args = apply_filters(sprintf('iwp/exporter/tax_query/%s', $this->taxonomy), $query_args); | |
| 118 | + $query_args = apply_filters(sprintf('iwp/exporter/%d/tax_query/%s', $exporter_id, $this->taxonomy), $query_args); | |
| 119 | + | |
| 120 | + $query_args = wp_parse_args($query_args, [ | |
| 101 | 121 | 'taxonomy' => $this->taxonomy, |
| 102 | - 'hide_empty' => false | |
| 103 | - )); | |
| 122 | + 'hide_empty' => false, | |
| 123 | + 'fields' => 'ids' | |
| 124 | + ]); | |
| 104 | 125 | |
| 126 | + $this->query = new \WP_Term_Query($query_args); | |
| 127 | + $this->items = (array)$this->query->terms; | |
| 128 | + | |
| 105 | 129 | return $this->found_records() > 0; |
| 106 | 130 | } |
| 107 | 131 | |
| 108 | 132 | public function found_records() |
| 109 | 133 | { |
| 110 | - return count($this->query->terms); | |
| 134 | + return count($this->items); | |
| 111 | 135 | } |
| 112 | 136 | |
| 137 | + public function get_records() | |
| 138 | + { | |
| 139 | + return $this->items; | |
| 140 | + } | |
| 141 | + | |
| 113 | 142 | public function setup($i) |
| 114 | 143 | { |
| 115 | - $this->record = (array)$this->query->terms[$i]; | |
| 116 | - $this->record['custom_fields'] = get_term_meta($this->record['term_id']); | |
| 144 | + $term = get_term($this->items[$i], '', ARRAY_A); | |
| 117 | 145 | |
| 118 | - $parent = get_term($this->record['parent'], $this->taxonomy); | |
| 119 | - if (!is_wp_error($parent)) { | |
| 120 | - $this->record['parent'] = [ | |
| 121 | - 'term_id' => $parent->term_id, | |
| 122 | - 'slug' => $parent->slug, | |
| 123 | - 'name' => $parent->name, | |
| 124 | - 'parent' => $parent->parent | |
| 125 | - ]; | |
| 126 | - } else { | |
| 127 | - $this->record['parent'] = [ | |
| 128 | - 'term_id' => '', | |
| 129 | - 'slug' => '', | |
| 130 | - 'name' => '', | |
| 131 | - 'parent' => '' | |
| 132 | - ]; | |
| 133 | - } | |
| 146 | + // Remove WP_Term attribute name conflict | |
| 147 | + $term['_parent'] = $term['parent']; | |
| 148 | + unset($term['parent']); | |
| 134 | 149 | |
| 135 | - $ancestor_ids = get_ancestors($this->record['term_id'], $this->taxonomy, 'taxonomy'); | |
| 136 | - $ancestor_ids = array_reverse($ancestor_ids); | |
| 137 | - | |
| 138 | - $this->record['anscestors'] = []; | |
| 139 | - | |
| 140 | - if ($ancestor_ids) { | |
| 141 | - foreach ($ancestor_ids as $ancestor_id) { | |
| 142 | - | |
| 143 | - $ancestor = get_term($ancestor_id, $this->taxonomy); | |
| 144 | - if (!is_wp_error($ancestor)) { | |
| 145 | - $this->record['anscestors'][] = [ | |
| 146 | - 'term_id' => $ancestor->term_id, | |
| 147 | - 'slug' => $ancestor->slug, | |
| 148 | - 'name' => $ancestor->name, | |
| 149 | - 'parent' => $ancestor->parent | |
| 150 | - ]; | |
| 151 | - } | |
| 152 | - } | |
| 153 | - } | |
| 154 | - | |
| 150 | + $this->record = new ExporterRecord($term, 'tax'); | |
| 151 | + $this->record = apply_filters('iwp/exporter/taxonomy/setup_data', $this->record, $this->taxonomy); | |
| 155 | 152 | return true; |
| 156 | 153 | } |
| 157 | 154 | |
| 158 | 155 | public function get_field($column, $record, $meta) |
| @@ -221,6 +218,56 @@ | ||
| 221 | 218 | } |
| 222 | 219 | |
| 223 | 220 | $output = apply_filters('iwp/exporter/taxonomy/value', $output, $column, $record, $meta); |
| 224 | 221 | return $output; |
| 222 | + } | |
| 223 | + | |
| 224 | + public function get_record_data($value, $key, $record) | |
| 225 | + { | |
| 226 | + switch ($key) { | |
| 227 | + case 'custom_fields': | |
| 228 | + $value = get_term_meta($record['term_id']); | |
| 229 | + $value = $this->modify_custom_field_data($value, 'taxonomy'); | |
| 230 | + break; | |
| 231 | + case 'parent': | |
| 232 | + $parent = get_term($record['_parent'], $this->taxonomy); | |
| 233 | + if (!is_wp_error($parent)) { | |
| 234 | + $value = [ | |
| 235 | + 'term_id' => $parent->term_id, | |
| 236 | + 'slug' => $parent->slug, | |
| 237 | + 'name' => $parent->name, | |
| 238 | + 'parent' => $parent->parent | |
| 239 | + ]; | |
| 240 | + } else { | |
| 241 | + $value = [ | |
| 242 | + 'term_id' => '', | |
| 243 | + 'slug' => '', | |
| 244 | + 'name' => '', | |
| 245 | + 'parent' => '' | |
| 246 | + ]; | |
| 247 | + } | |
| 248 | + break; | |
| 249 | + case 'anscestors': | |
| 250 | + $ancestor_ids = get_ancestors($record['term_id'], $this->taxonomy, 'taxonomy'); | |
| 251 | + $ancestor_ids = array_reverse($ancestor_ids); | |
| 252 | + | |
| 253 | + $value = []; | |
| 254 | + | |
| 255 | + if ($ancestor_ids) { | |
| 256 | + foreach ($ancestor_ids as $ancestor_id) { | |
| 257 | + $ancestor = get_term($ancestor_id, $this->taxonomy); | |
| 258 | + if (!is_wp_error($ancestor)) { | |
| 259 | + $value[] = [ | |
| 260 | + 'term_id' => $ancestor->term_id, | |
| 261 | + 'slug' => $ancestor->slug, | |
| 262 | + 'name' => $ancestor->name, | |
| 263 | + 'parent' => $ancestor->parent | |
| 264 | + ]; | |
| 265 | + } | |
| 266 | + } | |
| 267 | + } | |
| 268 | + break; | |
| 269 | + } | |
| 270 | + | |
| 271 | + return $value; | |
| 225 | 272 | } |
| 226 | 273 | } |