__construct();
}
function __construct(){
//add thickbox
add_action('wp_head', array(&$this,'enqueue_thickbox'),1);
//add the image to the feed
add_action('the_content',array(&$this,'add_image_to_feed'));
//add fields to the media manager to select default categories
add_filter('attachment_fields_to_edit', array(&$this,'add_category_image_field_to_edit'), 11, 2);
add_filter('attachment_fields_to_save', array(&$this,'save_category_image_field'), 9, 2);
}
/**
* Tells WordPress to loac the javascript and CSS required for thickbox
*/
function enqueue_thickbox() {
wp_enqueue_script('thickbox');
wp_enqueue_style('thickbox');
}
/**
* Adds a thumbnail post image, with a link to the post, to the feed
* @param String $content The post content
* @return String The amended post content
*/
function add_image_to_feed($content){
if (is_feed()){
$content = $this->the_image('thumbnail' , 'feed-image',true,false,false) . $content;
}
return $content;
}
/**
* Gets an image to display for the post
* @global Post $post The post obkect
* @param String $size thumbnail|medium|large|full-size
* @param String $class A class to add to the image declaration
* @param Boolean $post_link Link the image to the post or not
* @param Boolean $thickbox Use Thickbox to link to the large image - overrides post link if true
* @param Boolean $echo Whether to echo or return. Echos if true
* @return String The image, or linked image, if not echod.
*/
function the_image($size = 'medium' , $class = '' , $post_link = false , $thickbox = false, $echo = true){
global $post;
//setup the attachment array
$att_array = array(
'post_parent' => $post->ID,
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order_by' => 'menu_order'
);
//get the post attachments
$attachments = get_children($att_array);
//decide between attachments and defaults
if ( is_array($attachments) ){
foreach($attachments as $att){
if ( $att->menu_order == 0){
$image_src_array = wp_get_attachment_image_src($att->ID, $size);
//1 and 2 are the x and y dimensions
$image_src = $image_src_array[0];
$image_id = $att->ID;
$image_caption = $att->post_excerpt;
}
}
} elseif ($dia = get_option('theme-default_post_images')) {
foreach($dia as $cat => $image){
if ( in_category($cat) ){
$image_post = get_post($image);
$image_src_array = wp_get_attachment_image_src($image_post->ID, $size);
//1 and 2 are the x and y dimensions
$image_src = $image_src_array[0];
$image_id = $image_post->ID;
$image_caption = $image_post->post_excerpt;
//only get the first category (the earliest category set)
break;
}
}
}
//if the image details haven't been set then return false
if (!isset($image_id)){return false;}
//sort out the html
if ($post_link){
//if post_link is set then we want to link the image to the post
$image_html = '';
//combine the data
$post_link = get_permalink($post->ID);
$html = sprintf($image_html,$post_link,$post->post_title,$image_src,$image_caption,$class);
} elseif ($thickbox) {
//if thickbox then we want to provide a link to the fullsize version
//if post_link is set then we want to link the image to the post
$image_html = '';
//combine the data
$image_src_array = wp_get_attachment_image_src($image_id, 'full-size');
//1 and 2 are the x and y dimensions
$post_link = $image_src_array[0];
$html = sprintf($image_html,$post_link,$post->post_title,$image_src,$image_caption,$class);
} else {
$image_html = '';
//combine the data
$html = sprintf($image_html,$image_src,$image_caption,$class);
}
//output the result
if ($echo){
echo $html;
}
return $html;
}
/**
* Adds category checkboxes to media manager page
* @param Array $fields an Array of fields to allow editing in the media manager
* @param Post $att The current post item (i.e. the attachment)
* @return Array modified array of fields
*/
function add_category_image_field_to_edit($fields,$att){
//get all the categories
$defaults = array('type' => 'post',
'child_of' => 0,
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => false,
'include_last_update_time' => false,
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'number' => '',
'pad_counts' => false);
$categories = get_categories($defaults);
//get the saved settings
//dia = default_image_array
$dia = get_option('theme-default_post_images');
//create the html
$html = '';
//create the ID and name
$idn = 'attachments['.$att->ID.'][post_category][]';
//create the options
$options = array();
foreach($categories as $cat){
$o_template = '';
//is it selected
if (isset($dia[$cat->cat_ID]) && $dia[$cat->cat_ID] == $att->ID ){
$selected = 'checked="checked"';
} else {
$selected = '';
}
//combine
$options[] = sprintf($o_template,$idn,$cat->cat_ID,$selected,$cat->cat_name);
}
//combine all the html
$html = implode("\n",$options);
//add the new field
$fields["post_category"] = array(
"label"=>'category',
"value"=>'3',
"input"=>'html',
"html"=>$html);
return $fields;
}
/**
* Saves the selected categories into an option
* @param Array $post An array of the current attachment details
* @param Array $att An array of the posted form details
* @return Post unaltered.
*/
function save_category_image_field($post,$att){
//make sure the posted values exist
if ( !is_array($att['post_category']) ){
$att['post_category'] = array();
}
//get the option
$default_image_array = get_option('theme-default_post_images');
//make sure it is an array
if (!is_array($default_image_array)) {$default_image_array = array();}
//unset any values for this post
foreach( $default_image_array as $dc => $di ){
if ( $di == $post['ID'] ){
if ( !in_array($dc,$att['post_category']) ){
unset($default_image_array[$dc]);
}
}
}
//set any selected values
foreach( $att['post_category'] as $attcat ){
$default_image_array[$attcat] = $post['ID'];
}
//update the options
if ( is_array(get_option('theme-default_post_images')) ){
update_option('theme-default_post_images',$default_image_array);
} else {
add_option('theme-default_post_images',$default_image_array , 'no' , 'yes');
}
return $post;
}
}
//instantiate the object
$post_image = new post_image();
function the_image($size = 'medium' , $class = '' , $post_link = false , $thickbox = false, $echo = true){
global $post_image;
return $post_image->the_image($size , $class , $post_link , $thickbox, $echo);
}
?>