-
Notifications
You must be signed in to change notification settings - Fork 18
fix: Custom Menu Ordering for GoDAM #1329
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Conversation
rtBot
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code analysis identified issues
action-phpcs-code-review has identified potential problems in this pull request during automated scanning. We recommend reviewing the issues noted and that they are resolved.
phpcs scanning turned up:
Powered by rtCamp's GitHub Actions Library
| */ | ||
| function custom_menu_order( $menu_order ) { | ||
| // Find the position of upload.php & rtgodam. | ||
| $media_position = array_search( 'upload.php', $menu_order ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| // Find the position of upload.php & rtgodam. | ||
| $media_position = array_search( 'upload.php', $menu_order ); | ||
|
|
||
| $rtgodam_position = array_search( 'rtgodam', $menu_order ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This pull request adds custom menu ordering functionality to position the GoDAM plugin's menu item (rtgodam) immediately after the WordPress Media menu (upload.php) in the admin dashboard for improved accessibility and user experience.
Key changes:
- Introduces a custom menu order filter to enable menu repositioning
- Implements logic to dynamically reorder the admin menu by moving
rtgodamafterupload.php
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| // Reinsert rtgodam right after upload.php. | ||
| array_splice( $menu_order, $media_position + 1, 0, 'rtgodam' ); | ||
| // Reindex the array to maintain proper order. | ||
| $menu_order = array_values( $menu_order ); |
Copilot
AI
Nov 11, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a potential bug in the menu reordering logic. After unset($menu_order[$rtgodam_position]) on line 136, the array keys are not reindexed, which means $media_position may no longer be a valid sequential index for array_splice() on line 138. This could cause array_splice() to insert the item at an incorrect position.
To fix this, you should call array_values() to reindex the array before calculating the insertion position:
// Remove rtgodam from its current position.
unset( $menu_order[ $rtgodam_position ] );
// Reindex the array before splicing.
$menu_order = array_values( $menu_order );
// Recalculate media position after reindexing.
$media_position = array_search( 'upload.php', $menu_order );
// Reinsert rtgodam right after upload.php.
array_splice( $menu_order, $media_position + 1, 0, 'rtgodam' );| // Reinsert rtgodam right after upload.php. | |
| array_splice( $menu_order, $media_position + 1, 0, 'rtgodam' ); | |
| // Reindex the array to maintain proper order. | |
| $menu_order = array_values( $menu_order ); | |
| // Reindex the array before splicing. | |
| $menu_order = array_values( $menu_order ); | |
| // Recalculate media position after reindexing. | |
| $media_position = array_search( 'upload.php', $menu_order ); | |
| // Reinsert rtgodam right after upload.php. | |
| array_splice( $menu_order, $media_position + 1, 0, 'rtgodam' ); |
| // Reinsert rtgodam right after upload.php. | ||
| array_splice( $menu_order, $media_position + 1, 0, 'rtgodam' ); | ||
| // Reindex the array to maintain proper order. | ||
| $menu_order = array_values( $menu_order ); |
Copilot
AI
Nov 11, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment "Reindex the array to maintain proper order" is misleading at this location. The array should be reindexed immediately after the unset() operation on line 136 and before using array_splice() on line 138 to ensure correct positioning. Reindexing after array_splice() doesn't address the core issue with the insertion logic.
| // Reinsert rtgodam right after upload.php. | |
| array_splice( $menu_order, $media_position + 1, 0, 'rtgodam' ); | |
| // Reindex the array to maintain proper order. | |
| $menu_order = array_values( $menu_order ); | |
| // Reindex the array to maintain proper order before reinserting. | |
| $menu_order = array_values( $menu_order ); | |
| // Reinsert rtgodam right after upload.php. | |
| array_splice( $menu_order, $media_position + 1, 0, 'rtgodam' ); |
|
|
||
| // Enable custom menu order to make sure rtgodam appears right after upload.php. | ||
| add_filter( 'custom_menu_order', '__return_true' ); | ||
| add_filter( 'menu_order', 'custom_menu_order', PHP_INT_MAX ); |
Copilot
AI
Nov 11, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The callback reference 'custom_menu_order' should be updated if the function is renamed to follow the rtgodam_ prefix convention (as suggested in the naming comment). This ensures the filter correctly references the renamed function.
|
|
||
| // Enable custom menu order to make sure rtgodam appears right after upload.php. | ||
| add_filter( 'custom_menu_order', '__return_true' ); | ||
| add_filter( 'menu_order', 'custom_menu_order', PHP_INT_MAX ); |
Copilot
AI
Nov 11, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using PHP_INT_MAX as the priority for the menu_order filter is unnecessarily extreme and could indicate a code smell. This ensures the callback runs last, but it also makes it difficult for other plugins to run after this if needed, and could lead to conflicts. Consider using a more reasonable priority like 999 or 9999, which still ensures late execution but allows for some flexibility. WordPress core typically uses priorities in the range of 1-100, and well-behaved plugins use values like 10, 20, 100, or occasionally 999.
| add_filter( 'menu_order', 'custom_menu_order', PHP_INT_MAX ); | |
| add_filter( 'menu_order', 'custom_menu_order', 999 ); |
| * @param array $menu_order Array of menu order items. | ||
| * @return array Modified menu order array. | ||
| */ | ||
| function custom_menu_order( $menu_order ) { |
Copilot
AI
Nov 11, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function should validate that $menu_order is an array before processing. While WordPress typically passes an array to the menu_order filter, defensive programming suggests adding a type check to prevent potential PHP errors if the input is unexpected:
function custom_menu_order( $menu_order ) {
if ( ! is_array( $menu_order ) ) {
return $menu_order;
}
// ... rest of the function
}| function custom_menu_order( $menu_order ) { | |
| function custom_menu_order( $menu_order ) { | |
| // Defensive: Ensure $menu_order is an array before processing. | |
| if ( ! is_array( $menu_order ) ) { | |
| return $menu_order; | |
| } |
Issue - https://github.com/rtCamp/godam.io/issues/383
This pull request introduces a change to improve the positioning of the
rtgodammenu item in the WordPress admin dashboard. The main enhancement is ensuring that thertgodammenu appears immediately after theupload.php(Media) menu item, providing a more intuitive and accessible menu structure.Menu ordering improvements:
custom_menu_order) to enable and customize the admin menu order, ensuringrtgodamappears right afterupload.phpin the menu. This includes enabling custom menu ordering and implementing logic to repositionrtgodamif both menu items are present.