Make WordPress Core

Opened 13 years ago

Last modified 13 years ago

#18329 closed defect (bug)

update_gallery_tab in media.php has unexpected behavior — at Initial Version

Reported by: sastrugisnow's profile sastrugisnow Owned by:
Milestone: Priority: normal
Severity: minor Version: 3.2.1
Component: Media Keywords: has-patch
Focuses: Cc:

Description

wp-admin/includes/media.php has a default filter called update_gallery_tabs. The function unconditionally adds the $tabsgallery? back into $tabs if the post has image attachments. So if a user added a filter to remove the 'gallery' tab it will be added back if the post has images. You can get around this by making sure your filter runs after the default filter.

I think a saner behavior for any tab filter that does not expressly add a tab would be to check if the tab the filter is operation on is still present in $tabs.

I first wrapped the offending '$tabsgallery? = sprintf(('Gallery (%s)'),' statement in a if statement but I think an initial 'if tab is not set return' makes for more readable code.

below is the code with the 'if tab is not set return' added:

function update_gallery_tab($tabs) {

global $wpdb;

if ( !isset($_REQUESTpost_id?) ) {

unset($tabsgallery?);
return $tabs;

}


if ( !isset($tabsgallery?) ) {

return $tabs;

};

$post_id = intval($_REQUESTpost_id?);

if ( $post_id )

$attachments = intval( $wpdb->get_var( $wpdb->prepare( "SELECT count(*) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status != 'trash' AND post_parent = %d", $post_id ) ) );

if ( empty($attachments) ) {

unset($tabsgallery?);
return $tabs;

}

$tabsgallery? = sprintf(('Gallery (%s)'), "<span id='attachments-count'>$attachments</span>");


return $tabs;

}
add_filter('media_upload_tabs', 'update_gallery_tab')

Change History (1)

Note: See TracTickets for help on using tickets.