1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Carbon_Fields\Container; |
4
|
|
|
|
5
|
|
|
use Carbon_Fields\Datastore\Meta_Datastore; |
6
|
|
|
use Carbon_Fields\Datastore\Term_Meta_Datastore; |
7
|
|
|
use Carbon_Fields\Exception\Incorrect_Syntax_Exception; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Term meta container class. |
11
|
|
|
*/ |
12
|
|
|
class Term_Meta_Container extends Container { |
13
|
|
|
protected $term_id; |
14
|
|
|
|
15
|
|
|
public $settings = array( |
16
|
|
|
'taxonomy' => array( 'category' ), |
17
|
|
|
'show_on_level' => false, |
18
|
|
|
); |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Create a new term meta fields container |
22
|
|
|
* |
23
|
|
|
* @param string $title Unique title of the container |
24
|
|
|
**/ |
25
|
|
|
public function __construct( $title ) { |
26
|
|
|
parent::__construct( $title ); |
27
|
|
|
|
28
|
|
|
if ( ! $this->get_datastore() ) { |
29
|
|
|
$this->set_datastore( new Term_Meta_Datastore() ); |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Bind attach() and save() to the appropriate WordPress actions. |
35
|
|
|
**/ |
36
|
|
|
public function init() { |
37
|
|
|
// force taxonomy to be array |
38
|
|
|
if ( ! is_array( $this->settings['taxonomy'] ) ) { |
39
|
|
|
$this->settings['taxonomy'] = array( $this->settings['taxonomy'] ); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
add_action( 'admin_init', array( $this, '_attach' ) ); |
43
|
|
|
|
44
|
|
View Code Duplication |
foreach ( $this->settings['taxonomy'] as $taxonomy ) { |
|
|
|
|
45
|
|
|
add_action( 'edited_' . $taxonomy, array( $this, '_save' ), 10, 2 ); |
46
|
|
|
add_action( 'created_' . $taxonomy, array( $this, '_save' ), 10, 2 ); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Perform save operation after successful is_valid_save() check. |
52
|
|
|
* The call is propagated to all fields in the container. |
53
|
|
|
* |
54
|
|
|
* @param int $term_id ID of the term against which save() is ran |
55
|
|
|
**/ |
56
|
|
|
public function save( $term_id ) { |
57
|
|
|
$this->set_term_id( $term_id ); |
58
|
|
|
|
59
|
|
|
foreach ( $this->fields as $field ) { |
60
|
|
|
$field->set_value_from_input(); |
61
|
|
|
$field->save(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
do_action( 'carbon_after_save_term_meta', $term_id ); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Perform checks whether the container should be attached during the current request |
69
|
|
|
* |
70
|
|
|
* @return bool True if the container is allowed to be attached |
71
|
|
|
**/ |
72
|
|
|
public function is_valid_attach() { |
73
|
|
|
if ( isset( $_GET['taxonomy'] ) && in_array( $_GET['taxonomy'], $this->settings['taxonomy'] ) ) { |
|
|
|
|
74
|
|
|
return true; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return false; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Perform checks whether the current save() request is valid. |
82
|
|
|
* |
83
|
|
|
* @param int $term_id ID of the term against which save() is ran |
84
|
|
|
* @return bool |
85
|
|
|
**/ |
86
|
|
|
public function is_valid_save( $term_id = null ) { |
87
|
|
|
if ( ! isset( $_REQUEST[ $this->get_nonce_name() ] ) || ! wp_verify_nonce( $_REQUEST[ $this->get_nonce_name() ], $this->get_nonce_name() ) ) { // Input var okay. |
|
|
|
|
88
|
|
|
return false; |
89
|
|
|
} else if ( $term_id < 1 ) { |
90
|
|
|
return false; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return true; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Add term meta for each of the container taxonomies |
98
|
|
|
**/ |
99
|
|
|
public function attach() { |
100
|
|
|
foreach ( $this->settings['taxonomy'] as $taxonomy ) { |
101
|
|
|
add_action( $taxonomy . '_edit_form_fields', array( $this, 'render' ), 10, 2 ); |
102
|
|
|
add_action( $taxonomy . '_add_form_fields', array( $this, 'render' ), 10, 2 ); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Revert the result of attach() |
108
|
|
|
* |
109
|
|
|
**/ |
110
|
|
|
public function detach() { |
111
|
|
|
parent::detach(); |
112
|
|
|
|
113
|
|
|
remove_action( 'admin_init', array( $this, '_attach' ) ); |
114
|
|
|
|
115
|
|
View Code Duplication |
foreach ( $this->settings['taxonomy'] as $taxonomy ) { |
|
|
|
|
116
|
|
|
remove_action( 'edited_' . $taxonomy, array( $this, '_save' ), 10, 2 ); |
117
|
|
|
remove_action( 'created_' . $taxonomy, array( $this, '_save' ), 10, 2 ); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
// unregister field names |
121
|
|
|
foreach ( $this->fields as $field ) { |
122
|
|
|
$this->drop_unique_field_name( $field->get_name() ); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Output the container markup |
128
|
|
|
**/ |
129
|
|
|
public function render( $term = null ) { |
130
|
|
|
if ( is_object( $term ) ) { |
131
|
|
|
$this->set_term_id( $term->term_id ); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
include \Carbon_Fields\DIR . '/templates/Container/term_meta.php'; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Set the term ID the container will operate with. |
139
|
|
|
* |
140
|
|
|
* @param int $term_id |
141
|
|
|
**/ |
142
|
|
|
public function set_term_id( $term_id ) { |
143
|
|
|
$this->term_id = $term_id; |
144
|
|
|
$this->store->set_id( $term_id ); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Perform checks whether there is a field registered with the name $name. |
150
|
|
|
* If not, the field name is recorded. |
151
|
|
|
* |
152
|
|
|
* @param string $name |
153
|
|
|
**/ |
154
|
|
View Code Duplication |
public function verify_unique_field_name( $name ) { |
|
|
|
|
155
|
|
|
if ( empty( $this->settings['taxonomy'] ) ) { |
156
|
|
|
Incorrect_Syntax_Exception::raise( 'Panel instance is not setup correctly (missing taxonomy)' ); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
foreach ( $this->settings['taxonomy'] as $taxonomy ) { |
160
|
|
|
if ( ! isset( self::$registered_field_names[ $taxonomy ] ) ) { |
161
|
|
|
self::$registered_field_names[ $taxonomy ] = array(); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
if ( in_array( $name, self::$registered_field_names[ $taxonomy ] ) ) { |
165
|
|
|
Incorrect_Syntax_Exception::raise( 'Field name "' . $name . '" already registered' ); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
self::$registered_field_names[ $taxonomy ][] = $name; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Remove field name $name from the list of unique field names |
174
|
|
|
* |
175
|
|
|
* @param string $name |
176
|
|
|
**/ |
177
|
|
View Code Duplication |
public function drop_unique_field_name( $name ) { |
|
|
|
|
178
|
|
|
foreach ( $this->settings['taxonomy'] as $taxonomy ) { |
179
|
|
|
$index = array_search( $name, self::$registered_field_names[ $taxonomy ] ); |
180
|
|
|
if ( $index !== false ) { |
181
|
|
|
unset( self::$registered_field_names[ $taxonomy ][ $index ] ); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Show the container only on terms from the specified taxonomies. |
188
|
|
|
* |
189
|
|
|
* @param string|array $taxonomies |
190
|
|
|
* @return object $this |
191
|
|
|
**/ |
192
|
|
|
public function show_on_taxonomy( $taxonomies ) { |
193
|
|
|
$taxonomies = (array) $taxonomies; |
194
|
|
|
|
195
|
|
|
$this->settings['taxonomy'] = $taxonomies; |
196
|
|
|
|
197
|
|
|
return $this; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Show the container only on particular term level. |
202
|
|
|
* |
203
|
|
|
* @param int $term_level |
204
|
|
|
* @return object $this |
205
|
|
|
*/ |
206
|
|
|
public function show_on_level( $term_level ) { |
207
|
|
|
$this->settings['show_on_level'] = $term_level; |
208
|
|
|
return $this; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
} |
212
|
|
|
|
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.