Make WordPress Core

Changeset 51122


Ignore:
Timestamp:
06/08/2021 11:12:22 PM (4 years ago)
Author:
whyisjake
Message:

Media: Add new functions to return the previous/next attachment links.

New functions:

  • get_adjacent_image_link()
  • get_next_image_link()
  • get_previous_image_link()

Fixes #45708.

Props flixos90, DrewAPicture, mor10, antpb, hellofromTonya, whyisjake.

Location:
trunk
Files:
6 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/phpcs.xml.dist

    r50697 r51122  
    250250                <element value="Tests_Query_Conditionals"/>
    251251                <element value="WP_Test_XML_TestCase"/>
     252                <element value="WP_Test_Adjacent_Image_Link_TestCase"/>
    252253
    253254                <!-- Mock classes. -->
  • trunk/src/wp-includes/media.php

    r50831 r51122  
    33783378
    33793379/**
    3380  * Displays previous image link that has the same post parent.
    3381  *
    3382  * @since 2.5.0
    3383  *
    3384  * @see adjacent_image_link()
     3380 * Gets the previous image link that has the same post parent.
     3381 *
     3382 * @since 5.8.0
     3383 *
     3384 * @see get_adjacent_image_link()
    33853385 *
    33863386 * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array
    33873387 *                           of width and height values in pixels (in that order). Default 'thumbnail'.
    33883388 * @param string|false $text Optional. Link text. Default false.
    3389  */
    3390 function previous_image_link( $size = 'thumbnail', $text = false ) {
    3391     adjacent_image_link( true, $size, $text );
    3392 }
    3393 
    3394 /**
    3395  * Displays next image link that has the same post parent.
     3389 * @return string Markup for previous image link.
     3390 */
     3391function get_previous_image_link( $size = 'thumbnail', $text = false ) {
     3392    return get_adjacent_image_link( true, $size, $text );
     3393}
     3394
     3395/**
     3396 * Displays previous image link that has the same post parent.
    33963397 *
    33973398 * @since 2.5.0
    3398  *
    3399  * @see adjacent_image_link()
    34003399 *
    34013400 * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array
     
    34033402 * @param string|false $text Optional. Link text. Default false.
    34043403 */
     3404function previous_image_link( $size = 'thumbnail', $text = false ) {
     3405    echo get_previous_image_link( $size, $text );
     3406}
     3407
     3408/**
     3409 * Gets the next image link that has the same post parent.
     3410 *
     3411 * @since 5.8.0
     3412 *
     3413 * @see get_adjacent_image_link()
     3414 *
     3415 * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array
     3416 *                           of width and height values in pixels (in that order). Default 'thumbnail'.
     3417 * @param string|false $text Optional. Link text. Default false.
     3418 * @return string Markup for next image link.
     3419 */
     3420function get_next_image_link( $size = 'thumbnail', $text = false ) {
     3421    return get_adjacent_image_link( false, $size, $text );
     3422}
     3423
     3424/**
     3425 * Displays next image link that has the same post parent.
     3426 *
     3427 * @since 2.5.0
     3428 *
     3429 * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array
     3430 *                           of width and height values in pixels (in that order). Default 'thumbnail'.
     3431 * @param string|false $text Optional. Link text. Default false.
     3432 */
    34053433function next_image_link( $size = 'thumbnail', $text = false ) {
    3406     adjacent_image_link( false, $size, $text );
    3407 }
    3408 
    3409 /**
    3410  * Displays next or previous image link that has the same post parent.
     3434    echo get_next_image_link( $size, $text );
     3435}
     3436
     3437/**
     3438 * Gets the next or previous image link that has the same post parent.
    34113439 *
    34123440 * Retrieves the current attachment object from the $post global.
    34133441 *
    3414  * @since 2.5.0
     3442 * @since 5.8.0
    34153443 *
    34163444 * @param bool         $prev Optional. Whether to display the next (false) or previous (true) link. Default true.
     
    34183446 *                           of width and height values in pixels (in that order). Default 'thumbnail'.
    34193447 * @param bool         $text Optional. Link text. Default false.
    3420  */
    3421 function adjacent_image_link( $prev = true, $size = 'thumbnail', $text = false ) {
     3448 * @return string Markup for image link.
     3449 */
     3450function get_adjacent_image_link( $prev = true, $size = 'thumbnail', $text = false ) {
    34223451    $post        = get_post();
    34233452    $attachments = array_values(
     
    34743503     * @param string $text          Link text.
    34753504     */
    3476     echo apply_filters( "{$adjacent}_image_link", $output, $attachment_id, $size, $text );
     3505    return apply_filters( "{$adjacent}_image_link", $output, $attachment_id, $size, $text );
     3506}
     3507
     3508/**
     3509 * Displays next or previous image link that has the same post parent.
     3510 *
     3511 * Retrieves the current attachment object from the $post global.
     3512 *
     3513 * @since 2.5.0
     3514 *
     3515 * @param bool         $prev Optional. Whether to display the next (false) or previous (true) link. Default true.
     3516 * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array
     3517 *                           of width and height values in pixels (in that order). Default 'thumbnail'.
     3518 * @param bool         $text Optional. Link text. Default false.
     3519 */
     3520function adjacent_image_link( $prev = true, $size = 'thumbnail', $text = false ) {
     3521    echo get_adjacent_image_link( $prev, $size, $text );
    34773522}
    34783523
Note: See TracChangeset for help on using the changeset viewer.