class-acf-walker-nav-menu-edit.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit; // Exit if accessed directly
  4. }
  5. if ( ! class_exists( 'ACF_Walker_Nav_Menu_Edit' ) ) :
  6. class ACF_Walker_Nav_Menu_Edit extends Walker_Nav_Menu_Edit {
  7. /**
  8. * Starts the element output.
  9. *
  10. * Calls the Walker_Nav_Menu_Edit start_el function and then injects the custom field HTML
  11. *
  12. * @since 5.0.0
  13. * @since 5.7.2 Added preg_replace based on https://github.com/ineagu/wp-menu-item-custom-fields
  14. *
  15. * @param string $output Used to append additional content (passed by reference).
  16. * @param WP_Post $item Menu item data object.
  17. * @param int $depth Depth of menu item. Used for padding.
  18. * @param stdClass $args An object of wp_nav_menu() arguments.
  19. * @param int $id Current item ID.
  20. */
  21. function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
  22. // vars
  23. $item_output = '';
  24. // call parent function
  25. parent::start_el( $item_output, $item, $depth, $args, $id );
  26. // inject custom field HTML
  27. $output .= preg_replace(
  28. // NOTE: Check this regex from time to time!
  29. '/(?=<(fieldset|p)[^>]+class="[^"]*field-move)/',
  30. $this->get_fields( $item, $depth, $args, $id ),
  31. $item_output
  32. );
  33. }
  34. /**
  35. * Get custom fields HTML
  36. *
  37. * @since 5.0.0
  38. * @since 5.7.2 Added action based on https://github.com/ineagu/wp-menu-item-custom-fields
  39. *
  40. * @param object $item Menu item data object.
  41. * @param int $depth Depth of menu item. Used for padding.
  42. * @param array $args Menu item args.
  43. * @param int $id Nav menu ID.
  44. * @return string
  45. */
  46. function get_fields( $item, $depth, $args = array(), $id = 0 ) {
  47. ob_start();
  48. /**
  49. * Get menu item custom fields from plugins/themes
  50. *
  51. * @since 5.7.2
  52. *
  53. * @param int $item_id post ID of menu
  54. * @param object $item Menu item data object.
  55. * @param int $depth Depth of menu item. Used for padding.
  56. * @param array $args Menu item args.
  57. * @param int $id Nav menu ID.
  58. */
  59. do_action( 'wp_nav_menu_item_custom_fields', $item->ID, $item, $depth, $args, $id );
  60. return ob_get_clean();
  61. }
  62. }
  63. endif;