Taxonomy.php
2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
namespace ACP\Editing\Storage\Post;
use AC\Storage\Transaction;
use ACP\Editing\Storage;
use RuntimeException;
use WP_Error;
class Taxonomy implements Storage {
/**
* @var string
*/
private $taxonomy;
/**
* @var bool
*/
private $append;
public function __construct( $taxonomy, $append = false ) {
$this->taxonomy = $taxonomy;
$this->append = $append;
}
public function get( $id ) {
$terms = get_the_terms( $id, $this->taxonomy );
if ( ! $terms && is_wp_error( $terms ) ) {
return [];
}
$values = [];
foreach ( $terms as $term ) {
$values[ $term->term_id ] = htmlspecialchars_decode( $term->name );
}
return $values;
}
public function update( $id, $term_ids ) {
$_post = get_post( $id );
if ( ! $_post || ! taxonomy_exists( $this->taxonomy ) ) {
return [];
}
if ( empty( $term_ids ) ) {
$term_ids = [];
}
$transaction = new Transaction();
$term_ids = array_unique( (array) $term_ids );
// maybe create terms?
$created_term_ids = [];
foreach ( (array) $term_ids as $index => $term_id ) {
if ( is_numeric( $term_id ) ) {
continue;
}
$term = get_term_by( 'name', $term_id, $this->taxonomy );
if ( $term ) {
$term_ids[ $index ] = $term->term_id;
} else {
$created_term = wp_insert_term( $term_id, $this->taxonomy );
if ( $created_term instanceof WP_Error ) {
$transaction->rollback();
throw new RuntimeException( $created_term->get_error_message() );
}
$created_term_ids[] = $created_term['term_id'];
}
}
// merge
$term_ids = array_merge( $created_term_ids, $term_ids );
//to make sure the terms IDs is integers:
$term_ids = array_map( 'intval', (array) $term_ids );
$term_ids = array_unique( $term_ids );
if ( $this->taxonomy === 'category' && is_object_in_taxonomy( $_post->post_type, 'category' ) ) {
$result = wp_set_post_categories( $_post->ID, $term_ids, $this->append );
} else if ( $this->taxonomy === 'post_tag' && is_object_in_taxonomy( $_post->post_type, 'post_tag' ) ) {
$result = wp_set_post_tags( $_post->ID, $term_ids, $this->append );
} else {
$result = wp_set_object_terms( $_post->ID, $term_ids, $this->taxonomy, $this->append );
}
if ( is_wp_error( $result ) ) {
$transaction->rollback();
throw new RuntimeException( $result->get_error_message() );
}
$result = wp_update_post( [ 'ID' => $_post->ID ] );
if ( is_wp_error( $result ) ) {
$transaction->rollback();
throw new RuntimeException( $result->get_error_message() );
}
$transaction->commit();
return $term_ids;
}
}