1 | <?php |
||
8 | class Form |
||
9 | { |
||
10 | /** |
||
11 | * @var Singleton The reference to *Singleton* instance of this class |
||
12 | */ |
||
13 | private static $instance; |
||
14 | |||
15 | /** |
||
16 | * @var Array Stores all the registered fields for each taxonomy |
||
17 | */ |
||
18 | private $fields = array(); |
||
19 | |||
20 | /** |
||
21 | * Returns the *Singleton* instance of this class. |
||
22 | * |
||
23 | * @return Singleton The *Singleton* instance. |
||
24 | */ |
||
25 | public static function get_instance() |
||
33 | |||
34 | /** |
||
35 | * Add a form field to both the add & edit forms for a given taxonomy. |
||
36 | * |
||
37 | * @param string $taxonomy_name |
||
38 | * @param string $field_name |
||
39 | * @param array $field_props |
||
40 | * @throws \RuntimeException if duplicate names are registered under the same taxonomy |
||
41 | */ |
||
42 | public function add_field( $taxonomy_name, $field_name, $field_props ) |
||
69 | |||
70 | /** |
||
71 | * Render the 'edit term' form for a given taxonomy |
||
72 | * |
||
73 | * @param object $term Taxonomy term |
||
74 | */ |
||
75 | public function render_edit_form( $term ) |
||
87 | |||
88 | /** |
||
89 | * Render the 'add new term' form for a given taxonomy |
||
90 | * |
||
91 | * @param string $taxonomy Taxonomy name |
||
92 | */ |
||
93 | public function render_add_form( $taxonomy ) |
||
104 | |||
105 | /** |
||
106 | * Update the meta values for a given term. Called once one of the add/edit |
||
107 | * forms is saved. |
||
108 | * |
||
109 | * @param type $term_id |
||
110 | */ |
||
111 | function update_term( $term_id ) |
||
112 | { |
||
113 | $term = \get_term( $term_id ); |
||
114 | |||
115 | foreach( $this->fields[$term->taxonomy] as $name => $props ) |
||
116 | { |
||
117 | $term_meta = filter_input(INPUT_POST, $name); |
||
118 | if( null !== $term_meta ) |
||
119 | { |
||
120 | update_term_meta($term_id, $name, $term_meta); |
||
121 | } |
||
122 | } |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Add additional columns to the term table. |
||
127 | * |
||
128 | * @param array $columns |
||
129 | * @return array |
||
130 | */ |
||
131 | function modify_table_columns( $columns ) |
||
132 | { |
||
133 | $this->traverse_fields(function( $taxonomy, $name, $props ) use ( &$columns ) |
||
134 | { |
||
135 | if( $props['table']['show'] ) |
||
136 | { |
||
137 | $columns[$name] = $props['label']; |
||
138 | } |
||
139 | }); |
||
140 | return $columns; |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * Retrieve the data for a given column in the term table. |
||
145 | * |
||
146 | * @see https://developer.wordpress.org/reference/hooks/manage_this-screen-taxonomy_custom_column/ |
||
147 | * |
||
148 | * @param type $content |
||
149 | * @param type $column_name |
||
150 | * @param type $term_id |
||
151 | * @return type |
||
152 | */ |
||
153 | function modify_table_content( $content, $column_name, $term_id ) |
||
154 | { |
||
155 | $term = \get_term($term_id); |
||
156 | $this->traverse_fields(function( $taxonomy, $name, $props ) use ( &$content, $column_name, $term ) |
||
157 | { |
||
158 | if( $props['table']['show'] && |
||
159 | $term->taxonomy === $taxonomy && |
||
160 | $name === $column_name |
||
161 | ) { |
||
162 | $content = \get_term_meta($term->term_id, $name, true); |
||
163 | } |
||
164 | }); |
||
165 | return $content; |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Make custom table columns sortable. |
||
170 | * |
||
171 | * @param array $columns |
||
172 | * @return string |
||
173 | */ |
||
174 | function modify_table_sortable_columns( $columns ) |
||
175 | { |
||
176 | $this->traverse_fields(function( $taxonomy, $name, $props ) use ( &$columns ) |
||
177 | { |
||
178 | if( $props['table']['show'] && |
||
179 | $props['table']['sortable'] |
||
180 | ) { |
||
181 | $columns[$name] = $name; |
||
182 | } |
||
183 | }); |
||
184 | return $columns; |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * Modify terms_clauses to allow sorting custom WordPress Admin Table Columns by a custom Taxonomy Term meta |
||
189 | * |
||
190 | * @see https://developer.wordpress.org/reference/hooks/terms_clauses/ |
||
191 | * |
||
192 | * @global type $wpdb |
||
193 | * @param type $clauses |
||
194 | * @param type $taxonomies |
||
195 | * @param type $args |
||
196 | * @return string |
||
197 | */ |
||
198 | public function sort_custom_column( $clauses, $taxonomies, $args ) |
||
199 | { |
||
200 | $this->traverse_fields(function( $taxonomy, $name, $props ) use ( &$clauses, $args ) |
||
201 | { |
||
202 | if( in_array($taxonomy, $args['taxonomy']) && |
||
203 | $props['table']['sortable'] && |
||
204 | $name === $args['orderby'] |
||
205 | ) |
||
206 | { |
||
207 | global $wpdb; |
||
208 | // tt refers to the $wpdb->term_taxonomy table |
||
209 | $clauses['join'] .= " LEFT JOIN {$wpdb->termmeta} AS tm ON t.term_id = tm.term_id"; |
||
210 | $clauses['where'] = "tt.taxonomy = '{$taxonomy}' AND (tm.meta_key = '{$name}' OR tm.meta_key IS NULL)"; |
||
211 | $clauses['orderby'] = "ORDER BY tm.meta_value"; |
||
212 | } |
||
213 | }); |
||
214 | return $clauses; |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * The default form field properties. This is merged with the user given |
||
219 | * properties. When the component is rendered, this will be merged with the |
||
220 | * component's properties as well. |
||
221 | * |
||
222 | * @return array |
||
223 | */ |
||
224 | private function default_props() |
||
236 | |||
237 | /** |
||
238 | * Treverse the $fields array. |
||
239 | * |
||
240 | * @param collable $callback Called on each iteration |
||
241 | */ |
||
242 | private function traverse_fields( $callback ) |
||
252 | } |
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: