Ticket #13971: 13971-cdata-content.diff

File 13971-cdata-content.diff, 1.6 KB (added by dougal, 3 years ago)

Generic mechanism for applying filters only to 'CDATA' text, and not to HTML elements, with specific application to the capital_P_dangit problem.

  • formatting.php

     
    28292829       return str_replace( 'Wordpress', 'WordPress', $text ); 
    28302830} 
    28312831 
     2832/** 
     2833 * Apply capital_P_dangit() to just regular content. 
     2834 * 
     2835 * @since 3.0.1 
     2836 * @uses wp_filter_cdata_content(), capital_P_dangit() 
     2837 */ 
     2838function wp_capital_P_content( $str ) { 
     2839       return wp_filter_cdata_content( $str, 'capital_P_dangit' ); 
     2840} 
     2841 
     2842/** 
     2843 * Callback function which will only filter content *between* HTML tags, and 
     2844 * should ignore data in attributes (such as URLs) 
     2845 *  
     2846 * @since 3.0.1 
     2847 */ 
     2848function wp_filter_cdata_content( $content, $filter = null ) { 
     2849        if ( function_exists( $filter ) ) { 
     2850                // Yes, this is ugly. For the sake of being reusable. 
     2851                $content = preg_replace_callback( '/(?(?<=>)|\A)([^<>]+)(?(?=<)|\Z)/s', create_function( '$matches', 'return call_user_func('."'$filter'".', $matches[1]);' ), $content ); 
     2852        } 
     2853 
     2854        return $content; 
     2855} 
     2856 
     2857 
     2858 
    28322859?> 
  • default-filters.php

     
    8888 
    8989// Format WordPress 
    9090foreach ( array( 'the_content', 'the_title', 'comment_text' ) as $filter ) 
    91         add_filter( $filter, 'capital_P_dangit' ); 
     91        add_filter( $filter, 'wp_capital_P_content' ); 
    9292 
    9393// Format titles 
    9494foreach ( array( 'single_post_title', 'single_cat_title', 'single_tag_title', 'single_month_title', 'nav_menu_attr_title', 'nav_menu_description' ) as $filter ) {