WordPress.org

Make WordPress Core


Ignore:
Timestamp:
07/25/2011 12:36:06 AM (8 years ago)
Author:
azaozz
Message:

Use json_encode() for adding script data (formerly l10n). Add the same functionality to WP_Styles for adding inline css after a stylesheet has been outputted. See #11520

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/class.wp-styles.php

    r18446 r18464  
    2626    var $do_concat = false;
    2727    var $print_html = '';
     28    var $print_code = '';
    2829    var $default_dirs;
    2930
     
    3637            return false;
    3738
    38         if ( null === $this->registered[$handle]->ver )
     39        $obj = $this->registered[$handle];
     40        if ( null === $obj->ver )
    3941            $ver = '';
    4042        else
    41             $ver = $this->registered[$handle]->ver ? $this->registered[$handle]->ver : $this->default_version;
     43            $ver = $obj->ver ? $obj->ver : $this->default_version;
    4244
    4345        if ( isset($this->args[$handle]) )
     
    4547
    4648        if ( $this->do_concat ) {
    47             if ( $this->in_default_dir($this->registered[$handle]->src) && !isset($this->registered[$handle]->extra['conditional']) && !isset($this->registered[$handle]->extra['alt']) ) {
     49            if ( $this->in_default_dir($obj->src) && !isset($obj->extra['conditional']) && !isset($obj->extra['alt']) ) {
    4850                $this->concat .= "$handle,";
    4951                $this->concat_version .= "$handle$ver";
     52
     53                if ( !empty($this->registered[$handle]->extra['data']) )
     54                    $this->print_code .= $this->registered[$handle]->extra['data'];
     55
    5056                return true;
    5157            }
    5258        }
    5359
    54         if ( isset($this->registered[$handle]->args) )
    55             $media = esc_attr( $this->registered[$handle]->args );
     60        if ( isset($obj->args) )
     61            $media = esc_attr( $obj->args );
    5662        else
    5763            $media = 'all';
    5864
    59         $href = $this->_css_href( $this->registered[$handle]->src, $ver, $handle );
    60         $rel = isset($this->registered[$handle]->extra['alt']) && $this->registered[$handle]->extra['alt'] ? 'alternate stylesheet' : 'stylesheet';
    61         $title = isset($this->registered[$handle]->extra['title']) ? "title='" . esc_attr( $this->registered[$handle]->extra['title'] ) . "'" : '';
     65        $href = $this->_css_href( $obj->src, $ver, $handle );
     66        $rel = isset($obj->extra['alt']) && $obj->extra['alt'] ? 'alternate stylesheet' : 'stylesheet';
     67        $title = isset($obj->extra['title']) ? "title='" . esc_attr( $obj->extra['title'] ) . "'" : '';
    6268
    6369        $end_cond = $tag = '';
    64         if ( isset($this->registered[$handle]->extra['conditional']) && $this->registered[$handle]->extra['conditional'] ) {
    65             $tag .= "<!--[if {$this->registered[$handle]->extra['conditional']}]>\n";
     70        if ( isset($obj->extra['conditional']) && $obj->extra['conditional'] ) {
     71            $tag .= "<!--[if {$obj->extra['conditional']}]>\n";
    6672            $end_cond = "<![endif]-->\n";
    6773        }
    6874
    6975        $tag .= apply_filters( 'style_loader_tag', "<link rel='$rel' id='$handle-css' $title href='$href' type='text/css' media='$media' />\n", $handle );
    70         if ( 'rtl' === $this->text_direction && isset($this->registered[$handle]->extra['rtl']) && $this->registered[$handle]->extra['rtl'] ) {
    71             if ( is_bool( $this->registered[$handle]->extra['rtl'] ) ) {
    72                 $suffix = isset( $this->registered[$handle]->extra['suffix'] ) ? $this->registered[$handle]->extra['suffix'] : '';
    73                 $rtl_href = str_replace( "{$suffix}.css", "-rtl{$suffix}.css", $this->_css_href( $this->registered[$handle]->src , $ver, "$handle-rtl" ));
     76        if ( 'rtl' === $this->text_direction && isset($obj->extra['rtl']) && $obj->extra['rtl'] ) {
     77            if ( is_bool( $obj->extra['rtl'] ) ) {
     78                $suffix = isset( $obj->extra['suffix'] ) ? $obj->extra['suffix'] : '';
     79                $rtl_href = str_replace( "{$suffix}.css", "-rtl{$suffix}.css", $this->_css_href( $obj->src , $ver, "$handle-rtl" ));
    7480            } else {
    75                 $rtl_href = $this->_css_href( $this->registered[$handle]->extra['rtl'], $ver, "$handle-rtl" );
     81                $rtl_href = $this->_css_href( $obj->extra['rtl'], $ver, "$handle-rtl" );
    7682            }
    7783
     
    8187        $tag .= $end_cond;
    8288
    83         if ( $this->do_concat )
     89        if ( $this->do_concat ) {
    8490            $this->print_html .= $tag;
    85         else
     91            $this->print_html .= $this->print_inline_style( $handle, false );
     92        } else {
    8693            echo $tag;
     94            $this->print_inline_style( $handle );
     95        }
    8796
    88         // Could do something with $this->registered[$handle]->extra here to print out extra CSS rules
    89 //      echo "<style type='text/css'>\n";
    90 //      echo "/* <![CDATA[ */\n";
    91 //      echo "/* ]]> */\n";
    92 //      echo "</style>\n";
     97        return true;
     98    }
     99
     100    function add_inline_style( $handle, $data ) {
     101        if ( !$data )
     102            return false;
     103
     104        if ( !empty( $this->registered[$handle]->extra['data'] ) )
     105            $data .= "\n" . $this->registered[$handle]->extra['data'];
     106
     107        return $this->add_data( $handle, 'data', $data );
     108    }
     109
     110    function print_inline_style( $handle, $echo = true ) {
     111        if ( empty($this->registered[$handle]->extra['data']) )
     112            return false;
     113
     114        $output = $this->registered[$handle]->extra['data'];
     115
     116        if ( !$echo )
     117            return $output;
     118
     119        echo "<style type='text/css'>\n";
     120        echo "$output\n";
     121        echo "</style>\n";
    93122
    94123        return true;
Note: See TracChangeset for help on using the changeset viewer.