Error in array_merge
-
array_merge(): Argument #2 is not an array in /home/paleo/betaprimal/wp-content/plugins/members/inc/functions-users.php on line 45
The code is looking for denied capabilities. I haven’t run debugging code on it, but I assume that I don’t have any denied capabilities, and so the array is null, and breaking the code.
-
I believe
array_keys()should always return an array, even if it’s empty.Can you describe more about your scenario? I can’t reproduce this at all. Does the role itself have capabilities (granted or denied)? Under what circumstances do you see the error?
Yes, that seems right.
A longer error message is:
PHP message: array_merge(): Argument #2 is not an array in plugins/members/inc/functions-users.php on line 45
PHP message: array_keys() expects parameter 1 to be array, null given in plugins/members/inc/functions-users.php on line 45
“Denied capabilities should always overrule granted capabilities.” is set to true.
Still investigating.
I did a print_r() on $user->roles and I got subscriber, and “bad guy” which is a role that is placed on people who try to get to invalid places in the site – e.g. registered spammers.
(subscribers on our site aren’t allowed to use /wp-admin/, but can do other things when logged in).
The bad guy role has some deny capabilities set, I can look those up if you want, but I think they aren’t even allowed to login.
Ah – that is it. The bad guy role doesn’t have any positive capabilities – only negatives.
get_role() can return a null, so you should probably check if it is null before passing to array_keys().
And then if array_keys is given a null, I think the return value is suspect as well – it doesn’t appear to be documented what happens when given null input.
foreach ( (array) $user->roles as $role ) { if(count(get_role( $role )->capabilities)) $denied_caps = array_merge( $denied_caps, array_keys( get_role( $role )->capabilities, false ) ); $denied_caps = array_merge( $denied_caps, array_keys( get_role( $role )->capabilities, false ) ); }This might be all that you need.
Thanks for investigating. I’ll work on this a bit tomorrow. It’s late here.
Hi Justin,
I can confirm this, as we got the same error message when trying to login as admin. But the site still works fine, and when we refresh the page, we’re logged in.
Did you get a chance to look into this?
###
Warning: array_keys() expects parameter 1 to be array, boolean given in …/wp-content/plugins/members/inc/functions-users.php on line 41
Warning: array_merge(): Argument #1 is not an array in …/wp-content/plugins/members/inc/functions-users.php on line 45
Warning: array_merge(): Argument #1 is not an array in …/wp-content/plugins/members/inc/functions-users.php on line 45
Warning: Cannot modify header information – headers already sent by (output started at …/wp-content/plugins/members/inc/functions-users.php:41) in …/wp-includes/pluggable.php on line 1207
Sorry, I think I forgot to put this on my to-do list. I’m about to check it right now.
I still can’t reproduce the issue, but I added a check to make sure we have a role object before trying to get its caps.
Can someone test the 1.1 branch here to see if it corrects the issue? https://github.com/justintadlock/members/tree/1.1
I’Ve tested your 1.1 build on our problem site, works great, no more error messages. Looks good, tks!
Thanks for letting me know. I’ll make sure this gets pushed in the next update.
I have a different error than not having a role object.
Here is my code that now works:
if ( ! is_null( $role_obj ) ){ if(count($role_obj->capabilities)) $denied_caps = array_merge( $denied_caps, array_keys( $role_obj->capabilities, false ) ); }I suspect why you can’t duplicate the issue is that I have at least one role that doesn’t have any permissions associated with it, it is just for display. (I track spammers by watching who registers on the site, and then tries to use wp-admin to make a new post, something that isn’t allowed on our site, and there aren’t any links to do so, but spammers know that we are running wordpress, and so try go straight to the admin-side without following our links).
And sorry for not seeing your updated code, I guess I didn’t get notified somehow. And I also now see that my “patch format” post didn’t post properly, e.g. the minus and plus signs are missing, so that makes my post hard to read.
The topic ‘Error in array_merge’ is closed to new replies.