Last active
August 29, 2015 14:03
-
-
Save franz-josef-kaiser/086d754ac492f49b78b8 to your computer and use it in GitHub Desktop.
WordPress plugin: Adds a column to the media admin list table to show the count of posts. Also adds a "size" column which displays the duration of Audio files or sizes of image files.
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 | |
namespace WCM; | |
/** | |
* Plugin Name: (WCM) Media Count | |
* Description: Adds a column to the media admin list table to show the count of posts | |
* License: MIT | |
*/ | |
add_filter( 'manage_media_columns', function( $cols, $detached ) | |
{ | |
$cols['count'] = 'Count'; | |
$cols['size'] = 'Size'; | |
return $cols; | |
}, 10, 2 ); | |
add_action( 'manage_media_custom_column', function( $col, $id ) | |
{ | |
switch ( $col ) | |
{ | |
case 'size' : | |
$meta = wp_get_attachment_metadata( $id ); | |
// Image | |
isset( $meta['width'] ) | |
AND print "{$meta['width']} × {$meta['height']}"; | |
// Audio | |
isset( $meta['bitrate'] ) | |
AND print "{$meta['length_formatted']} min"; | |
break; | |
case 'count' : | |
$posts = \WCM\wcm_get_att_use_count( $id ); | |
foreach ( $posts as $post ) | |
edit_post_link( | |
$post->post_title, | |
'<strong>', | |
'</strong><br>', | |
$post->ID | |
); | |
break; | |
} | |
}, 10, 2 ); | |
/** | |
* Retrieve an array of IDs and post titles | |
* of posts where the image is in use | |
* (feat. image and in the post content) | |
* @author SQL Query Stephen Harris | |
* @license CC-BY-SA 3.0 | |
* @param int $id | |
* @return array | |
*/ | |
function wcm_get_att_use_count( $id ) | |
{ | |
global $wpdb; | |
$att = get_post_custom( $id ); | |
$file = $att['_wp_attached_file'][0]; | |
// Do not take full path as different image sizes could | |
// have different month, year folders due to theme and image size changes | |
$name = pathinfo( $file, PATHINFO_FILENAME ); | |
$ext = pathinfo( $file, PATHINFO_EXTENSION ); | |
$sql = <<<SQL | |
select distinct {$wpdb->posts}.ID, {$wpdb->posts}.post_title | |
from {$wpdb->posts} | |
inner join {$wpdb->postmeta} | |
on {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id | |
where | |
post_type not in ( 'attachment', 'revision', 'nav_menu_item' ) | |
and ( {$wpdb->posts}.post_status = 'publish' ) | |
and ( | |
( | |
{$wpdb->postmeta}.meta_key = '_thumbnail_id' | |
and cast({$wpdb->postmeta}.meta_value as char) = '%d' | |
) | |
or ( {$wpdb->posts}.post_content like %s ) | |
) | |
group by {$wpdb->posts}.ID | |
SQL; | |
return $wpdb->get_results( $wpdb->prepare( | |
$sql, | |
$id, | |
"%src=\"%" | |
. like_escape( $name ) | |
. "%" | |
. like_escape( $ext ) | |
. "\"%" | |
) ); | |
} |
@franz-josef-kaiser kind of. Except not as part of an SQL statement, but just regex $post->post_content
when the content is updated. You could then store any image files (or better still, look-up the image ID and store that). Then you'll have a reference of all images included/associated with the post in post meta and query by that.
It makes caching much easier as you can detect when an image is added/removed and regenerate the cache then.
@Giuseppe-Mazzapica agreed. I got lazy and just hard-coded stuff :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@stephenharris @franz-josef-kaiser I think use a post meta on attachment post is a better approach. Only problem there is that on plugin installation, it should check all already existent posts or count will be not available (or 0) for them. Other issues I see here are: