Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
191 changes: 182 additions & 9 deletions includes/admin/class-admin-columns.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?php
/**
* Admin columns class
*
* @since 2.3.0
* Admin Columns.
*
* @package WebberZone\Knowledge_Base
*/
Expand All @@ -15,7 +13,7 @@
}

/**
* Class to register the Better Search Admin Area.
* Admin Columns class.
*
* @since 2.3.0
*/
Expand All @@ -28,12 +26,19 @@ class Admin_Columns {
*/
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_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_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 );
}

/**
Expand All @@ -45,19 +50,37 @@ public function __construct() {
* @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 );
}

/**
* Add taxonomy ID to the admin column.
* 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
*
Expand All @@ -67,6 +90,156 @@ public static function tax_columns( $columns ) {
* @return int|string
*/
public static function tax_id( $value, $name, $id ) {
return 'tax_id' === $name ? $id : $value;
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 '&mdash;'; // 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 );
}
}
59 changes: 40 additions & 19 deletions includes/admin/class-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
namespace WebberZone\Knowledge_Base\Admin;

use WebberZone\Knowledge_Base\Util\Cache;
use WebberZone\Knowledge_Base\Admin\Activator;

// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
Expand Down Expand Up @@ -60,6 +59,15 @@ class Admin {
*/
public $admin_columns;

/**
* Product Migrator class.
*
* @since 3.0.0
*
* @var object Product Migrator class.
*/
public $product_migrator;

/**
* Main constructor class.
*
Expand All @@ -69,10 +77,11 @@ public function __construct() {
$this->hooks();

// Initialise admin classes.
$this->settings = new Settings\Settings();
$this->activator = new Activator();
$this->cache = new Cache();
$this->admin_columns = new Admin_Columns();
$this->settings = new Settings();
$this->activator = new Activator();
$this->cache = new Cache();
$this->admin_columns = new Admin_Columns();
$this->product_migrator = new Product_Migrator();
}

/**
Expand All @@ -98,28 +107,36 @@ public function admin_enqueue_scripts() {
$minimize = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';

wp_register_script(
'wzkb-admin-js',
plugins_url( 'js/admin-scripts' . $minimize . '.js', __FILE__ ),
'wzkb-admin',
plugins_url( "js/admin-scripts{$minimize}.js", __FILE__ ),
array( 'jquery', 'jquery-ui-tabs' ),
WZKB_VERSION,
true
);
wp_localize_script(
'wzkb-admin-js',
'wzkb_admin',
'wzkb-admin',
'WZKBAdminData',
array(
'nonce' => wp_create_nonce( 'wzkb_admin_nonce' ),
'ajax_url' => admin_url( 'admin-ajax.php' ),
'security' => wp_create_nonce( 'wzkb-admin' ),
'strings' => array(
'confirm_message' => esc_html__( 'Are you sure you want to clear the cache?', 'knowledgebase' ),
'success_message' => esc_html__( 'Cache cleared successfully!', 'knowledgebase' ),
'fail_message' => esc_html__( 'Failed to clear cache. Please try again.', 'knowledgebase' ),
'request_fail_message' => esc_html__( 'Request failed: ', 'knowledgebase' ),
),
)
);

wp_register_style(
'wzkb-admin-ui-css',
plugins_url( 'css/admin' . $minimize . '.css', __FILE__ ),
'wzkb-admin-ui',
plugins_url( "css/admin{$minimize}.css", __FILE__ ),
array(),
WZKB_VERSION
);

if ( isset( $_GET['post_type'] ) && 'wz_knowledgebase' === $_GET['post_type'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.NonceVerification.Recommended
wp_enqueue_style( 'wzkb-admin-ui-css' );
wp_enqueue_style( 'wzkb-admin-ui' );
}
}

Expand All @@ -129,23 +146,27 @@ public function admin_enqueue_scripts() {
* @since 2.3.0
*/
public function admin_notices() {
$kbslug = \wzkb_get_option( 'kb_slug', 'not-set-random-string' );
$catslug = \wzkb_get_option( 'category_slug', 'not-set-random-string' );
$tagslug = \wzkb_get_option( 'tag_slug', 'not-set-random-string' );
$kb_slug = \wzkb_get_option( 'kb_slug', 'not-set-random-string' );
$product_slug = \wzkb_get_option( 'product_slug', 'not-set-random-string' );
$cat_slug = \wzkb_get_option( 'category_slug', 'not-set-random-string' );
$tag_slug = \wzkb_get_option( 'tag_slug', 'not-set-random-string' );

// Only add the notice if the user is an admin.
if ( ! current_user_can( 'manage_options' ) ) {
return;
}

// Only add the notice if the settings cannot be found.
if ( 'not-set-random-string' === $kbslug || 'not-set-random-string' === $catslug || 'not-set-random-string' === $tagslug ) {
if ( 'not-set-random-string' === $kb_slug || 'not-set-random-string' === $product_slug || 'not-set-random-string' === $cat_slug || 'not-set-random-string' === $tag_slug ) {
?>
<div class="updated">
<p>
<?php
/* translators: 1. Link to admin page. */
printf( __( 'Knowledge Base settings for the slug have not been registered. Please visit the <a href="%s">admin page</a> to update and save the options.', 'knowledgebase' ), esc_url( admin_url( 'edit.php?post_type=wz_knowledgebase&page=wzkb-settings' ) ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
printf(
/* translators: 1. Link to admin page. */
esc_html__( 'Knowledge Base settings for the slug have not been registered. Please visit the <a href="%s">admin page</a> to update and save the options.', 'knowledgebase' ),
esc_url( admin_url( 'edit.php?post_type=wz_knowledgebase&page=wzkb-settings' ) )
);
?>
</p>
</div>
Expand Down
Loading