1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Carbon_Fields\Container; |
4
|
|
|
|
5
|
|
|
use Carbon_Fields\Datastore\Datastore; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Term meta container class. |
9
|
|
|
*/ |
10
|
|
|
class Term_Meta_Container extends Container { |
11
|
|
|
protected $term_id; |
12
|
|
|
|
13
|
|
|
public $settings = array( |
14
|
|
|
// TODO remove |
15
|
|
|
'taxonomy' => array( 'category' ), |
16
|
|
|
'show_on_level' => false, |
17
|
|
|
); |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Array of condition types that are checked during save requests |
21
|
|
|
* |
22
|
|
|
* @var array<string> |
23
|
|
|
*/ |
24
|
|
|
protected $static_conditions = array( 'term', 'term_taxonomy' ); |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Array of condition types that are checked during edit requests |
28
|
|
|
* |
29
|
|
|
* @var array<string> |
30
|
|
|
*/ |
31
|
|
|
protected $dynamic_conditions = array( 'term_level' ); |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Create a new container |
35
|
|
|
* |
36
|
|
|
* @param string $unique_id Unique id of the container |
37
|
|
|
* @param string $title title of the container |
38
|
|
|
* @param string $type Type of the container |
39
|
|
|
**/ |
40
|
|
View Code Duplication |
public function __construct( $unique_id, $title, $type ) { |
41
|
|
|
parent::__construct( $unique_id, $title, $type ); |
42
|
|
|
|
43
|
|
|
if ( ! $this->get_datastore() ) { |
44
|
|
|
$this->set_datastore( Datastore::make( 'term_meta' ), $this->has_default_datastore() ); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Bind attach() and save() to the appropriate WordPress actions. |
50
|
|
|
**/ |
51
|
|
|
public function init() { |
52
|
|
|
// force taxonomy to be array |
53
|
|
|
if ( ! is_array( $this->settings['taxonomy'] ) ) { |
54
|
|
|
$this->settings['taxonomy'] = array( $this->settings['taxonomy'] ); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
add_action( 'admin_init', array( $this, '_attach' ) ); |
58
|
|
|
|
59
|
|
|
foreach ( $this->settings['taxonomy'] as $taxonomy ) { |
60
|
|
|
add_action( 'edited_' . $taxonomy, array( $this, '_save' ), 10, 2 ); |
61
|
|
|
add_action( 'created_' . $taxonomy, array( $this, '_save' ), 10, 2 ); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Perform checks whether the current save() request is valid. |
67
|
|
|
* |
68
|
|
|
* @param int $term_id ID of the term against which save() is ran |
69
|
|
|
* @return bool |
70
|
|
|
**/ |
71
|
|
|
public function is_valid_save( $term_id = null ) { |
72
|
|
|
if ( ! $this->verified_nonce_in_request() ) { |
73
|
|
|
return false; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $this->is_valid_attach_for_object( $term_id ); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Perform save operation after successful is_valid_save() check. |
81
|
|
|
* The call is propagated to all fields in the container. |
82
|
|
|
* |
83
|
|
|
* @param int $term_id ID of the term against which save() is ran |
84
|
|
|
**/ |
85
|
|
View Code Duplication |
public function save( $term_id = null ) { |
|
|
|
|
86
|
|
|
$this->set_term_id( $term_id ); |
87
|
|
|
|
88
|
|
|
foreach ( $this->fields as $field ) { |
89
|
|
|
$field->set_value_from_input( stripslashes_deep( $_POST ) ); |
|
|
|
|
90
|
|
|
$field->save(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
do_action( 'carbon_after_save_term_meta', $term_id ); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Get environment array for page request (in admin) |
98
|
|
|
* |
99
|
|
|
* @return array |
100
|
|
|
**/ |
101
|
|
|
protected function get_environment_for_request() { |
102
|
|
|
$input = stripslashes_deep( $_GET ); |
|
|
|
|
103
|
|
|
$request_term_id = isset( $input['tag_ID'] ) ? intval( $input['tag_ID'] ) : 0; |
104
|
|
|
$request_taxonomy = isset( $input['taxonomy'] ) ? $input['taxonomy'] : ''; |
105
|
|
|
|
106
|
|
|
$term = get_term( $request_term_id ); |
107
|
|
|
$term = ( $term && ! is_wp_error( $term ) ) ? $term : null; |
108
|
|
|
$environment = array( |
109
|
|
|
'term_id' => $term ? intval( $term->term_id ) : 0, |
110
|
|
|
'term' => $term, |
111
|
|
|
'taxonomy' => $term ? $term->taxonomy : $request_taxonomy, |
112
|
|
|
); |
113
|
|
|
return $environment; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Perform checks whether the container should be attached during the current request |
118
|
|
|
* |
119
|
|
|
* @return bool True if the container is allowed to be attached |
120
|
|
|
**/ |
121
|
|
View Code Duplication |
public function is_valid_attach_for_request() { |
122
|
|
|
global $pagenow; |
123
|
|
|
|
124
|
|
|
if ( $pagenow !== 'edit-tags.php' && $pagenow !== 'term.php' ) { |
125
|
|
|
return false; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$environment = $this->get_environment_for_request(); |
129
|
|
|
$static_conditions_collection = $this->conditions_collection->evaluate( $this->get_dynamic_conditions(), true ); |
|
|
|
|
130
|
|
|
if ( ! $static_conditions_collection->is_fulfilled( $environment ) ) { |
131
|
|
|
return false; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return true; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Get environment array for object id |
139
|
|
|
* |
140
|
|
|
* @return array |
141
|
|
|
*/ |
142
|
|
|
protected function get_environment_for_object( $object_id ) { |
143
|
|
|
$term = get_term( intval( $object_id ) ); |
144
|
|
|
$environment = array( |
145
|
|
|
'term_id' => intval( $term->term_id ), |
146
|
|
|
'term' => $term, |
147
|
|
|
'taxonomy' => $term->taxonomy, |
148
|
|
|
); |
149
|
|
|
return $environment; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Check container attachment rules against object id |
154
|
|
|
* |
155
|
|
|
* @param int $object_id |
156
|
|
|
* @return bool |
157
|
|
|
**/ |
158
|
|
|
public function is_valid_attach_for_object( $object_id = null ) { |
159
|
|
|
$term = get_term( $object_id ); |
160
|
|
|
$term = ( $term && ! is_wp_error( $term ) ) ? $term : null; |
161
|
|
|
|
162
|
|
|
if ( ! $term ) { |
163
|
|
|
return false; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
$environment = $this->get_environment_for_object( $term->term_id ); |
167
|
|
|
if ( ! $this->conditions_collection->is_fulfilled( $environment ) ) { |
168
|
|
|
return false; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return true; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Add term meta for each of the container taxonomies |
176
|
|
|
**/ |
177
|
|
|
public function attach() { |
178
|
|
|
foreach ( $this->settings['taxonomy'] as $taxonomy ) { |
179
|
|
|
add_action( $taxonomy . '_edit_form_fields', array( $this, 'render' ), 10, 2 ); |
180
|
|
|
add_action( $taxonomy . '_add_form_fields', array( $this, 'render' ), 10, 2 ); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Output the container markup |
186
|
|
|
**/ |
187
|
|
|
public function render( $term = null ) { |
188
|
|
|
if ( is_object( $term ) ) { |
189
|
|
|
$this->set_term_id( $term->term_id ); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
include \Carbon_Fields\DIR . '/templates/Container/term_meta.php'; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Set the term ID the container will operate with. |
197
|
|
|
* |
198
|
|
|
* @param int $term_id |
199
|
|
|
**/ |
200
|
|
|
public function set_term_id( $term_id ) { |
201
|
|
|
$this->term_id = $term_id; |
202
|
|
|
$this->get_datastore()->set_id( $term_id ); |
|
|
|
|
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Get array of taxonomies this container can appear on conditionally |
207
|
|
|
* |
208
|
|
|
* @return array<string> |
209
|
|
|
*/ |
210
|
|
View Code Duplication |
public function get_taxonomy_visibility() { |
|
|
|
|
211
|
|
|
$all_taxonomies = get_taxonomies(); |
212
|
|
|
$filtered_collection = $this->conditions_collection->filter( array( 'term_taxonomy' ) ); |
213
|
|
|
|
214
|
|
|
$shown_on = array(); |
215
|
|
|
foreach ( $all_taxonomies as $taxonomy ) { |
216
|
|
|
$environment = array( |
217
|
|
|
'taxonomy' => $taxonomy, |
218
|
|
|
); |
219
|
|
|
if ( $filtered_collection->is_fulfilled( $environment ) ) { |
220
|
|
|
$shown_on[] = $taxonomy; |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
return $shown_on; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* COMMON USAGE METHODS |
228
|
|
|
*/ |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Show the container only on terms from the specified taxonomies. |
232
|
|
|
* |
233
|
|
|
* @deprecated |
234
|
|
|
* @param string|array $taxonomies |
235
|
|
|
* @return object $this |
236
|
|
|
**/ |
237
|
|
|
public function show_on_taxonomy( $taxonomies ) { |
238
|
|
|
$taxonomies = is_array( $taxonomies ) ? $taxonomies : array( $taxonomies ); |
239
|
|
|
$this->and_when( 'term_taxonomy', 'IN', $taxonomies ); |
240
|
|
|
return $this; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Show the container only on particular term level. |
245
|
|
|
* |
246
|
|
|
* @deprecated |
247
|
|
|
* @param int $term_level |
248
|
|
|
* @return object $this |
249
|
|
|
*/ |
250
|
|
|
public function show_on_level( $term_level ) { |
251
|
|
|
$this->and_when( 'term_level', '=', intval( $term_level ) ); |
252
|
|
|
return $this; |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.