class-walker-page-dropdown.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 string[]
  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. * @since 5.9.0 Renamed `$page` to `$data_object` and `$id` to `$current_object_id`
  44. * to match parent class for PHP 8 named parameter support.
  45. *
  46. * @see Walker::start_el()
  47. *
  48. * @param string $output Used to append additional content. Passed by reference.
  49. * @param WP_Post $data_object Page data object.
  50. * @param int $depth Optional. Depth of page in reference to parent pages.
  51. * Used for padding. Default 0.
  52. * @param array $args Optional. Uses 'selected' argument for selected page to
  53. * set selected HTML attribute for option element. Uses
  54. * 'value_field' argument to fill "value" attribute.
  55. * See wp_dropdown_pages(). Default empty array.
  56. * @param int $current_object_id Optional. ID of the current page. Default 0.
  57. */
  58. public function start_el( &$output, $data_object, $depth = 0, $args = array(), $current_object_id = 0 ) {
  59. // Restores the more descriptive, specific name for use within this method.
  60. $page = $data_object;
  61. $pad = str_repeat( '&nbsp;', $depth * 3 );
  62. if ( ! isset( $args['value_field'] ) || ! isset( $page->{$args['value_field']} ) ) {
  63. $args['value_field'] = 'ID';
  64. }
  65. $output .= "\t<option class=\"level-$depth\" value=\"" . esc_attr( $page->{$args['value_field']} ) . '"';
  66. if ( $page->ID == $args['selected'] ) {
  67. $output .= ' selected="selected"';
  68. }
  69. $output .= '>';
  70. $title = $page->post_title;
  71. if ( '' === $title ) {
  72. /* translators: %d: ID of a post. */
  73. $title = sprintf( __( '#%d (no title)' ), $page->ID );
  74. }
  75. /**
  76. * Filters the page title when creating an HTML drop-down list of pages.
  77. *
  78. * @since 3.1.0
  79. *
  80. * @param string $title Page title.
  81. * @param WP_Post $page Page data object.
  82. */
  83. $title = apply_filters( 'list_pages', $title, $page );
  84. $output .= $pad . esc_html( $title );
  85. $output .= "</option>\n";
  86. }
  87. }