10up / distributor

Share content between your websites.
https://distributorplugin.com
GNU General Public License v2.0
628 stars 155 forks source link

PHP warning thrown visiting non-existent connection pull screen. #1157

Closed peterwilsoncc closed 2 months ago

peterwilsoncc commented 9 months ago

Describe the bug

A PHP warning (notice in PHP 7.x and earlier) can be thrown visiting the pull content screen for an external connection that does not exist.

The error is caused by this block of code assuming than if a connection screen is internal if no valid external connection is found:

https://github.com/10up/distributor/blob/c83fde14ce73b81556adf6cd4f77ce0c63737fd1/includes/pull-ui.php#L577-L581

As the $connection object isn't an object, the site->blog_id property throws a notice.

The issue can be resolved by validating that the connection is an internal connection before accessing the property. If it's neither an internal or external connection then the function can return early.

Steps to Reproduce

  1. Visit the admin page for an external connection that does not exist /wp-admin/admin.php?page=pull&connection_type=external&connection_id=1 (this presumes Post 1 is not an external connection.
  2. A PHP notice will be thrown

Screenshots, screen recording, code snippet

Log

[21-Nov-2023 03:10:27 UTC] PHP Notice:  Trying to get property 'site' of non-object in /vagrant/content/plugins/distributor/includes/pull-ui.php on line 580
[21-Nov-2023 03:10:27 UTC] PHP Stack trace:
[21-Nov-2023 03:10:27 UTC] PHP   1. {main}() /vagrant/wp-build/wp-admin/admin.php:0
[21-Nov-2023 03:10:27 UTC] PHP   2. do_action($hook_name = 'distributor_page_pull') /vagrant/wp-build/wp-admin/admin.php:259
[21-Nov-2023 03:10:27 UTC] PHP   3. WP_Hook->do_action($args = [0 => '']) /vagrant/wp-build/wp-includes/plugin.php:517
[21-Nov-2023 03:10:27 UTC] PHP   4. WP_Hook->apply_filters($value = '', $args = [0 => '']) /vagrant/wp-build/wp-includes/class-wp-hook.php:348
[21-Nov-2023 03:10:27 UTC] PHP   5. Distributor\PullUI\dashboard('') /vagrant/wp-build/wp-includes/class-wp-hook.php:324
[21-Nov-2023 03:10:27 UTC] PHP   6. Distributor\PullUI\output_pull_errors() /vagrant/content/plugins/distributor/includes/pull-ui.php:527
[21-Nov-2023 03:10:27 UTC] PHP Notice:  Trying to get property 'blog_id' of non-object in /vagrant/content/plugins/distributor/includes/pull-ui.php on line 580
[21-Nov-2023 03:10:27 UTC] PHP Stack trace:
[21-Nov-2023 03:10:27 UTC] PHP   1. {main}() /vagrant/wp-build/wp-admin/admin.php:0
[21-Nov-2023 03:10:27 UTC] PHP   2. do_action($hook_name = 'distributor_page_pull') /vagrant/wp-build/wp-admin/admin.php:259
[21-Nov-2023 03:10:27 UTC] PHP   3. WP_Hook->do_action($args = [0 => '']) /vagrant/wp-build/wp-includes/plugin.php:517
[21-Nov-2023 03:10:27 UTC] PHP   4. WP_Hook->apply_filters($value = '', $args = [0 => '']) /vagrant/wp-build/wp-includes/class-wp-hook.php:348
[21-Nov-2023 03:10:27 UTC] PHP   5. Distributor\PullUI\dashboard('') /vagrant/wp-build/wp-includes/class-wp-hook.php:324
[21-Nov-2023 03:10:27 UTC] PHP   6. Distributor\PullUI\output_pull_errors() /vagrant/content/plugins/distributor/includes/pull-ui.php:527

Environment information

WordPress information

WordPress 6.4.1 Distributor: Develop

Code of Conduct

peterwilsoncc commented 9 months ago

It occurred to me that this is a warning in PHP 8.0 and later so I've updated the description accordingly. See https://3v4l.org/tfHlt