Commit 1db82a91 authored by John James Jacoby's avatar John James Jacoby
Browse files

Initial commit.

parents
/* Media Modal page */
.compat-item .field input[type="checkbox"] {
width: auto;
}
.term-list {
background-color: #fff;
border: 1px solid #dfdfdf;
border-radius: 3px;
padding: 10px 10px 5px;
margin: 0 0 10px;
}
.term-list li {
line-height: 22px;
}
.term-list li input[type="checkbox"] {
margin: -4px 4px 0 0 !important;
}
.term-list .children {
margin: 5px 0 0 20px;
}
/* Fix display attachment-filters */
.media-frame select.attachment-filters {
max-width: 30%;
}
\ No newline at end of file
/* global _, filters, wp_media_categories_taxonomies */
window.wp = window.wp || { };
( function ( $ ) {
var media = wp.media,
curAttachmentsBrowser = media.view.AttachmentsBrowser;
media.view.AttachmentFilters.Taxonomy = media.view.AttachmentFilters.extend( {
tagName: 'select',
createFilters: function () {
var filters = { },
that = this;
_.each( that.options.termList || { }, function ( term, key ) {
var term_id = term['term_id'],
term_name = $( '<div/>' ).html( term['term_name'] ).text();
filters[ term_id ] = {
text: term_name,
priority: key + 2
};
filters[term_id]['props'] = { };
filters[term_id]['props'][that.options.taxonomy] = term_id;
} );
filters.all = {
text: that.options.termListTitle,
priority: 1
};
filters['all']['props'] = { };
filters['all']['props'][that.options.taxonomy] = null;
this.filters = filters;
}
} );
media.view.AttachmentsBrowser = media.view.AttachmentsBrowser.extend( {
createToolbar: function () {
var that = this,
i = 1,
filters = that.options.filters;
curAttachmentsBrowser.prototype.createToolbar.apply( this, arguments );
$.each( wp_media_categories_taxonomies, function ( taxonomy, values ) {
if ( values.term_list && filters ) {
that.toolbar.set( taxonomy + '-filter', new media.view.AttachmentFilters.Taxonomy( {
controller: that.controller,
model: that.collection.props,
priority: -80 + 10 * i++,
taxonomy: taxonomy,
termList: values.term_list,
termListTitle: values.list_title,
className: 'attachment-filters'
} ).render() );
}
} );
}
} );
} )( jQuery );
\ No newline at end of file
<?php
/**
* Media Categories Admin
*
* @package Media/Categories/Admin
*/
// Exit if accessed directly
defined( 'ABSPATH' ) || exit;
/**
* Enqueue admin scripts and styles
*/
function wp_media_categories_enqueue_admin_scripts() {
global $pagenow;
// Asset management
$url = wp_media_categories_get_plugin_url();
$ver = wp_media_categories_get_asset_version();
// Media editor
if ( wp_script_is( 'media-editor' ) && ( ( 'upload.php' == $pagenow ) || ( 'post.php' == $pagenow ) || ( 'post-new.php' == $pagenow ) ) ) {
// Dropdown
$attachment_terms = wp_dropdown_categories( array(
'taxonomy' => 'media_category',
'hide_empty' => false,
'hierarchical' => true,
'orderby' => 'name',
'show_count' => true,
'walker' => new WP_Media_Categories_Media_Grid_Walker(),
'value' => 'id',
'echo' => false
) );
// No select
$attachment_terms = preg_replace( array( '/<select([^>]*)>/', '/<\/select>/' ), '', $attachment_terms );
// Add an attachment_terms for No category
$no_categories = __( 'No categories', 'wp-media-categories' );
$all_categories = __( 'View all categories', 'wp-media-categories' );
$no_category_term = ' ,{"term_id":"' . 'no_category' . '","term_name":"' . $no_categories . '"}';
$attachment_terms = $no_category_term . substr( $attachment_terms, 1 );
echo '<script type="text/javascript">';
echo '/* <![CDATA[ */';
echo 'var wp_media_categories_taxonomies = {"' . 'media_category' . '":';
echo '{"list_title":"' . html_entity_decode( $all_categories, ENT_QUOTES, 'UTF-8' ) . '",';
echo '"term_list":[' . substr( $attachment_terms, 2 ) . ']}};';
echo '/* ]]> */';
echo '</script>';
// Script
wp_enqueue_script( 'wp-media-categories-media-views', $url . '/assets/js/media-views.js', array( 'media-views' ), $ver, true );
}
// Styling
wp_enqueue_style( 'wp-media-categories-styling', $url . '/assets/css/admin.css', array(), $ver );
}
/**
* Add a category filter
*
* @since 0.1.0
*/
function wp_media_categories_add_category_filter() {
global $pagenow;
// Bail if not upload page
if ( 'upload.php' !== $pagenow ) {
return;
}
// Get media taxonomy
$selected_value = isset( $_GET[ 'term' ] )
? $_GET[ 'term' ]
: ''; ?>
<label for="media_category" class="screen-reader-text"><?php esc_html_e( 'Filter by Category', 'wp-media-categories' ); ?></label>
<?php
$dropdown_options = wp_media_categories_get_media_category_options( $selected_value );
wp_dropdown_categories( $dropdown_options );
}
/**
* Add a filter for restrict_manage_posts for categories
*
* @since 0.1.0
*/
function wp_media_categories_restrict_manage_posts() {
wp_media_categories_add_category_filter();
}
function wp_media_categories_create_sendback_url() {
// Create a sendback url to report the results
$sendback = remove_query_arg( array( 'exported', 'untrashed', 'deleted', 'ids' ), wp_get_referer() );
if ( empty( $sendback ) || ( false === strpos( wp_get_referer(), 'upload.php' ) ) ) {
$sendback = admin_url( "upload.php" );
}
// Remove some superfluous arguments
$sendback = remove_query_arg( array( 'action', 'action2', 'tags_input', 'post_author', 'comment_status', 'ping_status', '_status', 'post', 'bulk_edit', 'post_view' ), $sendback );
// Pagenumber
$pagenum = isset( $_REQUEST[ 'paged' ] ) ? absint( $_REQUEST[ 'paged' ] ) : 0;
$sendback = add_query_arg( 'paged', $pagenum, $sendback );
// Orderby
if ( isset( $_REQUEST[ 'orderby' ] ) ) {
$sOrderby = $_REQUEST[ 'orderby' ];
$sendback = add_query_arg( 'orderby', $sOrderby, $sendback );
}
// Order
if ( isset( $_REQUEST[ 'order' ] ) ) {
$sOrder = $_REQUEST[ 'order' ];
$sendback = add_query_arg( 'order', $sOrder, $sendback );
}
// Filters
if ( isset( $_REQUEST[ 'mode' ] ) ) {
$sMode = $_REQUEST[ 'mode' ];
$sendback = add_query_arg( 'mode', $sMode, $sendback );
}
if ( isset( $_REQUEST[ 'mode' ] ) ) {
$sMode = $_REQUEST[ 'mode' ];
$sendback = add_query_arg( 'mode', $sMode, $sendback );
}
if ( isset( $_REQUEST[ 'm' ] ) ) {
$sM = $_REQUEST[ 'm' ];
$sendback = add_query_arg( 'm', $sM, $sendback );
}
if ( isset( $_REQUEST[ 's' ] ) ) {
$sS = $_REQUEST[ 's' ];
$sendback = add_query_arg( 's', $sS, $sendback );
}
if ( isset( $_REQUEST[ 'attachment-filter' ] ) ) {
$sAttachmentFilter = $_REQUEST[ 'attachment-filter' ];
$sendback = add_query_arg( 'attachment-filter', $sAttachmentFilter, $sendback );
}
if ( isset( $_REQUEST[ 'filter_action' ] ) ) {
$sFilterAction = $_REQUEST[ 'filter_action' ];
$sendback = add_query_arg( 'filter_action', $sFilterAction, $sendback );
}
// Get media taxonomy
if ( isset( $_REQUEST[ 'media_category' ] ) ) {
$sMediaTaxonomy = $_REQUEST[ 'media_category' ];
$sendback = add_query_arg( 'media_category', $sMediaTaxonomy, $sendback );
}
return $sendback;
}
/**
* Get an array of term values, which type is determined by the parameter
*
* @since 0.1.0
*/
function wp_media_categories_get_terms_values( $keys = 'ids' ) {
// Get media taxonomy
$media_terms = get_terms( 'media_category', array(
'hide_empty' => 0,
'fields' => 'id=>slug',
) );
$media_values = array();
foreach ( $media_terms as $key => $value ) {
$media_values[] = ( $keys === 'ids' )
? $key
: $value;
}
return $media_values;
}
/**
* Check the current action selected from the bulk actions dropdown.
*
* @since 3.1.0
*
* @return bool Whether 'bulk_toggle' was selected or not
*/
function wp_media_categories_is_action_bulk_toggle() {
if ( isset( $_REQUEST[ 'action' ] ) && ( 'bulk_toggle' === $_REQUEST[ 'action' ] ) ) {
return true;
}
if ( isset( $_REQUEST[ 'action2' ] ) && ( 'bulk_toggle' === $_REQUEST[ 'action2' ] ) ) {
return true;
}
return false;
}
/**
* For Media Category Management, the actual category should be used
*
* @since 0.1.0
*/
function wp_media_categories_custom_bulk_admin_footer() {
global $post_type;
// Make an array of post_type
if ( is_array( $post_type ) ) {
$wp_media_categories_post_type = $post_type;
} else {
$wp_media_categories_post_type = array();
$wp_media_categories_post_type[] = $post_type;
}
// Check whether the post_type array contains attachment
if ( in_array( 'attachment', $wp_media_categories_post_type ) ) {
// Get media taxonomy and corresponding terms
$media_terms = get_terms( 'media_category', array(
'hide_empty' => '0'
) );
// If terms found ok then generate the additional bulk_actions
if ( ! empty( $media_terms ) && ! is_wp_error( $media_terms ) ) {
// Create the box div string.
$onChangeTxtTop = "jQuery(\'#bulk_tax_id\').val(jQuery(\'#bulk-action-selector-top option:selected\').attr(\'option_slug\'));";
$onChangeTxtBottom = "jQuery(\'#bulk_tax_id\').val(jQuery(\'#bulk-action-selector-bottom option:selected\').attr(\'option_slug\'));";
// Start the script to add bulk code
$wp_media_categories_footer_script = "";
$wp_media_categories_footer_script .= " <script type=\"text/javascript\">";
$wp_media_categories_footer_script .= "jQuery(document).ready(function(){";
// Add new hidden field to store the term_slug
$wp_media_categories_footer_script .= "jQuery('#posts-filter').prepend('<input type=\"hidden\" id=\"bulk_tax_cat\" name=\"bulk_tax_cat\" value=\"" . 'media_category' . "\" />');";
$wp_media_categories_footer_script .= "jQuery('#posts-filter').prepend('<input type=\"hidden\" id=\"bulk_tax_id\" name=\"bulk_tax_id\" value=\"\" />');";
// Add new action to #bulk-action-selector-top
$wp_media_categories_footer_script .= "jQuery('#bulk-action-selector-top')";
$wp_media_categories_footer_script .= ".attr('onChange','" . $onChangeTxtTop . "')";
// $wp_media_categories_footer_script .= ".attr('onClick','" . $onChangeTxt . "')";
$wp_media_categories_footer_script .= ";";
// Add new action to #bulk-action-selector-bottom
$wp_media_categories_footer_script .= "jQuery('#bulk-action-selector-bottom')";
$wp_media_categories_footer_script .= ".attr('onChange','" . $onChangeTxtBottom . "')";
// $wp_media_categories_footer_script .= ".attr('onClick','" . $onChangeTxt . "')";
$wp_media_categories_footer_script .= ";";
// add bulk_actions for each category term
foreach ( $media_terms as $term ) {
$optionTxt = esc_js( __( 'Toggle', 'wp-media-categories' ) . ' ' . $term->name );
$wp_media_categories_footer_script .= " jQuery('<option>').val('" . 'bulk_toggle' . "').attr('option_slug','" . $term->slug . "').text('" . $optionTxt . "').appendTo(\"select[name='action']\");";
$wp_media_categories_footer_script .= " jQuery('<option>').val('" . 'bulk_toggle' . "').attr('option_slug','" . $term->slug . "').text('" . $optionTxt . "').appendTo(\"select[name='action2']\");";
}
$wp_media_categories_footer_script .= '});';
$wp_media_categories_footer_script .= '</script>';
echo $wp_media_categories_footer_script;
}
}
}
/**
* Handle the custom Bulk Action
*
* @since 0.1.0
*/
function wp_media_categories_custom_bulk_action() {
// Check parameters provided
if ( ! isset( $_REQUEST[ 'bulk_tax_cat' ] ) ) {
return;
}
if ( ! isset( $_REQUEST[ 'bulk_tax_id' ] ) ) {
return;
}
if ( ! isset( $_REQUEST[ 'media' ] ) ) {
return;
}
if ( ! wp_media_categories_is_action_bulk_toggle() ) {
return;
}
// Set some variables
$num_bulk_toggled = 0;
$media_taxonomy = sanitize_key( $_REQUEST[ 'bulk_tax_cat' ] );
$bulk_media_category_id = (int) $_REQUEST[ 'bulk_tax_id' ];
// Process all media_id s found in the request
foreach ( ( array ) $_REQUEST[ 'media' ] as $media_id ) {
$media_id = ( int ) $media_id;
// Check whether this user can edit this post
if ( ! current_user_can( 'edit_post', $media_id ) ) {
continue;
}
// Set so remove the $bulk_media_category taxonomy from this media post
if ( has_term( $bulk_media_category_id, $media_taxonomy, $media_id ) ) {
$bulk_result = wp_remove_object_terms( $media_id, $bulk_media_category_id, $media_taxonomy );
// Not set so add the $bulk_media_category taxonomy to this media post
} else {
$bulk_result = wp_set_object_terms( $media_id, $bulk_media_category_id, $media_taxonomy, true );
}
if ( is_wp_error( $bulk_result ) ) {
return $bulk_result;
}
// Keep track of the number toggled
$num_bulk_toggled++;
}
// Create a sendback url to refresh the screen and report the results
$sendback = wp_media_categories_create_sendback_url();
$sendback = add_query_arg( array( 'bulk_toggled' => $num_bulk_toggled ), $sendback );
wp_redirect( $sendback );
exit();
}
/**
* Display an admin notice on the Posts page after exporting
*
* @since 0.1.0
*/
function wp_media_categories_custom_bulk_admin_notices() {
global $pagenow;
if ( ( 'upload.php' === $pagenow ) && ! empty( $_REQUEST[ 'bulk_toggled' ] ) ) {
$message = sprintf( _n( 'Media bulk toggled.', '%s media bulk toggled.', $_REQUEST[ 'bulk_toggled' ], 'wp-media-categories' ), number_format_i18n( $_REQUEST[ 'bulk_toggled' ] ) );
echo "<div class=\"updated\"><p>{$message}</p></div>";
}
}
/**
* Handle default category of attachments without category
*
* @since 0.1.0
*/
function wp_media_categories_set_attachment_category( $post_ID ) {
// Check whether this user can edit this post
if ( ! current_user_can( 'edit_post', $post_ID ) ) {
return;
}
// Only add default if attachment doesn't have categories
if ( ! wp_get_object_terms( $post_ID, 'media_category' ) ) {
// Get the default value
$default_category = get_option( 'default_media_category', 'uncategorized' );
// Check for valid $default_category
if ( 'uncategorized' !== $default_category ) {
// Not set so add the $media_category taxonomy to this media post
$add_result = wp_set_object_terms( $post_ID, $default_category, 'media_category', true );
// Check for error
if ( is_wp_error( $add_result ) ) {
return $add_result;
}
}
}
}
<?php
/**
* Changing categories in the grid view
*
* @since 0.1.0
*/
function wp_media_categories_ajax_query_attachments() {
// Bail if user cannot upload files
if ( ! current_user_can( 'upload_files' ) ) {
wp_send_json_error();
}
// Get names of media taxonomies
$taxonomies = get_object_taxonomies( 'attachment', 'names' );
// Look for query
$query = isset( $_REQUEST[ 'query' ] )
? (array) $_REQUEST[ 'query' ]
: array();
// Default arguments
$defaults = array(
's', 'order', 'orderby', 'posts_per_page', 'paged', 'post_mime_type',
'post_parent', 'post__in', 'post__not_in'
);
$query = array_intersect_key( $query, array_flip( array_merge( $defaults, $taxonomies ) ) );
$query[ 'post_type' ] = 'attachment';
$query[ 'post_status' ] = 'inherit';
if ( current_user_can( get_post_type_object( 'attachment' )->cap->read_private_posts ) ) {
$query[ 'post_status' ] .= ',private';
}
$query[ 'tax_query' ] = array( 'relation' => 'AND' );
foreach ( $taxonomies as $taxonomy ) {
if ( isset( $query[ $taxonomy ] ) ) {
// Filter a specific category
if ( is_numeric( $query[ $taxonomy ] ) ) {
array_push( $query[ 'tax_query' ], array(
'taxonomy' => $taxonomy,
'field' => 'id',
'terms' => $query[ $taxonomy ]
) );
}
// Filter No category
if ( $query[ $taxonomy ] == 'no_category' ) {
$all_terms_ids = wp_media_categories_get_terms_values( 'ids' );
array_push( $query[ 'tax_query' ], array(
'taxonomy' => $taxonomy,
'field' => 'id',
'terms' => $all_terms_ids,
'operator' => 'NOT IN',
) );
}
}
unset( $query[ $taxonomy ] );
}
$query = apply_filters( 'ajax_query_attachments_args', $query );
$query = new WP_Query( $query );
$posts = array_map( 'wp_prepare_attachment_for_js', $query->posts );
$posts = array_filter( $posts );
wp_send_json_success( $posts );
}
<?php
/**
* Media Categories Functions
*
* @package Media/Categories/Functions
*/
// Exit if accessed directly
defined( 'ABSPATH' ) || exit;
/**
* Custom update_count_callback
*
* @since 0.1.0
*/
function wp_media_categories_update_count_callback( $terms = array(), $media_taxonomy = 'category' ) {
global $wpdb;
// select id & count from taxonomy
$sql = "SELECT term_taxonomy_id, MAX(total) AS total FROM ((
SELECT tt.term_taxonomy_id, COUNT(*) AS total
FROM {$wpdb->term_relationships} tr, {$wpdb->term_taxonomy} tt
WHERE tr.term_taxonomy_id = tt.term_taxonomy_id
AND tt.taxonomy = %s
GROUP BY tt.term_taxonomy_id
) UNION ALL (
SELECT term_taxonomy_id, 0 AS total
FROM {$wpdb->term_taxonomy}
WHERE taxonomy = %s
)) AS unioncount GROUP BY term_taxonomy_id";
$prepared = $wpdb->prepare( $sql, 'media_category', 'media_category' );
$count = $wpdb->get_results( $prepared );
// 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 ) );
}
}
/**
* 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