[wp-hackers] Best Practice for accessing _WP_Dependency->extra?
Jason Penney
jpenney at jczorkmid.net
Sat Jun 13 10:58:56 GMT 2009
Hi,
In the process of updating my "Use Google Libraries" plugin
[http://jasonpenney.net/wordpress-plugins/use-google-libraries/] to take
advantage of the changes in WordPress 2.8 I saw that I needed to persist
any properties added using $scripts->add_data() to the new
registrations. I didn't see anything that looked like a valid API for
doing this, so I ended up doing something like the following:
// save off the old script before unregistering it
$oldscript = $scripts->registered[$script->handle];
...
// re-add all the extra data from the old script to the new
foreach ($oldscript->extra as $data_name => $data) {
$scripts->add_data($script->handle,$data_name,$data);
}
This seems to have done what I want, but probably not in the best
possible way (directly accessing 'registered' and 'extra' strike me as
likely to be problematic in future).
I was looking for something like the following:
class WP_Dependencies {
// ...
/**
* Retrieves extra data
*
* Returns data only if script has already been added
*
* @param string handle Script name
* @param string data_name (optional) Name of object in which to
* retrieve data from
* @return mixed false if handle not registered, Array value if
* $data_name is provided, Array containing all extra
* data if $data_name is false
*/
function get_data( $handle, $data_name = false) {
if ( !isset($this->registered[$handle]) )
return false;
if (!$data_name)
return $this->registered[$handle]->extra;
return $this->registered[$handle]->extra[$data_name];
}
// ...
}
Any pointers or suggestions appreciated,
Jay
--
Jason C Penney (jpenney at jczorkmid.net)
http://jasonpenney.net/
More information about the wp-hackers
mailing list