Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/wp-includes/class-wp-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -388,9 +388,11 @@ public function filter( $filter ) {
* @since 3.5.0
*
* @return array<string, mixed> Object as array.
*
* @phpstan-return non-empty-array<string, mixed>
*/
public function to_array() {
/** @var array<string, mixed> $post */
/** @var non-empty-array<string, mixed> $post */
$post = get_object_vars( $this );

foreach ( array( 'ancestors', 'page_template', 'post_category', 'tags_input' ) as $key ) {
Expand Down
39 changes: 24 additions & 15 deletions src/wp-includes/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -990,8 +990,8 @@ function _wp_relative_upload_path( $path ) {
* @phpstan-param 'OBJECT'|'ARRAY_A'|'ARRAY_N' $output
* @phpstan-return (
* $args is array{ fields: 'ids', ... } ? int[] : (
* $output is 'ARRAY_A' ? array<int, array<string, mixed>> : (
* $output is 'ARRAY_N' ? array<int, array<int, mixed>> : WP_Post[]
* $output is 'ARRAY_A' ? array<int, non-empty-array<string, mixed>> : (
* $output is 'ARRAY_N' ? array<int, non-empty-array<int, mixed>> : WP_Post[]
* )
* )
* )
Expand Down Expand Up @@ -1040,13 +1040,17 @@ function get_children( $args = '', $output = OBJECT ) {
} elseif ( ARRAY_A === $output ) {
$weeuns = array();
foreach ( (array) $kids as $kid ) {
$weeuns[ $kid->ID ] = get_object_vars( $kids[ $kid->ID ] );
/** @var non-empty-array<string, mixed> $vars */
$vars = get_object_vars( $kids[ $kid->ID ] );
$weeuns[ $kid->ID ] = $vars;
}
return $weeuns;
} elseif ( ARRAY_N === $output ) {
$babes = array();
foreach ( (array) $kids as $kid ) {
$babes[ $kid->ID ] = array_values( get_object_vars( $kids[ $kid->ID ] ) );
/** @var non-empty-array<string, mixed> $vars */
$vars = get_object_vars( $kids[ $kid->ID ] );
$babes[ $kid->ID ] = array_values( $vars );
}
return $babes;
} else {
Expand Down Expand Up @@ -1124,8 +1128,8 @@ function get_extended( $post ) {
* @phpstan-param 'OBJECT'|'ARRAY_A'|'ARRAY_N' $output
* @phpstan-param 'raw'|'edit'|'db'|'display' $filter
* @phpstan-return (
* $output is 'ARRAY_A' ? array<string, mixed>|null : (
* $output is 'ARRAY_N' ? array<int, mixed>|null : (
* $output is 'ARRAY_A' ? non-empty-array<string, mixed>|null : (
* $output is 'ARRAY_N' ? non-empty-array<int, mixed>|null : (
* WP_Post|null
* )
* )
Expand Down Expand Up @@ -4452,7 +4456,7 @@ function wp_get_post_terms( $post_id = 0, $taxonomy = 'post_tag', $args = array(
*
* @phpstan-param 'OBJECT'|'ARRAY_A' $output
* @phpstan-return (
* $output is 'ARRAY_A' ? array<int, array<string, mixed>> : WP_Post[]|false
* $output is 'ARRAY_A' ? array<int, non-empty-array<string, mixed>> : WP_Post[]|false
* )
*/
function wp_get_recent_posts( $args = array(), $output = ARRAY_A ) {
Expand Down Expand Up @@ -4485,12 +4489,13 @@ function wp_get_recent_posts( $args = array(), $output = ARRAY_A ) {

// Backward compatibility. Prior to 3.1 expected posts to be returned in array.
if ( ARRAY_A === $output ) {
$posts = array();
foreach ( $results as $key => $result ) {
/** @var array<string, mixed> $object_vars */
$object_vars = get_object_vars( $result );
$results[ $key ] = $object_vars;
/** @var non-empty-array<string, mixed> $object_vars */
$object_vars = get_object_vars( $result );
$posts[ $key ] = $object_vars;
}
return $results ? $results : array();
return $posts;
Comment on lines -4493 to +4498

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for a ternary here, because if $results is falsy, then it was an empty string to begin with. So passing through an empty $posts is the same.

}

return $results ? $results : false;
Expand Down Expand Up @@ -6146,6 +6151,10 @@ function trackback_url_list( $tb_list, $post_id ) {
// Get post data.
$postdata = get_post( $post_id, ARRAY_A );

if ( ! $postdata ) {
return;
}
Comment on lines +6154 to +6156

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This prevents a PHP error where the post_excerpt key could be attempted to be read from a null value on $postdata if the lookup failed.


// Form an excerpt.
$excerpt = strip_tags( $postdata['post_excerpt'] ? $postdata['post_excerpt'] : $postdata['post_content'] );

Expand Down Expand Up @@ -6206,8 +6215,8 @@ function get_all_page_ids() {
* @phpstan-param 'OBJECT'|'ARRAY_A'|'ARRAY_N' $output
* @phpstan-param 'raw'|'edit'|'db'|'display' $filter
* @phpstan-return (
* $output is 'ARRAY_A' ? array<string, mixed>|null : (
* $output is 'ARRAY_N' ? array<int, mixed>|null : (
* $output is 'ARRAY_A' ? non-empty-array<string, mixed>|null : (
* $output is 'ARRAY_N' ? non-empty-array<int, mixed>|null : (
* WP_Post|null
* )
* )
Expand All @@ -6234,8 +6243,8 @@ function get_page( $page, $output = OBJECT, $filter = 'raw' ) {
* @phpstan-param 'OBJECT'|'ARRAY_A'|'ARRAY_N' $output
* @phpstan-param string|string[] $post_type
* @phpstan-return (
* $output is 'ARRAY_A' ? array<string, mixed>|null : (
* $output is 'ARRAY_N' ? array<int, mixed>|null : (
* $output is 'ARRAY_A' ? non-empty-array<string, mixed>|null : (
* $output is 'ARRAY_N' ? non-empty-array<int, mixed>|null : (
* WP_Post|null
* )
* )
Expand Down
16 changes: 15 additions & 1 deletion src/wp-includes/revision.php
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,17 @@ function wp_save_revisioned_meta_fields( $revision_id, $post_id ) {
* respectively. Default OBJECT.
* @param string $filter Optional sanitization filter. See sanitize_post(). Default 'raw'.
* @return WP_Post|array|null WP_Post (or array) on success, or null on failure.
*
* @phpstan-param int|WP_Post $post
* @phpstan-param 'OBJECT'|'ARRAY_A'|'ARRAY_N' $output
* @phpstan-param 'raw'|'edit'|'db'|'display' $filter
* @phpstan-return (
* $output is 'ARRAY_A' ? non-empty-array<string, mixed>|null : (
* $output is 'ARRAY_N' ? non-empty-array<int, mixed>|null : (
* WP_Post|null
* )
* )
* )
*/
function wp_get_post_revision( &$post, $output = OBJECT, $filter = 'raw' ) {
$revision = get_post( $post, OBJECT, $filter );
Expand All @@ -438,10 +449,13 @@ function wp_get_post_revision( &$post, $output = OBJECT, $filter = 'raw' ) {
if ( OBJECT === $output ) {
return $revision;
} elseif ( ARRAY_A === $output ) {
/** @var non-empty-array<string, mixed> $_revision */
$_revision = get_object_vars( $revision );
return $_revision;
} elseif ( ARRAY_N === $output ) {
$_revision = array_values( get_object_vars( $revision ) );
/** @var non-empty-array<string, mixed> $vars */
$vars = get_object_vars( $revision );
$_revision = array_values( $vars );
return $_revision;
}

Expand Down
Loading