Make WordPress Core

Ticket #11095: 11095.patch

File 11095.patch, 15.7 KB (added by stevegrunwell, 11 years ago)

Adds 'format', 'before', and 'after' arguments to wp_list_pages()

  • src/wp-includes/post-template.php

     
    999999 * @param array|string $args {
    10001000 *     Array or string of arguments. Optional.
    10011001 *
     1002 *     @type string $after        Content to display after each page link when using the 'custom' format.
     1003 *                                Default empty.
     1004 *     @type string $authors      Comma-separated list of author IDs. Default empty (all authors).
     1005 *     @type string $before       Content to display before each page link when using the 'custom' format.
     1006 *                                Default empty.
    10021007 *     @type int    $child_of     Display only the sub-pages of a single page by ID. Default 0 (all pages).
    1003  *     @type string $authors      Comma-separated list of author IDs. Default empty (all authors).
    10041008 *     @type string $date_format  PHP date format to use for the listed pages. Relies on the 'show_date' parameter.
    10051009 *                                Default is the value of 'date_format' option.
    10061010 *     @type int    $depth        Number of levels in the hierarchy of pages to include in the generated list.
     
    10081012 *                                the given n depth). Default 0.
    10091013 *     @type bool   $echo         Whether or not to echo the list of pages. Default true.
    10101014 *     @type string $exclude      Comma-separated list of page IDs to exclude. Default empty.
     1015 *     @type string $format       How to display the pages. Accepts 'list' or 'custom'. Default 'list'.
    10111016 *     @type array  $include      Comma-separated list of page IDs to include. Default empty.
    10121017 *     @type string $link_after   Text or HTML to follow the page link label. Default null.
    10131018 *     @type string $link_before  Text or HTML to precede the page link label. Default null.
     
    10261031 */
    10271032function wp_list_pages( $args = '' ) {
    10281033        $defaults = array(
    1029                 'depth' => 0, 'show_date' => '',
     1034                'after' => '',
     1035                'authors' => '',
     1036                'before' => '',
     1037                'child_of' => 0,
    10301038                'date_format' => get_option( 'date_format' ),
    1031                 'child_of' => 0, 'exclude' => '',
    1032                 'title_li' => __( 'Pages' ), 'echo' => 1,
    1033                 'authors' => '', 'sort_column' => 'menu_order, post_title',
    1034                 'link_before' => '', 'link_after' => '', 'walker' => '',
     1039                'depth' => 0,
     1040                'echo' => 1,
     1041                'exclude' => '',
     1042                'format' => 'list',
     1043                'link_after' => '',
     1044                'link_before' => '',
     1045                'show_date' => '',
     1046                'sort_column' => 'menu_order, post_title',
     1047                'title_li' => __( 'Pages' ),
     1048                'walker' => '',
    10351049        );
    10361050
    10371051        $r = wp_parse_args( $args, $defaults );
     
    12021216 */
    12031217function walk_page_tree($pages, $depth, $current_page, $r) {
    12041218        if ( empty($r['walker']) )
    1205                 $walker = new Walker_Page;
     1219                $walker = 'custom' === $r['format'] ? new Walker_PageCustom : new Walker_Page;
    12061220        else
    12071221                $walker = $r['walker'];
    12081222
     
    14361450        }
    14371451}
    14381452
     1453/**
     1454 * Handle the "custom" format for wp_list_pages().
     1455 *
     1456 * @since 4.2
     1457 * @uses Walker
     1458 */
     1459class Walker_PageCustom extends Walker {
     1460        /**
     1461         * @see Walker::$tree_type
     1462         * @since 2.1.0
     1463         * @var string
     1464         */
     1465        public $tree_type = 'page';
     1466
     1467        /**
     1468         * @see Walker::$db_fields
     1469         * @since 2.1.0
     1470         * @todo Decouple this
     1471         * @var array
     1472         */
     1473        public $db_fields = array ('parent' => 'post_parent', 'id' => 'ID');
     1474
     1475        /**
     1476         * @see Walker::start_el()
     1477         * @since 2.1.0
     1478         *
     1479         * @param string $output Passed by reference. Used to append additional content.
     1480         * @param object $page Page data object.
     1481         * @param int $depth Depth of page. Used for padding.
     1482         * @param array $args Additional arguments passed to the walker.
     1483         * @param int $current_page Page ID.
     1484         */
     1485        public function start_el( &$output, $page, $depth = 0, $args = array(), $current_page = 0 ) {
     1486                $css_class = array( 'page_item', 'page-item-' . $page->ID );
     1487
     1488                if ( isset( $args['pages_with_children'][ $page->ID ] ) ) {
     1489                        $css_class[] = 'page_item_has_children';
     1490                }
     1491
     1492                if ( ! empty( $current_page ) ) {
     1493                        $_current_page = get_post( $current_page );
     1494                        if ( $_current_page && in_array( $page->ID, $_current_page->ancestors ) ) {
     1495                                $css_class[] = 'current_page_ancestor';
     1496                        }
     1497                        if ( $page->ID == $current_page ) {
     1498                                $css_class[] = 'current_page_item';
     1499                        } elseif ( $_current_page && $page->ID == $_current_page->post_parent ) {
     1500                                $css_class[] = 'current_page_parent';
     1501                        }
     1502                } elseif ( $page->ID == get_option('page_for_posts') ) {
     1503                        $css_class[] = 'current_page_parent';
     1504                }
     1505
     1506                /**
     1507                 * Filter the list of CSS classes to include with each page item in the list.
     1508                 *
     1509                 * @since 2.8.0
     1510                 *
     1511                 * @see wp_list_pages()
     1512                 *
     1513                 * @param array   $css_class    An array of CSS classes to be applied
     1514                 *                             to each list item.
     1515                 * @param WP_Post $page         Page data object.
     1516                 * @param int     $depth        Depth of page, used for padding.
     1517                 * @param array   $args         An array of arguments.
     1518                 * @param int     $current_page ID of the current page.
     1519                 */
     1520                $css_classes = implode( ' ', apply_filters( 'page_css_class', $css_class, $page, $depth, $args, $current_page ) );
     1521
     1522                if ( '' === $page->post_title ) {
     1523                        $page->post_title = sprintf( __( '#%d (no title)' ), $page->ID );
     1524                }
     1525
     1526                $args['before'] = empty( $args['before'] ) ? '' : $args['before'];
     1527                $args['link_before'] = empty( $args['link_before'] ) ? '' : $args['link_before'];
     1528                $args['link_after'] = empty( $args['link_after'] ) ? '' : $args['link_after'];
     1529                $args['after'] = empty( $args['after'] ) ? '' : $args['after'];
     1530
     1531                /** This filter is documented in wp-includes/post-template.php */
     1532                $output .= sprintf(
     1533                        "\n" . $args['before'],
     1534                        $css_classes
     1535                );
     1536                $output .= sprintf(
     1537                        '<a href="%s">%s%s%s</a>',
     1538                        get_permalink( $page->ID ),
     1539                        $args['link_before'],
     1540                        apply_filters( 'the_title', $page->post_title, $page->ID ),
     1541                        $args['link_after']
     1542                );
     1543
     1544                if ( ! empty( $args['show_date'] ) ) {
     1545                        if ( 'modified' == $args['show_date'] ) {
     1546                                $time = $page->post_modified;
     1547                        } else {
     1548                                $time = $page->post_date;
     1549                        }
     1550
     1551                        $date_format = empty( $args['date_format'] ) ? '' : $args['date_format'];
     1552                        $output .= " " . mysql2date( $date_format, $time );
     1553                }
     1554                $output .= $args['after'];
     1555        }
     1556}
     1557
    14391558//
    14401559// Attachments
    14411560//
  • tests/phpunit/tests/post/listPages.php

     
    2020                'include'      => '',
    2121                'post_type'    => 'page',
    2222                'post_status'  => 'publish',
     23                'format' => ''
     24                'after' => '',
     25                'before' => '',
    2326        );
    2427        */
    2528        function setUp() {
     
    342345                $actual = wp_list_pages( $args );
    343346                $this->AssertEquals( $expected['exclude'], $actual );
    344347        }
     348
     349        function test_wp_list_pages_format() {
     350                $args = array(
     351                        'echo' => false,
     352                        'format' => 'custom'
     353                );
     354                $expected['format'] = '<li class="pagenav">Pages<ul>
     355<a href="' . get_permalink( 1 ) . '">Parent 1</a>
     356<a href="' . get_permalink( 4 ) . '">Child 1</a>
     357<a href="' . get_permalink( 5 ) . '">Child 2</a>
     358<a href="' . get_permalink( 6 ) . '">Child 3</a>
     359<a href="' . get_permalink( 2 ) . '">Parent 2</a>
     360<a href="' . get_permalink( 7 ) . '">Child 1</a>
     361<a href="' . get_permalink( 8 ) . '">Child 2</a>
     362<a href="' . get_permalink( 9 ) . '">Child 3</a>
     363<a href="' . get_permalink( 3 ) . '">Parent 3</a>
     364<a href="' . get_permalink( 10 ) . '">Child 1</a>
     365<a href="' . get_permalink( 11 ) . '">Child 2</a>
     366<a href="' . get_permalink( 12 ) . '">Child 3</a></ul></li>';
     367                $actual = wp_list_pages( $args );
     368                $this->AssertEquals( $expected['format'], $actual );
     369        }
     370
     371        function test_wp_list_pages_before() {
     372                $args = array(
     373                        'echo' => false,
     374                        'format' => 'custom',
     375                        'before' => 'BEFORE'
     376                );
     377                $expected['before'] = '<li class="pagenav">Pages<ul>
     378BEFORE<a href="' . get_permalink( 1 ) . '">Parent 1</a>
     379BEFORE<a href="' . get_permalink( 4 ) . '">Child 1</a>
     380BEFORE<a href="' . get_permalink( 5 ) . '">Child 2</a>
     381BEFORE<a href="' . get_permalink( 6 ) . '">Child 3</a>
     382BEFORE<a href="' . get_permalink( 2 ) . '">Parent 2</a>
     383BEFORE<a href="' . get_permalink( 7 ) . '">Child 1</a>
     384BEFORE<a href="' . get_permalink( 8 ) . '">Child 2</a>
     385BEFORE<a href="' . get_permalink( 9 ) . '">Child 3</a>
     386BEFORE<a href="' . get_permalink( 3 ) . '">Parent 3</a>
     387BEFORE<a href="' . get_permalink( 10 ) . '">Child 1</a>
     388BEFORE<a href="' . get_permalink( 11 ) . '">Child 2</a>
     389BEFORE<a href="' . get_permalink( 12 ) . '">Child 3</a></ul></li>';
     390                $actual = wp_list_pages( $args );
     391                $this->AssertEquals( $expected['before'], $actual );
     392        }
     393
     394        function test_wp_list_pages_after() {
     395                $args = array(
     396                        'echo' => false,
     397                        'format' => 'custom',
     398                        'after' => 'AFTER'
     399                );
     400                $expected['after'] = '<li class="pagenav">Pages<ul>
     401<a href="' . get_permalink( 1 ) . '">Parent 1</a>AFTER
     402<a href="' . get_permalink( 4 ) . '">Child 1</a>AFTER
     403<a href="' . get_permalink( 5 ) . '">Child 2</a>AFTER
     404<a href="' . get_permalink( 6 ) . '">Child 3</a>AFTER
     405<a href="' . get_permalink( 2 ) . '">Parent 2</a>AFTER
     406<a href="' . get_permalink( 7 ) . '">Child 1</a>AFTER
     407<a href="' . get_permalink( 8 ) . '">Child 2</a>AFTER
     408<a href="' . get_permalink( 9 ) . '">Child 3</a>AFTER
     409<a href="' . get_permalink( 3 ) . '">Parent 3</a>AFTER
     410<a href="' . get_permalink( 10 ) . '">Child 1</a>AFTER
     411<a href="' . get_permalink( 11 ) . '">Child 2</a>AFTER
     412<a href="' . get_permalink( 12 ) . '">Child 3</a>AFTER</ul></li>';
     413                $actual = wp_list_pages( $args );
     414                $this->AssertEquals( $expected['after'], $actual );
     415        }
    345416}
  • tests/phpunit/tests/post/pageMenu.php

     
     1<?php
     2
     3class Tests_Page_Menu extends WP_UnitTestCase {
     4        var $pages;
     5
     6        /*
     7        $defaults = array(
     8                'depth' => 0,
     9                'show_date' => '',
     10                'date_format' => get_option('date_format'),
     11                'child_of' => 0,
     12                'exclude' => '',
     13                'title_li' => __('Pages'),
     14                'echo' => 1,
     15                'authors' => '',
     16                'sort_column' => 'menu_order, post_title',
     17                'link_before' => '',
     18                'link_after' => '',
     19                'walker' => '',
     20                'include'      => '',
     21                'post_type'    => 'page',
     22                'post_status'  => 'publish',
     23                'format' => ''
     24                'after' => '',
     25                'before' => '',
     26        );
     27        */
     28        function setUp() {
     29                parent::setUp();
     30                global $wpdb;
     31                $wpdb->query( 'TRUNCATE ' . $wpdb->prefix . 'posts' );
     32                $pages = array();
     33                $this->factory->user->create();
     34                $pages[] = $this->factory->post->create( array( 'post_type' => 'page', 'post_title' => 'Parent 1' ) );
     35                $pages[] = $this->factory->post->create( array( 'post_type' => 'page', 'post_title' => 'Parent 2' ) );
     36                $pages[] = $this->factory->post->create( array( 'post_type' => 'page', 'post_title' => 'Parent 3', 'post_author' => '2' ) );
     37
     38                foreach ( $pages as $page ) {
     39                        $this->pages[$page] = $this->factory->post->create( array( 'post_parent' => $page, 'post_type' => 'page', 'post_title' => 'Child 1' ) );
     40                        $this->pages[$page] = $this->factory->post->create( array( 'post_parent' => $page, 'post_type' => 'page', 'post_title' => 'Child 2' ) );
     41                        $this->pages[$page] = $this->factory->post->create( array( 'post_parent' => $page, 'post_type' => 'page', 'post_title' => 'Child 3' ) );
     42                }
     43        }
     44
     45        function test_wp_page_menu_default() {
     46                $args = array(
     47                        'echo' => false
     48                );
     49                $expected['default'] = '<div class="menu"><ul>
     50<li class="page_item page-item-1 page_item_has_children"><a href="' . get_permalink( 1 ) . '">Parent 1</a>
     51<ul class=\'children\'>
     52        <li class="page_item page-item-4"><a href="' . get_permalink( 4 ) . '">Child 1</a></li>
     53        <li class="page_item page-item-5"><a href="' . get_permalink( 5 ) . '">Child 2</a></li>
     54        <li class="page_item page-item-6"><a href="' . get_permalink( 6 ) . '">Child 3</a></li>
     55</ul>
     56</li>
     57<li class="page_item page-item-2 page_item_has_children"><a href="' . get_permalink( 2 ) . '">Parent 2</a>
     58<ul class=\'children\'>
     59        <li class="page_item page-item-7"><a href="' . get_permalink( 7 ) . '">Child 1</a></li>
     60        <li class="page_item page-item-8"><a href="' . get_permalink( 8 ) . '">Child 2</a></li>
     61        <li class="page_item page-item-9"><a href="' . get_permalink( 9 ) . '">Child 3</a></li>
     62</ul>
     63</li>
     64<li class="page_item page-item-3 page_item_has_children"><a href="' . get_permalink( 3 ) . '">Parent 3</a>
     65<ul class=\'children\'>
     66        <li class="page_item page-item-10"><a href="' . get_permalink( 10 ) . '">Child 1</a></li>
     67        <li class="page_item page-item-11"><a href="' . get_permalink( 11 ) . '">Child 2</a></li>
     68        <li class="page_item page-item-12"><a href="' . get_permalink( 12 ) . '">Child 3</a></li>
     69</ul>
     70</li>
     71</ul></div>';
     72                $actual = wp_page_menu( $args );
     73                $this->AssertEquals( $this->collapse_whitespace( $expected['default'] ), $actual );
     74        }
     75
     76        function test_wp_page_menu_echo() {
     77                $args = array(
     78                        'echo' => true,
     79                        'depth' => 1
     80                );
     81                $expected['echo'] = '<div class="menu"><ul>
     82<li class="page_item page-item-1 page_item_has_children"><a href="' . get_permalink( 1 ) . '">Parent 1</a></li>
     83<li class="page_item page-item-2 page_item_has_children"><a href="' . get_permalink( 2 ) . '">Parent 2</a></li>
     84<li class="page_item page-item-3 page_item_has_children"><a href="' . get_permalink( 3 ) . '">Parent 3</a></li>
     85</ul></div>';
     86                ob_start();
     87                wp_page_menu( $args );
     88                $actual = ob_get_clean();
     89                $this->AssertEquals( $this->collapse_whitespace( $expected['echo'] ), $actual );
     90        }
     91
     92        function test_wp_page_menu_show_home() {
     93                $args = array(
     94                        'echo' => false,
     95                        'depth' => 1,
     96                        'show_home' => 1
     97                );
     98                $expected['show_home'] = '<div class="menu"><ul>
     99<li ><a href="' . home_url( '/' ) . '">' . __( 'Home' ) . '</a></li>
     100<li class="page_item page-item-1 page_item_has_children"><a href="' . get_permalink( 1 ) . '">Parent 1</a></li>
     101<li class="page_item page-item-2 page_item_has_children"><a href="' . get_permalink( 2 ) . '">Parent 2</a></li>
     102<li class="page_item page-item-3 page_item_has_children"><a href="' . get_permalink( 3 ) . '">Parent 3</a></li>
     103</ul></div>';
     104                $actual = wp_page_menu( $args );
     105                $this->AssertEquals( $this->collapse_whitespace( $expected['show_home'] ), $actual );
     106
     107                // An integer *or* a boolean TRUE should cause the default home link
     108                $args['show_home'] = true;
     109                $actual = wp_page_menu( $args );
     110                $this->AssertEquals( $this->collapse_whitespace( $expected['show_home'] ), $actual );
     111        }
     112
     113        function test_wp_page_menu_show_home_custom() {
     114                $args = array(
     115                        'echo' => false,
     116                        'depth' => 1,
     117                        'show_home' => 'HOMEPAGE'
     118                );
     119                $expected['show_home_custom'] = '<div class="menu"><ul>
     120<li ><a href="' . home_url( '/' ) . '">HOMEPAGE</a></li>
     121<li class="page_item page-item-1 page_item_has_children"><a href="' . get_permalink( 1 ) . '">Parent 1</a></li>
     122<li class="page_item page-item-2 page_item_has_children"><a href="' . get_permalink( 2 ) . '">Parent 2</a></li>
     123<li class="page_item page-item-3 page_item_has_children"><a href="' . get_permalink( 3 ) . '">Parent 3</a></li>
     124</ul></div>';
     125                $actual = wp_page_menu( $args );
     126                $this->AssertEquals( $this->collapse_whitespace( $expected['show_home_custom'] ), $actual );
     127        }
     128
     129        /**
     130         * wp_page_menu() does some string replacement just before returning that collapses whitespace
     131         * characters. For the sake of our assertions, we'll run that same operation.
     132         *
     133         * @param str $expected Your expected output, with whitespace for readability.
     134         * @return str The collapsed expectation.
     135         */
     136  protected function collapse_whitespace( $expected ) {
     137        return str_replace( array( "\r", "\n", "\t" ), '', $expected ) . "\n";
     138  }
     139
     140}