';
echo ' var attachments_base = "' . WP_PLUGIN_URL . '/attachments"; ';
echo ' var attachments_media = ""; ';
echo ' Shadowbox.init({ skipSetup:true, onClose:attachments_update }); ';
echo '';
}
/**
* Fired when Post or Page is saved. Serializes all attachment data and saves to post_meta
*
* @param int $post_id The ID of the current post
* @return void
* @author Jonathan Christopher
*/
function attachments_save($post_id)
{
// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if ( !wp_verify_nonce( $_POST['attachments_nonce'], plugin_basename(__FILE__) )) {
return $post_id;
}
// verify if this is an auto save routine. If it is our form has not been submitted, so we dont want
// to do anything
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return $post_id;
// Check permissions
if ( 'page' == $_POST['post_type'] ) {
if ( !current_user_can( 'edit_page', $post_id ) )
return $post_id;
} else {
if ( !current_user_can( 'edit_post', $post_id ) )
return $post_id;
}
// OK, we're authenticated: we need to find and save the data
$total_attachments = 0;
// We'll build our array of attachments
foreach($_POST as $key => $data) {
// Arbitrarily using the location input
if( substr($key, 0, 20) == 'attachment_location_' )
{
$total_attachments++;
}
// If we have attachments, there's work to do
if( $total_attachments > 0 )
{
$attachments_data = array();
for ($i=1; $i <= $total_attachments; $i++)
{
if( !empty($_POST['attachment_location_' . $i]) )
{
$attachment_details = array(
'title' => $_POST['attachment_title_' . $i],
'caption' => $_POST['attachment_caption_' . $i],
'name' => $_POST['attachment_name_' . $i],
'location' => $_POST['attachment_location_' . $i],
'mime' => $_POST['attachment_mime_' . $i],
'id' => $_POST['attachment_id_' . $i],
'order' => $_POST['attachment_order_' . $i]
);
array_push($attachments_data, $attachment_details);
}
}
}
}
// Serialization goodness
$attachments_serialized = serialize($attachments_data);
update_post_meta($post_id, '_attachments', $attachments_serialized);
}
/**
* Retrieves all Attachments for provided Post or Page
*
* @param int $post_id (optional) ID of target Post or Page, otherwise pulls from global $post
* @return array $post_attachments
* @author Jonathan Christopher
*/
function attachments_get_attachments($post_id=null)
{
global $post;
if($post_id==null)
{
$post_id = $post->ID;
}
$existing_attachments = unserialize(get_post_meta($post_id, '_attachments', true));
if( is_array($existing_attachments) && count($existing_attachments) > 0 )
{
$post_attachments = array();
if( count($existing_attachments) > 1 )
{
usort($existing_attachments, "cmp");
}
foreach ($existing_attachments as $attachment)
{
array_push($post_attachments, array(
'id' => $attachment['id'],
'mime' => $attachment['mime'],
'title' => $attachment['title'],
'caption' => $attachment['caption'],
'location' => $attachment['location']
));
}
}
return $post_attachments;
}
/**
* This is the main initialization function, it will invoke the necessary meta_box
*
* @return void
* @author Jonathan Christopher
*/
function attachments_init()
{
wp_enqueue_style('shadowbox', WP_PLUGIN_URL . '/attachments/lib/shadowbox/shadowbox.css');
wp_enqueue_style('attachments', WP_PLUGIN_URL . '/attachments/css/attachments.css');
wp_enqueue_script('shadowbox', WP_PLUGIN_URL . '/attachments/lib/shadowbox/shadowbox.js');
wp_enqueue_script('attachments', WP_PLUGIN_URL . '/attachments/js/attachments.js');
attachments_meta_box();
}