Make WordPress Core

Opened 12 years ago

Closed 12 years ago

Last modified 12 years ago

#21601 closed defect (bug) (wontfix)

Introduce WP_User::has_prop_val() magic method

Reported by: wpsmith's profile wpsmith Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Users Keywords: has-patch 2nd-opinion dev-feedback
Focuses: Cc:

Description (last modified by scribu)

When using WP_User::has_prop( $key ), I believe the expectation is that if the property has a value, it would return true. However, it is returning true if the key is set or if the key exists. So if one uses it like WP_User::has_prop( 'user_url' ), it returns true regardless, simply because that's a standard WordPress user field.

So, keeping that functionality for checking even custom properties, I thought it may be beneficial to add a magic method for checking whether a property is set ( WP_User::has_prop( $key ) ) and has a value ( WP_User::get( $key ) ).

Thus, this ticket desires to add two methods: has_prop_val() & __hasval()

Attachments (1)

21601_wp_user.patch (1.3 KB) - added by wpsmith 12 years ago.

Download all attachments as: .zip

Change History (6)

#1 @scribu
12 years ago

  • Description modified (diff)

Magic methods aren't something you can arbitrarily create. They're a predefined set of special constructs in the PHP language. __hasval() is not one of them: http://php.net/manual/en/language.oop5.magic.php

#2 @scribu
12 years ago

This comes down to an inconsistency in PHP, where __isset() is called by both isset() and empty():

$user->user_url = '';

isset( $user->user_url );   // true
!empty( $user->user_url );  // false

For explicit calls, you have to be more verbose:

$user->user_url = '';

$user->has_prop( 'user_url' );  // true
$user->has_prop( 'user_url' ) && !empty( $user->get( 'user_url' ) );  // false

So, has_prop_val() sort of makes sense, but drop __hasval().

Last edited 12 years ago by scribu (previous) (diff)

#3 @scribu
12 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to wontfix
  • Status changed from new to closed

Actually, since get() doesn't throw a notice for undefined properties, you could just do this:

$value = $user->get( 'user_url' );

if ( !empty( $value ) ) {
  ...
}
Last edited 12 years ago by scribu (previous) (diff)

#4 @wpsmith
12 years ago

Makes sense. Thanks!

#5 @nacin
12 years ago

!empty( $user->get( 'user_url' ) ) won't work because it's not a variable, but you can just drop the empty() call.

if ( $user->get( 'user_url' ) )

Note: See TracTickets for help on using tickets.