Skip to content

Commit 850e2de

Browse files
committed
Revamp blocks, frontend styles, and templates for the new KB experience.
1 parent c9eac3a commit 850e2de

File tree

94 files changed

+4169
-2133
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+4169
-2133
lines changed

build-assets.js

Lines changed: 243 additions & 164 deletions
Large diffs are not rendered by default.

includes/admin/class-admin-columns.php

Lines changed: 64 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,12 @@ public function __construct() {
3131
Hook_Registry::add_filter( 'manage_edit-wzkb_category_sortable_columns', array( $this, 'tax_sortable_columns' ) );
3232
Hook_Registry::add_filter( 'manage_edit-wzkb_tag_columns', array( $this, 'tax_columns' ) );
3333
Hook_Registry::add_filter( 'manage_edit-wzkb_tag_sortable_columns', array( $this, 'tax_sortable_columns' ) );
34+
Hook_Registry::add_filter( 'manage_edit-wzkb_product_columns', array( $this, 'tax_columns' ) );
35+
Hook_Registry::add_filter( 'manage_edit-wzkb_product_sortable_columns', array( $this, 'tax_sortable_columns' ) );
3436

3537
Hook_Registry::add_filter( 'manage_wzkb_category_custom_column', array( $this, 'tax_id' ), 10, 3 );
3638
Hook_Registry::add_filter( 'manage_wzkb_tag_custom_column', array( $this, 'tax_id' ), 10, 3 );
39+
Hook_Registry::add_filter( 'manage_wzkb_product_custom_column', array( $this, 'tax_id' ), 10, 3 );
3740

3841
// Register Product filter for Articles admin screen.
3942
Hook_Registry::add_action( 'restrict_manage_posts', array( $this, 'add_product_filter_dropdown' ) );
@@ -44,7 +47,8 @@ public function __construct() {
4447
Hook_Registry::add_filter( 'terms_clauses', array( $this, 'filter_sections_by_product' ), 10, 2 );
4548

4649
// Add sorting.
47-
Hook_Registry::add_filter( 'terms_clauses', array( $this, 'sort_terms_by_product' ), 10, 2 );
50+
Hook_Registry::add_action( 'pre_get_terms', array( $this, 'set_default_sections_order' ) );
51+
Hook_Registry::add_filter( 'terms_clauses', array( $this, 'sort_terms_by_product' ), 10, 3 );
4852
}
4953

5054
/**
@@ -124,9 +128,10 @@ public static function tax_id( $value, $name, $id ) {
124128
*
125129
* @param array $pieces Array of query SQL clauses.
126130
* @param array $taxonomies Array of taxonomy names.
131+
* @param array $args Additional term query arguments.
127132
* @return array Modified clauses.
128133
*/
129-
public function sort_terms_by_product( $pieces, $taxonomies ) {
134+
public function sort_terms_by_product( $pieces, $taxonomies, $args ) {
130135
global $wpdb;
131136

132137
// Only run for wzkb_category in admin.
@@ -135,31 +140,72 @@ public function sort_terms_by_product( $pieces, $taxonomies ) {
135140
}
136141

137142
// Check if sorting by product.
138-
$orderby = isset( $_GET['orderby'] ) ? sanitize_text_field( wp_unslash( $_GET['orderby'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
143+
$orderby = isset( $args['orderby'] ) ? sanitize_key( $args['orderby'] ) : '';
139144
if ( 'product' !== $orderby ) {
140145
return $pieces;
141146
}
142147

143148
// Get sort order.
144-
$order = isset( $_GET['order'] ) ? strtoupper( sanitize_text_field( wp_unslash( $_GET['order'] ) ) ) : 'ASC'; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
149+
$order = isset( $args['order'] ) ? strtoupper( sanitize_text_field( $args['order'] ) ) : 'ASC';
145150
$order = in_array( $order, array( 'ASC', 'DESC' ), true ) ? $order : 'ASC';
146151

147-
// Join with termmeta to get product_id.
148-
$pieces['join'] .= " LEFT JOIN {$wpdb->termmeta} AS tm ON t.term_id = tm.term_id AND tm.meta_key = 'product_id'";
152+
$meta_alias = 'tm_product_sort';
153+
$product_alias = 'pt_product_sort';
154+
$taxonomy_alias = 'ptt_product_sort';
155+
156+
// Join with termmeta to get product_id while avoiding alias collisions.
157+
$pieces['join'] .= " LEFT JOIN {$wpdb->termmeta} AS {$meta_alias} ON t.term_id = {$meta_alias}.term_id AND {$meta_alias}.meta_key = 'product_id'";
149158

150159
// Join with terms and term_taxonomy to get wzkb_product name.
151-
$pieces['join'] .= " LEFT JOIN {$wpdb->terms} AS pt ON tm.meta_value = pt.term_id";
152-
$pieces['join'] .= " LEFT JOIN {$wpdb->term_taxonomy} AS ptt ON pt.term_id = ptt.term_id AND ptt.taxonomy = 'wzkb_product'";
160+
$pieces['join'] .= " LEFT JOIN {$wpdb->terms} AS {$product_alias} ON {$meta_alias}.meta_value = {$product_alias}.term_id";
161+
$pieces['join'] .= " LEFT JOIN {$wpdb->term_taxonomy} AS {$taxonomy_alias} ON {$product_alias}.term_id = {$taxonomy_alias}.term_id AND {$taxonomy_alias}.taxonomy = 'wzkb_product'";
153162

154163
// Set the ORDER BY clause with the "ORDER BY" prefix.
155-
$pieces['orderby'] = "ORDER BY COALESCE(pt.name, '') $order, t.name $order";
164+
$pieces['orderby'] = "ORDER BY COALESCE({$product_alias}.name, '') $order, t.name $order";
156165

157166
// Prevent WordPress from appending the order.
158167
$pieces['order'] = '';
159168

160169
return $pieces;
161170
}
162171

172+
/**
173+
* Ensure Sections screen defaults to sorting by Product.
174+
*
175+
* @since 3.0.0
176+
*
177+
* @param \WP_Term_Query $query Term query instance.
178+
*/
179+
public function set_default_sections_order( $query ) {
180+
if ( ! is_admin() || ! $query instanceof \WP_Term_Query ) {
181+
return;
182+
}
183+
184+
$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
185+
if ( ! $screen || 'edit-tags' !== $screen->base || 'wzkb_category' !== $screen->taxonomy ) {
186+
return;
187+
}
188+
189+
if ( ! current_user_can( 'manage_categories' ) ) {
190+
return;
191+
}
192+
193+
// Respect explicit user choice.
194+
if ( isset( $_GET['orderby'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
195+
return;
196+
}
197+
198+
$orderby = isset( $query->query_vars['orderby'] ) ? $query->query_vars['orderby'] : '';
199+
if ( ! empty( $orderby ) && 'name' !== $orderby ) {
200+
return;
201+
}
202+
203+
$query->query_vars['orderby'] = 'product';
204+
if ( empty( $query->query_vars['order'] ) ) {
205+
$query->query_vars['order'] = 'ASC';
206+
}
207+
}
208+
163209
/**
164210
* Add product filter dropdown to Knowledgebase admin screen.
165211
*
@@ -343,12 +389,15 @@ public function enqueue_sections_filter_script() {
343389
'wzkb-sections-product-filter',
344390
'knowledgebaseProductFilter',
345391
array(
346-
'products' => $products_data,
347-
'selectedProduct' => $selected,
348-
'queryParams' => $query_params,
349-
'strings' => array(
350-
'allProducts' => __( 'All Products', 'knowledgebase' ),
351-
'filter' => __( 'Filter', 'knowledgebase' ),
392+
'products' => $products_data,
393+
'currentProduct' => $selected,
394+
'queryParams' => $query_params,
395+
'currentScreen' => $screen->id,
396+
'nonces' => array(
397+
'filter' => wp_create_nonce( 'wzkb_sections_filter' ),
398+
),
399+
'strings' => array(
400+
'filterInstruction' => __( 'Filter sections by product:', 'knowledgebase' ),
352401
'productLabel' => __( 'Product:', 'knowledgebase' ),
353402
'searchPlaceholder' => __( 'Search sections...', 'knowledgebase' ),
354403
),

0 commit comments

Comments
 (0)