Make WordPress Core


Ignore:
Timestamp:
10/20/2020 08:09:39 PM (3 years ago)
Author:
SergeyBiryukov
Message:

Comments: Add a filter for top level comments query arguments in comments_template().

Props hellofromTonya, Howdy_McGee, garrett-eclipse, davidbaumwald, thomaslhotta.
Fixes #38074.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/comment/commentsTemplate.php

    r46586 r49256  
    961961        $this->assertSame( array( $comment_3 ), $found_cids );
    962962    }
     963
     964    /**
     965     * @ticket 38074
     966     * @dataProvider data_comments_template_top_level_query_args
     967     *
     968     * @param array $expected             Array of expected values.
     969     * @param array $query_args           Args for the 'comments_template_query_args' filter.
     970     * @param array $top_level_query_args Args for the 'comments_template_top_level_query_args' filter.
     971     */
     972    public function test_comments_template_top_level_query_args( $expected, $query_args, $top_level_query_args ) {
     973        $now         = time();
     974        $offset      = 0;
     975        $p           = self::factory()->post->create();
     976        $comment_ids = array();
     977
     978        for ( $num = 1; $num <= 6; $num++ ) {
     979            $comment_ids[ $num ] = self::factory()->comment->create(
     980                array(
     981                    'comment_post_ID'  => $p,
     982                    'comment_content'  => "{$num}",
     983                    'comment_date_gmt' => gmdate( 'Y-m-d H:i:s', $now - 100 * $num ),
     984                )
     985            );
     986            add_comment_meta( $comment_ids[ $num ], 'featured', $num > 3 ? '1' : '0' );
     987        }
     988
     989        update_option( 'comment_order', 'asc' );
     990        update_option( 'comments_per_page', 3 );
     991        update_option( 'page_comments', 1 );
     992        update_option( 'default_comments_page', 'newest' );
     993
     994        add_filter(
     995            'comments_template_query_args',
     996            function ( $args ) use ( &$offset, $query_args ) {
     997                $offset = $args['offset'];
     998
     999                return array_merge( $args, $query_args );
     1000            }
     1001        );
     1002
     1003        if ( ! empty( $top_level_query_args ) ) {
     1004            add_filter(
     1005                'comments_template_top_level_query_args',
     1006                function ( $args ) use ( $top_level_query_args ) {
     1007                    return array_merge( $args, $top_level_query_args );
     1008                }
     1009            );
     1010        }
     1011
     1012        $this->go_to( get_permalink( $p ) );
     1013
     1014        $found = get_echo( 'comments_template' );
     1015        preg_match_all( '/id="comment-([0-9]+)"/', $found, $matches );
     1016
     1017        $expected_ids = array();
     1018        foreach ( $expected['ids'] as $index ) {
     1019            $expected_ids[] = $comment_ids[ $index ];
     1020        }
     1021
     1022        $this->assertSame( $expected_ids, array_map( 'intval', $matches[1] ) );
     1023        $this->assertEquals( $expected['offset'], $offset );
     1024    }
     1025
     1026    public function data_comments_template_top_level_query_args() {
     1027        return array(
     1028            array(
     1029                array(
     1030                    'ids'    => array(),
     1031                    'offset' => 3,
     1032                ),
     1033                array(
     1034                    'meta_key'   => 'featured',
     1035                    'meta_value' => '1',
     1036                ),
     1037                array(),
     1038            ),
     1039            array(
     1040                array(
     1041                    'ids'    => array(),
     1042                    'offset' => 3,
     1043                ),
     1044                array(
     1045                    'order'      => 'DESC',
     1046                    'meta_key'   => 'featured',
     1047                    'meta_value' => '0',
     1048                ),
     1049                array(),
     1050            ),
     1051            array(
     1052                array(
     1053                    'ids'    => array( 6, 5, 4 ),
     1054                    'offset' => 0,
     1055                ),
     1056                array(
     1057                    'meta_key'   => 'featured',
     1058                    'meta_value' => '1',
     1059                ),
     1060                array(
     1061                    'meta_key'   => 'featured',
     1062                    'meta_value' => '1',
     1063                ),
     1064            ),
     1065            array(
     1066                array(
     1067                    'ids'    => array( 4, 5, 6 ),
     1068                    'offset' => 0,
     1069                ),
     1070                array(
     1071                    'order'      => 'DESC',
     1072                    'meta_key'   => 'featured',
     1073                    'meta_value' => '1',
     1074                ),
     1075                array(
     1076                    'order'      => 'DESC',
     1077                    'meta_key'   => 'featured',
     1078                    'meta_value' => '1',
     1079                ),
     1080            ),
     1081        );
     1082    }
    9631083}
Note: See TracChangeset for help on using the changeset viewer.