| 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 images |
| 1574 | * |
| 1575 | * @param string $content A string which might contain image data. |
| 1576 | * @param boolean $remove Whether to remove the found data from the passed content. |
| 1577 | * @return string The found data |
| 1578 | */ |
| 1579 | function get_content_images( &$content, $remove = false ) { |
| 1580 | $src = ''; |
| 1581 | $srcs = array(); |
| 1582 | $matches = array(); |
| 1583 | $match = array(); |
| 1584 | |
| 1585 | if ( preg_match_all( '/' . get_shortcode_regex() . '/s', $content, $match, PREG_SET_ORDER ) && ! empty( $match ) ) { |
| 1586 | foreach ( $match as $i => $shortcode ) { |
| 1587 | if ( 'gallery' === $shortcode[2] ) { |
| 1588 | $count = 1; |
| 1589 | if ( $remove ) |
| 1590 | $content = str_replace( $shortcode[0], '', $content, $count ); |
| 1591 | |
| 1592 | $gallery = do_shortcode_tag( $shortcode ); |
| 1593 | preg_match_all( '#src=[\'"](.+?)[\'"]#is', $gallery, $src, PREG_SET_ORDER ); |
| 1594 | if ( ! empty( $src ) ) { |
| 1595 | foreach ( $src as $s ) |
| 1596 | $srcs[] = $s[1]; |
| 1597 | } |
| 1598 | } |
| 1599 | } |
| 1600 | } |
| 1601 | |
| 1602 | if ( preg_match_all( '#' . get_tag_regex( 'img' ) . '#i', $content, $matches, PREG_SET_ORDER ) && ! empty( $matches ) ) { |
| 1603 | foreach ( $matches as $tag ) { |
| 1604 | $count = 1; |
| 1605 | if ( $remove ) |
| 1606 | $content = str_replace( $tag[0], '', $content, $count ); |
| 1607 | |
| 1608 | preg_match( '#src=[\'"](.+?)[\'"]#is', $tag[1], $src ); |
| 1609 | if ( ! empty( $src[1] ) ) |
| 1610 | $srcs[] = $src[1]; |
| 1611 | } |
| 1612 | } |
| 1613 | |
| 1614 | return array_values( array_unique( $srcs ) ); |
| 1615 | } |
| 1616 | |
| 1617 | /** |
| 1618 | * Return the found gallery / image data for the passed post |
| 1619 | * |
| 1620 | * @since 3.6.0 |
| 1621 | * |
| 1622 | * @param int $id Optional. Post ID |
| 1623 | */ |
| 1624 | function get_the_images( $id = 0 ) { |
| 1625 | $post = empty( $id ) ? get_post() : get_post( $id ); |
| 1626 | if ( empty( $post ) ) |
| 1627 | return ''; |
| 1628 | |
| 1629 | $srcs = array(); |
| 1630 | |
| 1631 | $data = get_content_images( $post->post_content ); |
| 1632 | if ( ! empty( $data ) ) |
| 1633 | $srcs = array_merge( $srcs, $data ); |
| 1634 | |
| 1635 | $images = get_post_images( $post->ID ); |
| 1636 | if ( empty( $images ) ) |
| 1637 | return $srcs; |
| 1638 | |
| 1639 | foreach ( $images as $image ) |
| 1640 | $srcs[] = wp_get_attachment_url( $image->ID ); |
| 1641 | |
| 1642 | return array_values( array_unique( $srcs ) ); |
| 1643 | } |
| 1644 | No newline at end of file |