| 1 |
<?php |
| 2 |
namespace Elementor\App\Modules\ImportExport\Runners\Export; |
| 3 |
|
| 4 |
use Elementor\App\Modules\ImportExport\Utils as ImportExportUtils; |
| 5 |
|
| 6 |
class Taxonomies extends Export_Runner_Base { |
| 7 |
|
| 8 |
public static function get_name(): string { |
| 9 |
return 'taxonomies'; |
| 10 |
} |
| 11 |
|
| 12 |
public function should_export( array $data ) { |
| 13 |
return ( |
| 14 |
isset( $data['include'] ) && |
| 15 |
in_array( 'content', $data['include'], true ) |
| 16 |
); |
| 17 |
} |
| 18 |
|
| 19 |
public function export( array $data ) { |
| 20 |
$wp_builtin_post_types = ImportExportUtils::get_builtin_wp_post_types(); |
| 21 |
$selected_custom_post_types = isset( $data['selected_custom_post_types'] ) ? $data['selected_custom_post_types'] : []; |
| 22 |
$post_types = array_merge( $wp_builtin_post_types, $selected_custom_post_types ); |
| 23 |
|
| 24 |
$export = $this->export_taxonomies( $post_types ); |
| 25 |
|
| 26 |
$manifest_data['taxonomies'] = $export['manifest']; |
| 27 |
|
| 28 |
return [ |
| 29 |
'files' => $export['files'], |
| 30 |
'manifest' => [ |
| 31 |
$manifest_data, |
| 32 |
], |
| 33 |
]; |
| 34 |
} |
| 35 |
|
| 36 |
private function export_taxonomies( array $post_types ) { |
| 37 |
$files = []; |
| 38 |
$manifest = []; |
| 39 |
|
| 40 |
$taxonomies = get_taxonomies(); |
| 41 |
|
| 42 |
foreach ( $taxonomies as $taxonomy ) { |
| 43 |
$taxonomy_post_types = get_taxonomy( $taxonomy )->object_type; |
| 44 |
$intersected_post_types = array_intersect( $taxonomy_post_types, $post_types ); |
| 45 |
|
| 46 |
if ( empty( $intersected_post_types ) ) { |
| 47 |
continue; |
| 48 |
} |
| 49 |
|
| 50 |
$data = $this->export_terms( $taxonomy ); |
| 51 |
|
| 52 |
if ( empty( $data ) ) { |
| 53 |
continue; |
| 54 |
} |
| 55 |
|
| 56 |
foreach ( $intersected_post_types as $post_type ) { |
| 57 |
$manifest[ $post_type ][] = $taxonomy; |
| 58 |
} |
| 59 |
|
| 60 |
$files[] = [ |
| 61 |
'path' => 'taxonomies/' . $taxonomy, |
| 62 |
'data' => $data, |
| 63 |
]; |
| 64 |
} |
| 65 |
|
| 66 |
return [ |
| 67 |
'files' => $files, |
| 68 |
'manifest' => $manifest, |
| 69 |
]; |
| 70 |
} |
| 71 |
|
| 72 |
private function export_terms( $taxonomy ) { |
| 73 |
$terms = get_terms( [ |
| 74 |
'taxonomy' => (array) $taxonomy, |
| 75 |
'hide_empty' => true, |
| 76 |
'get' => 'all', |
| 77 |
] ); |
| 78 |
|
| 79 |
$ordered_terms = $this->order_terms( $terms ); |
| 80 |
|
| 81 |
if ( empty( $ordered_terms ) ) { |
| 82 |
return []; |
| 83 |
} |
| 84 |
|
| 85 |
$data = []; |
| 86 |
|
| 87 |
foreach ( $ordered_terms as $term ) { |
| 88 |
$data[] = [ |
| 89 |
'term_id' => $term->term_id, |
| 90 |
'name' => $term->name, |
| 91 |
'slug' => $term->slug, |
| 92 |
'taxonomy' => $term->taxonomy, |
| 93 |
'description' => $term->description, |
| 94 |
'parent' => $term->parent, |
| 95 |
]; |
| 96 |
} |
| 97 |
|
| 98 |
return $data; |
| 99 |
} |
| 100 |
/** |
| 101 |
* Put terms in order with no child going before its parent. |
| 102 |
*/ |
| 103 |
private function order_terms( array $terms ) { |
| 104 |
$ordered_terms = []; |
| 105 |
|
| 106 |
while ( $term = array_shift( $terms ) ) { |
| 107 |
$is_top_level = 0 === $term->parent; |
| 108 |
$is_parent_exits = isset( $ordered_terms[ $term->parent ] ); |
| 109 |
|
| 110 |
if ( $is_top_level || $is_parent_exits ) { |
| 111 |
$ordered_terms[ $term->term_id ] = $term; |
| 112 |
} else { |
| 113 |
$terms[] = $term; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
return $ordered_terms; |
| 118 |
} |
| 119 |
} |
| 120 |
|