-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm.php
More file actions
248 lines (225 loc) · 7.57 KB
/
Copy pathForm.php
File metadata and controls
248 lines (225 loc) · 7.57 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<?php
namespace Amarkal\Taxonomy;
/**
* WordPress taxonomy form utilities
*/
class Form
{
/**
* @var Singleton The reference to *Singleton* instance of this class
*/
private static $instance;
/**
* @var Array Stores a form for each taxonomy
*/
private $forms = array();
/**
* Returns the *Singleton* instance of this class.
*
* @return Singleton The *Singleton* instance.
*/
public static function get_instance()
{
if( null === static::$instance )
{
static::$instance = new static();
}
return static::$instance;
}
/**
* Add a form field to both the add & edit forms for a given taxonomy.
*
* @param string $taxonomy_name
* @param array $component
* @throws \RuntimeException if duplicate names are registered under the same taxonomy
*/
public function add_field( $taxonomy_name, $component )
{
if( !isset($this->forms[$taxonomy_name]) )
{
// Add fields to taxonomy add and edit forms
add_action( "{$taxonomy_name}_add_form_fields", array($this, 'render_add_form') );
add_action( "{$taxonomy_name}_edit_form_fields", array($this, 'render_edit_form') );
// Save the data from taxonomy add and edit forms
add_action( "create_{$taxonomy_name}", array($this, 'update_term') );
add_action( "edited_{$taxonomy_name}", array($this, 'update_term') );
// Modify the taxonomy term table
add_filter( "manage_edit-{$taxonomy_name}_columns", array($this, 'modify_table_columns') );
add_filter( "manage_{$taxonomy_name}_custom_column", array($this, 'modify_table_content'), 10, 3 );
add_filter( "manage_edit-{$taxonomy_name}_sortable_columns", array($this, 'modify_table_sortable_columns') );
add_filter( 'terms_clauses', array($this, 'sort_custom_column'), 10, 3 );
$this->forms[$taxonomy_name] = new \Amarkal\UI\Form();
}
$this->forms[$taxonomy_name]->get_component_list()->add_component(
array_merge( $this->default_props(), $component )
);
}
/**
* Render the 'edit term' form for a given taxonomy
*
* @param object $term Taxonomy term
*/
public function render_edit_form( $term )
{
$form = $this->forms[$term->taxonomy];
$new_instance = array();
foreach( $form->get_component_list()->get_value_components() as $component )
{
$new_instance[$component->name] = \get_term_meta($term->term_id, $component->name, true);
}
$form->update($new_instance);
include __DIR__.'/EditForm.phtml';
}
/**
* Render the 'add new term' form for a given taxonomy
*
* @param string $taxonomy Taxonomy name
*/
public function render_add_form( $taxonomy )
{
$form = $this->forms[$taxonomy];
$form->update();
include __DIR__.'/AddForm.phtml';
}
/**
* Update the meta values for a given term. Called once one of the add/edit
* forms is saved.
*
* @param type $term_id
*/
function update_term( $term_id )
{
$term = \get_term( $term_id );
$form = $this->forms[$term->taxonomy];
foreach( $form->get_component_list()->get_value_components() as $component )
{
$term_meta = filter_input(INPUT_POST, $component->name);
if( null !== $term_meta )
{
\update_term_meta($term_id, $component->name, $term_meta);
}
}
}
/**
* Add additional columns to the term table.
*
* @param array $columns
* @return array
*/
function modify_table_columns( $columns )
{
$this->traverse_components(function( $taxonomy, $component ) use ( &$columns )
{
if( $component->table['show'] )
{
$columns[$component->name] = $component->title;
}
});
return $columns;
}
/**
* Retrieve the data for a given column in the term table.
*
* @see https://developer.wordpress.org/reference/hooks/manage_this-screen-taxonomy_custom_column/
*
* @param type $content
* @param type $column_name
* @param type $term_id
* @return type
*/
function modify_table_content( $content, $column_name, $term_id )
{
$term = \get_term($term_id);
$this->traverse_components(function( $taxonomy, $component ) use ( &$content, $column_name, $term )
{
if( $component->table['show'] &&
$term->taxonomy === $taxonomy &&
$component->name === $column_name
) {
$content = \get_term_meta($term->term_id, $component->name, true);
}
});
return $content;
}
/**
* Make custom table columns sortable.
*
* @param array $columns
* @return string
*/
function modify_table_sortable_columns( $columns )
{
$this->traverse_components(function( $taxonomy, $component ) use ( &$columns )
{
if( $component->table['show'] &&
$component->table['sortable']
) {
$columns[$component->name] = $component->name;
}
});
return $columns;
}
/**
* Modify terms_clauses to allow sorting custom WordPress Admin Table Columns by a custom Taxonomy Term meta
*
* @see https://developer.wordpress.org/reference/hooks/terms_clauses/
*
* @global type $wpdb
* @param type $clauses
* @param type $taxonomies
* @param type $args
* @return string
*/
public function sort_custom_column( $clauses, $taxonomies, $args )
{
$this->traverse_components(function( $taxonomy, $component ) use ( &$clauses, $args )
{
if( in_array($taxonomy, $args['taxonomy']) &&
$component->table['sortable'] &&
$component->name === $args['orderby']
)
{
global $wpdb;
// tt refers to the $wpdb->term_taxonomy table
$clauses['join'] .= " LEFT JOIN {$wpdb->termmeta} AS tm ON t.term_id = tm.term_id";
$clauses['where'] = "tt.taxonomy = '{$taxonomy}' AND (tm.meta_key = '{$component->name}' OR tm.meta_key IS NULL)";
$clauses['orderby'] = "ORDER BY tm.meta_value";
}
});
return $clauses;
}
/**
* The default form field properties. This is merged with the user given
* properties. When the component is rendered, this will be merged with the
* component's properties as well.
*
* @return array
*/
private function default_props()
{
return array(
'type' => null,
'title' => null,
'description' => null,
'table' => array(
'show' => false,
'sortable' => false
)
);
}
/**
* Treverse the $fields array.
*
* @param collable $callback Called on each iteration
*/
private function traverse_components( $callback )
{
foreach( $this->forms as $taxonomy => $form )
{
foreach( $form->get_component_list()->get_value_components() as $component )
{
$callback( $taxonomy, $component );
}
}
}
}