version = '3.0.8.1';
$this->url = ATTACHMENTS_URL;
$this->dir = ATTACHMENTS_DIR;
// includes
include_once( ATTACHMENTS_DIR . 'upgrade.php' );
include_once( ATTACHMENTS_DIR . '/classes/class.field.php' );
// deal with our legacy issues if the user hasn't dismissed or migrated already
if( false == get_option( 'attachments_migrated' ) && false == get_option( 'attachments_ignore_migration' ) )
{
$legacy = new WP_Query( 'post_type=any&post_status=any&posts_per_page=1&meta_key=_attachments' );
$this->legacy = empty( $legacy->found_posts ) ? false : true;
}
else
{
$this->legacy = false;
}
// set our image sizes
$this->image_sizes = array_merge( $this->image_sizes, get_intermediate_image_sizes() );
// include our fields
$this->fields = $this->get_field_types();
// hook into WP
add_action( 'admin_enqueue_scripts', array( $this, 'assets' ), 999, 1 );
add_action( 'admin_enqueue_scripts', array( $this, 'admin_pointer' ), 999 );
// register our user-defined instances
add_action( 'init', array( $this, 'do_actions_filters' ) );
// determine which instances apply to the current post type
add_action( 'init', array( $this, 'set_instances_for_current_post_type' ), 999 );
add_action( 'add_meta_boxes', array( $this, 'meta_box_init' ) );
add_action( 'admin_footer', array( $this, 'admin_footer' ) );
add_action( 'save_post', array( $this, 'save' ) );
add_action( 'admin_menu', array( $this, 'admin_page' ) );
// with version 3 we'll be giving at least one admin notice
add_action( 'admin_notices', array( $this, 'admin_notice' ) );
// set our attachments if necessary
if( !is_null( $instance ) )
$this->attachments = $this->get_attachments( $instance, $post_id );
}
/**
* Returns whether or not the current object has any Attachments
*
* @since 3.0
*/
function exist()
{
return !empty( $this->attachments );
}
/**
* Returns the number of Attachments
*
* @since 3.0.6
*/
function total()
{
return count( $this->attachments );
}
/**
* Returns the next Attachment for the current object and increments the index
*
* @since 3.0
*/
function get()
{
$this->attachments_ref++;
if( !count( $this->attachments ) || $this->attachments_ref >= count( $this->attachments ) )
return false;
return $this->attachments[$this->attachments_ref];
}
/**
* Returns the asset (array) for the current Attachment
*
* @since 3.0.6
*/
function asset( $size = 'thumbnail' )
{
// do we have our meta yet?
if( !isset( $this->attachments[$this->attachments_ref]->meta ) )
$this->attachments[$this->attachments_ref]->meta = wp_get_attachment_metadata( $this->attachments[$this->attachments_ref]->id );
// is it an image?
if(
isset( $this->attachments[$this->attachments_ref]->meta['sizes'] ) && // is it an image?
in_array( $size, $this->image_sizes ) ) // do we have the right size?
{
$asset = wp_get_attachment_image_src( $this->attachments[$this->attachments_ref]->id, $size );
}
else
{
// either it's not an image or we don't have the proper size, so we'll use the icon
$asset = $this->icon();
}
return $asset;
}
/**
* Returns the icon (array) for the current Attachment
*
* @since 3.0.6
*/
function icon()
{
$asset = wp_get_attachment_image_src( $this->attachments[$this->attachments_ref]->id, null, true );
return $asset;
}
/**
* Returns an appropriate for the current Attachment if it's an image
*
* @since 3.0
*/
function image( $size = 'thumbnail' )
{
$asset = $this->asset( $size );
$image_src = $asset[0];
$image_width = $asset[1];
$image_height = $asset[2];
$image_alt = get_post_meta( $this->attachments[$this->attachments_ref]->id, '_wp_attachment_image_alt', true );
$image = '
';
return $image;
}
/**
* Returns the URL for the current Attachment if it's an image
*
* @since 3.0
*/
function src( $size = 'thumbnail' )
{
$asset = $this->asset( $size );
return $asset[0];
}
/**
* Returns the formatted filesize of the current Attachment
*
* @since 3.0
*/
function filesize()
{
if( !isset( $this->attachments[$this->attachments_ref]->id ) )
return false;
$url = wp_get_attachment_url( $this->attachments[$this->attachments_ref]->id );
$uploads = wp_upload_dir();
$file_path = str_replace( $uploads['baseurl'], $uploads['basedir'], $url );
$formatted = '0 bytes';
if( file_exists( $file_path ) )
{
$formatted = size_format( @filesize( $file_path ) );
}
return $formatted;
}
/**
* Returns the type of the current Attachment
*
* @since 3.0
*/
function type()
{
if( !isset( $this->attachments[$this->attachments_ref]->id ) )
return false;
$attachment_mime = explode( '/', get_post_mime_type( $this->attachments[$this->attachments_ref]->id ) );
return isset( $attachment_mime[0] ) ? $attachment_mime[0] : null;
}
/**
* Returns the subtype of the current Attachment
*
* @since 3.0
*/
function subtype()
{
if( !isset( $this->attachments[$this->attachments_ref]->id ) )
return false;
$attachment_mime = explode( '/', get_post_mime_type( $this->attachments[$this->attachments_ref]->id ) );
return isset( $attachment_mime[1] ) ? $attachment_mime[1] : null;
}
/**
* Returns the id of the current Attachment
*
* @since 3.0
*/
function id()
{
return isset( $this->attachments[$this->attachments_ref]->id ) ? $this->attachments[$this->attachments_ref]->id : null;
}
/**
* Returns the URL for the current Attachment
*
* @since 3.0
*/
function url()
{
if( !isset( $this->attachments[$this->attachments_ref]->id ) )
return false;
return wp_get_attachment_url( $this->attachments[$this->attachments_ref]->id );
}
/**
* Returns the field value for the submitted field name
*
* @since 3.0
*/
function field( $name = 'title' )
{
return isset( $this->attachments[$this->attachments_ref]->fields->$name ) ? $this->attachments[$this->attachments_ref]->fields->$name : false;
}
/**
* Fires all of our actions
*
* @since 3.0
*/
function do_actions_filters()
{
// implement our default instance if appropriate
if( !defined( 'ATTACHMENTS_DEFAULT_INSTANCE' ) )
$this->register();
// facilitate user-defined instance registration
do_action( 'attachments_register', $this );
}
/**
* Enqueues our necessary assets
*
* @since 3.0
*/
function assets( $hook )
{
global $post;
// we only want our assets on edit screens
if( !empty( $this->instances_for_post_type ) && 'edit.php' != $hook && 'post.php' != $hook && 'post-new.php' != $hook )
return;
// we only want to enqueue if appropriate
if( empty( $this->instances_for_post_type ) )
return;
$post_id = isset( $post->ID ) ? $post->ID : null;
wp_enqueue_media( array( 'post' => $post_id ) );
wp_enqueue_style( 'attachments', trailingslashit( $this->url ) . 'css/attachments.css', null, $this->version, 'screen' );
wp_enqueue_script( 'attachments', trailingslashit( $this->url ) . 'js/attachments.js', array( 'jquery', 'jquery-ui-sortable' ), $this->version, true );
}
/**
* Registers meta box(es) for the current edit screen
*
* @since 3.0
*/
function meta_box_init()
{
$nonce_sent = false;
if( !empty( $this->instances_for_post_type ) )
{
foreach( $this->instances_for_post_type as $instance )
{
$instance_name = $instance;
$instance = (object) $this->instances[$instance];
$instance->name = $instance_name;
add_meta_box( 'attachments-' . $instance_name, __( esc_attr( $instance->label ) ), array( $this, 'meta_box_markup' ), $this->get_post_type(), 'normal', 'high', array( 'instance' => $instance, 'setup_nonce' => !$nonce_sent ) );
$nonce_sent = true;
}
}
}
/**
* Callback that outputs the meta box markup
*
* @since 3.0
*/
function meta_box_markup( $post, $metabox )
{
// single out our $instance
$instance = (object) $metabox['args']['instance'];
if( $metabox['args']['setup_nonce'] )
wp_nonce_field( 'attachments_save', 'attachments_nonce' );
?>
". __( esc_html( 'It is very important that you take a few minutes to see what has been updated. The changes will affect your themes/plugins.' ), 'attachments' ) ."
"; ?>