Commit 90ec7300 authored by John James Jacoby's avatar John James Jacoby
Browse files

Fix search incompatibility, and improve performance of `no_category` filtering.

Bump to 1.0.1.
parent 78c7f058
......@@ -3,7 +3,7 @@ Contributors: johnjamesjacoby, stuttter
Tags: media, library, attachment, category, categories, organizer, management, files
Requires at least: 4.6
Tested up to: 4.7
Stable tag: 1.0.0
Stable tag: 1.0.1
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=9Q4F4EL5YJ62J
......@@ -57,6 +57,10 @@ http://github.com/stuttter/wp-media-categories/
== Changelog ==
= [1.0.1] - 2016-09-06 =
* Fix theme-side search interference
* Improve performance of "no_category" filtering
= [1.0.0] - 2016-09-06 =
* Fix bulk edits
* Move files into subdirectory for improved mu-plugins support
......
......@@ -10,7 +10,7 @@
* Text Domain: wp-media-categories
* Domain Path: /wp-media-categories/lang
* Description: Categories for media & attachments
* Version: 1.0.0
* Version: 1.0.1
*/
// Exit if accessed directly
......@@ -56,5 +56,5 @@ function wp_media_categories_get_plugin_url() {
* @return int
*/
function wp_media_categories_get_asset_version() {
return 201609060001;
return 201609060002;
}
......@@ -14,7 +14,7 @@ defined( 'ABSPATH' ) || exit;
*
* @since 0.1.0
*/
function wp_media_categories_update_count_callback( $terms = array(), $media_taxonomy = 'category' ) {
function wp_media_categories_update_count_callback( $terms = array(), $media_taxonomy = 'media_category' ) {
global $wpdb;
// select id & count from taxonomy
......@@ -35,88 +35,20 @@ function wp_media_categories_update_count_callback( $terms = array(), $media_tax
// update all count values from taxonomy
foreach ( $count as $row_count ) {
$wpdb->update( $wpdb->term_taxonomy, array( 'count' => $row_count->total ), array( 'term_taxonomy_id' => $row_count->term_taxonomy_id ) );
$wpdb->update(
$wpdb->term_taxonomy,
array( 'count' => $row_count->total ),
array( 'term_taxonomy_id' => $row_count->term_taxonomy_id )
);
}
}
/**
* Add values to query vars to extend the query
*
* @since 0.1.0
*/
function wp_media_categories_query_vars_add_values( $query_vars = '', $values_to_add = '' ) {
// Make input into array
$new_query_vars = $query_vars;
$new_values_to_add = $values_to_add;
if ( ! is_array( $query_vars ) ) {
$new_query_vars = array( $query_vars );
}
if ( ! is_array( $values_to_add ) ) {
$new_values_to_add = array( $values_to_add );
}
// Merge inputs to return
return array_merge( $new_query_vars, $new_values_to_add );
}
/**
* Get posts for media taxonomy
*
* @since 0.1.0
*
* @global WPDB $wpdb
*
* @param string $taxonomy
*
* @return array
*/
function wp_media_categories_get_posts_for_media_taxonomy( $taxonomy = '' ) {
global $wpdb;
// Validate input
if ( empty( $taxonomy ) ) {
return array();
}
// Get the terms for this taxonomy
$sql = "SELECT * FROM {$wpdb->term_taxonomy} AS tt WHERE tt.taxonomy = %s";
$prepare = $wpdb->prepare( $sql, $taxonomy );
$terms = $wpdb->get_results( $prepare );
// Validate $terms found
if ( is_wp_error( $terms ) || (count( $terms ) == 0 ) ) {
return array();
}
// Create a list of taxonomyTermIDs to be used for the query
$term_ids = array();
foreach ( $terms as $term ) {
$term_ids[] = $term->term_taxonomy_id;
}
$term_ids = implode( ',', $term_ids );
$query = "SELECT $wpdb->posts.* FROM $wpdb->posts ";
$query .= " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) ";
$query .= " WHERE 1=1 ";
$query .= " AND $wpdb->posts.post_type = 'attachment' ";
$query .= " AND ($wpdb->term_relationships.term_taxonomy_id IN ($term_ids)) ";
$query .= " GROUP BY $wpdb->posts.ID";
$taxonomyPosts = $wpdb->get_results( $query );
return $taxonomyPosts;
}
/**
* Get the options to determine the list of media_category
*
* @since 0.1.0
*/
function wp_media_categories_get_media_category_options( $selected_value = '') {
function wp_media_categories_get_media_category_options( $selected_value = '' ) {
return array(
'taxonomy' => 'media_category',
'name' => 'media_category',
......@@ -134,30 +66,36 @@ function wp_media_categories_get_media_category_options( $selected_value = '') {
}
/**
* Implement the request to filter media without category
* Manipulate the request to filter media without category
*
* @since 0.1.0
* @since 1.0.1
*/
function wp_media_categories_request( $query_args = array() ) {
function wp_media_categories_no_category_request( $query_args = array() ) {
// Bail if not in admin
if ( ! is_admin() ) {
return $query_args;
}
// No category?
$media_category = wp_media_categories_get_no_category_search();
// Cuz I'm searchin...
if ( ! empty( $media_category ) ) {
// Find all posts for the current mediaCategory to use for filtering them out
$all_attachments = wp_media_categories_get_posts_for_media_taxonomy( $media_category );
// Post not in?
$post_not_in = array();
foreach ( $all_attachments as $key => $val) {
$post_not_in[] = $val->ID;
if ( ! empty( $media_category ) && ! empty( $query_args[ $media_category ] ) ) {
// No categories, so do a "NOT EXISTS" taxonomy query
if ( 'no_category' === $query_args[ $media_category ] ) {
$query_args['tax_query'] = array(
array(
'taxonomy' => $media_category,
'operator' => 'NOT EXISTS',
'include_children' => false
)
);
// Nullify the query argument to prevent incorrect core assertions
$query_args[ $media_category ] = null;
}
$query_args['post__not_in'] = $post_not_in;
// Reset the search query parameters to search for all attachments
$query_args[ $media_category ] = 0;
}
return $query_args;
......@@ -170,6 +108,7 @@ function wp_media_categories_request( $query_args = array() ) {
*/
function wp_media_categories_get_no_category_search() {
// Default return value
$search = '';
// Check for correct Filter situation
......@@ -204,48 +143,31 @@ function wp_media_categories_get_no_category_search() {
*/
function wp_media_categories_pre_get_posts( WP_Query $query ) {
// Only perform search on non-admin pages
// Bail if in admin
if ( is_admin() ) {
return;
}
// Check whether this is the main query
// Bail if not main query
if ( ! $query->is_main_query() ) {
return;
}
// Handle query if it is used for media is_archive
// Bail if not media_category query
if ( ! is_tax( 'media_category' ) ) {
return;
}
// Looking at some kind of media_category term archive
if ( is_archive() ) {
// Get media taxonomy and categories to find
// Get media taxonomy and categories to find, default to __not_found
$media_categories = $query->get( 'media_category', '__not_found' );
// Check categories to find
// Looking at a specific media category
if ( '__not_found' !== $media_categories ) {
$query->set( 'post_type', 'attachment' );
$query->set( 'post_status', 'inherit' );
}
}
// Add media for search when desired
if ( is_search() ) {
// Add attachment to post_type
$query_post_type = $query->get( 'post_type', '__not_found' );
if ( '__not_found' !== $query_post_type ) {
$query_post_type = 'post';
}
$query_post_type = wp_media_categories_query_vars_add_values( $query_post_type, 'attachment' );
$query->set( 'post_type', $query_post_type);
// Add inherit to post_status
$query_post_status = $query->get( 'post_status', '__not_found' );
if ( '__not_found' !== $query_post_status ) {
$query_post_status = 'publish';
}
$query_post_status = wp_media_categories_query_vars_add_values( $query_post_status, 'inherit' );
$query->set( 'post_status', $query_post_status );
}
}
......@@ -28,5 +28,9 @@ add_action( 'wp_ajax_query-attachments', 'wp_media_categories_ajax_query_attachm
// Some filters and action to process categories
add_action( 'restrict_manage_posts', 'wp_media_categories_restrict_manage_posts' );
add_filter( 'request', 'wp_media_categories_request' );
add_filter( 'pre_get_posts', 'wp_media_categories_pre_get_posts' );
// Filter for `no_category` media category attachments list-table requests
add_filter( 'request', 'wp_media_categories_no_category_request' );
// Filter theme-side media category queries
add_action( 'pre_get_posts', 'wp_media_categories_pre_get_posts' );
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment