publishpress / PublishPress-Authors

PublishPress Authors is the best plugin for adding many authors to one WordPress post. You can create multiple authors, co-authors and guest authors.
14 stars 8 forks source link

Error 500 in Gutenberg if the custom layout contains an invalid syntax or trigger an error #337

Open andergmartins opened 3 years ago

andergmartins commented 3 years ago

Expected Behavior

I expect to see an error log but do not block the editor or break the page if the custom layout has a syntax issue or throws an error.

Current Behavior

If I try to print an author object by mistake ({{ author }}, instead of a string the block editor doesn't load showing an error 500. The frontend that displays that layout (author box or shortcode) breaks as well.

[29-Jan-2021 01:39:18 UTC] PHP Recoverable fatal error:  Object of class MultipleAuthors\Classes\Objects\Author could not be converted to string in /Users/anderson/Local Sites/dev/app/public/wp-content/plugins/publishpress/vendor/twig/twig/src/Environment.php(497) : eval()'d code on line 33
[29-Jan-2021 01:39:18 UTC] PHP Stack trace:
[29-Jan-2021 01:39:18 UTC] PHP   1. {main}() /Users/anderson/Local Sites/dev/app/public/index.php:0
[29-Jan-2021 01:39:18 UTC] PHP   2. require() /Users/anderson/Local Sites/dev/app/public/index.php:17
[29-Jan-2021 01:39:18 UTC] PHP   3. require_once() /Users/anderson/Local Sites/dev/app/public/wp-blog-header.php:19
[29-Jan-2021 01:39:18 UTC] PHP   4. include() /Users/anderson/Local Sites/dev/app/public/wp-includes/template-loader.php:106
[29-Jan-2021 01:39:18 UTC] PHP   5. get_template_part() /Users/anderson/Local Sites/dev/app/public/wp-content/themes/twentytwentyone/single.php:18
[29-Jan-2021 01:39:18 UTC] PHP   6. locate_template() /Users/anderson/Local Sites/dev/app/public/wp-includes/general-template.php:204
[29-Jan-2021 01:39:18 UTC] PHP   7. load_template() /Users/anderson/Local Sites/dev/app/public/wp-includes/template.php:676
[29-Jan-2021 01:39:18 UTC] PHP   8. require() /Users/anderson/Local Sites/dev/app/public/wp-includes/template.php:732
[29-Jan-2021 01:39:18 UTC] PHP   9. the_content() /Users/anderson/Local Sites/dev/app/public/wp-content/themes/twentytwentyone/template-parts/content/content-single.php:23
[29-Jan-2021 01:39:18 UTC] PHP  10. apply_filters() /Users/anderson/Local Sites/dev/app/public/wp-includes/post-template.php:253
[29-Jan-2021 01:39:18 UTC] PHP  11. WP_Hook->apply_filters() /Users/anderson/Local Sites/dev/app/public/wp-includes/plugin.php:212
[29-Jan-2021 01:39:18 UTC] PHP  12. do_shortcode() /Users/anderson/Local Sites/dev/app/public/wp-includes/class-wp-hook.php:287
[29-Jan-2021 01:39:18 UTC] PHP  13. preg_replace_callback() /Users/anderson/Local Sites/dev/app/public/wp-includes/shortcodes.php:218
[29-Jan-2021 01:39:18 UTC] PHP  14. do_shortcode_tag() /Users/anderson/Local Sites/dev/app/public/wp-includes/shortcodes.php:218
[29-Jan-2021 01:39:18 UTC] PHP  15. MultipleAuthors\Plugin->shortcode_author_box() /Users/anderson/Local Sites/dev/app/public/wp-includes/shortcodes.php:343
[29-Jan-2021 01:39:18 UTC] PHP  16. MultipleAuthors\Plugin->get_author_box_markup() /Users/anderson/Local Sites/dev/app/public/wp-content/plugins/publishpress-authors-pro/vendor/publishpress/publishpress-authors/src/core/Plugin.php:1637
[29-Jan-2021 01:39:18 UTC] PHP  17. apply_filters() /Users/anderson/Local Sites/dev/app/public/wp-content/plugins/publishpress-authors-pro/vendor/publishpress/publishpress-authors/src/core/Traits/Author_box.php:177
[29-Jan-2021 01:39:18 UTC] PHP  18. WP_Hook->apply_filters() /Users/anderson/Local Sites/dev/app/public/wp-includes/plugin.php:212
[29-Jan-2021 01:39:18 UTC] PHP  19. MA_Author_Custom_Layouts->filterAuthorBoxHtml() /Users/anderson/Local Sites/dev/app/public/wp-includes/class-wp-hook.php:287
[29-Jan-2021 01:39:18 UTC] PHP  20. Twig\TemplateWrapper->render() /Users/anderson/Local Sites/dev/app/public/wp-content/plugins/publishpress-authors-pro/modules/author-custom-layouts/author-custom-layouts.php:443
[29-Jan-2021 01:39:18 UTC] PHP  21. __TwigTemplate_8f43b78f5be925ab93942aacd8cb72e3df96f73c4d6f2070ba919291f967220d->render() /Users/anderson/Local Sites/dev/app/public/wp-content/plugins/publishpress/vendor/twig/twig/src/TemplateWrapper.php:47
[29-Jan-2021 01:39:18 UTC] PHP  22. __TwigTemplate_8f43b78f5be925ab93942aacd8cb72e3df96f73c4d6f2070ba919291f967220d->display() /Users/anderson/Local Sites/dev/app/public/wp-content/plugins/publishpress/vendor/twig/twig/src/Template.php:434
[29-Jan-2021 01:39:18 UTC] PHP  23. __TwigTemplate_8f43b78f5be925ab93942aacd8cb72e3df96f73c4d6f2070ba919291f967220d->displayWithErrorHandling() /Users/anderson/Local Sites/dev/app/public/wp-content/plugins/publishpress/vendor/twig/twig/src/Template.php:422
[29-Jan-2021 01:39:18 UTC] PHP  24. __TwigTemplate_8f43b78f5be925ab93942aacd8cb72e3df96f73c4d6f2070ba919291f967220d->doDisplay() /Users/anderson/Local Sites/dev/app/public/wp-content/plugins/publishpress/vendor/twig/twig/src/Template.php:455

Possible Solution

We need to catch errors in the layout processing and log them, but not break the output.

Steps to Reproduce (for bugs)

  1. Edit a layout trying to output an author object: {{ author }}
  2. Add a shortcode to a post for using the same layout you edited.
  3. Try to save and refresh the post edit page
  4. Try to see the post in the frontend

Context

Your Environment

andergmartins commented 3 years ago

Since it triggers a PHP error and not an exception we would need to implement an error handling method to throw a exception we can catch.