update( $wpdb->posts, array( 'post_parent' => '' ), array( 'ID' => $attachment->ID ) );
}
}
/**
* 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;
}
/**
* 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() {
?>
' . __( 'Attach to post', 'a-gallery' ) . '';
}
return $form_fields;
}
add_filter( 'attachment_fields_to_edit', 'ag_add_fields_to_edit', 10, 2 );
/**
* Display images box for sorting and detaching.
*
* @uses add_meta_box(), wp_nonce_field()
*/
function ag_add_detach_box() {
add_meta_box( 'ag_detach_box', __( 'Sort or detach images' ), 'ag_inner_custom_box', 'post' );
// display box of images
function ag_inner_custom_box() {
global $post;
wp_nonce_field( plugin_basename( __FILE__ ), 'ag_nname' );
$attachments = ag_get_attachments( $post->ID );
// save sort order like in post edit page
$meta = get_post_meta( $post->ID, 'ag_post_images' );
$meta = $meta[0];
foreach ( $attachments as $place => $i ) {
foreach ( $meta as $key => $value ) {
if ( wp_get_attachment_url( $i->ID ) == $value ) {
$attachments[$key] = $attachments[$place];
unset( $attachments[$place] );
}
}
}
ksort( $attachments );
?>
$attachment ):
$imagelink = wp_get_attachment_url( $attachment->ID );
$w = get_option( 'ag-admin-thumbnail-w' );
$h = get_option( 'ag-admin-thumbnail-h' );
?>
-
x
ID );
if ( ! in_array( $image, $ag_images_to_save ) ) {
ag_detach_image( $post_id, $image );
}
}
}
add_action( 'save_post', 'ag_save_postdata' );
/**
* 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) );
$r = ag_get_attachments( $post_id );
$exclude = explode( ',', $exclude );
foreach ( $exclude as $ex ) {
unset( $r[$ex] );
}
// save sort order like in post edit page
$meta = get_post_meta( $post_id, 'ag_post_images' );
$meta = $meta[0];
foreach ( $r as $place => $i ) {
foreach ( $meta as $key => $value ) {
if ( wp_get_attachment_url( $i->ID ) == $value ) {
$r[$key] = $r[$place];
unset( $r[$place] );
}
}
}
ksort( $r );
if ( count( $r ) < $count ) $count = count( $r );
$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 ( $r as $i ) {
$imagelink = wp_get_attachment_url( $i->ID );
$thumblink = A_GALLERY_URL . 'timthumb.php?src=' . $imagelink . '&w=' . $item_w . '&h=' . $item_h . '&zc=1';
$c++;
$last = '';
if ( $c >= $count ) $last = "last";
$html .= "\t\t\t
\n";
$html .= "\t\t\t\t

\n";
$html .= "\t\t\t
\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;
}
add_shortcode( 'a_gallery', 'ag_shortcode' );
/**
* Enqueue javascript and css for administration pages
*
* @uses wp_register_script(), wp_enqueue_script(), wp_register_style(), wp_enqueue_style()
*/
function ag_admin_head_inserts() {
wp_register_script( 'a-gallery-admin-js', A_GALLERY_URL . 'js/a-gallery-admin.js', array( 'jquery', 'jquery-ui-core', 'jquery-ui-sortable' ) );
wp_enqueue_script( 'a-gallery-admin-js' );
wp_register_style('a-gallery-admin-css', A_GALLERY_URL . 'css/a-gallery-admin.css' );
wp_enqueue_style( 'a-gallery-admin-css');
}
add_action('admin_print_scripts', 'ag_admin_head_inserts' );
/**
* Enqueue javascript and css
*
* @uses wp_register_script(), wp_enqueue_script(), wp_register_style(), wp_enqueue_style()
*/
function ag_head_inserts() {
wp_register_script( 'jquery-lightbox-js', A_GALLERY_URL . 'js/jquery-lightbox.js', array( 'jquery' ) );
wp_enqueue_script( 'jquery-lightbox-js' );
wp_register_style( 'jquery-lightbox-css', A_GALLERY_URL . 'css/jquery-lightbox.css' );
wp_enqueue_style( 'jquery-lightbox-css');
wp_register_style('a-gallery-css', A_GALLERY_URL . 'css/a-gallery.css' );
wp_enqueue_style( 'a-gallery-css');
}
add_action('init', 'ag_head_inserts');
?>