kasparsd / minit

A WordPress plugin to combine CSS and Javascript files.
GNU General Public License v2.0
286 stars 46 forks source link

Ignoring styles added via wp_add_inline_style() function #18

Closed mekshq closed 10 years ago

mekshq commented 10 years ago

Hi,

First of all, thanks for the great plugin!

The only issue I have noticed is that it ignores styles added via wp_add_inline_style() function.

For example I have this:

    wp_register_style('thr_style', THEME_URI . '/style.css', false, THEME_VERSION, 'screen');
    wp_enqueue_style('thr_style');

    $thr_dynamic_css = 'div.example{ display: none;}';
    wp_add_inline_style('thr_style', $thr_dynamic_css);

This is just an example, $thr_dynamic_css is actually dynamicaly generated css string which is appended to a main css file. So, when I use "minit" everything is ok with the main css file, it just excludes style appended via wp_add_inline_style.

Can you please take a look?

kasparsd commented 10 years ago

Thanks for reporting this @mekshq . I'll have a look at it now.

mekshq commented 10 years ago

I have fixed it myself but maybe you will find a better approach. Here is my solution:

foreach ( $minit_todo as $t => $script ) {
            // Make sure this is a relative URL so that we can check if it is local
            $src = str_replace( $object->base_url, '', $object->registered[ $script ]->src );
            // Skip if the file is not hosted locally
            if ( ! file_exists( ABSPATH . $src ) || empty( $src ) )
                continue;
            $done[ $script ] = apply_filters( 
                    'minit-item-' . $extension, 
                    file_get_contents( ABSPATH . $src ),
                    $object,
                    $script
                );

            if($extension == 'css'){
                if(isset($object->registered[ $script ]->extra['after']) && !empty($object->registered[ $script ]->extra['after'])){
                    foreach($object->registered[ $script ]->extra['after'] as $inline){
                        $done[ $script ] .= $inline;
                    }
                }
            }

        }

I'm also about to add simple function which will compress css in this foreach loop, so css can be minified.

$done[ $script ] = compress_css_code(done[ $script ]);
function compress_css_code($code) {

    // Remove Comments
    $code = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $code);

    // Remove tabs, spaces, newlines, etc.
    $code = str_replace(array("\r\n", "\r", "\n", "\t", '  ', '    ', '    '), '', $code);

    return $code;
  }

What do you think?

kasparsd commented 10 years ago

Should be fixed with ae12afda2 and 1.0.