-
Notifications
You must be signed in to change notification settings - Fork 423
/
taxonomy-advanced.php
132 lines (118 loc) · 3.36 KB
/
taxonomy-advanced.php
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
/**
* Taxonomy advanced field which saves terms' IDs in the post meta in CSV format.
*
* @package Meta Box
*/
/**
* The taxonomy advanced field class.
*/
class RWMB_Taxonomy_Advanced_Field extends RWMB_Taxonomy_Field {
/**
* Get meta values to save.
* Save terms in custom field in form of comma-separated IDs, no more by setting post terms.
*
* @param mixed $new The submitted meta value.
* @param mixed $old The existing meta value.
* @param int $post_id The post ID.
* @param array $field The field parameters.
*
* @return string
*/
public static function value( $new, $old, $post_id, $field ) {
return implode( ',', array_unique( (array) $new ) );
}
/**
* Save meta value.
*
* @param mixed $new The submitted meta value.
* @param mixed $old The existing meta value.
* @param int $post_id The post ID.
* @param array $field The field parameters.
*/
public static function save( $new, $old, $post_id, $field ) {
if ( empty( $field['id'] ) || ! $field['save_field'] ) {
return;
}
$storage = $field['storage'];
if ( ! $new ) {
$storage->delete( $post_id, $field['id'] );
return;
}
if ( ! $field['clone'] || ! $field['clone_as_multiple'] ) {
$storage->update( $post_id, $field['id'], $new );
return;
}
// clone and clone_as_multiple.
$storage->delete( $post_id, $field['id'] );
foreach ( $new as $value ) {
$storage->add( $post_id, $field['id'], $value );
}
}
/**
* Get raw meta value.
*
* @param int $object_id Object ID.
* @param array $field Field parameters.
* @param array $args Arguments of {@see rwmb_meta()} helper.
*
* @return mixed
*/
public static function raw_meta( $object_id, $field, $args = array() ) {
$args['single'] = true;
$meta = RWMB_Field::raw_meta( $object_id, $field, $args );
if ( empty( $meta ) ) {
return $field['multiple'] ? array() : '';
}
$meta = is_array( $meta ) ? array_map( 'wp_parse_id_list', $meta ) : wp_parse_id_list( $meta );
$meta = array_filter( $meta );
return $meta;
}
/**
* Get the field value.
* Return list of post term objects.
*
* @param array $field Field parameters.
* @param array $args Additional arguments.
* @param int|null $post_id Post ID. null for current post. Optional.
*
* @return array List of post term objects.
*/
public static function get_value( $field, $args = array(), $post_id = null ) {
$value = RWMB_Field::get_value( $field, $args, $post_id );
if ( ! $field['clone'] ) {
$value = self::call( 'terms_info', $field, $value, $args );
} else {
$return = array();
foreach ( $value as $subvalue ) {
$return[] = self::call( 'terms_info', $field, $subvalue, $args );
}
$value = $return;
}
return $value;
}
/**
* Get terms information.
*
* @param array $field Field parameters.
* @param string $term_ids Term IDs, in CSV format.
* @param array $args Additional arguments (for image size).
*
* @return array
*/
public static function terms_info( $field, $term_ids, $args ) {
if ( empty( $term_ids ) ) {
return array();
}
$args = wp_parse_args(
array(
'include' => $term_ids,
'hide_empty' => false,
),
$args
);
$info = get_terms( $field['taxonomy'], $args );
$info = is_array( $info ) ? $info : array();
return $field['multiple'] ? $info : reset( $info );
}
}