Skip to content

Instantly share code, notes, and snippets.

@retgef
Created October 20, 2012 04:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save retgef/3922050 to your computer and use it in GitHub Desktop.
Save retgef/3922050 to your computer and use it in GitHub Desktop.
WordPress Plugin - Audit Post Attachments
<?php
/*
Plugin Name: Audit Attachments
Description: Reports on a set of post types and their respective attachments
Version: 0.1
Author: Brian Fegter
Author URI: http://coderrr.com/
*/
/**
* Copyright (c) 2012 Brian Fegter. All rights reserved.
*
* Released under the GPL license
* http://www.opensource.org/licenses/gpl-license.php
*
* This is an add-on for WordPress
* http://wordpress.org/
*
* **********************************************************************
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* **********************************************************************
*/
/**
* Usage: http://yourdomain.com/wp-admin/?audit-attachments=true
*/
class Audit_attachments{
# Default Post Types
protected $post_types = array('post', 'page');
/**
* Hook WordPress
* @param array $post_types List of post type slugs
* @return void
*/
public function _construct(Array $post_types = array()){
$this->post_types = $post_types ? $post_types : $this->post_types;
add_action('admin_init' array($this, 'handle_audit'));
}
/**
* Check security then process audit
* @return void - Messages are rendered through output buffering using show_message
*/
public function handle_audit(){
# Allow only admins
if(!current_user_can('manage_options'))
return;
if(!isset($_GET['audit-attachments']) && $_GET['audit-attachments'] == 'true')
return;
foreach($post_types as $post_type){
show_message('<h3>'.ucwords(str_replace('_', ' ', $post_type)).'</h3>');
$this->audit_attachments($post_type);
}
# Don't render the admin area.
exit;
}
/**
* Audit attachments that are children of a specified post type
* @param array $post_type slug
* @return void
*/
protected function audit_attachments($post_type){
global $wpdb;
$sql = $wpdb->prepare("SELECT ID, post_title FROM $wpdb->posts WHERE post_type = '$post_type'");
$posts = $wpdb->get_results($sql);
foreach($posts as $post){
# Query posts from the specified post type
$sql = $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_parent = '$post->ID'");
$count = $wpdb->get_var($sql);
# Send the count message to the screen
$message .= $count ? $count > 1 ? "$count Images" : "$count Image" : '<span style="color:red;">No Images</span>';
$message .= " - $post->post_title";
show_message($message);
# Free Memory
unset($count);
}
}
}
# Specify post types
$post_types = array('post', 'page');
# Instantiate
new AuditAttachments($post_types);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment