[wp-trac] [WordPress Trac] #38253: Site icon functions using `ms_is_switched()` incorrectly

WordPress Trac noreply at wordpress.org
Fri Oct 7 14:03:04 UTC 2016


#38253: Site icon functions using `ms_is_switched()` incorrectly
--------------------------------+-----------------------------
 Reporter:  jdgrimes            |      Owner:
     Type:  defect (bug)        |     Status:  new
 Priority:  normal              |  Milestone:  Awaiting Review
Component:  Networks and Sites  |    Version:  4.3
 Severity:  normal              |   Keywords:
  Focuses:  multisite           |
--------------------------------+-----------------------------
 `get_custom_logo()`, `get_site_icon_url()`, and `has_custom_log()` all use
 `ms_is_switched()` in the same way:

 {{{#!php
         // At the top of the function:
         if ( is_multisite() && (int) $blog_id !== get_current_blog_id() )
 {
                 switch_to_blog( $blog_id );
         }

         // ...

         // At the bottom of the function:
         if ( is_multisite() && ms_is_switched() ) {
                 restore_current_blog();
         }
 }}}

 However, what if our code looks something like this:

 {{{#!php
 switch_to_blog( $blog_id );

 // Do some stuff...

 // Grab the icon for this blog.
 $icon_url = get_site_icon_url( 512, 'full', $blog_id );

 // Let's do some more stuff...but wait!
 // Uh-oh, now we aren't switched anymore!
 if ( $blog_id !== get_current_blog_id() ) {
      echo 'Oops.'
 }
 }}}

 You see, if we are already switched to the site when one of these
 functions is called, `ms_is_switched()` will already be true. Thus, the
 function will call `restore_current_blog()` internally, even though it
 didn't call `switch_to_blog()` internally. So calling these functions
 unexpectedly pops one site off from the switched stack.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/38253>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list