Make WordPress Core


Ignore:
Timestamp:
02/18/2013 07:22:58 PM (14 years ago)
Author:
helen
Message:

Output fallbacks / theme compat for post format metadata.

  • Currently handles link, quote, image, gallery, audio, and video formats.
  • add_theme_support() for a given post format is now an indicator that the theme handles format-specific metadata.
  • If no support for a given format is defined, fallback output will be generated and hooked onto the_content if a post has metadata for that format.
  • Fallbacks attempt to be smart about not duplicating data already appearing in the post content itself. Gallery is particularly liberal, looking for any instance of the gallery shortcode in the content, not just an exact match to the gallery shortcode defined in the format-specific meta.
  • Compat output defaults to being wrapped in a div with a class of post-format-content.

Theme authors: please test and evaluate, keeping in mind that the goal is to support user expectations of not losing format-specific data they've entered in the admin when viewing the front-end of their site.

props wonderboymusic, beaulebens, helen. see #23347.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/formatting.php

    r23434 r23450  
    17961796                $output = $text;
    17971797        }
     1798        return $output;
     1799}
     1800
     1801/**
     1802 * Return the class for a post format content wrapper
     1803 *
     1804 * @since 3.6.0
     1805 *
     1806 * @param string $format
     1807 */
     1808function get_post_format_content_class( $format ) {
     1809        return apply_filters( 'post_format_content_class', '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 */
     1819function 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 */
     1830function 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                'link_class' => '',
     1847                'image_class' => '',
     1848                'gallery' => '[gallery]',
     1849                'audio' => '',
     1850                'video' => ''
     1851        );
     1852
     1853        $args = apply_filters( 'post_format_compat', array() );
     1854        $compat = wp_parse_args( $args, $defaults );
     1855
     1856        $show_content = true;
     1857        $format_output = '';
     1858        $meta = get_post_format_meta( $post->ID );
     1859
     1860        switch ( $format ) {
     1861                case 'link':
     1862                        $compat['tag'] = '';
     1863
     1864                        if ( ! empty( $meta['url'] ) ) {
     1865                                $esc_url = preg_quote( $meta['url'], '#' );
     1866                                // Make sure the same URL isn't in the post (modified/extended versions allowed)
     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                                }
     1875                        }
     1876                        break;
     1877
     1878                case 'quote':
     1879                        if ( ! empty( $meta['quote'] ) && ! stristr( $content, $meta['quote'] ) ) {
     1880                                $format_output .= sprintf( '<blockquote>%s</blockquote>', $meta['quote'] );
     1881                                if ( ! empty( $meta['quote_source'] ) ) {
     1882                                        $format_output .= sprintf(
     1883                                                '<cite>%s</cite>',
     1884                                                ! empty( $meta['url'] ) ?
     1885                                                        sprintf( '<a href="%s">%s</a>', esc_url( $meta['url'] ), $meta['quote_source'] ) :
     1886                                                        $meta['quote_source']
     1887                                        );
     1888                                }
     1889                        }
     1890                        break;
     1891
     1892                case 'image':
     1893                        if ( ! empty( $meta['image'] ) ) {
     1894                                $image = is_numeric( $meta['image'] ) ? wp_get_attachment_url( $meta['image'] ) : $meta['image'];
     1895
     1896                                if ( ! empty( $image ) && ! stristr( $content, $image ) ) {
     1897                                        $image_html = sprintf(
     1898                                                '<img %ssrc="%s" alt="" />',
     1899                                                empty( $compat['image_class'] ) ? '' : sprintf( 'class="%s" ', esc_attr( $compat['image_class'] ) ),
     1900                                                $image
     1901                                        );
     1902                                        if ( empty( $meta['url'] ) ) {
     1903                                                $format_output .= $image_html;
     1904                                        } else {
     1905                                                $format_output .= sprintf(
     1906                                                        '<a href="%s">%s</a>',
     1907                                                        esc_url( $meta['url'] ),
     1908                                                        $image_html
     1909                                                );
     1910                                        }
     1911                                }
     1912                        }
     1913                        break;
     1914
     1915                case 'gallery':
     1916                        preg_match_all( '/' . get_shortcode_regex() . '/s', $content, $matches );
     1917                        if ( ! empty( $matches ) && isset( $matches[2] ) ) {
     1918                                foreach ( (array) $matches[2] as $match ) {
     1919                                        if ( 'gallery' === $match )
     1920                                                break 2; // foreach + case
     1921                                }
     1922                        }
     1923
     1924                        if ( empty( $meta['gallery'] ) && ! empty( $compat['gallery'] ) ) {
     1925                                $format_output .= $compat['gallery'];
     1926                        } elseif ( ! empty( $meta['gallery'] ) ) {
     1927                                $format_output .= $meta['gallery'];
     1928                        }
     1929                        break;
     1930
     1931                case 'video':
     1932                case 'audio':
     1933                        $shortcode_regex = '/' . get_shortcode_regex() . '/s';
     1934                        $matches = preg_match( $shortcode_regex, $content );
     1935                        if ( ! $matches || $format !== $matches[2] ) {
     1936                                if ( empty( $meta['media'] ) && ! empty( $compat[$format] ) ) {
     1937                                        $format_output .= $compat[$format];
     1938                                } elseif ( ! empty( $meta['media'] ) ) {
     1939                                        // the metadata is a shortcode or an embed code
     1940                                        if ( preg_match( $shortcode_regex, $meta['media'] ) || preg_match( '#<[^>]+>#', $meta['media'] ) ) {
     1941                                                $format_output .= $meta['media'];
     1942                                        } elseif ( ! stristr( $content, $meta['media'] ) ) {
     1943                                                // attempt to embed the URL
     1944                                                $format_output .= sprintf( '[embed]%s[/embed]', $meta['media'] );
     1945                                        }
     1946                                }
     1947                        }
     1948                        break;
     1949                default:
     1950                        return $content;
     1951                        break;
     1952        }
     1953
     1954        if ( empty( $format_output ) )
     1955                return $content;
     1956
     1957        $output = '';
     1958
     1959        if ( ! empty( $content ) && $show_content && 'before' !== $compat['position'] )
     1960                $output .= $content . PHP_EOL . PHP_EOL;
     1961
     1962        if ( ! empty( $compat['tag'] ) )
     1963                $output .= sprintf( '<%s class="%s">', tag_escape( $compat['tag'] ), esc_attr( $compat['class'] ) );
     1964
     1965        $output .= $format_output;
     1966
     1967        if ( ! empty( $compat['tag'] ) )
     1968                $output .= sprintf( '</%s>', tag_escape( $compat['tag'] ) );
     1969
     1970        if ( ! empty( $content ) && $show_content && 'before' === $compat['position'] )
     1971                $output .= PHP_EOL . PHP_EOL . $content;
     1972
    17981973        return $output;
    17991974}
Note: See TracChangeset for help on using the changeset viewer.