-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathclass-admin-columns.php
More file actions
245 lines (210 loc) · 7.08 KB
/
Copy pathclass-admin-columns.php
File metadata and controls
245 lines (210 loc) · 7.08 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
<?php
/**
* Admin Columns.
*
* @package WebberZone\Knowledge_Base
*/
namespace WebberZone\Knowledge_Base\Admin;
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
/**
* Admin Columns class.
*
* @since 2.3.0
*/
class Admin_Columns {
/**
* Main constructor class.
*
* @since 2.3.0
*/
public function __construct() {
add_filter( 'manage_edit-wzkb_category_columns', array( $this, 'tax_columns' ) );
add_filter( 'manage_edit-wzkb_category_sortable_columns', array( $this, 'tax_sortable_columns' ) );
add_filter( 'manage_edit-wzkb_tag_columns', array( $this, 'tax_columns' ) );
add_filter( 'manage_edit-wzkb_tag_sortable_columns', array( $this, 'tax_sortable_columns' ) );
add_filter( 'manage_wzkb_category_custom_column', array( $this, 'tax_id' ), 10, 3 );
add_filter( 'manage_wzkb_tag_custom_column', array( $this, 'tax_id' ), 10, 3 );
// Register Product filter for Articles admin screen.
add_action( 'restrict_manage_posts', array( $this, 'add_product_filter_dropdown' ) );
add_action( 'pre_get_posts', array( $this, 'filter_articles_by_product' ) );
// Add sorting.
add_filter( 'terms_clauses', array( $this, 'sort_terms_by_product' ), 10, 2 );
}
/**
* Customise the taxonomy columns.
*
* @since 2.3.0
*
* @param array $columns Columns in the admin view.
* @return array Updated columns.
*/
public static function tax_columns( $columns ) {
// Remove the description column.
unset( $columns['description'] );
$new_columns = array(
'tax_id' => 'ID',
);
// Only add Product column for wzkb_category taxonomy.
$screen = get_current_screen();
if ( isset( $screen->taxonomy ) && 'wzkb_category' === $screen->taxonomy ) {
$new_columns['product'] = __( 'Product', 'knowledgebase' );
}
return array_merge( $columns, $new_columns );
}
/**
* Make the Product column sortable.
*
* @since 3.0.0
*
* @param array $columns Array of sortable columns.
* @return array Modified array of sortable columns.
*/
public function tax_sortable_columns( $columns ) {
$columns['product'] = 'product';
return $columns;
}
/**
* Add taxonomy ID and Product to the admin column.
*
* @since 2.3.0
*
* @param string $value Deprecated.
* @param string $name Name of the column.
* @param int|string $id Category ID.
* @return int|string
*/
public static function tax_id( $value, $name, $id ) {
if ( 'tax_id' === $name ) {
return $id;
}
if ( 'product' === $name ) {
// Get linked product for this section.
$product_id = get_term_meta( $id, 'product_id', true );
if ( $product_id ) {
$product = get_term( $product_id, 'wzkb_product' );
if ( $product && ! is_wp_error( $product ) ) {
return sprintf(
'<a href="%s">%s</a>',
esc_url( admin_url( 'edit.php?post_type=wz_knowledgebase&wzkb_product=' . $product->slug ) ),
esc_html( $product->name )
);
}
}
return '—'; // Em dash if not linked.
}
return $value;
}
/**
* Sort wzkb_category terms by wzkb_product name.
*
* @since 3.0.0
*
* @param array $pieces Array of query SQL clauses.
* @param array $taxonomies Array of taxonomy names.
* @return array Modified clauses.
*/
public function sort_terms_by_product( $pieces, $taxonomies ) {
global $wpdb;
// Only run for wzkb_category in admin.
if ( ! is_admin() || ! in_array( 'wzkb_category', $taxonomies, true ) ) {
return $pieces;
}
// Check if sorting by product.
$orderby = isset( $_GET['orderby'] ) ? sanitize_text_field( wp_unslash( $_GET['orderby'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( 'product' !== $orderby ) {
return $pieces;
}
// Get sort order.
$order = isset( $_GET['order'] ) ? strtoupper( sanitize_text_field( wp_unslash( $_GET['order'] ) ) ) : 'ASC'; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$order = in_array( $order, array( 'ASC', 'DESC' ), true ) ? $order : 'ASC';
// Join with termmeta to get product_id.
$pieces['join'] .= " LEFT JOIN {$wpdb->termmeta} AS tm ON t.term_id = tm.term_id AND tm.meta_key = 'product_id'";
// Join with terms and term_taxonomy to get wzkb_product name.
$pieces['join'] .= " LEFT JOIN {$wpdb->terms} AS pt ON tm.meta_value = pt.term_id";
$pieces['join'] .= " LEFT JOIN {$wpdb->term_taxonomy} AS ptt ON pt.term_id = ptt.term_id AND ptt.taxonomy = 'wzkb_product'";
// Set the ORDER BY clause with the "ORDER BY" prefix.
$pieces['orderby'] = "ORDER BY COALESCE(pt.name, '') $order, t.name $order";
// Prevent WordPress from appending the order.
$pieces['order'] = '';
return $pieces;
}
/**
* Add product filter dropdown to Knowledgebase admin screen.
*
* @since 3.0.0
*/
public function add_product_filter_dropdown() {
global $pagenow;
// Only run on the edit.php page for wz_knowledgebase post type.
if ( 'edit.php' !== $pagenow || ! isset( $_GET['post_type'] ) || 'wz_knowledgebase' !== $_GET['post_type'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
return;
}
// Get all wzkb_product terms.
$terms = get_terms(
array(
'taxonomy' => 'wzkb_product',
'hide_empty' => false,
)
);
if ( empty( $terms ) || is_wp_error( $terms ) ) {
return;
}
// Get the currently selected product filter.
$selected = isset( $_GET['wzkb_product'] ) ? sanitize_text_field( wp_unslash( $_GET['wzkb_product'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
// Output the dropdown.
?>
<select name="wzkb_product" id="wzkb_product_filter">
<option value=""><?php esc_html_e( 'All Products', 'knowledgebase' ); ?></option>
<?php
foreach ( $terms as $term ) {
printf(
'<option value="%s" %s>%s</option>',
esc_attr( $term->slug ),
selected( $selected, $term->slug, false ),
esc_html( $term->name )
);
}
?>
</select>
<?php
}
/**
* Apply Product filter to Articles admin query.
*
* @since 3.0.0
*
* @param \WP_Query $query The current WP_Query instance (passed by reference).
*/
public function filter_articles_by_product( $query ) {
global $pagenow;
// Only run in admin post list, main query, and correct post type.
if ( ! is_admin() || 'edit.php' !== $pagenow || ! $query->is_main_query() ) {
return;
}
$post_type = isset( $_GET['post_type'] ) ? sanitize_text_field( wp_unslash( $_GET['post_type'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( 'wz_knowledgebase' !== $post_type ) {
return;
}
// Get the product filter value.
$product = isset( $_GET['wzkb_product'] ) ? sanitize_text_field( wp_unslash( $_GET['wzkb_product'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( empty( $product ) ) {
return;
}
// Ensure the taxonomy exists.
if ( ! taxonomy_exists( 'wzkb_product' ) ) {
return;
}
// Add the tax query.
$tax_query = array(
array(
'taxonomy' => 'wzkb_product',
'field' => is_numeric( $product ) ? 'term_id' : 'slug',
'terms' => is_numeric( $product ) ? absint( $product ) : $product,
),
);
$query->set( 'tax_query', $tax_query );
}
}