[wp-trac] [WordPress Trac] #18329: update_gallery_tab in media.php has unexpected behavior

WordPress Trac wp-trac at lists.automattic.com
Thu Aug 4 17:41:39 UTC 2011


#18329: update_gallery_tab in media.php has unexpected behavior
--------------------------+------------------------------
 Reporter:  sastrugisnow  |       Owner:
     Type:  defect (bug)  |      Status:  new
 Priority:  normal        |   Milestone:  Awaiting Review
Component:  Media         |     Version:  3.2.1
 Severity:  minor         |  Resolution:
 Keywords:  has-patch     |
--------------------------+------------------------------

Old description:

> wp-admin/includes/media.php has a default filter called
> update_gallery_tabs. The function unconditionally adds the
> $tabs['gallery'] 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 '$tabs['gallery'] = 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($_REQUEST['post_id']) ) {
>                 unset($tabs['gallery']);
>                 return $tabs;
>         }
>
>         if ( !isset($tabs['gallery']) ) {
>                 return $tabs;
>         };
>
>         $post_id = intval($_REQUEST['post_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($tabs['gallery']);
>                 return $tabs;
>         }
>
>         $tabs['gallery'] = sprintf(__('Gallery (%s)'), "<span id
> ='attachments-count'>$attachments</span>");
>
>         return $tabs;
> }
> add_filter('media_upload_tabs', 'update_gallery_tab')

New description:

 wp-admin/includes/media.php has a default filter called
 update_gallery_tabs. The function unconditionally adds
 {{{$tabs['gallery']}}} 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 {{{$tabs!['gallery'] = 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($_REQUEST['post_id']) ) {
                 unset($tabs['gallery']);
                 return $tabs;
         }

         if ( !isset($tabs['gallery']) ) {
                 return $tabs;
         };

         $post_id = intval($_REQUEST['post_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($tabs['gallery']);
                 return $tabs;
         }

         $tabs['gallery'] = sprintf(__('Gallery (%s)'), "<span id
 ='attachments-count'>$attachments</span>");

         return $tabs;
 }
 add_filter('media_upload_tabs', 'update_gallery_tab')
 }}}

--

Comment (by ocean90):

 Code blocks added.

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/18329#comment:1>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list