@@ -1358,3 +1358,96 @@ function _wp_multiple_block_styles( $metadata ) {
1358
1358
return $ metadata ;
1359
1359
}
1360
1360
add_filter ( 'block_type_metadata ' , '_wp_multiple_block_styles ' );
1361
+
1362
+ /**
1363
+ * Helper function that constructs a comment query vars array from the passed
1364
+ * block properties.
1365
+ *
1366
+ * It's used with the Comment Query Loop inner blocks.
1367
+ *
1368
+ * @since 6.0.0
1369
+ *
1370
+ * @param WP_Block $block Block instance.
1371
+ *
1372
+ * @return array Returns the comment query parameters to use with the
1373
+ * WP_Comment_Query constructor.
1374
+ */
1375
+ function build_comment_query_vars_from_block ( $ block ) {
1376
+
1377
+ $ comment_args = array (
1378
+ 'orderby ' => 'comment_date_gmt ' ,
1379
+ 'order ' => 'ASC ' ,
1380
+ 'status ' => 'approve ' ,
1381
+ 'no_found_rows ' => false ,
1382
+ 'update_comment_meta_cache ' => false , // We lazy-load comment meta for performance.
1383
+ );
1384
+
1385
+ if ( ! empty ( $ block ->context ['postId ' ] ) ) {
1386
+ $ comment_args ['post_id ' ] = (int ) $ block ->context ['postId ' ];
1387
+ }
1388
+
1389
+ if ( get_option ( 'thread_comments ' ) ) {
1390
+ $ comment_args ['hierarchical ' ] = 'threaded ' ;
1391
+ } else {
1392
+ $ comment_args ['hierarchical ' ] = false ;
1393
+ }
1394
+
1395
+ $ per_page = get_option ( 'comments_per_page ' );
1396
+ $ default_page = get_option ( 'default_comments_page ' );
1397
+
1398
+ if ( $ per_page > 0 ) {
1399
+ $ comment_args ['number ' ] = $ per_page ;
1400
+
1401
+ $ page = (int ) get_query_var ( 'cpage ' );
1402
+ if ( $ page ) {
1403
+ $ comment_args ['paged ' ] = $ page ;
1404
+ } elseif ( 'oldest ' === $ default_page ) {
1405
+ $ comment_args ['paged ' ] = 1 ;
1406
+ } elseif ( 'newest ' === $ default_page ) {
1407
+ $ comment_args ['paged ' ] = (int ) ( new WP_Comment_Query ( $ comment_args ) )->max_num_pages ;
1408
+ }
1409
+ // Set the `cpage` query var to ensure the previous and next pagination links are correct
1410
+ // when inheriting the Discussion Settings.
1411
+ if ( 0 === $ page && isset ( $ comment_args ['paged ' ] ) && $ comment_args ['paged ' ] > 0 ) {
1412
+ set_query_var ( 'cpage ' , $ comment_args ['paged ' ] );
1413
+ }
1414
+ }
1415
+
1416
+ return $ comment_args ;
1417
+ }
1418
+
1419
+ /**
1420
+ * Helper function that returns the proper pagination arrow html for
1421
+ * `CommentsPaginationNext` and `CommentsPaginationPrevious` blocks based on the
1422
+ * provided `paginationArrow` from `CommentsPagination` context.
1423
+ *
1424
+ * It's used in CommentsPaginationNext and CommentsPaginationPrevious blocks.
1425
+ *
1426
+ * @since 6.0.0
1427
+ *
1428
+ * @param WP_Block $block Block instance.
1429
+ * @param string $pagination_type Type of the arrow we will be rendering.
1430
+ * Default 'next'. Accepts 'next' or 'previous'.
1431
+ *
1432
+ * @return string|null Returns the constructed WP_Query arguments.
1433
+ */
1434
+ function get_comments_pagination_arrow ( $ block , $ pagination_type = 'next ' ) {
1435
+ $ arrow_map = array (
1436
+ 'none ' => '' ,
1437
+ 'arrow ' => array (
1438
+ 'next ' => '→ ' ,
1439
+ 'previous ' => '← ' ,
1440
+ ),
1441
+ 'chevron ' => array (
1442
+ 'next ' => '» ' ,
1443
+ 'previous ' => '« ' ,
1444
+ ),
1445
+ );
1446
+ if ( ! empty ( $ block ->context ['comments/paginationArrow ' ] ) && ! empty ( $ arrow_map [ $ block ->context ['comments/paginationArrow ' ] ][ $ pagination_type ] ) ) {
1447
+ $ arrow_attribute = $ block ->context ['comments/paginationArrow ' ];
1448
+ $ arrow = $ arrow_map [ $ block ->context ['comments/paginationArrow ' ] ][ $ pagination_type ];
1449
+ $ arrow_classes = "wp-block-comments-pagination- $ pagination_type-arrow is-arrow- $ arrow_attribute " ;
1450
+ return "<span class=' $ arrow_classes'> $ arrow</span> " ;
1451
+ }
1452
+ return null ;
1453
+ }
0 commit comments