• Resolved kirpich1

    (@kirpich1)


    Is the latest version of the plugin compatible with PHP 8.0, 8.1?

    Plugin ver. 2.2.2 crash my site with PHP 8.1, fatal error.

    ___

    Error in the file (line 602):

    /autoptimize/classes/autoptimizeStyles.php

    Text of error:

    PHP Fatal error:  Array and string offset access syntax with curly braces is no longer supported in /public_html/wp-content/plugins/autoptimize/classes/autoptimizeStyles.php on line 602

    Target code:

    $url = $noQurl;
    if ( '/' === $url{0} || preg_match( '#^(https?://|ftp://|data:)#i', $url ) ) {
      // URL is protocol-relative, host-relative or something we don't touch
      continue;
    } else {
      // Relative URL
      $newurl = preg_replace( '/https?:/', '', str_replace( ' ', '%20', AUTOPTIMIZE_WP_ROOT_URL . str_replace( '//', '/', $dir . '/' . $url ) ) );
    
      // Hash the url + whatever was behind potentially for replacement
      // We must do this, or different css classes referencing the same bg image (but
      // different parts of it, say, in sprites and such) loose their stuff...
      $hash = md5( $url . $matches[2][$k] );
      $code = str_replace( $matches[0][$k], $hash, $code );
    
      if ( $removedQuotes ) {
        $replace[$hash] = "url('" . $newurl . "')" . $matches[2][$k];
      } else {
        $replace[$hash] = 'url(' . $newurl . ')' . $matches[2][$k];
      }
    }
    • This topic was modified 2 years, 2 months ago by kirpich1.
    • This topic was modified 2 years, 2 months ago by kirpich1.
Viewing 14 replies - 1 through 14 (of 14 total)
  • Plugin Author Optimizing Matters

    (@optimizingmatters)

    2.2.2 is ancient, there are no more curly braces in more recent versions (see https://github.com/futtta/autoptimize/blob/beta/classes/autoptimizeStyles.php#L1142) and AO indeed does work fine under PHP8 ??

    hope this helps,
    frank

    Thread Starter kirpich1

    (@kirpich1)

    @optimizingmatters, thanks for the answer. I have updated the plugin.

    Your plugin have small bugs in the old and new versions.

    In this classes:

    autoptimize/classes/autoptimizeScripts.php
    autoptimize/classes/autoptimizeStyles.php

    The code inspector shows “script” next to “/body”. Visually bad

    In this code on autoptimizeScripts.php:

    $bodyreplacementpayload = '<script ' . $type_js . $defer . 'src="' . $this->url . '"></script>';

    I see in inspector:

    ...some code...
    <script defer src="https://domain.ru/wp-content/cache/autoptimize/js/autoptimize_c8dd705d1d4aa47ee037557413d3b89b.js"></script></body>

    I made the code like this (add PHP_EOL):

    $bodyreplacementpayload = '<script ' . $type_js . $defer . 'src="' . $this->url . '"></script>' . PHP_EOL;

    And i see:

    ...some code...
    <script defer src="https://domain.ru/wp-content/cache/autoptimize/js/autoptimize_c8dd705d1d4aa47ee037557413d3b89b.js"></script>
    </body>

    This is also in autoptimizeStyles.php:

    $this->inject_in_html( apply_filters( 'autoptimize_filter_css_bodyreplacementpayload', '<link ' . $type_css . 'media="' . $media . '" href="' . $url . '" rel="stylesheet">'), $replace_tag );

    I made the code like this (add PHP_EOL):

    $this->inject_in_html( apply_filters( 'autoptimize_filter_css_bodyreplacementpayload', '<link ' . $type_css . 'media="' . $media . '" href="' . $url . '" rel="stylesheet">' . PHP_EOL ), $replace_tag );

    In the file autoptimizeStyles.php there are several lines at once where it would be useful to add PHP_EOL:

    Line 1022
    Line 1062
    Line 1069
    Line 1071

    Sorry for my bad english ??

    Plugin Author Optimizing Matters

    (@optimizingmatters)

    AO does not aim to deliver visually pleasing HTML I’m afraid kirpich1; when “optimize HTML” is on (which is almost always the case) all linebreaks are removed anyway ??

    Thread Starter kirpich1

    (@kirpich1)

    And also the W3C validator shows INFO message when i checking my site with HTML5.

    Info messages are associated with the final slash in the link tags.

    $this->inject_in_html( apply_filters( 'autoptimize_filter_css_bodyreplacementpayload', '<link ' . $type_css . 'media="' . $media . '" href="' . $url . '" rel="stylesheet" />' . PHP_EOL ), $replace_tag );

    I mean this:

    rel="stylesheet" />    <-- /
    Thread Starter kirpich1

    (@kirpich1)

    Some users do not enable HTML optimization. I don’t enable this function for some reasons ??

    when “optimize HTML” is on (which is almost always the case) all linebreaks are removed anyway
    • This reply was modified 2 years, 2 months ago by kirpich1.
    • This reply was modified 2 years, 2 months ago by kirpich1.
    Thread Starter kirpich1

    (@kirpich1)

    Apologies for my poor English.

    I tweaked the plugin code for myself.

    Probably this will help you in development. The idea itself may be useful to you. And its implementation is very simple.

    Thank you for the great plugin!

    Plugin Author Optimizing Matters

    (@optimizingmatters)

    well, if you really want, you can use a filter like this one to remove the closing slash and add the EOL (and use autoptimize_filter_js_bodyreplacementpayload for JS);

    add_filter( 'autoptimize_filter_css_bodyreplacementpayload', function( $html ){
        return str_replace( '/>', '>' . PHP_EOL , $html );
    });

    that’s better then tweaking the code (as your tweaks could be overwritten with the next update(s)? ??

    Thread Starter kirpich1

    (@kirpich1)

    I understand that. But I haven’t updated your plugin since version 2.2.2 ?? I want to switch to PHP 8.1. That’s why I’ve made an update now.

    that’s better then tweaking the code (as your tweaks could be overwritten with the next update(s)?

    How to add a filter to functions.php for all the strings I need? ??

    Plugin Author Optimizing Matters

    (@optimizingmatters)

    if you add the above snippet + a similar snippet for JS (using that other filter mentioned) and add it using the code snippets plugin (as functions.php gets overwritten by theme updates), all strings you mentioned earlier will likely be the way you want them ??

    Thread Starter kirpich1

    (@kirpich1)

    @optimizingmatters, thank’s for answers! Good developer ??

    Resolved

    • This reply was modified 2 years, 2 months ago by kirpich1.
    Plugin Author Optimizing Matters

    (@optimizingmatters)

    you’re welcome, feel free to leave a review of the plugin and support here! ??

    Thread Starter kirpich1

    (@kirpich1)

    @optimizingmatters, ok. But I wrote a 5-star review ~ in 2015 ??

    • This reply was modified 2 years, 2 months ago by kirpich1.
    • This reply was modified 2 years, 2 months ago by kirpich1.
    Plugin Author Optimizing Matters

    (@optimizingmatters)

    ah, didn’t check ?? thanks for the 5 stars!

    Thread Starter kirpich1

    (@kirpich1)

    @optimizingmatters, 3 April, 2016 y. I’m leaving the topic. A lot of work with SEO. Thanks for the plugin ??

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘Compatible with PHP 8.0, 8.1?’ is closed to new replies.