> $tabs Tools tabs. * @return array> */ public function add_tab( array $tabs ): array { if ( ! current_user_can( self::CAP ) ) { return $tabs; } $tabs[ self::TAB ] = array( 'label' => __( 'Duplicate Menu', 'all-in-one-utilities' ), 'description' => __( 'Make a full copy of a navigation menu, for example to try changes without touching the live one.', 'all-in-one-utilities' ), 'tips' => array( __( 'The copy keeps every item, its order, and its parent/child structure.', 'all-in-one-utilities' ), __( 'The copy is not assigned to any menu location -- assign it under Appearance → Menus when it is ready.', 'all-in-one-utilities' ), ), 'priority' => 17, 'owner' => 'free', ); return $tabs; } /** * Send the old submenu page's address to the tab. * * @return void */ public function redirect_legacy_page(): void { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only redirect. if ( ! isset( $_GET['page'] ) || self::LEGACY_PAGE !== $_GET['page'] || ! current_user_can( self::CAP ) ) { return; } wp_safe_redirect( ToolsScreen::url( self::TAB ) ); exit; } /** * Render the screen. * * @return void */ public function render(): void { if ( ! current_user_can( self::CAP ) ) { return; } $notice = get_transient( 'aiou_duplicate_menu_notice_' . get_current_user_id() ); delete_transient( 'aiou_duplicate_menu_notice_' . get_current_user_id() ); $nav_menus = wp_get_nav_menus(); ?>

403 ) ); } check_admin_referer( 'aiou-duplicate-menu' ); $redirect = wp_get_referer(); $redirect = $redirect ? $redirect : ToolsScreen::url( self::TAB ); $source_id = isset( $_POST['source'] ) ? absint( $_POST['source'] ) : 0; $new_name = isset( $_POST['new_menu_name'] ) ? sanitize_text_field( wp_unslash( $_POST['new_menu_name'] ) ) : ''; if ( ! $source_id || '' === $new_name ) { $this->notice( $redirect, 'error', __( 'Please choose a menu and enter a name for the new one.', 'all-in-one-utilities' ) ); } $new_menu_id = MenuDuplicator::duplicate( $source_id, $new_name ); if ( ! $new_menu_id ) { $this->notice( $redirect, 'error', __( 'There was a problem duplicating that menu. No action was taken.', 'all-in-one-utilities' ) ); } $view_url = add_query_arg( array( 'action' => 'edit', 'menu' => $new_menu_id, ), admin_url( 'nav-menus.php' ) ); /* translators: %s: link to view the new menu */ $this->notice( $redirect, 'success', sprintf( __( 'Menu duplicated. %s', 'all-in-one-utilities' ), '' . esc_html__( 'View', 'all-in-one-utilities' ) . '' ) ); } /** * Store a one-time notice and redirect back to the screen. * * @param string $redirect Redirect URL. * @param string $type 'success' or 'error'. * @param string $message Notice text (may include safe inline HTML, e.g. a link). * @return never */ private function notice( string $redirect, string $type, string $message ) { set_transient( 'aiou_duplicate_menu_notice_' . get_current_user_id(), array( 'type' => $type, 'message' => $message, ), MINUTE_IN_SECONDS ); wp_safe_redirect( $redirect ); exit; } }