[wp-trac] [WordPress Trac] #31047: Option nonexistence should not be checked against false

WordPress Trac noreply at wordpress.org
Sun Jan 18 18:16:05 UTC 2015


#31047: Option nonexistence should not be checked against false
--------------------------------+-----------------------------
 Reporter:  GregLone            |      Owner:
     Type:  defect (bug)        |     Status:  new
 Priority:  normal              |  Milestone:  Awaiting Review
Component:  Options, Meta APIs  |    Version:  3.4
 Severity:  normal              |   Keywords:
  Focuses:                      |
--------------------------------+-----------------------------
 Hello.

 In 3.4, was introduced a new filter `"default_option_{$option_name}"` in
 `get_option()`. If used, it might cause problems.
 Example:
 - I want my option `'foobar'` to return an empty array by default instead
 of `false`: `add_filter('default_option_foobar', '__return_empty_array')`.
 - My option does not exist in the database yet.
 - For simpler code, I use `update_option()`, it will fallback to
 `add_option()` if my option does not exist anyway.

 Let's see the important parts in
 [https://core.trac.wordpress.org/browser/trunk/src/wp-
 includes/option.php#L229 update_option()]:

 {{{
 $old_value = get_option( $option );
 // ...
 if ( false === $old_value )
     return add_option( $option, $value );
 }}}

 In this case, I've filtered
 [https://core.trac.wordpress.org/browser/trunk/src/wp-
 includes/option.php#L25 get_option()] to return an empty array, not
 `false`. So `update_option()` will never trigger `add_option()`. As a
 result, the option will never be created.
 We have the same test in
 [https://core.trac.wordpress.org/browser/trunk/src/wp-
 includes/option.php#L352 add_option()], so the same kind of problem.

 Thanks.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/31047>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list