WebberZone / contextual-related-posts

Contextual Related Posts WordPress plugin
https://webberzone.com/plugins/contextual-related-posts/
38 stars 23 forks source link

CRP triggers warning on network admin requests #201

Closed jhard closed 3 weeks ago

jhard commented 3 weeks ago

Describe the bug CRP triggers a warning when using the network admin panel. PHP Warning: Attempt to read property "settings_page" on null in /var/www/public/wp-content/plugins/contextual-related-posts/includes/admin/settings/class-settings.php on line 1303

To Reproduce Steps to reproduce the behavior:

  1. Open Network Admin (Multisite)
  2. observe error logs

Expected behavior No warning should be triggered.

The problem stems from settings_api being initalized in initialise_settings() which is hooked to admin_menu. admin_menu is not being fired on network admin requests, it's called network_admin_menu in that context. However, admin_enqueue_scripts still fires in network admin, which leaves us with an uninitialized settings_api, generating a warning in if ( $this->settings_api->settings_page !== $hook ) {.

I suggest to either initialize the settings also on network_admin_menu if there might be any settings in network admin, to check for the environment (i.e. if ( is_multisite() && is_network_admin() ) { return; } in admin_enqueue_scripts($hook), or to check whether settings_api is defined, i.e. if ( $this->settings_api && $this->settings_api->settings_page !== $hook ) {.

ajaydsouza commented 3 weeks ago

Hi,

Thank you for the detailed testing. I have fixed this in https://github.com/WebberZone/contextual-related-posts/commit/12837e439dbe2ac771c27c2e088eacf3e2537d78

I am releasing v3.5.2 which should also fix this.