| 1547 | |
| 1548 | /** |
| 1549 | * Retrieve images attached to the passed post |
| 1550 | * |
| 1551 | * @since 3.6.0 |
| 1552 | * |
| 1553 | * @param int $post_id Post ID |
| 1554 | * @return array Found image attachments |
| 1555 | */ |
| 1556 | function get_attached_images( $post_id = 0 ) { |
| 1557 | $post = empty( $post_id ) ? get_post() : get_post( $post_id ); |
| 1558 | if ( empty( $post ) ) |
| 1559 | return array(); |
| 1560 | |
| 1561 | $children = get_children( array( |
| 1562 | 'post_parent' => $post->ID, |
| 1563 | 'post_type' => 'attachment', |
| 1564 | 'post_mime_type' => 'image', |
| 1565 | 'posts_per_page' => -1, |
| 1566 | 'orderby' => 'menu_order', |
| 1567 | 'order' => 'ASC' |
| 1568 | ) ); |
| 1569 | |
| 1570 | if ( ! empty( $children ) ) |
| 1571 | return $children; |
| 1572 | |
| 1573 | return array(); |
| 1574 | } |
| 1575 | |
| 1576 | /** |
| 1577 | * Retrieve images attached to the passed post |
| 1578 | * |
| 1579 | * @since 3.6.0 |
| 1580 | * |
| 1581 | * @param int $post_id Post ID |
| 1582 | * @return array Found image attachments |
| 1583 | */ |
| 1584 | function get_attached_image_srcs( $post_id = 0 ) { |
| 1585 | $children = get_attached_images( $post_id ); |
| 1586 | if ( empty( $children ) ) |
| 1587 | return array(); |
| 1588 | |
| 1589 | $srcs = array(); |
| 1590 | foreach ( $children as $attachment ) |
| 1591 | $srcs[] = wp_get_attachment_url( $attachment->ID ); |
| 1592 | |
| 1593 | return $srcs; |
| 1594 | } |
| 1595 | |
| 1596 | /** |
| 1597 | * Check the content blob for images |
| 1598 | * |
| 1599 | * @since 3.6.0 |
| 1600 | * |
| 1601 | * @param string $content A string which might contain image data. |
| 1602 | * @param boolean $remove Whether to remove the found data from the passed content. |
| 1603 | * @param int $limit Optional. The number of galleries to return |
| 1604 | * @return string The found data |
| 1605 | */ |
| 1606 | function get_content_images( &$content, $remove = false, $limit = 0 ) { |
| 1607 | $src = ''; |
| 1608 | $srcs = array(); |
| 1609 | $matches = array(); |
| 1610 | |
| 1611 | if ( preg_match_all( '#' . get_tag_regex( 'img' ) . '#i', $content, $matches, PREG_SET_ORDER ) && ! empty( $matches ) ) { |
| 1612 | foreach ( $matches as $tag ) { |
| 1613 | $count = 1; |
| 1614 | if ( $remove ) |
| 1615 | $content = str_replace( $tag[0], '', $content, $count ); |
| 1616 | |
| 1617 | preg_match( '#src=[\'"](.+?)[\'"]#is', $tag[1], $src ); |
| 1618 | if ( ! empty( $src[1] ) ) { |
| 1619 | $srcs[] = $src[1]; |
| 1620 | if ( $limit > 0 && count( $srcs ) >= $limit ) |
| 1621 | break; |
| 1622 | } |
| 1623 | } |
| 1624 | } |
| 1625 | |
| 1626 | return array_values( array_unique( $srcs ) ); |
| 1627 | } |
| 1628 | |
| 1629 | /** |
| 1630 | * Check the content blob for images and return the first |
| 1631 | * |
| 1632 | * @since 3.6.0 |
| 1633 | * |
| 1634 | * @param string $content A string which might contain image data. |
| 1635 | * @param boolean $remove Whether to remove the found data from the passed content. |
| 1636 | * @return string The found data |
| 1637 | */ |
| 1638 | function get_content_image( &$content, $remove = false ) { |
| 1639 | $srcs = get_content_images( $content, $remove, 1 ); |
| 1640 | return reset( $srcs ); |
| 1641 | } |
| 1642 | |
| 1643 | /** |
| 1644 | * Check the content blob for galleries and return their image srcs |
| 1645 | * |
| 1646 | * @since 3.6.0 |
| 1647 | * |
| 1648 | * @param string $content A string which might contain image data. |
| 1649 | * @param boolean $remove Optional. Whether to remove the found data from the passed content. |
| 1650 | * @param int $limit Optional. The number of galleries to return |
| 1651 | * @return array A list of galleries, which in turn are a list of their srcs in order |
| 1652 | */ |
| 1653 | function get_content_galleries( &$content, $remove = false, $limit = 0 ) { |
| 1654 | $src = ''; |
| 1655 | $galleries = array(); |
| 1656 | $matches = array(); |
| 1657 | |
| 1658 | if ( preg_match_all( '/' . get_shortcode_regex() . '/s', $content, $matches, PREG_SET_ORDER ) && ! empty( $matches ) ) { |
| 1659 | foreach ( $matches as $shortcode ) { |
| 1660 | if ( 'gallery' === $shortcode[2] ) { |
| 1661 | $srcs = array(); |
| 1662 | $data = array(); |
| 1663 | $count = 1; |
| 1664 | if ( $remove ) |
| 1665 | $content = str_replace( $shortcode[0], '', $content, $count ); |
| 1666 | |
| 1667 | $gallery = do_shortcode_tag( $shortcode ); |
| 1668 | preg_match_all( '#src=[\'"](.+?)[\'"]#is', $gallery, $src, PREG_SET_ORDER ); |
| 1669 | if ( ! empty( $src ) ) { |
| 1670 | foreach ( $src as $s ) |
| 1671 | $srcs[] = $s[1]; |
| 1672 | } |
| 1673 | // the function will eventually return more data about the gallery |
| 1674 | $data['src'] = array_values( array_unique( $srcs ) ); |
| 1675 | $galleries[] = $data; |
| 1676 | if ( $limit > 0 && count( $galleries ) >= $limit ) |
| 1677 | break; |
| 1678 | } |
| 1679 | } |
| 1680 | } |
| 1681 | |
| 1682 | return $galleries; |
| 1683 | } |
| 1684 | |
| 1685 | /** |
| 1686 | * Retrieve galleries from the passed post's content |
| 1687 | * |
| 1688 | * @since 3.6.0 |
| 1689 | * |
| 1690 | * @param int $post_id A string which might contain image data. |
| 1691 | * @return array A list of galleries, which in turn are a list of their srcs in order |
| 1692 | */ |
| 1693 | function get_post_galleries_images( $post_id = 0 ) { |
| 1694 | $post = empty( $post_id ) ? clone get_post() : get_post( $post_id ); |
| 1695 | if ( empty( $post ) ) |
| 1696 | return array(); |
| 1697 | |
| 1698 | $data = get_content_galleries( $post->post_content ); |
| 1699 | return wp_list_pluck( $data, 'src' ); |
| 1700 | } |
| 1701 | |
| 1702 | /** |
| 1703 | * Check the content blob for galleries and return the image srcs for the first found gallery |
| 1704 | * |
| 1705 | * @since 3.6.0 |
| 1706 | * |
| 1707 | * @param int $post_id A string which might contain image data. |
| 1708 | * @return array A list of a gallery's image srcs in order |
| 1709 | */ |
| 1710 | function get_post_gallery_images( $post_id = 0 ) { |
| 1711 | $post = empty( $post_id ) ? clone get_post() : get_post( $post_id ); |
| 1712 | if ( empty( $post ) ) |
| 1713 | return array(); |
| 1714 | |
| 1715 | $data = get_content_galleries( $post->post_content, false, 1 ); |
| 1716 | return reset( wp_list_pluck( $data, 'src' ) ); |
| 1717 | } |
| 1718 | No newline at end of file |