/** * If a gallery is detected, then get all the images from it. */ static function from_gallery($post_id) { $images = array(); $post = get_post($post_id); if (!empty($post->post_password)) { return $images; } $permalink = get_permalink($post->ID); $galleries = get_post_galleries($post->ID, false); foreach ($galleries as $gallery) { if (isset($gallery['type']) && 'slideshow' === $gallery['type'] && !empty($gallery['ids'])) { $image_ids = explode(',', $gallery['ids']); $image_size = isset($gallery['size']) ? $gallery['size'] : 'thumbnail'; foreach ($image_ids as $image_id) { $image = wp_get_attachment_image_src($image_id, $image_size); if (!empty($image[0])) { list($raw_src) = explode('?', $image[0]); // pull off any Query string (?w=250) $raw_src = wp_specialchars_decode($raw_src); // rawify it $raw_src = esc_url_raw($raw_src); // clean it $images[] = array('type' => 'image', 'from' => 'gallery', 'src' => $raw_src, 'href' => $permalink); } } } elseif (!empty($gallery['src'])) { foreach ($gallery['src'] as $src) { list($raw_src) = explode('?', $src); // pull off any Query string (?w=250) $raw_src = wp_specialchars_decode($raw_src); // rawify it $raw_src = esc_url_raw($raw_src); // clean it $images[] = array('type' => 'image', 'from' => 'gallery', 'src' => $raw_src, 'href' => $permalink); } } } return $images; }
/** * Retrieve the image srcs from galleries from a post's content, if present * * @since 3.6.0 * * @see get_post_galleries() * * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`. * @return array A list of lists, each containing image srcs parsed. * from an expanded shortcode */ function get_post_galleries_images($post = 0) { $galleries = get_post_galleries($post, false); return wp_list_pluck($galleries, 'src'); }
function et_gallery_images() { $output = $images_ids = ''; if (function_exists('get_post_galleries')) { $galleries = get_post_galleries(get_the_ID(), false); if (empty($galleries)) { return false; } foreach ($galleries as $gallery) { // Grabs all attachments ids from one or multiple galleries in the post $images_ids .= ('' !== $images_ids ? ',' : '') . $gallery['ids']; } $attachments_ids = explode(',', $images_ids); // Removes duplicate attachments ids $attachments_ids = array_unique($attachments_ids); } else { $pattern = get_shortcode_regex(); preg_match("/{$pattern}/s", get_the_content(), $match); $atts = shortcode_parse_atts($match[3]); if (isset($atts['ids'])) { $attachments_ids = explode(',', $atts['ids']); } else { return false; } } $slides = ''; foreach ($attachments_ids as $attachment_id) { $attachment_attributes = wp_get_attachment_image_src($attachment_id, 'et-pb-post-main-image-fullwidth'); $attachment_image = !is_single() ? $attachment_attributes[0] : wp_get_attachment_image($attachment_id, 'et-pb-portfolio-image'); if (!is_single()) { $slides .= sprintf('<div class="et_pb_slide" style="background: url(%1$s);"></div>', esc_attr($attachment_image)); } else { $full_image = wp_get_attachment_image_src($attachment_id, 'full'); $full_image_url = $full_image[0]; $attachment = get_post($attachment_id); $slides .= sprintf('<li class="et_gallery_item"> <a href="%1$s" title="%3$s"> <span class="et_portfolio_image"> %2$s <span class="et_overlay"></span> </span> </a> </li>', esc_url($full_image_url), $attachment_image, esc_attr($attachment->post_title)); } } if (!is_single()) { $output = '<div class="et_pb_slider et_pb_slider_fullwidth_off"> <div class="et_pb_slides"> %1$s </div> </div>'; } else { $output = '<ul class="et_post_gallery clearfix"> %1$s </ul>'; } printf($output, $slides); }
function et_gallery_images() { $output = $images_ids = ''; if (function_exists('get_post_galleries')) { $galleries = get_post_galleries(get_the_ID(), false); if (empty($galleries)) { return false; } foreach ($galleries as $gallery) { // Grabs all attachments ids from one or multiple galleries in the post $images_ids .= ('' !== $images_ids ? ',' : '') . $gallery['ids']; } $attachments_ids = explode(',', $images_ids); // Removes duplicate attachments ids $attachments_ids = array_unique($attachments_ids); } else { $pattern = get_shortcode_regex(); preg_match("/{$pattern}/s", get_the_content(), $match); if (empty($match)) { return false; } $atts = shortcode_parse_atts($match[3]); if (isset($atts['ids'])) { $attachments_ids = explode(',', $atts['ids']); } else { return false; } } echo '<ul id="et-projects" class="clearfix">'; foreach ($attachments_ids as $attachment_id) { $attachment = get_post($attachment_id); $fullimage_attributes = wp_get_attachment_image_src($attachment_id, 'full'); printf('<li><a href="%s" class="fancybox" rel="gallery" title="%s">%s<span class="project-description"><span class="et-zoom"></span></span></a></li>', esc_url($fullimage_attributes[0]), esc_attr($attachment->post_title), wp_get_attachment_image($attachment_id, 'et-project-thumb')); } echo '</ul>'; return $output; }
/** * Gets the gallery *item* count. This is different from getting the gallery *image* count. By default, * WordPress only allows attachments with the 'image' mime type in galleries. However, some scripts such * as Cleaner Gallery allow for other mime types. This is a more accurate count than the * hybrid_get_gallery_image_count() function since it will count all gallery items regardless of mime type. * * @todo Check for the [gallery] shortcode with the 'mime_type' parameter and use that in get_posts(). * * @since 1.6.0 * @access public * @return int */ function hybrid_get_gallery_item_count() { /* Check the post content for galleries. */ $galleries = get_post_galleries(get_the_ID(), true); /* If galleries were found in the content, get the gallery item count. */ if (!empty($galleries)) { $items = ''; foreach ($galleries as $gallery => $gallery_items) { $items .= $gallery_items; } preg_match_all('#src=([\'"])(.+?)\\1#is', $items, $sources, PREG_SET_ORDER); if (!empty($sources)) { return count($sources); } } /* If an item count wasn't returned, get the post attachments. */ $attachments = get_posts(array('fields' => 'ids', 'post_parent' => get_the_ID(), 'post_type' => 'attachment', 'numberposts' => -1)); /* Return the attachment count if items were found. */ if (!empty($attachments)) { return count($attachments); } /* Return 0 for everything else. */ return 0; }
/** * Output the HTML for this widget. * * @access public * @since Twenty Fourteen 1.0 * * @param array $args An array of standard parameters for widgets in this theme. * @param array $instance An array of settings for this widget instance. */ public function widget($args, $instance) { $format = isset($instance['format']) && in_array($instance['format'], $this->formats) ? $instance['format'] : 'aside'; switch ($format) { case 'image': $format_string = __('Images', 'twentyfourteen'); $format_string_more = __('More images', 'twentyfourteen'); break; case 'video': $format_string = __('Videos', 'twentyfourteen'); $format_string_more = __('More videos', 'twentyfourteen'); break; case 'audio': $format_string = __('Audio', 'twentyfourteen'); $format_string_more = __('More audio', 'twentyfourteen'); break; case 'quote': $format_string = __('Quotes', 'twentyfourteen'); $format_string_more = __('More quotes', 'twentyfourteen'); break; case 'link': $format_string = __('Links', 'twentyfourteen'); $format_string_more = __('More links', 'twentyfourteen'); break; case 'gallery': $format_string = __('Galleries', 'twentyfourteen'); $format_string_more = __('More galleries', 'twentyfourteen'); break; case 'aside': default: $format_string = __('Asides', 'twentyfourteen'); $format_string_more = __('More asides', 'twentyfourteen'); break; } $number = empty($instance['number']) ? 2 : absint($instance['number']); $title = apply_filters('widget_title', empty($instance['title']) ? $format_string : $instance['title'], $instance, $this->id_base); $ephemera = new WP_Query(array('order' => 'DESC', 'posts_per_page' => $number, 'no_found_rows' => true, 'post_status' => 'publish', 'post__not_in' => get_option('sticky_posts'), 'tax_query' => array(array('taxonomy' => 'post_format', 'terms' => array("post-format-{$format}"), 'field' => 'slug', 'operator' => 'IN')))); if ($ephemera->have_posts()) { $tmp_content_width = $GLOBALS['content_width']; $GLOBALS['content_width'] = 306; echo $args['before_widget']; ?> <h1 class="widget-title <?php echo esc_attr($format); ?> "> <a class="entry-format" href="<?php echo esc_url(get_post_format_link($format)); ?> "><?php echo $title; ?> </a> </h1> <ol> <?php while ($ephemera->have_posts()) { $ephemera->the_post(); $tmp_more = $GLOBALS['more']; $GLOBALS['more'] = 0; ?> <li> <article <?php post_class(); ?> > <div class="entry-content"> <?php if (has_post_format('gallery')) { if (post_password_required()) { the_content(__('Continue reading <span class="meta-nav">→</span>', 'twentyfourteen')); } else { $images = array(); $galleries = get_post_galleries(get_the_ID(), false); if (isset($galleries[0]['ids'])) { $images = explode(',', $galleries[0]['ids']); } if (!$images) { $images = get_posts(array('fields' => 'ids', 'numberposts' => -1, 'order' => 'ASC', 'orderby' => 'menu_order', 'post_mime_type' => 'image', 'post_parent' => get_the_ID(), 'post_type' => 'attachment')); } $total_images = count($images); if (has_post_thumbnail()) { $post_thumbnail = get_the_post_thumbnail(); } elseif ($total_images > 0) { $image = array_shift($images); $post_thumbnail = wp_get_attachment_image($image, 'post-thumbnail'); } if (!empty($post_thumbnail)) { ?> <a href="<?php the_permalink(); ?> "><?php echo $post_thumbnail; ?> </a> <?php } ?> <p class="wp-caption-text"> <?php printf(_n('This gallery contains <a href="%1$s" rel="bookmark">%2$s photo</a>.', 'This gallery contains <a href="%1$s" rel="bookmark">%2$s photos</a>.', $total_images, 'twentyfourteen'), esc_url(get_permalink()), number_format_i18n($total_images)); ?> </p> <?php } } else { the_content(__('Continue reading <span class="meta-nav">→</span>', 'twentyfourteen')); } ?> </div><!-- .entry-content --> <header class="entry-header"> <div class="entry-meta"> <?php if (!has_post_format('link')) { the_title('<h1 class="entry-title"><a href="' . esc_url(get_permalink()) . '" rel="bookmark">', '</a></h1>'); } /* printf( '<span class="entry-date"><a href="%1$s" rel="bookmark"><time class="entry-date" datetime="%2$s">%3$s</time></a></span> <span class="byline"><span class="author vcard"><a class="url fn n" href="%4$s" rel="author">%5$s</a></span></span>', esc_url( get_permalink() ), esc_attr( get_the_date( 'c' ) ), esc_html( get_the_date() ), esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ), get_the_author() ); */ printf('<span class="entry-date"><a href="%1$s" rel="bookmark"><time class="entry-date" datetime="%2$s">%3$s</time></a></span>', esc_url(get_permalink()), esc_attr(get_the_date('c')), esc_html(get_the_date())); if (!post_password_required() && (comments_open() || get_comments_number())) { ?> <span class="comments-link"><?php comments_popup_link(__('Leave a comment', 'twentyfourteen'), __('1 Comment', 'twentyfourteen'), __('% Comments', 'twentyfourteen')); ?> </span> <?php } ?> </div><!-- .entry-meta --> </header><!-- .entry-header --> </article><!-- #post-## --> </li> <?php } ?> </ol> <a class="post-format-archive-link" href="<?php echo esc_url(get_post_format_link($format)); ?> "> <?php /* translators: used with More archives link */ printf(__('%s <span class="meta-nav">→</span>', 'twentyfourteen'), $format_string_more); ?> </a> <?php echo $args['after_widget']; // Reset the post globals as this query will have stomped on it. wp_reset_postdata(); $GLOBALS['more'] = $tmp_more; $GLOBALS['content_width'] = $tmp_content_width; } // End check for ephemeral posts. }
the_ID(); ?> " <?php post_class(); ?> > <?php twentyfourteen_post_thumbnail(); ?> <?php get_template_part('partials/entry-header'); ?> <div class="entry-content"> <?php $galleries = get_post_galleries(get_the_ID()); if (isset($galleries[0])) { echo $galleries[0]; } else { the_content(__('Continue reading <span class="meta-nav">→</span>', 'twentyfourteen')); } wp_link_pages(array('before' => '<div class="page-links"><span class="page-links-title">' . __('Pages:', 'twentyfourteen') . '</span>', 'after' => '</div>', 'link_before' => '<span>', 'link_after' => '</span>')); ?> </div><!-- .entry-content --> <?php the_tags('<footer class="entry-meta"><span class="tag-links">', '', '</span></footer>'); ?> </article><!-- #post-## -->
/** * Print a gallery by merging all the galleries in the post. * If no [gallery] found, it creates a gallery using the images attached to post * * Based on \wp-includes\media.php -> gallery_shortcode(). * * @since 0.37 * * @param array $attr { * @type int $id Post ID. * @type string $class Gallery class. * @type int $columns Number of columns. * @type int $slice Max number of items. * } * @return int The number of items in merged gallery */ function fastfood_gallery_merge($args) { global $post; $defaults = array('id' => $post->ID, 'class' => '', 'columns' => 3, 'slice' => 0); $args = wp_parse_args($args, $defaults); $galleries = get_post_galleries($args['id'], false); $result = array(); foreach ($galleries as $gallery) { $result = array_merge($result, wp_list_pluck(fastfood_get_gallery_items($gallery), 'ID')); } if (!$result) { $result = wp_list_pluck(get_children(array('post_parent' => $args['id'], 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID')), 'ID'); } if (!($count = count($result))) { return 0; } if ($args['slice']) { $result = array_slice($result, 0, absint($args['slice'])); $args['columns'] = count($result) - 1; } $args['class'] = esc_attr(join(' ', array('gallery', 'gallery-columns-' . $args['columns'], $args['class']))); $output = "<div class='{$args['class']}'>"; foreach ($result as $key => $id) { $size = $key ? 'thumbnail' : 'full'; $image_output = wp_get_attachment_image($id, $size, false); $image_output = $key ? $image_output : '<a href="' . get_permalink($args['id']) . '">' . $image_output . '</a>'; $image_meta = wp_get_attachment_metadata($id); $orientation = ''; if (isset($image_meta['height'], $image_meta['width'])) { $orientation = $image_meta['height'] > $image_meta['width'] ? 'portrait' : 'landscape'; } $output .= "<dl class='gallery-item'>"; $output .= "\n\t\t\t<dt class='gallery-icon {$orientation}'>\n\t\t\t\t{$image_output}\n\t\t\t</dt>"; $output .= "</dl>"; } $output .= "</div>"; echo $output; return $count; }
function serene_gallery_images() { $output = $images_ids = ''; if (function_exists('get_post_galleries')) { $galleries = get_post_galleries(get_the_ID(), false); if (empty($galleries)) { return false; } if (isset($galleries[0]['ids'])) { foreach ($galleries as $gallery) { // Grabs all attachments ids from one or multiple galleries in the post $images_ids .= ('' !== $images_ids ? ',' : '') . $gallery['ids']; } $attachments_ids = explode(',', $images_ids); // Removes duplicate attachments ids $attachments_ids = array_unique($attachments_ids); } else { $attachments_ids = get_posts(array('fields' => 'ids', 'numberposts' => 999, 'order' => 'ASC', 'orderby' => 'menu_order', 'post_mime_type' => 'image', 'post_parent' => get_the_ID(), 'post_type' => 'attachment')); } } else { $pattern = get_shortcode_regex(); preg_match("/{$pattern}/s", get_the_content(), $match); $atts = shortcode_parse_atts($match[3]); if (isset($atts['ids'])) { $attachments_ids = explode(',', $atts['ids']); } else { return false; } } echo '<div class="et-gallery-slider flexslider">'; echo ' <ul class="et-gallery-slides">'; foreach ($attachments_ids as $attachment_id) { printf('<li class="et-gallery-slide"><a href="%s">%s</a></li>', esc_url(get_permalink()), wp_get_attachment_image($attachment_id, 'serene-featured-image')); } echo ' </ul> <!-- .et-gallery-slides -->'; echo '</div> <!-- .et-gallery-slider -->'; return $output; }
function et_gallery_images() { $output = $images_ids = ''; if (function_exists('get_post_galleries')) { $galleries = get_post_galleries(get_the_ID(), false); if (empty($galleries)) { return false; } foreach ($galleries as $gallery) { if (isset($gallery['ids'])) { // Grabs all attachments ids from one or multiple galleries in the post $images_ids .= ('' !== $images_ids ? ',' : '') . $gallery['ids']; } else { $image_ids = false; // If user doesn't define ids of images on galleries, get attached media $attached_media = get_attached_media('image', get_the_id()); } } if ($images_ids) { $attachments_ids = explode(',', $images_ids); } elseif (isset($attached_media) && is_array($attached_media) && !empty($attached_media)) { $attachments_ids = wp_list_pluck($attached_media, 'ID'); } else { $attachments_ids = false; } if (!$attachments_ids) { // Print no gallery found message ?> <div class="et_pb_module et_pb_slider et_pb_slider_fullwidth_off et_pb_bg_layout_light gallery-not-found"> <div class="et_pb_slides"> <div class="et_pb_slide et_pb_bg_layout_light et_pb_media_alignment_center et-pb-active-slide" style="background-color:#ffffff;"> <div class="et_pb_container clearfix"> <div class="et_pb_slide_description"> <h2><?php _e('No Gallery Found', 'Divi'); ?> </h2> <div class="et_pb_slide_content"> <p><?php _e('No items found', 'Divi'); ?> </p> </div> </div> <!-- .et_pb_slide_description --> </div> <!-- .et_pb_container --> </div> <!-- .et_pb_slide --> </div> <!-- .et_pb_slides --> </div> <?php return; } // Removes duplicate attachments ids $attachments_ids = array_unique($attachments_ids); } else { $pattern = get_shortcode_regex(); preg_match("/{$pattern}/s", get_the_content(), $match); $atts = shortcode_parse_atts($match[3]); if (isset($atts['ids'])) { $attachments_ids = explode(',', $atts['ids']); } else { return false; } } $slides = ''; foreach ($attachments_ids as $attachment_id) { $attachment_attributes = wp_get_attachment_image_src($attachment_id, 'et-pb-post-main-image-fullwidth'); $attachment_image = !is_single() ? $attachment_attributes[0] : wp_get_attachment_image($attachment_id, 'et-pb-portfolio-image'); if (!is_single()) { $slides .= sprintf('<div class="et_pb_slide" style="background: url(%1$s);"></div>', esc_attr($attachment_image)); } else { $full_image = wp_get_attachment_image_src($attachment_id, 'full'); $full_image_url = $full_image[0]; $attachment = get_post($attachment_id); $slides .= sprintf('<li class="et_gallery_item"> <a href="%1$s" title="%3$s"> <span class="et_portfolio_image"> %2$s <span class="et_overlay"></span> </span> </a> </li>', esc_url($full_image_url), $attachment_image, esc_attr($attachment->post_title)); } } if (!is_single()) { $output = '<div class="et_pb_slider et_pb_slider_fullwidth_off et_pb_gallery_post_type"> <div class="et_pb_slides"> %1$s </div> </div>'; } else { $output = '<ul class="et_post_gallery clearfix"> %1$s </ul>'; } printf($output, $slides); }
/** * Add images associated with a WP_Post up to limit. * * @since 1.0.0 * * @param WP_Post $post post of interest * * @return void */ public function addPostImages($post) { // bad parameter if (!$post || !isset($post->ID)) { return; } // does current post type and the current theme support post thumbnails? if (post_type_supports(get_post_type($post), 'thumbnail') && function_exists('has_post_thumbnail') && has_post_thumbnail()) { if ($this->addImageByAttachmentID(get_post_thumbnail_id($post->ID))) { if ($this->isFull()) { return; } } } // image attachments $attached_images = get_attached_media('image', $post); if (!empty($attached_images)) { foreach ($attached_images as $attached_image) { if (!isset($attached_image->ID)) { continue; } if (!$this->addImageByAttachmentID($attached_image->ID)) { continue; } // hit the limit. end the search if ($this->isFull()) { return; } } } unset($attached_images); // post galleries $galleries = get_post_galleries($post, false); foreach ($galleries as $gallery) { if (!(isset($gallery['ids']) && $gallery['ids'])) { continue; } $gallery_ids = explode(',', $gallery['ids']); foreach ($gallery_ids as $attachment_id) { if (!$this->addImageByAttachmentID($attachment_id)) { continue; } // hit the limit. end the search if ($this->isFull()) { return; } } unset($gallery_ids); } unset($galleries); }
/** * Fetch image post objects for all gallery images in a post. * * @param $post_id * * @return array */ function hannover_get_gallery_images($post_id) { $post = get_post($post_id); // Den Beitrag gibt es nicht, oder er ist leer. if (!$post || empty($post->post_content)) { return array(); } $galleries = get_post_galleries($post, false); if (empty($galleries)) { return array(); } $ids = array(); foreach ($galleries as $gallery) { if (!empty($gallery['ids'])) { $ids = array_merge($ids, explode(',', $gallery['ids'])); } } $ids = array_unique($ids); if (empty($ids)) { $attachments = get_children(array('post_parent' => $post_id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order')); if (empty($attachments)) { return array(); } } $images = get_posts(array('post_type' => 'attachment', 'post_mime_type' => 'image', 'orderby' => 'post__in', 'numberposts' => 999, 'include' => $ids)); if (!$images && !$attachments) { return array(); } elseif (!$images) { $images = $attachments; } return $images; }
/** * Identify image attachments related to the post. * * Request the full size of the attachment, not necessarily the same as the size used in the post. * * @since 1.5 * * @param stdClass|WP_Post $post WordPress post of interest * @return array { * Open Graph protocol image structured data * * @type string URL of the image * @type array associative array of image data * } */ public static function get_og_images($post) { $og_images = array(); if (!$post || !isset($post->ID)) { return $og_images; } // does current post type and the current theme support post thumbnails? if (post_type_supports(get_post_type($post), 'thumbnail') && function_exists('has_post_thumbnail') && has_post_thumbnail()) { list($post_thumbnail_url, $post_thumbnail_width, $post_thumbnail_height) = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'full'); $image = self::attachment_to_og_image(get_post_thumbnail_id($post->ID)); if (isset($image['url']) && !isset($og_images[$image['url']])) { $og_images[$image['url']] = $image; } unset($image); } // get image attachments if (function_exists('get_attached_media')) { $images = get_attached_media('image', $post->ID); if (!empty($images)) { foreach ($images as $i) { if (!isset($i->ID)) { continue; } $image = self::attachment_to_og_image($i->ID); if (!isset($image['url']) || isset($og_images[$image['url']])) { continue; } $og_images[$image['url']] = $image; if (count($og_images) === self::MAX_IMAGE_COUNT) { return $og_images; } } } unset($images); } // test for WP_Embed handled images if (!empty($post->post_content)) { // Instagram preg_match_all('#\\s*http://instagr(\\.am|am\\.com)/p/(.*)/\\s*#i', $post->post_content, $matches); if (isset($matches[2])) { foreach ($matches[2] as $instagram_id) { $instagram_url = esc_url_raw('http://instagram.com/p/' . $instagram_id . '/media/?size=l', array('http')); if (!$instagram_url || isset($og_images[$instagram_url])) { continue; } $og_images[$instagram_url] = array('url' => $instagram_url); unset($instagram_url); } } unset($matches); } // add gallery content if (function_exists('get_post_galleries')) { // use get_post_galleries function from WP 3.6+ $galleries = get_post_galleries($post, false); foreach ($galleries as $gallery) { // use ids to request full-size image URI if (!(isset($gallery['ids']) && $gallery['ids'])) { continue; } $gallery['ids'] = explode(',', $gallery['ids']); foreach ($gallery['ids'] as $attachment_id) { $image = self::attachment_to_og_image($attachment_id); if (!isset($image['url']) || isset($og_images[$image['url']])) { continue; } $og_images[$image['url']] = $image; if (count($og_images) === self::MAX_IMAGE_COUNT) { return $og_images; } unset($image); } } unset($galleries); } else { // extract full-size images from gallery shortcode $og_images = self::gallery_images($post, $og_images); } return $og_images; }
function update_gallery($action, $pid) { global $wpdb; $post_types = get_post_types(array('public' => true, 'exclude_from_search' => false)); $posts = $wpdb->get_results("SELECT * FROM " . $wpdb->prefix . 'posts'); foreach ($posts as $post) { if (in_array($post->post_type, $post_types)) { $galleries = get_post_galleries($post->ID, false); foreach ($galleries as $gallery) { $ids_old = 'ids="' . $gallery['ids'] . '"'; $ids_old_array = explode(',', $gallery['ids']); if (isset($gallery['autoinsert']) && $gallery['autoinsert'] == 1) { if ($action == 'upload' || $action == 'update') { $ids_new_array = $this->upload_update_post($gallery, $ids_old_array); $ids_new = 'ids="' . trim(implode(',', $ids_new_array), ',') . '"'; if ($ids_new_array != $ids_old_array) { $post_content = str_replace($ids_old, $ids_new, $post->post_content); wp_update_post(array('ID' => $post->ID, 'post_content' => $post_content)); } } else { if ($action == 'delete') { if (in_array($pid, $ids_old_array) == true) { $key = array_search($pid, $ids_old_array); unset($ids_old_array[$key]); } $ids_new = 'ids="' . trim(implode(',', $ids_old_array), ',') . '"'; $post_content = str_replace($ids_old, $ids_new, $post->post_content); wp_update_post(array('ID' => $post->ID, 'post_content' => $post_content)); } else { // $action = 'move' $ids = explode(',', $pid); foreach ($ids as $id) { if (in_array($id, $ids_old_array) == true) { $key = array_search($id, $ids_old_array); unset($ids_old_array[$key]); } } $ids_new_array = $this->upload_update_post($gallery, $ids_old_array); $ids_new = 'ids="' . trim(implode(',', $ids_new_array), ',') . '"'; $post_content = str_replace($ids_old, $ids_new, $post->post_content); wp_update_post(array('ID' => $post->ID, 'post_content' => $post_content)); } } } } } } }
/** * Get all port with some gallery, this will help to show what image is used in some post, for the moment is only one notification * @global type $wpdb * @return type */ function DNUI_getInfoImageFormPostsWithGallery($without = false) { global $wpdb; if ($without) { $sql = "SELECT id FROM " . $wpdb->prefix . "posts WHERE post_content is not null and post_content!='' and post_type not in ('attachment','nav_menu_item','revision') and post_status !='draft' AND post_content LIKE '%[gallery%'; "; } else { $sql = "SELECT id FROM " . $wpdb->prefix . "posts WHERE post_content is not null and post_content!='' and post_type not in ('attachment','nav_menu_item') and post_content LIKE '%[gallery%'"; } //$sql = "SELECT id FROM " . $wpdb->prefix . "posts WHERE post_content LIKE '%[gallery%' and post_type='post'; "; $result = $wpdb->get_results($sql, "ARRAY_A"); $info = array(); foreach ($result as $id) { $galleries = get_post_galleries($id['id'], false); foreach ($galleries as $gallery) { $idsImage = explode(',', $gallery['ids']); foreach ($idsImage as $idImage) { if (!array_key_exists($idImage, $info)) { $info[$idImage] = array('sizes' => array()); } $size = "original"; if (array_key_exists('size', $gallery)) { $size = $gallery['size']; } if (!in_array($size, $info[$idImage]['sizes'])) { $info[$idImage]['sizes'][] = $size; } } } } return $info; }
/** * Extract images in [galleries] shortcodes from text. * * @since 2.3.0 * * @param string $richtext Content to parse. * @param string $plaintext Sanitized version of the content. * @param array $extra_args Bespoke data for a particular extractor (optional). * * @return array */ protected function extract_images_from_galleries($richtext, $plaintext, $extra_args = array()) { if (!isset($extra_args['post']) || !is_a($extra_args['post'], 'WP_Post')) { $post = new WP_Post((object) array('post_content' => $richtext)); } else { $post = $extra_args['post']; } // We're not using get_post_galleries_images() because it returns thumbnails; we want the original image. $galleries = get_post_galleries($post, false); $galleries_data = array(); if (!empty($galleries)) { // Validate the size of the images requested. if (isset($extra_args['width'])) { // A width was specified but not a height, so calculate it assuming a 4:3 ratio. if (!isset($extra_args['height']) && ctype_digit($extra_args['width'])) { $extra_args['height'] = round($extra_args['width'] / 4 * 3); } if (ctype_digit($extra_args['width'])) { $image_size = array($extra_args['width'], $extra_args['height']); } else { $image_size = $extra_args['width']; // e.g. "thumb", "medium". } } else { $image_size = 'full'; } /** * There are two variants of gallery shortcode. * * One kind specifies the image (post) IDs via an `ids` parameter. * The other gets the image IDs from post_type=attachment and post_parent=get_the_ID(). */ foreach ($galleries as $gallery_id => $gallery) { $data = array(); $images = array(); // Gallery ids= variant. if (isset($gallery['ids'])) { $images = wp_parse_id_list($gallery['ids']); // Gallery post_parent variant. } elseif (isset($extra_args['post'])) { $images = wp_parse_id_list(get_children(array('fields' => 'ids', 'order' => 'ASC', 'orderby' => 'menu_order ID', 'post_mime_type' => 'image', 'post_parent' => $extra_args['post']->ID, 'post_status' => 'inherit', 'post_type' => 'attachment'))); } // Extract the data we need from each image in this gallery. foreach ($images as $image_id) { $image = wp_get_attachment_image_src($image_id, $image_size); $data[] = array('url' => $image[0], 'width' => $image[1], 'height' => $image[2], 'gallery_id' => 1 + $gallery_id); } $galleries_data[] = $data; } } /** * Filters image galleries extracted from text. * * @since 2.3.0 * * @param array $galleries_data Galleries. See {@link BP_Media_Extractor::extract_images_from_galleries()}. * @param string $richtext Content to parse. * @param string $plaintext Copy of $richtext without any markup. * @param array $extra_args Bespoke data for a particular extractor. */ return apply_filters('bp_media_extractor_galleries', $galleries_data, $richtext, $plaintext, $extra_args); }
/** * Retrieve the IDs for images in a gallery. * * @uses get_post_galleries() First, if available. Falls back to shortcode parsing, * then as last option uses a get_posts() call. * * @since Twenty Eleven 1.6 * * @return array List of image IDs from the post gallery. */ function twentyeleven_get_gallery_images() { $images = array(); if (function_exists('get_post_galleries')) { $galleries = get_post_galleries(get_the_ID(), false); if (isset($galleries[0]['ids'])) { $images = explode(',', $galleries[0]['ids']); } } else { $pattern = get_shortcode_regex(); preg_match("/{$pattern}/s", get_the_content(), $match); $atts = shortcode_parse_atts($match[3]); if (isset($atts['ids'])) { $images = explode(',', $atts['ids']); } } if (!$images) { $images = get_posts(array('fields' => 'ids', 'numberposts' => 999, 'order' => 'ASC', 'orderby' => 'menu_order', 'post_mime_type' => 'image', 'post_parent' => get_the_ID(), 'post_type' => 'attachment')); } return $images; }
/** * Display Gallery Slider * * @param integer $post_id get Id of singular * @param string $thumbnail get type thumbnail * * @return void * @since 1.0 */ function totomo_gallery_slider($thumbnail) { $post_id = get_the_ID(); $images = array(); $galleries = get_post_galleries($post_id, false); if (isset($galleries[0]['ids'])) { $images = explode(',', $galleries[0]['ids']); } if (!empty($images) && is_array($images)) { ?> <div id="slider-<?php echo $post_id; ?> " class="carousel slide" data-ride="carousel"> <ol class="carousel-indicators"> <?php foreach ($images as $i => $image) { printf('<li data-target="#slider-%s" data-slide-to="%s"%s></li>', $post_id, $i, 0 == $i ? ' class="active"' : ''); } ?> </ol> <div class="carousel-inner"> <?php foreach ($images as $i => $image) { printf('<div class="item%s">%s</div>', 0 == $i ? ' active' : '', wp_get_attachment_image($image, $thumbnail)); } ?> </div> </div> <?php } }