| 1802 | * Return the class for a post format content wrapper |
| 1803 | * |
| 1804 | * @since 3.6.0 |
| 1805 | * |
| 1806 | * @param string $format |
| 1807 | */ |
| 1808 | function get_post_format_content_class( $format ) { |
| 1809 | return apply_filters( 'post_format_content_class', $format . '-post-format-content', $format ); |
| 1810 | } |
| 1811 | |
| 1812 | /** |
| 1813 | * Ouput the class for a post format content wrapper |
| 1814 | * |
| 1815 | * @since 3.6.0 |
| 1816 | * |
| 1817 | * @param string $format |
| 1818 | */ |
| 1819 | function post_format_content_class( $format ) { |
| 1820 | echo get_post_format_content_class( $format ); |
| 1821 | } |
| 1822 | |
| 1823 | /** |
| 1824 | * Provide fallback behavior for Posts that have associated post format |
| 1825 | * |
| 1826 | * @since 3.6.0 |
| 1827 | * |
| 1828 | * @param string $content |
| 1829 | */ |
| 1830 | function post_formats_compat( $content, $id = 0 ) { |
| 1831 | $post = empty( $id ) ? get_post() : get_post( $id ); |
| 1832 | if ( empty( $post ) ) |
| 1833 | return $content; |
| 1834 | |
| 1835 | $format = get_post_format( $post ); |
| 1836 | if ( empty( $format ) || in_array( $format, array( 'status', 'aside', 'chat' ) ) ) |
| 1837 | return $content; |
| 1838 | |
| 1839 | if ( current_theme_supports( 'post-formats', $format ) ) |
| 1840 | return $content; |
| 1841 | |
| 1842 | $defaults = array( |
| 1843 | 'position' => 'after', |
| 1844 | 'tag' => 'div', |
| 1845 | 'class' => get_post_format_content_class( $format ), |
| 1846 | 'chat_tag' => 'span', |
| 1847 | 'chat_tag_class' => 'chat-line', |
| 1848 | 'chat_delimiter' => PHP_EOL, |
| 1849 | 'link_class' => '', |
| 1850 | 'image_class' => '', |
| 1851 | 'gallery' => '[gallery]', |
| 1852 | 'audio' => '[audio]', |
| 1853 | 'video' => '[video]' |
| 1854 | ); |
| 1855 | |
| 1856 | $args = apply_filters( 'post_format_compat', array() ); |
| 1857 | $compat = wp_parse_args( $args, $defaults ); |
| 1858 | |
| 1859 | $show_content = true; |
| 1860 | $format_output = ''; |
| 1861 | $meta = get_post_format_meta( $post->ID ); |
| 1862 | |
| 1863 | switch ( $format ) { |
| 1864 | case 'link': |
| 1865 | $compat['tag'] = ''; |
| 1866 | |
| 1867 | if ( ! empty( $meta['url'] ) ) { |
| 1868 | $esc_url = preg_quote( $meta['url'], '#' ); |
| 1869 | // Make sure the same URL isn't in the post (modified/extended versions allowed) |
| 1870 | if ( ! preg_match( '#' . $esc_url . '[^/&\?]#', $content ) ) { |
| 1871 | $format_output .= sprintf( |
| 1872 | '<a %shref="%s">%s</a>', |
| 1873 | empty( $compat['link_class'] ) ? '' : sprintf( 'class="%s" ', $compat['link_class'] ), |
| 1874 | esc_url( $meta['url'] ), |
| 1875 | empty( $post->post_title ) ? esc_url( $meta['url'] ) : apply_filters( 'the_title', $post->post_title ) |
| 1876 | ); |
| 1877 | } |
| 1878 | } |
| 1879 | break; |
| 1880 | |
| 1881 | case 'quote': |
| 1882 | if ( ! empty( $meta['quote'] ) && ! stristr( $content, $meta['quote'] ) ) { |
| 1883 | $format_output .= sprintf( '<blockquote>%s</blockquote>', $meta['quote'] ); |
| 1884 | if ( ! empty( $meta['quote_source'] ) ) { |
| 1885 | $format_output .= sprintf( |
| 1886 | '<cite>%s</cite>', |
| 1887 | ! empty( $meta['url'] ) ? |
| 1888 | sprintf( '<a href="%s">%s</a>', esc_url( $meta['url'] ), $meta['quote_source'] ) : |
| 1889 | $meta['quote_source'] |
| 1890 | ); |
| 1891 | } |
| 1892 | } |
| 1893 | break; |
| 1894 | |
| 1895 | case 'image': |
| 1896 | if ( ! empty( $meta['image'] ) ) { |
| 1897 | $image = is_numeric( $meta['image'] ) ? wp_get_attachment_url( $meta['image'] ) : $meta['image']; |
| 1898 | |
| 1899 | if ( ! empty( $image ) && ! stristr( $content, $image ) ) { |
| 1900 | $image_html = sprintf( |
| 1901 | '<img %ssrc="%s" alt="" />', |
| 1902 | empty( $compat['image_class'] ) ? '' : sprintf( 'class="%s" ', $compat['image_class'] ), |
| 1903 | $image |
| 1904 | ); |
| 1905 | if ( empty( $meta['url'] ) ) { |
| 1906 | $format_output .= $image_html; |
| 1907 | } else { |
| 1908 | $format_output .= sprintf( |
| 1909 | '<a href="%s">%s</a>', |
| 1910 | esc_url( $meta['url'] ), |
| 1911 | $image_html |
| 1912 | ); |
| 1913 | } |
| 1914 | } |
| 1915 | } |
| 1916 | break; |
| 1917 | |
| 1918 | case 'gallery': |
| 1919 | preg_match_all( '/' . get_shortcode_regex() . '/s', $content, $matches ); |
| 1920 | if ( ! empty( $matches ) && isset( $matches[2] ) ) { |
| 1921 | foreach ( (array) $matches[2] as $match ) { |
| 1922 | if ( 'gallery' === $match ) |
| 1923 | break 2; // foreach + case |
| 1924 | } |
| 1925 | } |
| 1926 | |
| 1927 | if ( empty( $meta['gallery'] ) && ! empty( $compat['gallery'] ) ) { |
| 1928 | $format_output .= $compat['gallery']; |
| 1929 | } elseif ( ! empty( $meta['gallery'] ) ) { |
| 1930 | $format_output .= $meta['gallery']; |
| 1931 | } |
| 1932 | break; |
| 1933 | |
| 1934 | case 'video': |
| 1935 | case 'audio': |
| 1936 | $shortcode_regex = '/' . get_shortcode_regex() . '/s'; |
| 1937 | $matches = preg_match( $shortcode_regex, $content ); |
| 1938 | if ( ! $matches || $format !== $matches[2] ) { |
| 1939 | if ( empty( $meta['media'] ) && ! empty( $compat[$format] ) ) { |
| 1940 | $format_output .= $compat[$format]; |
| 1941 | } elseif ( ! empty( $meta['media'] ) ) { |
| 1942 | // the metadata is a shortcode or an embed code |
| 1943 | if ( preg_match( $shortcode_regex, $meta['media'] ) || preg_match( '#<[^>]+>#', $meta['media'] ) ) { |
| 1944 | $format_output .= $meta['media']; |
| 1945 | } elseif ( ! stristr( $content, $meta['media'] ) ) { |
| 1946 | $mime_type = wp_check_filetype( $meta['media'] ); |
| 1947 | // URL that can be shortcode'd |
| 1948 | if ( ! empty( $mime_type['type'] ) |
| 1949 | && ( ( 'video' === $format && 'video/mp4' === $mime_type['type'] ) |
| 1950 | || ( 'audio' === $format && 'audio/mpeg' === $mime_type['type'] ) ) ) { |
| 1951 | $format_output .= sprintf( '[%s src="%s"]', $format, esc_url( $meta['media'] ) ); |
| 1952 | // URL that isn't embeddable outputs string |
| 1953 | } else { |
| 1954 | $format_output .= sprintf( '[embed]%s[/embed]', $meta['media'] ); |
| 1955 | } |
| 1956 | } |
| 1957 | } |
| 1958 | } |
| 1959 | break; |
| 1960 | default: |
| 1961 | return $content; |
| 1962 | break; |
| 1963 | } |
| 1964 | |
| 1965 | if ( empty( $format_output ) ) |
| 1966 | return $content; |
| 1967 | |
| 1968 | $output = ''; |
| 1969 | if ( ! empty( $compat['tag'] ) ) |
| 1970 | $output .= sprintf( '<%s class="%s">', $compat['tag'], $compat['class'] ); |
| 1971 | |
| 1972 | if ( ! empty( $content ) && $show_content && 'before' !== $compat['position'] ) |
| 1973 | $output .= $content . PHP_EOL . PHP_EOL; |
| 1974 | |
| 1975 | $output .= $format_output; |
| 1976 | |
| 1977 | if ( ! empty( $content ) && $show_content && 'before' === $compat['position'] ) |
| 1978 | $output .= PHP_EOL . PHP_EOL . $content; |
| 1979 | |
| 1980 | if ( ! empty( $compat['tag'] ) ) |
| 1981 | $output .= sprintf( '</%s>', $compat['tag'] );; |
| 1982 | |
| 1983 | return $output; |
| 1984 | } |
| 1985 | |
| 1986 | /** |