| 1545 | |
| 1546 | /** |
| 1547 | * Retrieve images attached to the passed post |
| 1548 | * |
| 1549 | * @since 3.6.0 |
| 1550 | * |
| 1551 | * @param int $post_id Post ID |
| 1552 | * @return array Found image attachments |
| 1553 | */ |
| 1554 | function get_post_images( $post_id = 0 ) { |
| 1555 | $post = empty( $post_id ) ? get_post() : get_post( $post_id ); |
| 1556 | if ( empty( $post ) ) |
| 1557 | return; |
| 1558 | |
| 1559 | $children = get_children( array( |
| 1560 | 'post_parent' => $post->ID, |
| 1561 | 'post_type' => 'attachment', |
| 1562 | 'post_mime_type' => 'image', |
| 1563 | 'posts_per_page' => -1, |
| 1564 | 'orderby' => 'menu_order', |
| 1565 | 'order' => 'ASC' |
| 1566 | ) ); |
| 1567 | |
| 1568 | if ( ! empty( $children ) ) |
| 1569 | return $children; |
| 1570 | } |
| 1571 | |
| 1572 | /** |
| 1573 | * Check the content blob for an <img> |
| 1574 | * If no HTML tag is found, check the first line of the post for a URL |
| 1575 | * |
| 1576 | * @param string $content A string which might contain image data. |
| 1577 | * @param boolean $remove Whether to remove the found data from the passed content. |
| 1578 | * @return string The found data |
| 1579 | */ |
| 1580 | function get_content_image( &$content, $remove = false ) { |
| 1581 | $matches = $match = $src = ''; |
| 1582 | |
| 1583 | if ( preg_match( '/' . get_shortcode_regex() . '/s', $content, $match ) && 'gallery' == $match[2] ) { |
| 1584 | $count = 1; |
| 1585 | if ( $remove ) |
| 1586 | $content = str_replace( $match[0], '', $content, $count ); |
| 1587 | |
| 1588 | $gallery = do_shortcode_tag( $match ); |
| 1589 | preg_match( '#src=[\'"](.+?)[\'"]#is', $gallery, $src ); |
| 1590 | if ( ! empty( $src[1] ) ) |
| 1591 | return $src[1]; |
| 1592 | } |
| 1593 | |
| 1594 | if ( preg_match( '#' . get_tag_regex( 'img' ) . '#i', $content, $matches ) ) { |
| 1595 | $count = 1; |
| 1596 | if ( $remove ) |
| 1597 | $content = str_replace( $matches[0], '', $content, $count ); |
| 1598 | |
| 1599 | preg_match( '#src=[\'"](.+?)[\'"]#is', $matches[1], $src ); |
| 1600 | if ( ! empty( $src[1] ) ) |
| 1601 | return $src[1]; |
| 1602 | } |
| 1603 | |
| 1604 | $lines = explode( "\n", trim( $content ) ); |
| 1605 | $line = trim( array_shift( $lines ) ); |
| 1606 | |
| 1607 | if ( 0 === stripos( $line, 'http' ) ) { |
| 1608 | if ( $remove ) |
| 1609 | $content = join( "\n", $lines ); |
| 1610 | |
| 1611 | return $line; |
| 1612 | } |
| 1613 | } |
| 1614 | |
| 1615 | /** |
| 1616 | * Return the found image data for the passed post |
| 1617 | * |
| 1618 | * @since 3.6.0 |
| 1619 | * |
| 1620 | * @param int $id Optional. Post ID |
| 1621 | */ |
| 1622 | function get_the_image( $id = 0 ) { |
| 1623 | $post = empty( $id ) ? get_post() : get_post( $id ); |
| 1624 | if ( empty( $post ) ) |
| 1625 | return ''; |
| 1626 | |
| 1627 | $data = get_content_image( $post->post_content ); |
| 1628 | if ( ! empty( $data ) ) |
| 1629 | return $data; |
| 1630 | |
| 1631 | $images = get_post_images( $post->ID ); |
| 1632 | if ( empty( $images ) ) |
| 1633 | return ''; |
| 1634 | |
| 1635 | $image = reset( $images ); |
| 1636 | return wp_get_attachment_url( $image->ID ); |
| 1637 | } |
| 1638 | |
| 1639 | /** |
| 1640 | * Output the found image data for the current post |
| 1641 | * |
| 1642 | * @since 3.6.0 |
| 1643 | */ |
| 1644 | function the_image() { |
| 1645 | echo apply_filters( 'the_image', get_the_image() ); |
| 1646 | } |
| 1647 | No newline at end of file |