WordPress.org

Make WordPress Core

Ticket #23044: 23044.diff

File 23044.diff, 2.7 KB (added by wonderboymusic, 6 years ago)
  • wp-includes/media.php

    diff --git a/wp-includes/media.php b/wp-includes/media.php
    index 6111f36..6f7f5cb 100644
    a b function next_image_link($size = 'thumbnail', $text = false) { 
    11221122 * Retrieves the current attachment object from the $post global.
    11231123 *
    11241124 * @since 2.5.0
     1125 * @uses _previous_image_posts_where_filter_once()
     1126 * @uses _next_image_posts_where_filter_once()
    11251127 *
    11261128 * @param bool $prev Optional. Default is true to display previous link, false for next.
    11271129 */
    11281130function adjacent_image_link($prev = true, $size = 'thumbnail', $text = false) {
     1131        $adjacent = $prev ? 'previous' : 'next';
     1132        $order = $prev ? 'DESC' : 'ASC';
    11291133        $post = get_post();
    1130         $attachments = array_values( get_children( array( 'post_parent' => $post->post_parent, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID' ) ) );
    1131 
    1132         foreach ( $attachments as $k => $attachment )
    1133                 if ( $attachment->ID == $post->ID )
    1134                         break;
    1135 
    1136         $k = $prev ? $k - 1 : $k + 1;
     1134        add_filter( 'posts_where', "_{$adjacent}_image_posts_where_filter_once" );
     1135        $attachment = array_pop( get_children( array(
     1136                'post_parent' => $post->post_parent,
     1137                'post_status' => 'inherit',
     1138                'post_type' => 'attachment',
     1139                'post_mime_type' => 'image',
     1140                'orderby' => "menu_order $order, ID $order",
     1141                'posts_per_page' => 1,
     1142                'suppress_filters' => false,
     1143        ) ) );
    11371144
    11381145        $output = $attachment_id = null;
    1139         if ( isset( $attachments[ $k ] ) ) {
    1140                 $attachment_id = $attachments[ $k ]->ID;
     1146        if ( isset( $attachment ) ) {
     1147                $attachment_id = $attachment->ID;
    11411148                $output = wp_get_attachment_link( $attachment_id, $size, true, false, $text );
    11421149        }
    11431150
    1144         $adjacent = $prev ? 'previous' : 'next';
    11451151        echo apply_filters( "{$adjacent}_image_link", $output, $attachment_id, $size, $text );
    11461152}
    11471153
    11481154/**
     1155 * Append to WHERE clause for selecting only the previous image.
     1156 *
     1157 * @since 3.7.0
     1158 *
     1159 * @params string $where
     1160 * @return string
     1161 */
     1162function _previous_image_posts_where_filter_once( $where ) {
     1163        remove_filter( 'posts_where', __FUNCTION__ );
     1164        $post = get_post();
     1165        return "$where AND (menu_order < $post->menu_order OR (menu_order = $post->menu_order AND ID < $post->ID))";
     1166}
     1167
     1168/**
     1169 * Append to WHERE clause for selecting only the next image.
     1170 *
     1171 * @since 3.7.0
     1172 *
     1173 * @params string $where
     1174 * @return string
     1175 */
     1176function _next_image_posts_where_filter_once( $where ) {
     1177        remove_filter( 'posts_where', __FUNCTION__ );
     1178        $post = get_post();
     1179        return "$where AND (menu_order > $post->menu_order OR (menu_order = $post->menu_order AND ID > $post->ID))";
     1180}
     1181
     1182/**
    11491183 * Retrieve taxonomies attached to the attachment.
    11501184 *
    11511185 * @since 2.5.0