Opened 8 years ago
Closed 8 years ago
#37738 closed defect (bug) (maybelater)
WP_Post::get_instance returns erroneous data for invalid input such as an array or object instead of false.
Reported by: | deeptiboddapati | Owned by: | boonebgorges |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | 3.5 |
Component: | Posts, Post Types | Keywords: | has-patch has-unit-tests |
Focuses: | Cc: |
Description
Steps to reproduce the problem:
- On any wp site which has a post with id =1 run:
$post = WP_Post::get_instance(array("a","b")); print_r($post); //will print the post with id = 1 (most probably hello world)
- It does the same thing with objects
$obj = (object) array("a","b"); $post = WP_Post::get_instance($obj); print_r($post);
- floats it rounds down to the nearest int and tries to get that post
$post = WP_Post::get_instance(3.5); returns post with id = 3
- If you pass in a boolean it returns the first post for true and false for false.
$post = WP_Post::get_instance(true); //returns post id =1 $post = WP_Post::get_instance(false); //returns false
Attachments (4)
Change History (13)
@
8 years ago
Adds a line to check if the input param is an int,bool or float and returns false if it isn't.
#1
@
8 years ago
- Keywords has-patch has-unit-tests added
- Version changed from trunk to 3.5
Similar: #33372
#2
@
8 years ago
That ticket was really good to read. It helped me notice that the patch missed numeric strings such as '1' or '1.4'. In that ticket they used is_scalar but that returns true for 'apple' as well as '1'. I used is_numeric in this redone patch since it returns true for '1.36546' or '3' but not for '3rd' or 'apple'.
Additionally negative integers don't seem to be a problem. They return false because the ID field in wp_posts is unsigned and as a result there are no posts with negative IDs. The code handles it the same as it would if there were no post with that number.
#3
@
8 years ago
- Milestone changed from Awaiting Review to 4.7
@deeptiboddapati Thank you for the patch and for the tests!
It seems incorrect to me that 1.4
should be rounded down to 1
. That may be how things currently work, but it definitely feels wrong. I assume that backward compatibility will be covered by ensuring that get_post( 1.0 )
does match post 1
(but not get_post( 1.5 )
)
#4
@
8 years ago
- Owner set to boonebgorges
- Resolution set to fixed
- Status changed from new to closed
In 38381:
#5
@
8 years ago
- Milestone changed from 4.7 to 4.8
- Resolution fixed deleted
- Status changed from closed to reopened
There are reports coming in via Slack that this caused issues with plugins that weren't prepared to correctly handle the newer, quality-controlled return values. After a quick chat, I'm reopening this so we can: revert this to stop that plugin breakage, and look into a better solution in 4.8 or beyond.
Test class for WP_Post:get_instance showing where it fails.