[wp-trac] [WordPress Trac] #5384: wp_flag api

WordPress Trac wp-trac at lists.automattic.com
Sat Nov 24 00:58:45 GMT 2007


#5384: wp_flag api
-------------------------+--------------------------------------------------
 Reporter:  tellyworth   |       Owner:  anonymous
     Type:  enhancement  |      Status:  new      
 Priority:  normal       |   Milestone:  2.5      
Component:  General      |     Version:           
 Severity:  normal       |    Keywords:           
-------------------------+--------------------------------------------------
 There's a common pattern in wp code that looks like this:

 {{{
 turn_something_on();
 if (something_is_on())
   do_something();
 turn_something_off();
 }}}

 Currently there are two messy options for storing the toggle value when it
 needs to be shared between functions: either use a global, or write some
 get/set functions that store a static variable.

 The enclosed patch provides a simple reusable api for this and similar
 patterns.  Simple example:

 {{{
 wp_flag_set('myflag');
 if ( wp_flag_get('myflag') )
   do_something();
 wp_flag_unset('myflag');
 }}}

 It's not limited to booleans, you can store and fetch a value of any type:

 {{{
 wp_flag_set('myflag', 'myvalue');
 if ( wp_flag_get('myflag') == 'myvalue' )
   do_something();
 wp_flag_unset('myflag');
 }}}

 Flag values are not persistent and not shared across concurrent requests.
 They are stored in a static array so as not to pollute the global
 namespace.

 Unit tests are in http://svn.automattic.com/wordpress-tests/wp-
 testcase/test_includes_functions.php, in the TestFlagFunctions class.

-- 
Ticket URL: <http://trac.wordpress.org/ticket/5384>
WordPress Trac <http://trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list