← All changes
|
class/Common/Importer/Template/PostTemplate.php
+104
-9
2.14.23
→
2.15.0
View file →
| @@ -255,9 +255,9 @@ | ||
| 255 | 255 | ], |
| 256 | 256 | 'type' => 'select' |
| 257 | 257 | ]), |
| 258 | 258 | ], ['type' => 'settings']) |
| 259 | - ], ['type' => 'repeatable', 'row_base' => true, 'link' => 'https://www.importwp.com/docs/how-to-import-wordpress-taxonomies-onto-a-post-type/']); | |
| 259 | + ], ['type' => 'repeatable', 'row_base' => true, 'row_summary' => ['tax', 'term'], 'link' => 'https://www.importwp.com/docs/how-to-import-wordpress-taxonomies-onto-a-post-type/']); | |
| 260 | 260 | } |
| 261 | 261 | |
| 262 | 262 | public function register_settings() {} |
| 263 | 263 | |
| @@ -956,23 +956,39 @@ | ||
| 956 | 956 | 'settings._append' => 'no', |
| 957 | 957 | 'settings._delimiter' => '', |
| 958 | 958 | 'settings._hierarchy' => 'no', |
| 959 | 959 | 'settings._hierarchy_character' => '>', |
| 960 | + 'settings._type' => 'name', | |
| 960 | 961 | ]; |
| 961 | 962 | |
| 962 | 963 | foreach ($taxonomies as $taxonomy => $taxonomy_data) { |
| 964 | + $term_map = isset($taxonomy_data['map']) && is_array($taxonomy_data['map']) ? $taxonomy_data['map'] : []; | |
| 963 | 965 | $data = []; |
| 964 | 966 | |
| 965 | - if (isset($taxonomy_data['map']['hierarchy::name'])) { | |
| 966 | - $data['term'] = $taxonomy_data['map']['hierarchy::name']; | |
| 967 | + // Prefer name/slug over hierarchy::* (those extra columns are always | |
| 968 | + // present on a full CSV export). Always store term type so IDs are | |
| 969 | + // not imported as names. | |
| 970 | + if (isset($term_map['name'])) { | |
| 971 | + $data['term'] = $term_map['name']; | |
| 972 | + $data['settings._type'] = 'name'; | |
| 973 | + } elseif (isset($term_map['slug'])) { | |
| 974 | + $data['term'] = $term_map['slug']; | |
| 975 | + $data['settings._type'] = 'slug'; | |
| 976 | + } elseif (isset($term_map['hierarchy::name'])) { | |
| 977 | + $data['term'] = $term_map['hierarchy::name']; | |
| 967 | 978 | $data['settings._hierarchy'] = 'yes'; |
| 968 | - } elseif (isset($taxonomy_data['map']['hierarchy::slug'])) { | |
| 969 | - $data['term'] = $taxonomy_data['map']['hierarchy::slug']; | |
| 979 | + $data['settings._type'] = 'name'; | |
| 980 | + } elseif (isset($term_map['hierarchy::slug'])) { | |
| 981 | + $data['term'] = $term_map['hierarchy::slug']; | |
| 970 | 982 | $data['settings._hierarchy'] = 'yes'; |
| 971 | - } elseif (isset($taxonomy_data['map']['name'])) { | |
| 972 | - $data['term'] = $taxonomy_data['map']['name']; | |
| 973 | - } elseif (isset($taxonomy_data['map']['slug'])) { | |
| 974 | - $data['term'] = $taxonomy_data['map']['slug']; | |
| 983 | + $data['settings._type'] = 'slug'; | |
| 984 | + } elseif (isset($term_map['id'])) { | |
| 985 | + $data['term'] = $term_map['id']; | |
| 986 | + $data['settings._type'] = 'term_id'; | |
| 987 | + } elseif (isset($term_map['hierarchy::id'])) { | |
| 988 | + $data['term'] = $term_map['hierarchy::id']; | |
| 989 | + $data['settings._hierarchy'] = 'yes'; | |
| 990 | + $data['settings._type'] = 'term_id'; | |
| 975 | 991 | } else { |
| 976 | 992 | continue; |
| 977 | 993 | } |
| 978 | 994 | |
| @@ -977,8 +993,14 @@ | ||
| 977 | 993 | } |
| 978 | 994 | |
| 979 | 995 | $data['tax'] = $taxonomy; |
| 980 | 996 | |
| 997 | + $row_base = $this->find_exporter_loop_row_base($fields, 'tax_' . $taxonomy); | |
| 998 | + if ($row_base !== '') { | |
| 999 | + $data['row_base'] = $row_base; | |
| 1000 | + $data['term'] = $this->relative_exporter_map($data['term'], $row_base); | |
| 1001 | + } | |
| 1002 | + | |
| 981 | 1003 | $data = wp_parse_args($data, $defaults); |
| 982 | 1004 | |
| 983 | 1005 | $map = array_merge($map, array_reduce(array_keys($data), function ($carry, $key) use ($data, $taxonomy_counter) { |
| 984 | 1006 | $carry[sprintf('taxonomies.%d.%s', $taxonomy_counter, $key)] = $data[$key]; |
| @@ -1163,6 +1185,79 @@ | ||
| 1163 | 1185 | return array_merge( |
| 1164 | 1186 | $output, |
| 1165 | 1187 | $this->get_unique_identifier_options_from_map($importer_model, $unique_fields, $this->field_map, $this->optional_fields) |
| 1166 | 1188 | ); |
| 1189 | + } | |
| 1190 | + | |
| 1191 | + /** | |
| 1192 | + * XML/JSON from-exporter headings are path => selection; CSV is a numeric list. | |
| 1193 | + * | |
| 1194 | + * @param array $fields | |
| 1195 | + * @return bool | |
| 1196 | + */ | |
| 1197 | + protected function headings_are_paths($fields) | |
| 1198 | + { | |
| 1199 | + if (!is_array($fields) || empty($fields)) { | |
| 1200 | + return false; | |
| 1201 | + } | |
| 1202 | + | |
| 1203 | + foreach (array_keys($fields) as $key) { | |
| 1204 | + if (is_string($key) && $key !== '' && !is_numeric($key) && strpos($key, '/') !== false) { | |
| 1205 | + return true; | |
| 1206 | + } | |
| 1207 | + } | |
| 1208 | + | |
| 1209 | + return false; | |
| 1210 | + } | |
| 1211 | + | |
| 1212 | + /** | |
| 1213 | + * Loop node path used as row_base when creating an importer from an XML/JSON export. | |
| 1214 | + * | |
| 1215 | + * @param array $fields | |
| 1216 | + * @param string $loop_selection | |
| 1217 | + * @return string | |
| 1218 | + */ | |
| 1219 | + protected function find_exporter_loop_row_base($fields, $loop_selection) | |
| 1220 | + { | |
| 1221 | + if ($loop_selection === '' || !$this->headings_are_paths($fields)) { | |
| 1222 | + return ''; | |
| 1223 | + } | |
| 1224 | + | |
| 1225 | + foreach ($fields as $path => $selection) { | |
| 1226 | + if ($selection === $loop_selection && is_string($path) && strpos($path, '/') !== false) { | |
| 1227 | + return $path; | |
| 1228 | + } | |
| 1229 | + } | |
| 1230 | + | |
| 1231 | + return ''; | |
| 1232 | + } | |
| 1233 | + | |
| 1234 | + /** | |
| 1235 | + * Rewrite a mapped path so it is relative to the exporter loop node. | |
| 1236 | + * | |
| 1237 | + * @param string $mapped | |
| 1238 | + * @param string $row_base | |
| 1239 | + * @return string | |
| 1240 | + */ | |
| 1241 | + protected function relative_exporter_map($mapped, $row_base) | |
| 1242 | + { | |
| 1243 | + if ($row_base === '' || !is_string($mapped) || $mapped === '') { | |
| 1244 | + return $mapped; | |
| 1245 | + } | |
| 1246 | + | |
| 1247 | + if (preg_match('/^\{(.*)\}$/', $mapped, $matches) !== 1) { | |
| 1248 | + return $mapped; | |
| 1249 | + } | |
| 1250 | + | |
| 1251 | + $path = $matches[1]; | |
| 1252 | + if (strpos($path, $row_base) !== 0 || $path === $row_base) { | |
| 1253 | + return $mapped; | |
| 1254 | + } | |
| 1255 | + | |
| 1256 | + $relative = substr($path, strlen($row_base)); | |
| 1257 | + if ($relative !== '' && $relative[0] !== '/') { | |
| 1258 | + $relative = '/' . $relative; | |
| 1259 | + } | |
| 1260 | + | |
| 1261 | + return '{' . $relative . '}'; | |
| 1167 | 1262 | } |
| 1168 | 1263 | } |