| | 1802 | * Extract a URL from passed content, if possible |
| | 1803 | * Checks for a URL on the first line of the content or the first encountered href attribute. |
| | 1804 | * |
| | 1805 | * @since 3.6.0 |
| | 1806 | * |
| | 1807 | * @param string $content A string which might contain a URL. |
| | 1808 | * @param boolean $remove Whether the remove the found URL from the passed content. |
| | 1809 | * @return string The found URL. |
| | 1810 | */ |
| | 1811 | function get_content_link( &$content, $remove = false ) { |
| | 1812 | if ( empty( $content ) ) |
| | 1813 | return; |
| | 1814 | |
| | 1815 | // the content is a URL |
| | 1816 | $trimmed = trim( $content ); |
| | 1817 | if ( 0 === stripos( $trimmed, 'http' ) && ! preg_match( '#\s#', $trimmed ) ) { |
| | 1818 | if ( $remove ) |
| | 1819 | $content = ''; |
| | 1820 | |
| | 1821 | return $trimmed; |
| | 1822 | // the content is HTML so we grab the first href |
| | 1823 | } elseif ( preg_match( '/<a\s[^>]*?href=[\'"](.+?)[\'"]/is', $content, $matches ) ) { |
| | 1824 | return esc_url_raw( $matches[1] ); |
| | 1825 | } |
| | 1826 | |
| | 1827 | $lines = explode( "\n", $trimmed ); |
| | 1828 | $line = trim( array_shift( $lines ) ); |
| | 1829 | |
| | 1830 | // the content is a URL followed by content |
| | 1831 | if ( 0 === stripos( $line, 'http' ) ) { |
| | 1832 | if ( $remove ) |
| | 1833 | $content = trim( join( "\n", $lines ) ); |
| | 1834 | |
| | 1835 | return esc_url_raw( $line ); |
| | 1836 | } |
| | 1837 | } |
| | 1838 | |
| | 1839 | /** |
| | 1840 | * Attempt to retrieve a URL from a post's content |
| | 1841 | * |
| | 1842 | * @since 3.6.0 |
| | 1843 | * |
| | 1844 | * @param int $id Optional. Post ID. |
| | 1845 | * @return string A URL, if found. |
| | 1846 | */ |
| | 1847 | function get_the_link( $id = 0 ) { |
| | 1848 | $post = empty( $id ) ? get_post() : get_post( $id ); |
| | 1849 | |
| | 1850 | if ( empty( $post ) ) |
| | 1851 | return; |
| | 1852 | |
| | 1853 | if ( has_post_format( 'link', $post ) ) { |
| | 1854 | $meta = get_post_format_meta( $post->ID ); |
| | 1855 | if ( ! empty( $meta['url'] ) ) |
| | 1856 | return esc_url_raw( $meta['url'] ); |
| | 1857 | } |
| | 1858 | |
| | 1859 | if ( ! empty( $post->post_content ) ) |
| | 1860 | return get_content_link( $post->post_content ); |
| | 1861 | } |
| | 1862 | |
| | 1863 | /** |
| | 1864 | * Attempt to output a URL from a post's content |
| | 1865 | * |
| | 1866 | * @since 3.6.0 |
| | 1867 | *. |
| | 1868 | */ |
| | 1869 | function the_link() { |
| | 1870 | echo get_the_link( 0 ); |
| | 1871 | } |
| | 1872 | |
| | 1873 | /** |
| 1867 | | if ( ! preg_match( '#' . $esc_url . '[^/&\?]#', $content ) ) { |
| 1868 | | $format_output .= sprintf( |
| 1869 | | '<a %shref="%s">%s</a>', |
| 1870 | | empty( $compat['link_class'] ) ? '' : sprintf( 'class="%s" ', esc_attr( $compat['link_class'] ) ), |
| 1871 | | esc_url( $meta['url'] ), |
| 1872 | | empty( $post->post_title ) ? esc_url( $meta['url'] ) : apply_filters( 'the_title', $post->post_title ) |
| 1873 | | ); |
| 1874 | | } |
| | 1939 | if ( ! preg_match( '#' . $esc_url . '[^/&\?]#', $content ) ) |
| | 1940 | $url = $meta['url']; |
| | 1941 | } else { |
| | 1942 | $compat['position'] = 'before'; |
| | 1943 | $content_before = $content; |
| | 1944 | $url = get_content_link( $content, true ); |
| | 1945 | if ( $content_before == $content ) |
| | 1946 | $url = ''; |
| | 1947 | } |
| | 1948 | |
| | 1949 | if ( ! empty( $url ) ) { |
| | 1950 | $format_output .= sprintf( |
| | 1951 | '<a %shref="%s">%s</a>', |
| | 1952 | empty( $compat['link_class'] ) ? '' : sprintf( 'class="%s" ', esc_attr( $compat['link_class'] ) ), |
| | 1953 | esc_url( $url ), |
| | 1954 | empty( $post->post_title ) ? esc_url( $meta['url'] ) : apply_filters( 'the_title', $post->post_title ) |
| | 1955 | ); |