Нет описания

class-nav-menu-edit-custom-fields.php 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /** Walker_Nav_Menu_Edit class */
  3. if ( ! class_exists( 'Walker_Nav_Menu_Edit' ) ) {
  4. global $wp_version;
  5. if ( version_compare( $wp_version, '4.4', '>=' ) ) {
  6. require_once ABSPATH . 'wp-admin/includes/class-walker-nav-menu-edit.php';
  7. } else {
  8. require_once ABSPATH . 'wp-admin/includes/nav-menu.php';
  9. }
  10. }
  11. /**
  12. * Custom Walker for Nav Menu Editor
  13. *
  14. * Add wp_nav_menu_item_custom_fields hook to the nav menu editor.
  15. *
  16. * Credits:
  17. * @helgatheviking - Initial concept which has made adding settings in the menu editor in a compatible way.
  18. * @kucrut - preg_replace() method so that we no longer have to translate core strings
  19. * @danieliser - refactor for less complexity between WP versions & updating versioned classes for proper backward compatibility with the new methods.
  20. *
  21. * @since WordPress 3.6.0
  22. * @uses Walker_Nav_Menu_Edit
  23. */
  24. class Walker_Nav_Menu_Edit_Custom_Fields extends Walker_Nav_Menu_Edit {
  25. /**
  26. * Start the element output.
  27. *
  28. * @see Walker_Nav_Menu_Edit::start_el()
  29. *
  30. * @param string $output Passed by reference. Used to append additional content.
  31. * @param object $item Menu item data object.
  32. * @param int $depth Depth of menu item.
  33. * @param array $args
  34. * @param int $id
  35. */
  36. public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
  37. $item_output = '';
  38. $output .= parent::start_el( $item_output, $item, $depth, $args, $id );
  39. // NOTE: Check this regex on major WP version updates!
  40. $output .= preg_replace( '/(?=<fieldset[^>]+class="[^"]*field-move)/', $this->get_custom_fields( $item, $depth, $args ), $item_output );
  41. }
  42. /**
  43. * Get custom fields
  44. *
  45. * @uses do_action() Calls 'menu_item_custom_fields' hook
  46. *
  47. * @param object $item Menu item data object.
  48. * @param int $depth Depth of menu item. Used for padding.
  49. * @param array $args Menu item args.
  50. *
  51. * @return string Additional fields or html for the nav menu editor.
  52. */
  53. protected function get_custom_fields( $item, $depth, $args = array() ) {
  54. ob_start();
  55. $item_id = intval( $item->ID );
  56. /**
  57. * Get menu item custom fields from plugins/themes
  58. *
  59. * @param int $item_id post ID of menu
  60. * @param object $item Menu item data object.
  61. * @param int $depth Depth of menu item. Used for padding.
  62. * @param array $args Menu item args.
  63. *
  64. * @return string Custom fields
  65. */
  66. do_action( 'wp_nav_menu_item_custom_fields', $item_id, $item, $depth, $args );
  67. return ob_get_clean();
  68. }
  69. }