Created
October 6, 2017 09:15
-
-
Save wpmudev-sls/b05ea300518c000a266ad9eb5616a259 to your computer and use it in GitHub Desktop.
[MarketPress] - Admin Products List. Adds a select option "No category" in admin products table. This should display all products that have no product category set.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: [MarketPress] - Admin Products List | |
* Plugin URI: https://premium.wpmudev.org/ | |
* Description: Adds a select option "No category" in admin products table. This should display all products that have no product category set. | |
* Author: Panos Lyrakis @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org/ | |
* License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
if ( ! class_exists( 'WPMUDEV_MP_Admin_List_Table' ) ) { | |
class WPMUDEV_MP_Admin_List_Table { | |
private static $_instance = null; | |
private static $_products_taxonomy = null; | |
public static function get_instance() { | |
if( is_null( self::$_instance ) ){ | |
self::$_instance = new WPMUDEV_MP_Admin_List_Table(); | |
} | |
return self::$_instance; | |
} | |
private function __construct() { | |
self::$_products_taxonomy = 'product_category'; | |
add_filter( 'wp_dropdown_cats', array( $this, 'no_term_option' ) ); | |
add_action( 'parse_query', array( $this, 'no_term_filter' ), 999 ); | |
} | |
public function no_term_option( $html ){ | |
global $pagenow, $wp_query; | |
$qv = &$wp_query->query_vars; | |
if ( ( 'edit.php' === $pagenow ) && ( 'product' === $qv['post_type'] ) ) { | |
$html = str_replace( '</select>', '<option value="none" '. selected( $_GET['cat'], 'none', false ) .'>No category</option></select>', $html ); | |
} | |
return $html; | |
} | |
public function no_term_filter( $query ){ | |
global $pagenow; | |
$qv = &$query->query_vars; | |
//$qv['cat'] has been unset in class-mp-products-admin.php. Lets use $_GET instead | |
if ( ( 'edit.php' === $pagenow ) && ( 'product' === $qv['post_type'] ) && ( ! empty( $_GET['cat'] ) ) && ( $_GET['cat'] == 'none' ) ) { | |
$qv['tax_query'] = array( | |
array( | |
'taxonomy' => self::$_products_taxonomy, | |
'field' => 'term_id', | |
'terms' => get_terms( self::$_products_taxonomy, [ 'fields' => 'ids' ] ), | |
'operator' => 'NOT IN' | |
), | |
); | |
} | |
} | |
} | |
add_action( 'plugins_loaded', function(){ | |
$GLOBALS['WPMUDEV_MP_Admin_List_Table'] = WPMUDEV_MP_Admin_List_Table::get_instance(); | |
}, 10 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment