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