= $aspect_ratio ) {
// resize
$tmpfile = image_resize( $file, $height * $orig_aspect_ratio, $height, false, "a-gallery" );
// crop
$file = image_resize( $file, $width, $height, true, "{$width}x{$height}" );
unlink( $tmpfile );
}
else {
// resize
$tmpfile = image_resize( $file, $width, $wigth * $orig_aspect_ratio, false, "a-gallery" );
// crop
$file = image_resize( $file, $width, $height, true, "{$width}x{$height}" );
unlink( $tmpfile );
}
}
return $imageurl;
}
/**
* Get and return post attachments by ID.
*
* @uses get_children(), get_posts()
*
* @param int $post_id Post ID.
* @return array Contains attachments objects.
*/
function ag_get_attachments( $post_id ) {
$attachments = get_children( array(
'post_parent' => $post_id,
'post_type' => 'attachment',
'post_mime_type' => 'image',
'orderby' => 'menu_order'
) );
return $attachments;
}
/**
* Handle image uploads: update post meta.
*
* @param $post_ID int Post id.
*/
function ag_image_upload_handler( $attachment_ID ) {
$post_ID = get_post( $attachment_ID )->post_parent;
$order = array_keys( ag_get_attachments( $post_ID ) );
if ( count( $order ) > 0 ) {
delete_post_meta( $post_ID, 'ag_attached_images' );
add_post_meta( $post_ID, 'ag_attached_images', implode( ",", $order ) );
}
}
/**
* Show and remove images.
*
* @param $post int Post id.
* @param $save bool Save accepted order or not.
* @param $order string String with attachments ids "1,2,3,4,5".
*
*/
function ag_save_and_show( $post, $save = false, $order = "" ) {
$post = get_post( $post );
if ( $save ) {
// Validate $order. It must be a string with comma separated numbers: "1,20,3,40"
if ( preg_match( "/(\d{1,},){0,}\d{1,}/", $order ) ) {
global $wpdb;
$order = explode( ",", $order );
$attached = array_keys( ag_get_attachments( $post->ID ) );
$detach = array_diff( $attached, $order );
$attach = array_diff( $order, $attached );
// detach
foreach ( $detach as $key => $id ) {
$wpdb->update( $wpdb->posts, array( 'post_parent' => 0 ), array( 'ID' => intval( $id ) ) );
}
// attach
foreach ( $attach as $key => $id ) {
$wpdb->update( $wpdb->posts, array( 'post_parent' => $post->ID ), array( 'ID' => intval( $id ) ) );
}
// save new order
delete_post_meta( $post->ID, 'ag_attached_images' );
add_post_meta( $post->ID, 'ag_attached_images', implode( ",", $order ) );
}
} else {
// load order from post meta
$post_meta = get_post_meta( $post->ID, 'ag_attached_images' );
$ids_string = array_shift( $post_meta );
// validate $ids_string format
if ( preg_match( "/(\d{1,},){0,}\d{1,}/", $ids_string ) ) {
$order = explode( ",", $ids_string );
} else {
$order = array();
}
}
// show //
$id_order = $order;
$attachments = ag_get_attachments( $post->ID );
// sort attachments in user specified order
if ( count( $order ) > 0 ):
foreach ( $order as $position => $attachment_id ) {
if ( array_key_exists( $attachment_id, $attachments ) ) {
$order[ $position ] = $attachments[ $attachment_id ];
}
}
?>
">
$attachment ):
$w = get_option( 'ag-admin-thumbnail-w' );
$h = get_option( 'ag-admin-thumbnail-h' );
$imagesrc = ag_image( $attachment->ID, $w, $h );
?>
-
ID}&order=" . implode( ",", $id_order ); ?>" class="button" id="ag-add-images">
ID}&order="; ?>" class="button" id="ag-save-state">
'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => 0 );
$attachments = get_posts( $args );
?>
ID}&order="; ?>" class="button" id="ag-update-state">
ID, $w, $h );
$image_name = $attachment->post_title;
$image_alt = $image_name;
$image_date = $attachment->post_date;
?>
|
+
|
$post->ID )
);
}
/**
* Callback for add_meta_box().
*
* @param $post object Post object.
* @param $metabox array Array with metabox id, title, callback, and args elements.
*
**/
function ag_detachbox_callback( $post, $metabox ) {
$post_id = $metabox['args']['post_id'];
ag_save_and_show( $post_id );
}
/**
* Process shortcode [a_gallery].
*
* @uses apply_filters(), get_option(), get_post_meta(), wp_get_attachment_url()
*
* @param string $attr Shortcode attributes.
* @return string $html Processed html.
*/
function ag_shortcode( $attr ) {
global $post;
$attr = apply_filters( 'post_a_gallery', $attr );
extract( shortcode_atts( array(
'post_id' => $post->ID,
'count' => get_option( 'ag-default-count' ),
'border' => get_option( 'ag-default-border' ),
'border_color' => get_option( 'ag-default-border-color' ),
'item_w' => get_option( 'ag-default-item-w' ),
'item_h' => get_option( 'ag-default-item-h' ),
'columns' => get_option( 'ag-default-columns' ),
'max_width' => get_option( 'ag-default-max-width' ),
'max_height' => get_option( 'ag-default-max-height' ),
'exclude' => ''
), $attr) );
$attachments = ag_get_attachments( $post_id );
// exclude attachments
$exclude = explode( ',', $exclude );
foreach ( $exclude as $id ) {
unset( $attachments[$id] );
}
// save sort order like in post edit page
$post_meta = get_post_meta( $post->ID, 'ag_attached_images' );
$ids_string = array_shift( $post_meta );
$order = explode( ',', $ids_string );
foreach ( $order as $position => $attachment_id ) {
if ( array_key_exists( $attachment_id, $attachments ) ) {
$order[ $position ] = $attachments[ $attachment_id ];
}
}
if ( count( $order ) < $count ) $count = count( $order );
$item_c_w = $item_w + $border * 2;
$item_c_h = $item_h + $border * 2;
$c_width = ( $item_c_w + 10 ) * $columns - 10;
$s_width = ( $item_c_w + 10 ) * $count - 10;
$html = "\n";
if ( $columns < $count ) $html .= "\t
\n";
$html .= "\t
\n";
if ( $columns < $count ) $html .= "\t\t
\n";
$c = 0;
foreach ( $order as $i ) {
$imagelink = wp_get_attachment_url( $i->ID );
$thumblink = ag_image( $i->ID, $item_w, $item_h );
$c++;
$last = '';
if ( $c >= $count ) $last = "last";
$html .= "
\n";
if ( $c >= $count ) break;
}
$html .= "\t\t
\n";
if ( $columns < $count ) $html .= "\t
\n";
if ( $columns < $count ) $html .= "\t
\n";
$html .= "
\n";
$max_right_count = $count - $columns;
$item_c_w = $item_c_w + 10;
$lightboxurl = A_GALLERY_URL;
$html .= '';
return $html;
}
/**
* Adds plugin settings page.
*
* @uses add_options_page()
*/
function ag_add_options_page() {
add_options_page( __( 'A. Gallery options', 'a-gallery' ), __( 'A. Gallery', 'a-gallery' ), 8, basename(__FILE__), 'ag_display_options' );
}
add_action( 'admin_menu', 'ag_add_options_page' );
/**
* Return plugin settings page.
*
* @uses get_option()
*/
function ag_display_options() {
?>