WordPress.org

Make WordPress Core

Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#20285 closed defect (bug) (fixed)

Author meta fields aren't displaying correctly due to broken logic

Reported by: FredWu Owned by: ryan
Milestone: 3.4 Priority: high
Severity: major Version: 3.3.1
Component: Users Keywords: has-patch
Focuses: Cc:

Description

Some authors have missing meta data even though the data are in the database. For example, some authors' description/biography are empty when retrieving them from the_author_meta('description'), even though $authordata->description from the function shows the data correctly.

This is caused by a bug in wp-includes/author-template.php, starting from line 99:

function get_the_author_meta($field = '', $user_id = false) {
	if ( ! $user_id )
		global $authordata;
	else
		$authordata = get_userdata( $user_id );

	var_dump($authordata);

	// Keys used as object vars cannot have dashes.
	$field = str_replace('-', '', $field);
	$field = strtolower($field);
	$user_field = "user_$field";

	if ( 'id' == $field )
		$value = isset($authordata->ID) ? (int)$authordata->ID : 0;
	elseif ( isset($authordata->$user_field) )
		$value = $authordata->$user_field;
	else
		$value = isset($authordata->$field) ? $authordata->$field : '';

	return apply_filters('get_the_author_' . $field, $value, $user_id);
}

elseif ( isset($authordata->$user_field) ) is the problem, because for some users this field is set, but is empty, so the system never gets through to the real 'description' field.

I've attached a patch that fixes this.

Attachments (4)

author_metadata_fix.patch (594 bytes) - added by FredWu 2 years ago.
author_metadata_fix2.patch (561 bytes) - added by fredwu 2 years ago.
20285.diff (933 bytes) - added by scribu 2 years ago.
20285.2.diff (1.4 KB) - added by ryan 2 years ago.

Download all attachments as: .zip

Change History (19)

FredWu2 years ago

comment:1 FredWu2 years ago

  • Severity changed from major to normal

comment:2 follow-up: scribu2 years ago

  • Cc scribu added

You can just replace isset() with !empty() since empty doesn't cause a notice.

Version 0, edited 2 years ago by scribu (next)

comment:3 in reply to: ↑ 2 fredwu2 years ago

Replying to scribu:

You can just replace isset() with !empty() since empty() doesn't cause a notice.

Good point! I've attached the updated patch.

comment:4 scribu2 years ago

  • Milestone changed from Awaiting Review to 3.4

scribu2 years ago

comment:5 scribu2 years ago

I just realised we missed cleaning up get_the_author_meta() in #15458. All the logic is duplicated in WP_User::__get(). Fixed in 20285.diff.

comment:6 nacin2 years ago

  • Milestone changed from 3.4 to 3.3.2
  • Priority changed from normal to high
  • Severity changed from normal to major

comment:7 ryan2 years ago

Added some author tests:

http://unit-tests.trac.wordpress.org/changeset/691

20285.diff fails for get_the_author_meta( 'login' ). Perhaps we should add some shorthand mappings to WP_User::back_compat_keys.

comment:8 nacin2 years ago

Tests well. Fine with me.

ryan2 years ago

comment:9 ryan2 years ago

20285.2.diff does the mapping in get_the_author_meta() instead of in WP_User::back_compat_keys to reduce the possibility of unintended consequences for 3.3.2.

comment:11 ryan2 years ago

To reproduce this, you must have a users table that still has user_description in the schema. user_description hasn't been in the users table since 2.0. Somehow the schema for the users table in question has not been updated for a long time.

For the case of user_description existing in meta, 3.3.1 already correctly handles mapping this to description.

comment:12 nacin2 years ago

  • Milestone changed from 3.3.2 to 3.4

comment:13 ryan2 years ago

  • Owner set to ryan
  • Resolution set to fixed
  • Status changed from new to closed

In [20565]:

Remove key mangling code from get_the_author_meta(). Make it more inline with get_user_option(). Handle collision with old users table columns that are on old schema, particularly user_description.

Props scribu, fredwu.
Fixes #20285

comment:15 nacin2 years ago

In [20575]:

Fix a notice in [20565] where get_the_author_meta() may be called prior to postdata from being set up. In this case, the function should return nothing (via the filter). props johnjamesjacoby. fixes #20529. see #20285.

Note: See TracTickets for help on using tickets.