class-walker-page-dropdown.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. * Post API: Walker_PageDropdown class
  4. *
  5. * @package WordPress
  6. * @subpackage Post
  7. * @since 4.4.0
  8. */
  9. /**
  10. * Core class used to create an HTML drop-down list of pages.
  11. *
  12. * @since 2.1.0
  13. *
  14. * @see Walker
  15. */
  16. class Walker_PageDropdown extends Walker {
  17. /**
  18. * What the class handles.
  19. *
  20. * @since 2.1.0
  21. * @var string
  22. *
  23. * @see Walker::$tree_type
  24. */
  25. public $tree_type = 'page';
  26. /**
  27. * Database fields to use.
  28. *
  29. * @since 2.1.0
  30. * @var array
  31. *
  32. * @see Walker::$db_fields
  33. * @todo Decouple this
  34. */
  35. public $db_fields = array(
  36. 'parent' => 'post_parent',
  37. 'id' => 'ID',
  38. );
  39. /**
  40. * Starts the element output.
  41. *
  42. * @since 2.1.0
  43. *
  44. * @see Walker::start_el()
  45. *
  46. * @param string $output Used to append additional content. Passed by reference.
  47. * @param WP_Post $page Page data object.
  48. * @param int $depth Optional. Depth of page in reference to parent pages. Used for padding.
  49. * Default 0.
  50. * @param array $args Optional. Uses 'selected' argument for selected page to set selected HTML
  51. * attribute for option element. Uses 'value_field' argument to fill "value"
  52. * attribute. See wp_dropdown_pages(). Default empty array.
  53. * @param int $id Optional. ID of the current page. Default 0 (unused).
  54. */
  55. public function start_el( &$output, $page, $depth = 0, $args = array(), $id = 0 ) {
  56. $pad = str_repeat( '&nbsp;', $depth * 3 );
  57. if ( ! isset( $args['value_field'] ) || ! isset( $page->{$args['value_field']} ) ) {
  58. $args['value_field'] = 'ID';
  59. }
  60. $output .= "\t<option class=\"level-$depth\" value=\"" . esc_attr( $page->{$args['value_field']} ) . '"';
  61. if ( $page->ID == $args['selected'] ) {
  62. $output .= ' selected="selected"';
  63. }
  64. $output .= '>';
  65. $title = $page->post_title;
  66. if ( '' === $title ) {
  67. /* translators: %d: ID of a post. */
  68. $title = sprintf( __( '#%d (no title)' ), $page->ID );
  69. }
  70. /**
  71. * Filters the page title when creating an HTML drop-down list of pages.
  72. *
  73. * @since 3.1.0
  74. *
  75. * @param string $title Page title.
  76. * @param WP_Post $page Page data object.
  77. */
  78. $title = apply_filters( 'list_pages', $title, $page );
  79. $output .= $pad . esc_html( $title );
  80. $output .= "</option>\n";
  81. }
  82. }