Ticket #24688: 24688-3.diff
| File 24688-3.diff, 4.8 KB (added by , 7 years ago) |
|---|
-
tests/phpunit/tests/attachment/adjecentImageLink.php
1 <?php 2 3 /** 4 * @group attachment 5 * @ticket 24688 6 */ 7 8 class Tests_Attachment_AdjecentImageLink extends WP_UnitTestCase { 9 function test_previous_image_link() { 10 global $post; 11 $parent_id = self::factory()->post->create(); 12 $args = array( 13 'file' => 'image.jpg', 14 'post_parent' => $parent_id, 15 'post_mime_type' => 'image/jpeg', 16 ); 17 $prev_attachment_id = self::factory()->attachment->create_object( $args ); 18 $curr_attachment_id = self::factory()->attachment->create_object( $args ); 19 20 $post = get_post( $curr_attachment_id ); 21 setup_postdata( $post ); 22 ob_start(); 23 previous_image_link(); 24 $prev_link = ob_get_clean(); 25 26 $this->assertEquals( wp_get_attachment_link( $prev_attachment_id, 'thumbnail', true, false, false ), $prev_link ); 27 } 28 29 function test_next_image_link() { 30 global $post; 31 $parent_id = self::factory()->post->create(); 32 $args = array( 33 'file' => 'image.jpg', 34 'post_parent' => $parent_id, 35 'post_mime_type' => 'image/jpeg', 36 ); 37 $curr_attachment_id = self::factory()->attachment->create_object( $args ); 38 $next_attachment_id = self::factory()->attachment->create_object( $args ); 39 40 $post = get_post( $curr_attachment_id ); 41 setup_postdata( $post ); 42 ob_start(); 43 next_image_link(); 44 $next_link = ob_get_clean(); 45 46 $this->assertEquals( wp_get_attachment_link( $next_attachment_id, 'thumbnail', true, false, false ), $next_link ); 47 } 48 } -
src/wp-includes/media.php
2793 2793 * 2794 2794 * @since 2.5.0 2795 2795 * 2796 * @uses _previous_image_post_where_filter_once() 2797 * @uses _next_image_post_where_filter_once() 2798 * 2796 2799 * @param bool $prev Optional. Whether to display the next (false) or previous (true) link. Default true. 2797 2800 * @param string|array $size Optional. Image size. Accepts any valid image size, or an array of width and height 2798 2801 * values in pixels (in that order). Default 'thumbnail'. 2799 2802 * @param bool $text Optional. Link text. Default false. 2800 2803 */ 2801 2804 function adjacent_image_link( $prev = true, $size = 'thumbnail', $text = false ) { 2802 $post = get_post(); 2803 $attachments = array_values( 2805 $adjacent = $prev ? 'previous' : 'next'; 2806 $order = $prev ? 'DESC' : 'ASC'; 2807 $post = get_post(); 2808 2809 add_filter( 'posts_where', "_{$adjacent}_image_posts_where_filter_once" ); 2810 2811 $attachment = array_values( 2804 2812 get_children( 2805 2813 array( 2806 'post_parent' => $post->post_parent, 2807 'post_status' => 'inherit', 2808 'post_type' => 'attachment', 2809 'post_mime_type' => 'image', 2810 'order' => 'ASC', 2811 'orderby' => 'menu_order ID', 2814 'post_parent' => $post->post_parent, 2815 'post_status' => 'inherit', 2816 'post_type' => 'attachment', 2817 'post_mime_type' => 'image', 2818 'orderby' => array( 2819 'menu_order' => $order, 2820 'ID' => $order, 2821 ), 2822 'posts_per_page' => 1, 2823 'suppress_filters' => false, 2812 2824 ) 2813 2825 ) 2814 2826 ); 2815 2827 2816 foreach ( $attachments as $k => $attachment ) {2817 if ( $attachment->ID == $post->ID ) {2818 break;2819 }2820 }2821 2822 2828 $output = ''; 2823 2829 $attachment_id = 0; 2824 2830 2825 if ( $attachments ) { 2826 $k = $prev ? $k - 1 : $k + 1; 2827 2828 if ( isset( $attachments[ $k ] ) ) { 2829 $attachment_id = $attachments[ $k ]->ID; 2830 $output = wp_get_attachment_link( $attachment_id, $size, true, false, $text ); 2831 } 2831 if ( ! empty( $attachment ) ) { 2832 $attachment_id = $attachment[0]->ID; 2833 $output = wp_get_attachment_link( $attachment_id, $size, true, false, $text ); 2832 2834 } 2833 2835 2834 $adjacent = $prev ? 'previous' : 'next';2835 2836 2836 /** 2837 2837 * Filters the adjacent image link. 2838 2838 * … … 2850 2850 } 2851 2851 2852 2852 /** 2853 * Append to WHERE clause for selecting only the previous image. 2854 * 2855 * @param string $where The WHERE clause of the query. 2856 */ 2857 function _previous_image_posts_where_filter_once( $where ) { 2858 remove_filter( 'posts_where', __FUNCTION__ ); 2859 $post = get_post(); 2860 return "$where AND (menu_order < $post->menu_order OR (menu_order = $post->menu_order AND ID < $post->ID))"; 2861 } 2862 2863 /** 2864 * Append to WHERE clause for selecting only the next image. 2865 * 2866 * @param string $where The WHERE clause of the query. 2867 */ 2868 function _next_image_posts_where_filter_once( $where ) { 2869 remove_filter( 'posts_where', __FUNCTION__ ); 2870 $post = get_post(); 2871 return "$where AND (menu_order > $post->menu_order OR (menu_order = $post->menu_order AND ID > $post->ID))"; 2872 } 2873 2874 /** 2853 2875 * Retrieves taxonomies attached to given the attachment. 2854 2876 * 2855 2877 * @since 2.5.0