-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Comments Query Loop: Improve the context handling in inner blocks
- Loading branch information
Showing
11 changed files
with
85 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
/** | ||
* Temporary compatibility shims for features present in Gutenberg. | ||
* | ||
* @package gutenberg | ||
*/ | ||
|
||
if ( ! function_exists( 'build_comments_query_vars_from_block' ) ) { | ||
/** | ||
* Helper function that constructs a comment query vars array from the passed block properties. | ||
* | ||
* It's used with the Comment Query Loop inner blocks. | ||
* | ||
* @param WP_Block $block Block instance. | ||
* | ||
* @return array Returns the comment query parameters to use with the WP_Comment_Query constructor. | ||
*/ | ||
function build_comment_query_vars_from_block( $block ) { | ||
$comment_args = array( | ||
'orderby' => 'comment_date_gmt', | ||
'order' => 'ASC', | ||
'status' => 'approve', | ||
'no_found_rows' => false, | ||
'update_comment_meta_cache' => false, // We lazy-load comment meta for performance. | ||
); | ||
|
||
if ( ! empty( $block->context['postId'] ) ) { | ||
$comment_args['post_id'] = (int) $block->context['postId']; | ||
} | ||
|
||
if ( get_option( 'thread_comments' ) ) { | ||
$comment_args['hierarchical'] = 'threaded'; | ||
} else { | ||
$comment_args['hierarchical'] = false; | ||
} | ||
|
||
$per_page = ! empty( $block->context['comments/perPage'] ) ? (int) $block->context['comments/perPage'] : 0; | ||
if ( 0 === $per_page && get_option( 'page_comments' ) ) { | ||
$per_page = (int) get_query_var( 'comments_per_page' ); | ||
if ( 0 === $per_page ) { | ||
$per_page = (int) get_option( 'comments_per_page' ); | ||
} | ||
} | ||
if ( $per_page > 0 ) { | ||
$comment_args['number'] = $per_page; | ||
$page = (int) get_query_var( 'cpage' ); | ||
|
||
if ( $page ) { | ||
$comment_args['offset'] = ( $page - 1 ) * $per_page; | ||
} elseif ( 'oldest' === get_option( 'default_comments_page' ) ) { | ||
$comment_args['offset'] = 0; | ||
} | ||
} | ||
|
||
return $comment_args; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters