wp/wp-includes/class-walker-page.php
changeset 7 cf61fcea0001
child 9 177826044cd9
equal deleted inserted replaced
6:490d5cc509ed 7:cf61fcea0001
       
     1 <?php
       
     2 /**
       
     3  * Post API: Walker_Page class
       
     4  *
       
     5  * @package WordPress
       
     6  * @subpackage Template
       
     7  * @since 4.4.0
       
     8  */
       
     9 
       
    10 /**
       
    11  * Core walker class used to create an HTML list of pages.
       
    12  *
       
    13  * @since 2.1.0
       
    14  *
       
    15  * @see Walker
       
    16  */
       
    17 class Walker_Page extends Walker {
       
    18 
       
    19 	/**
       
    20 	 * What the class handles.
       
    21 	 *
       
    22 	 * @since 2.1.0
       
    23 	 * @var string
       
    24 	 *
       
    25 	 * @see Walker::$tree_type
       
    26 	 */
       
    27 	public $tree_type = 'page';
       
    28 
       
    29 	/**
       
    30 	 * Database fields to use.
       
    31 	 *
       
    32 	 * @since 2.1.0
       
    33 	 * @var array
       
    34 	 *
       
    35 	 * @see Walker::$db_fields
       
    36 	 * @todo Decouple this.
       
    37 	 */
       
    38 	public $db_fields = array( 'parent' => 'post_parent', 'id' => 'ID' );
       
    39 
       
    40 	/**
       
    41 	 * Outputs the beginning of the current level in the tree before elements are output.
       
    42 	 *
       
    43 	 * @since 2.1.0
       
    44 	 *
       
    45 	 * @see Walker::start_lvl()
       
    46 	 *
       
    47 	 * @param string $output Used to append additional content (passed by reference).
       
    48 	 * @param int    $depth  Optional. Depth of page. Used for padding. Default 0.
       
    49 	 * @param array  $args   Optional. Arguments for outputting the next level.
       
    50 	 *                       Default empty array.
       
    51 	 */
       
    52 	public function start_lvl( &$output, $depth = 0, $args = array() ) {
       
    53 		if ( isset( $args['item_spacing'] ) && 'preserve' === $args['item_spacing'] ) {
       
    54 			$t = "\t";
       
    55 			$n = "\n";
       
    56 		} else {
       
    57 			$t = '';
       
    58 			$n = '';
       
    59 		}
       
    60 		$indent = str_repeat( $t, $depth );
       
    61 		$output .= "{$n}{$indent}<ul class='children'>{$n}";
       
    62 	}
       
    63 
       
    64 	/**
       
    65 	 * Outputs the end of the current level in the tree after elements are output.
       
    66 	 *
       
    67 	 * @since 2.1.0
       
    68 	 *
       
    69 	 * @see Walker::end_lvl()
       
    70 	 *
       
    71 	 * @param string $output Used to append additional content (passed by reference).
       
    72 	 * @param int    $depth  Optional. Depth of page. Used for padding. Default 0.
       
    73 	 * @param array  $args   Optional. Arguments for outputting the end of the current level.
       
    74 	 *                       Default empty array.
       
    75 	 */
       
    76 	public function end_lvl( &$output, $depth = 0, $args = array() ) {
       
    77 		if ( isset( $args['item_spacing'] ) && 'preserve' === $args['item_spacing'] ) {
       
    78 			$t = "\t";
       
    79 			$n = "\n";
       
    80 		} else {
       
    81 			$t = '';
       
    82 			$n = '';
       
    83 		}
       
    84 		$indent = str_repeat( $t, $depth );
       
    85 		$output .= "{$indent}</ul>{$n}";
       
    86 	}
       
    87 
       
    88 	/**
       
    89 	 * Outputs the beginning of the current element in the tree.
       
    90 	 *
       
    91 	 * @see Walker::start_el()
       
    92 	 * @since 2.1.0
       
    93 	 *
       
    94 	 * @param string  $output       Used to append additional content. Passed by reference.
       
    95 	 * @param WP_Post $page         Page data object.
       
    96 	 * @param int     $depth        Optional. Depth of page. Used for padding. Default 0.
       
    97 	 * @param array   $args         Optional. Array of arguments. Default empty array.
       
    98 	 * @param int     $current_page Optional. Page ID. Default 0.
       
    99 	 */
       
   100 	public function start_el( &$output, $page, $depth = 0, $args = array(), $current_page = 0 ) {
       
   101 		if ( isset( $args['item_spacing'] ) && 'preserve' === $args['item_spacing'] ) {
       
   102 			$t = "\t";
       
   103 			$n = "\n";
       
   104 		} else {
       
   105 			$t = '';
       
   106 			$n = '';
       
   107 		}
       
   108 		if ( $depth ) {
       
   109 			$indent = str_repeat( $t, $depth );
       
   110 		} else {
       
   111 			$indent = '';
       
   112 		}
       
   113 
       
   114 		$css_class = array( 'page_item', 'page-item-' . $page->ID );
       
   115 
       
   116 		if ( isset( $args['pages_with_children'][ $page->ID ] ) ) {
       
   117 			$css_class[] = 'page_item_has_children';
       
   118 		}
       
   119 
       
   120 		if ( ! empty( $current_page ) ) {
       
   121 			$_current_page = get_post( $current_page );
       
   122 			if ( $_current_page && in_array( $page->ID, $_current_page->ancestors ) ) {
       
   123 				$css_class[] = 'current_page_ancestor';
       
   124 			}
       
   125 			if ( $page->ID == $current_page ) {
       
   126 				$css_class[] = 'current_page_item';
       
   127 			} elseif ( $_current_page && $page->ID == $_current_page->post_parent ) {
       
   128 				$css_class[] = 'current_page_parent';
       
   129 			}
       
   130 		} elseif ( $page->ID == get_option('page_for_posts') ) {
       
   131 			$css_class[] = 'current_page_parent';
       
   132 		}
       
   133 
       
   134 		/**
       
   135 		 * Filters the list of CSS classes to include with each page item in the list.
       
   136 		 *
       
   137 		 * @since 2.8.0
       
   138 		 *
       
   139 		 * @see wp_list_pages()
       
   140 		 *
       
   141 		 * @param array   $css_class    An array of CSS classes to be applied
       
   142 		 *                              to each list item.
       
   143 		 * @param WP_Post $page         Page data object.
       
   144 		 * @param int     $depth        Depth of page, used for padding.
       
   145 		 * @param array   $args         An array of arguments.
       
   146 		 * @param int     $current_page ID of the current page.
       
   147 		 */
       
   148 		$css_classes = implode( ' ', apply_filters( 'page_css_class', $css_class, $page, $depth, $args, $current_page ) );
       
   149 
       
   150 		if ( '' === $page->post_title ) {
       
   151 			/* translators: %d: ID of a post */
       
   152 			$page->post_title = sprintf( __( '#%d (no title)' ), $page->ID );
       
   153 		}
       
   154 
       
   155 		$args['link_before'] = empty( $args['link_before'] ) ? '' : $args['link_before'];
       
   156 		$args['link_after'] = empty( $args['link_after'] ) ? '' : $args['link_after'];
       
   157 
       
   158 		$atts = array();
       
   159 		$atts['href'] = get_permalink( $page->ID );
       
   160 
       
   161 		/**
       
   162 		 * Filters the HTML attributes applied to a page menu item's anchor element.
       
   163 		 *
       
   164 		 * @since 4.8.0
       
   165 		 *
       
   166 		 * @param array $atts {
       
   167 		 *     The HTML attributes applied to the menu item's `<a>` element, empty strings are ignored.
       
   168 		 *
       
   169 		 *     @type string $href The href attribute.
       
   170 		 * }
       
   171 		 * @param WP_Post $page         Page data object.
       
   172 		 * @param int     $depth        Depth of page, used for padding.
       
   173 		 * @param array   $args         An array of arguments.
       
   174 		 * @param int     $current_page ID of the current page.
       
   175 		 */
       
   176 		$atts = apply_filters( 'page_menu_link_attributes', $atts, $page, $depth, $args, $current_page );
       
   177 
       
   178 		$attributes = '';
       
   179 		foreach ( $atts as $attr => $value ) {
       
   180 			if ( ! empty( $value ) ) {
       
   181 				$value = esc_attr( $value );
       
   182 				$attributes .= ' ' . $attr . '="' . $value . '"';
       
   183 			}
       
   184 		}
       
   185 
       
   186 		$output .= $indent . sprintf(
       
   187 			'<li class="%s"><a%s>%s%s%s</a>',
       
   188 			$css_classes,
       
   189 			$attributes,
       
   190 			$args['link_before'],
       
   191 			/** This filter is documented in wp-includes/post-template.php */
       
   192 			apply_filters( 'the_title', $page->post_title, $page->ID ),
       
   193 			$args['link_after']
       
   194 		);
       
   195 
       
   196 		if ( ! empty( $args['show_date'] ) ) {
       
   197 			if ( 'modified' == $args['show_date'] ) {
       
   198 				$time = $page->post_modified;
       
   199 			} else {
       
   200 				$time = $page->post_date;
       
   201 			}
       
   202 
       
   203 			$date_format = empty( $args['date_format'] ) ? '' : $args['date_format'];
       
   204 			$output .= " " . mysql2date( $date_format, $time );
       
   205 		}
       
   206 	}
       
   207 
       
   208 	/**
       
   209 	 * Outputs the end of the current element in the tree.
       
   210 	 *
       
   211 	 * @since 2.1.0
       
   212 	 *
       
   213 	 * @see Walker::end_el()
       
   214 	 *
       
   215 	 * @param string  $output Used to append additional content. Passed by reference.
       
   216 	 * @param WP_Post $page   Page data object. Not used.
       
   217 	 * @param int     $depth  Optional. Depth of page. Default 0 (unused).
       
   218 	 * @param array   $args   Optional. Array of arguments. Default empty array.
       
   219 	 */
       
   220 	public function end_el( &$output, $page, $depth = 0, $args = array() ) {
       
   221 		if ( isset( $args['item_spacing'] ) && 'preserve' === $args['item_spacing'] ) {
       
   222 			$t = "\t";
       
   223 			$n = "\n";
       
   224 		} else {
       
   225 			$t = '';
       
   226 			$n = '';
       
   227 		}
       
   228 		$output .= "</li>{$n}";
       
   229 	}
       
   230 
       
   231 }