PHP fatal error Cannot access protected property $slug
-
Hello,
In a previous communication, I asked if it was possible to make the logs visible to certain admins and you provided a code snippet from your documentation. But after I updated the plugin to the latest version I got the error below:
AH01071: Got error ‘PHP message: PHP Fatal error: Uncaught Error: Cannot access protected property Simple_History\\Loggers\\Simple_History_Logger::$slug
Do you have any solution regarding this?
Best Regards
-
Hi @stevendigital, could you show the code you are using? That would help me to fix your issue (and update the documentation for the plugin too)?
Hello @eskapism,
Yes of course. Here is the code
add_filter( 'simple_history/loggers_user_can_read/can_read_single_logger', function ( $user_can_read_logger, $logger_instance, $user_id ) { if ( $logger_instance->slug == 'SimplePostLogger' && $user_id === 1 ) { $user_can_read_logger = true; } else { $user_can_read_logger = false; } return $user_can_read_logger; }, 10, 3 );And here is the link from the documentation:
https://docs.simple-history.com/hooks#simple_history/loggers_user_can_read/can_read_single_logger
I found the previous ticket that i opened here, 4 months ago, so I hope this helps.
Let me know if I can assist any further.
Thanks for the snippet. Could you test to change it so instead of
$logger_instance->slugyou use$logger_instance->get_slug(). I made the slug variable protected while refactoring the code a while back, but forgot that users could have accessed it like you did (because that’s the way I also used it before the get_slug() method was added 🙈.I will update the plugin with a fix for this, but until then you can try the solution above. Let me know if it works!
Perfect. Now there are no errors and the history can be seen only by the $user_id that has been set. Thank you for your immediate response on this matter.
I might be missing something here so feel free to provide any documentation if available, but I noticed that just only hiding the history on an admin user is a solution that has defects overall. For example, another admin user may not have access to the log screen, but he can still see the admin dashboard menu item (if enabled) and can also see the plugin under the installed plugins and deactivate the plugin. I am aware that there are some limitations because of WordPress architecture, but I think that every menu that is created by the plugin must be overall hidden by any user that is not declared on the $user_id.
Again, feel free to provide any documentation for the above or correct me if there is anything that I am missing.
@stevendigital So what you want is to hide all Simple History from all users but one? Hide all pages, all settings, and so on?
I believe that this could work. Everything that is related to the plugin, just be visible to only one admin.
So @eskapism is the last question posible to be achieved?
I did not get any reply, so i thought i might ask again.
Best Regards
There are some filters that control where to show the plugin:
simple_history_show_on_dashboard,simple_history_show_as_page,simple_history/show_settings_page.
You could add a filter that returns false for those filters, except when the current user is the only admin you want to allow access for.
Perhaps something like this (not tested):
// Only allow user with username "JaneDoe" to access Simple History pages. $filters = [ 'simple_history_show_as_page', 'simple_history_show_on_dashboard', 'simple_history/show_settings_page', ]; foreach ( $filters as $filter ) { add_filter( $filter, function ( $show ) { if ( ! is_user_logged_in() ) { return $show; } $user = wp_get_current_user(); if ( 'JaneDoe' === $user->user_login ) { return true; } return $show; } ); }Thank you for the example of the code. I will try to test it. But first, I would like to point out that in this code snippet:
add_filter( 'simple_history/loggers_user_can_read/can_read_single_logger', function ( $user_can_read_logger, $logger_instance, $user_id ) { if ( $logger_instance->get_slug() == 'SimplePostLogger' && $user_id === 1 ) { $user_can_read_logger = true; } else { $user_can_read_logger = false; } return $user_can_read_logger; }, 10, 3 );Even if the user is the correct one, based on ID then it still does not return anything. Are there any changes made to the filter?
The check that happens on the user with ID===1 returns this message “Your search did not match any history events.”
Best Regards
I tried the code above and it works for me. But it may depend on what exacly you want to accomplish. As I interpret the code above you want to:
- give user 1 access to only the postlogger and nothing else
- all other users will have no access to anything
Does that sound like it’s what you want? Otherwise we may need to modify the snippet.
I tried to give the user with ID 1 the ability to see the logs of simple loger. but only he should see them and no other user. And afterwards, I was going to modify the other snippet that you provided. But the first snippet does not return any logs at all.
Should i enable the debug and check if there are any logs displayed on my end?
-
This reply was modified 2 years, 8 months ago by
Efs. Reason: Added problem clarifications
The topic ‘PHP fatal error Cannot access protected property $slug’ is closed to new replies.