| | 1545 | |
| | 1546 | /** |
| | 1547 | * Retrieve audio attached to the passed post |
| | 1548 | * |
| | 1549 | * @since 3.6.0 |
| | 1550 | * |
| | 1551 | * @param int $post_id Post ID |
| | 1552 | * @return array Found audio attachments |
| | 1553 | */ |
| | 1554 | function get_post_audio( $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' => 'audio', |
| | 1563 | 'posts_per_page' => -1 |
| | 1564 | ) ); |
| | 1565 | |
| | 1566 | if ( ! empty( $children ) ) |
| | 1567 | return $children; |
| | 1568 | } |
| | 1569 | |
| | 1570 | /** |
| | 1571 | * Check the content blob for an <audio>, <object>, <embed>, or <iframe>, in that order |
| | 1572 | * If no HTML tag is found, check the first line of the post for a URL |
| | 1573 | * |
| | 1574 | * @param string $content A string which might contain audio data. |
| | 1575 | * @param boolean $remove Whether to remove the found URL from the passed content. |
| | 1576 | * @return string The found data |
| | 1577 | */ |
| | 1578 | function get_content_audio( &$content, $remove = false ) { |
| | 1579 | $html = $matches = ''; |
| | 1580 | foreach ( array( 'audio', 'object', 'embed', 'iframe' ) as $tag ) { |
| | 1581 | if ( preg_match( '#' . get_tag_regex( $tag ) . '#', $content, $matches ) ) { |
| | 1582 | $html = $matches[1]; |
| | 1583 | if ( $remove ) |
| | 1584 | $content = str_replace( $matches[0], '', $content ); |
| | 1585 | |
| | 1586 | return $html; |
| | 1587 | } |
| | 1588 | } |
| | 1589 | |
| | 1590 | $lines = explode( "\n", trim( $content ) ); |
| | 1591 | $line = trim( array_shift( $lines ) ); |
| | 1592 | |
| | 1593 | if ( 0 === stripos( $line, 'http' ) ) { |
| | 1594 | if ( $remove ) |
| | 1595 | $content = join( "\n", $lines ); |
| | 1596 | |
| | 1597 | return $line; |
| | 1598 | } |
| | 1599 | } |
| | 1600 | |
| | 1601 | /** |
| | 1602 | * Return the found audio data for the passed post |
| | 1603 | * |
| | 1604 | * @since 3.6.0 |
| | 1605 | * |
| | 1606 | * @param int $id Optional. Post ID |
| | 1607 | */ |
| | 1608 | function get_the_audio( $id = 0 ) { |
| | 1609 | $post = empty( $id ) ? get_post() : get_post( $id ); |
| | 1610 | if ( empty( $post ) ) |
| | 1611 | return; |
| | 1612 | |
| | 1613 | if ( shortcode_exists( 'audio' ) ) |
| | 1614 | return do_shortcode( '[audio /]' ); |
| | 1615 | |
| | 1616 | $data = get_content_audio( $post->post_content ); |
| | 1617 | if ( ! empty( $data ) ) |
| | 1618 | return $data; |
| | 1619 | |
| | 1620 | $audios = get_post_audio( $post->ID ); |
| | 1621 | if ( empty( $audios ) ) |
| | 1622 | return; |
| | 1623 | |
| | 1624 | $audio = reset( $audios ); |
| | 1625 | return wp_get_attachment_url( $audio->ID ); |
| | 1626 | } |
| | 1627 | |
| | 1628 | /** |
| | 1629 | * Output the found audio data for the current post |
| | 1630 | * |
| | 1631 | * @since 3.6.0 |
| | 1632 | */ |
| | 1633 | function the_audio() { |
| | 1634 | echo apply_filters( 'the_audio', get_the_audio() ); |
| | 1635 | } |
| | 1636 | |
| | 1637 | /** |
| | 1638 | * Retrieve video attached to the passed post |
| | 1639 | * |
| | 1640 | * @since 3.6.0 |
| | 1641 | * |
| | 1642 | * @param int $post_id Post ID |
| | 1643 | * @return array Found video attachments |
| | 1644 | */ |
| | 1645 | function get_post_video( $post_id = 0 ) { |
| | 1646 | $post = empty( $post_id ) ? get_post() : get_post( $post_id ); |
| | 1647 | if ( empty( $post ) ) |
| | 1648 | return; |
| | 1649 | |
| | 1650 | $children = get_children( array( |
| | 1651 | 'post_parent' => $post->ID, |
| | 1652 | 'post_type' => 'attachment', |
| | 1653 | 'post_mime_type' => 'video', |
| | 1654 | 'posts_per_page' => -1 |
| | 1655 | ) ); |
| | 1656 | |
| | 1657 | if ( ! empty( $children ) ) |
| | 1658 | return $children; |
| | 1659 | } |
| | 1660 | |
| | 1661 | /** |
| | 1662 | * Check the content blob for a <video>, <object>, <embed>, or <iframe>, in that order |
| | 1663 | * If no HTML tag is found, check the first line of the post for a URL |
| | 1664 | * |
| | 1665 | * @param string $content A string which might contain video data. |
| | 1666 | * @param boolean $remove Whether to remove the found URL from the passed content. |
| | 1667 | * @return string The found data |
| | 1668 | */ |
| | 1669 | function get_content_video( &$content, $remove = false ) { |
| | 1670 | $html = $matches = ''; |
| | 1671 | foreach ( array( 'video', 'object', 'embed', 'iframe' ) as $tag ) { |
| | 1672 | if ( preg_match( '#' . get_tag_regex( $tag ) . '#', $content, $matches ) ) { |
| | 1673 | $html = $matches[1]; |
| | 1674 | if ( $remove ) |
| | 1675 | $content = str_replace( $matches[0], '', $content ); |
| | 1676 | |
| | 1677 | return $html; |
| | 1678 | } |
| | 1679 | } |
| | 1680 | |
| | 1681 | $lines = explode( "\n", trim( $content ) ); |
| | 1682 | $line = trim( array_shift( $lines ) ); |
| | 1683 | |
| | 1684 | if ( 0 === stripos( $line, 'http' ) ) { |
| | 1685 | if ( $remove ) |
| | 1686 | $content = join( "\n", $lines ); |
| | 1687 | |
| | 1688 | return $line; |
| | 1689 | } |
| | 1690 | } |
| | 1691 | |
| | 1692 | /** |
| | 1693 | * Return the found video data for the passed post |
| | 1694 | * |
| | 1695 | * @since 3.6.0 |
| | 1696 | * |
| | 1697 | * @param int $id Optional. Post ID |
| | 1698 | */ |
| | 1699 | function get_the_video( $id = 0 ) { |
| | 1700 | $post = empty( $id ) ? get_post() : get_post( $id ); |
| | 1701 | if ( empty( $post ) ) |
| | 1702 | return; |
| | 1703 | |
| | 1704 | if ( shortcode_exists( 'video' ) ) |
| | 1705 | return do_shortcode( '[video /]' ); |
| | 1706 | |
| | 1707 | $data = get_content_video( $post->post_content ); |
| | 1708 | if ( ! empty( $data ) ) |
| | 1709 | return $data; |
| | 1710 | |
| | 1711 | $videos = get_post_video( $post->ID ); |
| | 1712 | if ( empty( $videos ) ) |
| | 1713 | return; |
| | 1714 | |
| | 1715 | $video = reset( $videos ); |
| | 1716 | return wp_get_attachment_url( $video->ID ); |
| | 1717 | } |
| | 1718 | |
| | 1719 | /** |
| | 1720 | * Output the found video data for the current post |
| | 1721 | * |
| | 1722 | * @since 3.6.0 |
| | 1723 | */ |
| | 1724 | function the_video() { |
| | 1725 | echo apply_filters( 'the_video', get_the_video() ); |
| | 1726 | } |
| | 1727 | No newline at end of file |