Make WordPress Core

Opened 10 years ago

Closed 10 years ago

#34348 closed defect (bug) (fixed)

Error during add term

Reported by: sebastianpisula's profile sebastian.pisula Owned by: boonebgorges's profile boonebgorges
Milestone: 4.4 Priority: normal
Severity: major Version: 4.4
Component: Taxonomy Keywords: needs-patch
Focuses: administration Cc:

Description

I have error during add new term

Catchable fatal error: Object of class stdClass could not be converted to string in class-wp-ajax-response.php on line 107

PHP Version 5.5.9
Windows 10
My WordPress version: 4.4-alpha-35263

Form data:

action:add-tag
screen:edit-news-categories
taxonomy:news-categories
post_type:news
_wpnonce_add-tag:9cab9c52c5
_wp_http_referer:/wordpress/wp-admin/edit-tags.php?taxonomy=news-categories&post_type=news
tag-name:new
slug:new
parent:-1
description:

Attachments (1)

34348.diff (1.7 KB) - added by boonebgorges 10 years ago.

Download all attachments as: .zip

Change History (4)

#1 @johnbillion
10 years ago

  • Component changed from General to Taxonomy
  • Focuses administration added
  • Keywords needs-patch added
  • Milestone changed from Awaiting Review to 4.4
  • Owner set to boonebgorges
  • Severity changed from normal to major
  • Status changed from new to assigned
  • Version set to trunk

Confirmed. This is caused by the data property on the WP_Term class which WP_Ajax_Response::send() attempts to echo.

@boonebgorges
10 years ago

#2 @boonebgorges
10 years ago

sebastian.pisula - Thanks for the report, and good find.

This is a very, very annoying problem. The problematic code is this:

$term = get_term( $term_id, $taxonomy );
$term = (array) $term;

If I thought that wp_ajax_add_tag() were the only place in the universe where this happens, I'd just change how it works - by adding a to_array() method or something like that. But it's likely that there are plugins doing the exact same thing. The problem is that, in order to support uses of (array) $term where the client expects each term property to be scalar, we can never add any additional structure to the term object. That's pretty ridiculous.

So, in the long run, I think we should do one or more of the following:

  • Make WP_Ajax_Response do something more intelligent when one or more of its supplemental fields is non-scalar (skip it, json_encode() it, whatever)
  • Stop doing foreach ( $term as $k => $v ) and expecting $v to be scalar. If you need the raw data, we'll provide a method for that purpose.
  • Tell plugin authors that they cannot expect object properties to be scalar.

For the time being, I can rewrite the way that data is implemented so that we avoid non-scalar properties on WP_Term. It's not a restriction that I want to be held to in the future, but for now we'll make it work.

#3 @boonebgorges
10 years ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 35269:

Don't store data as a property on WP_Term objects.

wp_ajax_add_term() fetches a term using get_term(), and passes the term to
WP_Ajax_Response, which expects each of the term's properties to be scalar.
Having $data as a stdClass (meant to mimic WP_User::data, populated by
a get_row() database query) violated this expectation, causing fatal string
conversion errors. As a workaround, $term->data is converted so that it is
no longer an actual property of the term object, but is assembled only when
requested in the magic __get() method.

Fixes #34348.

Note: See TracTickets for help on using tickets.