class-walker-page-dropdown.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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( 'parent' => 'post_parent', 'id' => 'ID' );
  36. /**
  37. * Starts the element output.
  38. *
  39. * @since 2.1.0
  40. *
  41. * @see Walker::start_el()
  42. *
  43. * @param string $output Used to append additional content. Passed by reference.
  44. * @param WP_Post $page Page data object.
  45. * @param int $depth Optional. Depth of page in reference to parent pages. Used for padding.
  46. * Default 0.
  47. * @param array $args Optional. Uses 'selected' argument for selected page to set selected HTML
  48. * attribute for option element. Uses 'value_field' argument to fill "value"
  49. * attribute. See wp_dropdown_pages(). Default empty array.
  50. * @param int $id Optional. ID of the current page. Default 0 (unused).
  51. */
  52. public function start_el( &$output, $page, $depth = 0, $args = array(), $id = 0 ) {
  53. $pad = str_repeat('&nbsp;', $depth * 3);
  54. if ( ! isset( $args['value_field'] ) || ! isset( $page->{$args['value_field']} ) ) {
  55. $args['value_field'] = 'ID';
  56. }
  57. $output .= "\t<option class=\"level-$depth\" value=\"" . esc_attr( $page->{$args['value_field']} ) . "\"";
  58. if ( $page->ID == $args['selected'] )
  59. $output .= ' selected="selected"';
  60. $output .= '>';
  61. $title = $page->post_title;
  62. if ( '' === $title ) {
  63. /* translators: %d: ID of a post */
  64. $title = sprintf( __( '#%d (no title)' ), $page->ID );
  65. }
  66. /**
  67. * Filters the page title when creating an HTML drop-down list of pages.
  68. *
  69. * @since 3.1.0
  70. *
  71. * @param string $title Page title.
  72. * @param object $page Page data object.
  73. */
  74. $title = apply_filters( 'list_pages', $title, $page );
  75. $output .= $pad . esc_html( $title );
  76. $output .= "</option>\n";
  77. }
  78. }