elementor / static-html-output

Static HTML Output Plugin for WordPress
https://statichtmloutput.com
The Unlicense
125 stars 35 forks source link

Images with data srcsets result in undefined offset error while generating #128

Open Flynsarmy opened 4 years ago

Flynsarmy commented 4 years ago

One of my pages has the following image:

<img width="408" height="306" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7%20" data-src="https://PLACEHOLDER.wpsho/wp-content/uploads/2020/07/my-image.jpg" class="attachment-9999x306 size-9999x306 wp-post-image" alt="my image caption" loading="lazy" srcset="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7 100w" data-srcset="https://PLACEHOLDER.wpsho/wp-content/uploads/2020/07/my-image.jpg 960w, https://PLACEHOLDER.wpsho/wp-content/uploads/2020/07/my-image-480x360.jpg 480w, https://PLACEHOLDER.wpsho/wp-content/uploads/2020/07/my-image-768x576.jpg 768w, https://PLACEHOLDER.wpsho/wp-content/uploads/2020/07/my-image-240x180.jpg 240w" sizes="(max-width: 408px) 100vw, 408px">

When running wp wp2static generate I get spammed with millions of lines of

PHP Notice:  Undefined offset: 1 in /path/to/wp-content/plugins/static-html-output-plugin/plugin/WP2Static/HTMLProcessor.php on line 136
PHP Stack trace:
PHP   1. {main}() /usr/local/Cellar/wp-cli/2.4.0/bin/wp:0
PHP   2. include() /usr/local/Cellar/wp-cli/2.4.0/bin/wp:4
PHP   3. include() phar:///usr/local/Cellar/wp-cli/2.4.0/bin/wp/php/boot-phar.php:11
PHP   4. WP_CLI\bootstrap() phar:///usr/local/Cellar/wp-cli/2.4.0/bin/wp/vendor/wp-cli/wp-cli/php/wp-cli.php:27
PHP   5. WP_CLI\Bootstrap\LaunchRunner->process() phar:///usr/local/Cellar/wp-cli/2.4.0/bin/wp/vendor/wp-cli/wp-cli/php/bootstrap.php:74
PHP   6. WP_CLI\Runner->start() phar:///usr/local/Cellar/wp-cli/2.4.0/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Bootstrap/LaunchRunner.php:23
PHP   7. WP_CLI\Runner->run_command_and_exit() phar:///usr/local/Cellar/wp-cli/2.4.0/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Runner.php:1160
PHP   8. WP_CLI\Runner->run_command() phar:///usr/local/Cellar/wp-cli/2.4.0/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Runner.php:394
PHP   9. WP_CLI\Dispatcher\Subcommand->invoke() phar:///usr/local/Cellar/wp-cli/2.4.0/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Runner.php:371
PHP  10. call_user_func:{phar:///usr/local/Cellar/wp-cli/2.4.0/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Dispatcher/Subcommand.php:451}() phar:///usr/local/Cellar/wp-cli/2.4.0/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Dispatcher/Subcommand.php:451
PHP  11. WP_CLI\Dispatcher\CommandFactory::WP_CLI\Dispatcher\{closure:phar:///usr/local/Cellar/wp-cli/2.4.0/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Dispatcher/CommandFactory.php:95-102}() phar:///usr/local/Cellar/wp-cli/2.4.0/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Dispatcher/Subcommand.php:451
PHP  12. call_user_func:{phar:///usr/local/Cellar/wp-cli/2.4.0/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Dispatcher/CommandFactory.php:98}() phar:///usr/local/Cellar/wp-cli/2.4.0/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Dispatcher/CommandFactory.php:98
PHP  13. WP2Static_CLI->generate() phar:///usr/local/Cellar/wp-cli/2.4.0/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Dispatcher/CommandFactory.php:98
PHP  14. SiteCrawler->crawl_site() /path/to/wp-content/plugins/static-html-output-plugin/plugin/wp2static-wp-cli-commands.php:2
PHP  15. SiteCrawler->crawlABitMore() /path/to/wp-content/plugins/static-html-output-plugin/plugin/WP2Static/SiteCrawler.php:2
PHP  16. SiteCrawler->loadFileForProcessing() /path/to/wp-content/plugins/static-html-output-plugin/plugin/WP2Static/SiteCrawler.php:2
PHP  17. HTMLProcessor->processHTML() /path/to/wp-content/plugins/static-html-output-plugin/plugin/WP2Static/SiteCrawler.php:2
PHP  18. HTMLProcessor->processImageSrcSet() /path/to/wp-content/plugins/static-html-output-plugin/plugin/WP2Static/HTMLProcessor.php:28

The error is coming from HTMLProcessor.php's processImageSrcSet method:

$src_set = $element->getAttribute('srcset');
$src_set_lines = explode(',', $src_set);
foreach ($src_set_lines as $src_set_line) {
    $all_pieces = explode(' ', $src_set_line);
    $pieces = array_filter($all_pieces);
    $pieces = array_values($pieces);
    $url = $pieces[0];
    $dimension = $pieces[1];

The problem is the explode by comma line. My srcset includes a comma so it's incorrectly exploded.