Problem with CODE and PRE tags #93

Closed
Monter opened this Issue May 7, 2017 · 7 comments

Comments

Projects
None yet
2 participants

Monter commented May 7, 2017 edited

I'm happy to use your Autooptimize plugin, but I have a small problem with it. Namely, on the blog containing CODE or PRE tags with CSS, HTML, etc., Autooptimize will spoil their contents when the minify option is enabled. Partially I was able to work around the problem by editing some plugin files:

  • editing line "private $exclude = array('','< code >','< pre >','< pre class="htmlcode" >');" in autoptimizeHTML.php
  • editing line "private $whitelist = 'htmlcode, pre, code';" in autoptimizeScripts.php
  • editing line "private $whitelist = 'htmlcode, pre, code';" in autoptimizeStyles.php
    (where "htmlcode" is a class of my PRE tag with my codes, see http://monter.techlog.pl/poprawiamy-widget-od-tablica-pl/ )
    but the effect is not 100% satisfactory. Maybe I'm doing something wrong?

Is it possible to expand the plugin with the option of deactivating tags containing code from optimization that spoils its content?

Owner

futtta commented May 7, 2017

You're making it way to complicated; simply wrap your code in noptimize-tags, cfr. AO's FAQ :-)

Monter commented May 7, 2017

I do not see the description in the FAQ, show an example of how to disable the plugin in a particular tag, eg PRE from the plugin (without editing many blog posts)

Owner

futtta commented May 7, 2017

Ah, I see the new plugin-pages ate my code (as well), how meta ;-)

Anyway, on a per-post level noptimize works like this;

<!--noptimize-->
<pre class="htmlcode">
<code><strong>all of this<strong> will <h1>not</h1> get optmized</code>
</pre>
<!--/noptimize-->

now if you don't want to change all your posts, your best approach would be to hook into AO's API, like this;

add_filter('autoptimize_filter_html_before_minify','monter_noptimize');
function monter_noptimize($in) {
    // code here to add noptimize tags around pre class="htmlcode" blocks
}

Only thing left for you to do is to add code (preg_replace and a regex should do the trick) that indeed adds the noptimize tags :-)

Owner

futtta commented May 7, 2017

this one should work;

add_filter('autoptimize_filter_html_before_minify','monter_noptimize');
function monter_noptimize($in) {
  	$out = preg_replace('#<pre class="htmlcode">.*</pre>#Us','<!--noptimize-->$0<!--/noptimize-->',$in);
    return $out;
}

Monter commented May 7, 2017 edited

Thank you.
Before I read your answer I created something like this (also works):

add_filter( 'the_content', 'autooptimize_pre_content_filter', 99 );
function autooptimize_pre_content_filter( $content ) {
    $content = preg_replace( '|<pre|isU', '<!--noptimize--><pre', $content );
    $content = preg_replace( '|</pre>|isU', '</pre><!--/noptimize-->', $content );
    return $content;
}

Monter closed this May 7, 2017

Monter commented May 14, 2017

Small Idea: Can you add a new plugin configuration option in the minify html section to disable code compression in PRE tags? Many people use WordPress to publish snippets of code, and minify html interactions also within the PRE tags can not enable this option because it causes the page to display incorrectly.

Owner

futtta commented May 14, 2017 edited

everything inside <pre> tags should actually already be excluded from HTML optimization as per https://github.com/futtta/autoptimize/blob/master/classes/external/php/minify-html.php#L121-L124 @Monter Did a quick test and this indeed seems to work (all spacing and such was left untouched)?

Can you give an example of something that does not work?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment