get_menus( $object_id ); foreach ( $menus as $menu ) { $items = wp_get_nav_menu_items( $menu->term_id, array( 'post_status' => 'any' ) ); if ( empty( $items ) ) { continue; } foreach ( $items as $item ) { if ( 'custom' !== $item->object ) { continue; } $url = get_post_meta( $item->ID, '_menu_item_url', true ); if ( ! empty( $url ) ) { $urls[] = $url; } } } return array_unique( $urls ); } /** * Replace URLs in nav menu custom-link items. * * Returns a JSON-encoded summary of changes (item_id => old => new) so the * rewriter can back it up. The actual DB writes happen in persist(). * * @param string $object_type 'nav_menu'. * @param int $object_id Menu ID (0 = all menus). * @param array $mapping [ old_url => new_url ]. * @return string|null JSON summary of changes, or null if unchanged. */ public function replace_urls( $object_type, $object_id, array $mapping ) { $menus = $this->get_menus( $object_id ); $changes = array(); foreach ( $menus as $menu ) { $items = wp_get_nav_menu_items( $menu->term_id, array( 'post_status' => 'any' ) ); if ( empty( $items ) ) { continue; } foreach ( $items as $item ) { if ( 'custom' !== $item->object ) { continue; } $old_url = get_post_meta( $item->ID, '_menu_item_url', true ); if ( empty( $old_url ) ) { continue; } $new_url = $this->apply_mapping( $old_url, $mapping ); if ( $new_url !== $old_url ) { $changes[] = array( 'item_id' => (int) $item->ID, 'menu_id' => (int) $menu->term_id, 'old' => $old_url, 'new' => $new_url, ); } } } if ( empty( $changes ) ) { return null; } return wp_json_encode( $changes ); } /** * Persist the nav menu URL changes. * * The $new_content here is the JSON summary produced by replace_urls(). * We decode it and apply each change via update_post_meta, then invalidate * the menu cache. * * @param string $object_type 'nav_menu'. * @param int $object_id Menu ID (unused — summary carries item IDs). * @param string $new_content JSON summary of changes. * @return bool True on success. */ public function persist( $object_type, $object_id, $new_content ) { $changes = json_decode( $new_content, true ); if ( ! is_array( $changes ) ) { return false; } $touched_menus = array(); foreach ( $changes as $change ) { $item_id = isset( $change['item_id'] ) ? (int) $change['item_id'] : 0; $new_url = isset( $change['new'] ) ? $change['new'] : ''; if ( ! $item_id ) { continue; } update_post_meta( $item_id, '_menu_item_url', $new_url ); if ( isset( $change['menu_id'] ) ) { $touched_menus[ (int) $change['menu_id'] ] = true; } } // Invalidate the menu cache so the front end sees new URLs. foreach ( array_keys( $touched_menus ) as $menu_id ) { wp_cache_delete( $menu_id, 'nav_menu' ); } // Also clear the nav_menu_items lookup. wp_cache_delete( 'nav_menu_items', 'nav_menu' ); return true; } /** * Get the menus to process. * * If $object_id is a specific menu term ID, only that menu is processed. * Otherwise all menus are processed. * * @param int $object_id Menu ID (0 = all). * @return array Array of menu term objects. */ private function get_menus( $object_id ) { if ( $object_id ) { $menu = get_term( $object_id, 'nav_menu' ); if ( $menu && ! is_wp_error( $menu ) ) { return array( $menu ); } return array(); } return wp_get_nav_menus(); } /** * Apply the URL mapping to a single URL value. * * Tries exact match first, then path-only match. * * @param string $url URL. * @param array $mapping [ old_url => new_url ]. * @return string */ private function apply_mapping( $url, array $mapping ) { if ( isset( $mapping[ $url ] ) ) { return $mapping[ $url ]; } $url_path = wp_parse_url( $url, PHP_URL_PATH ); if ( ! $url_path ) { return $url; } foreach ( $mapping as $old => $new ) { $old_path = wp_parse_url( $old, PHP_URL_PATH ); if ( $old_path && $old_path === $url_path ) { $scheme = wp_parse_url( $url, PHP_URL_SCHEME ); $host = wp_parse_url( $url, PHP_URL_HOST ); $new_path = wp_parse_url( $new, PHP_URL_PATH ); if ( $scheme && $host && $new_path ) { return $scheme . '://' . $host . $new_path; } return $new; } } return $url; } }