/**
 * Filters the gallery image if it has a link and adds the appropriate attributes for the lightbox
 * scripts.
 *
 * @since  0.9.0
 * @access public
 * @param  string  $image
 * @param  int     $id
 * @param  array   $attr
 * @param  int     $instance
 * @return string
 */
function cleaner_gallery_plugin_gallery_image($image, $id, $attr, $instance)
{
    /* If the image should link to the 'file' (full-size image), add in extra link attributes. */
    if ('file' == $attr['link']) {
        $attributes = cleaner_gallery_link_attributes($instance);
        if (!empty($attributes)) {
            $image = str_replace('<a href=', "<a{$attributes} href=", $image);
        }
    } else {
        if (in_array($attr['link'], get_intermediate_image_sizes())) {
            $post = get_post($id);
            $image_src = wp_get_attachment_image_src($id, $attr['link']);
            $attributes = cleaner_gallery_link_attributes($instance);
            $attributes .= " href='{$image_src[0]}'";
            $attributes .= " title='" . esc_attr($post->post_title) . "'";
            $image = preg_replace('/<a.*?>(.*?)<\\/a>/', "<a{$attributes}>\$1</a>", $image);
        }
    }
    /* Return the formatted image. */
    return $image;
}
示例#2
0
/**
 * Overwrites the original WordPress gallery shortcode.  This is where all the main gallery 
 * stuff is pieced together.  What we're doing is completely rewriting how the gallery works.
 * Most of the functionality is from the default gallery shortcode, so most of the work has 
 * already been done.  Just plugging in the extras.
 *
 * @since 0.6
 * @param string $output The formatted gallery.
 * @param array $attr Arguments for displaying the gallery.
 * @return string $output
 */
function cleaner_gallery_shortcode($output, $attr)
{
    global $post;
    /* Orderby. */
    if (isset($attr['orderby'])) {
        $attr['orderby'] = sanitize_sql_orderby($attr['orderby']);
        if (!$attr['orderby']) {
            unset($attr['orderby']);
        }
    }
    /* Default gallery settings. */
    $defaults = array('order' => 'ASC', 'orderby' => 'menu_order ID', 'id' => $post->ID, 'link' => '', 'itemtag' => 'dl', 'icontag' => 'dt', 'captiontag' => 'dd', 'columns' => 3, 'size' => 'thumbnail', 'include' => '', 'exclude' => '', 'numberposts' => -1, 'offset' => '');
    /* Apply filters to the default arguments. */
    $defaults = apply_filters('cleaner_gallery_defaults', $defaults);
    /* Merge the defaults with user input. Make sure $id is an integer. */
    extract(shortcode_atts($defaults, $attr));
    $id = intval($id);
    /* Arguments for get_children(). */
    $children = array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby, 'exclude' => $exclude, 'include' => $include, 'numberposts' => $numberposts, 'offset' => $offset);
    /* Get image attachments. If none, return. */
    $attachments = get_children($children);
    if (empty($attachments)) {
        return '';
    }
    /* If is feed, leave the default WP settings. We're only worried about on-site presentation. */
    if (is_feed()) {
        $output = "\n";
        foreach ($attachments as $id => $attachment) {
            $output .= wp_get_attachment_link($id, $size, true) . "\n";
        }
        return $output;
    }
    /* Set up some important variables. */
    $itemtag = tag_escape($itemtag);
    $captiontag = tag_escape($captiontag);
    $columns = intval($columns);
    $itemwidth = $columns > 0 ? floor(100 / $columns) : 100;
    $i = 0;
    /*
     * Remove the style output in the middle of the freakin' page.
     * This needs to be added to the header.
     * The width applied through CSS but limits it a bit.
     */
    /* Make sure posts with multiple galleries have different IDs. */
    $gallery_id = cleaner_gallery_id($id);
    /* Class and rel attributes. */
    $attributes = cleaner_gallery_link_attributes($gallery_id);
    /* Open the gallery <div>. */
    $output = "\t\t\t<div id='gallery-{$gallery_id}' class='gallery gallery-{$id}'>";
    /* Loop through each attachment. */
    foreach ($attachments as $id => $attachment) {
        /* Get the caption and title. */
        $caption = esc_html($attachment->post_excerpt);
        $title = esc_attr($attachment->post_title);
        if (empty($caption) && cleaner_gallery_get_setting('caption_title')) {
            $caption = $title;
        }
        if (cleaner_gallery_get_setting('caption_remove')) {
            $caption = false;
        }
        /* Open each gallery row. */
        if ($columns > 0 && $i % $columns == 0) {
            $output .= "\n\t\t\t\t<div class='gallery-row clear'>";
        }
        /* Open each gallery item. */
        $output .= "\n\t\t\t\t\t<{$itemtag} class='gallery-item col-{$columns}'>";
        /* Open the element to wrap the image. */
        $output .= "\n\t\t\t\t\t\t<{$icontag} class='gallery-icon'>";
        /* If user links to file. */
        if ('file' == $link) {
            $output .= '<a href="' . wp_get_attachment_url($id) . '" title="' . $title . '"' . $attributes . '>';
            $img = wp_get_attachment_image_src($id, $size);
            $output .= '<img src="' . $img[0] . '" alt="' . $title . '" title="' . $title . '" />';
            $output .= '</a>';
        } elseif (empty($link) || 'attachment' == $link) {
            $output .= wp_get_attachment_link($id, $size, true, false);
        } elseif ('none' == $link) {
            $img = wp_get_attachment_image_src($id, $size);
            $output .= '<img src="' . $img[0] . '" alt="' . $title . '" title="' . $title . '" />';
        } elseif ('full' == $link || in_array($link, get_intermediate_image_sizes())) {
            $img_src = wp_get_attachment_image_src($id, $link);
            /* Output the link. */
            $output .= '<a href="' . $img_src[0] . '" title="' . $title . '"' . $attributes . '>';
            $img = wp_get_attachment_image_src($id, $size);
            $output .= '<img src="' . $img[0] . '" alt="' . $title . '" title="' . $title . '" />';
            $output .= '</a>';
        }
        /* Close the image wrapper. */
        $output .= "</{$icontag}>";
        /* If image caption is set. */
        if ($captiontag && $caption) {
            $output .= "\n\t\t\t\t\t\t<{$captiontag} class='gallery-caption'>";
            if (cleaner_gallery_get_setting('caption_link')) {
                $output .= '<a href="' . get_attachment_link($id) . '" title="' . $title . '">' . $caption . '</a>';
            } else {
                $output .= $caption;
            }
            $output .= "</{$captiontag}>";
        }
        /* Close individual gallery item. */
        $output .= "\n\t\t\t\t\t</{$itemtag}>";
        /* Close gallery row. */
        if ($columns > 0 && ++$i % $columns == 0) {
            $output .= "\n\t\t\t\t</div>";
        }
    }
    /* Close gallery and return it. */
    if ($columns > 0 && $i % $columns !== 0) {
        $output .= "\n\t\t\t</div>";
    }
    $output .= "\n\t\t\t</div>\n";
    /* Return out very nice, valid XHTML gallery. */
    return $output;
}