Version 2.16.0 followed fairly quickly after version 2.15.0 so this post will cover the highlights from both versions. 2.15.0 was all about enhancing/streamlining the meta repo and related mutator trait. Version 2.16.0 adds a long desired persistent
method to the memoize trait.
Open last used tab after saving CMB2 options page.
Now, saving option pages with tabs will bring you back where you left off after saving instead of reloading with the first tab active.
Meta repo enhancements
Fallback to default meta handling for non-registered meta fields.
Previously, to access the meta data from classes using the Mutator trait, you were required to register the field with CMB2 using the helper classes. Now, registering the fields is optional.
You may access any meta data field registered or not using the same methods, or the array access and repo will fall back to the default WordPress get_metadata
automatically.
Add support for accessing an object’s properties directly.
The various object type traits which extend the Mutator trait have now been enhanced to allow for accessing their object properties directly. For instance when using the Post_Object_Trait
you may access any of the $post
properties directly.
The only requirement for this functionality to work is, the class using the Mutator trait must have a get_object()
method which returns the object who’s properties you want to access. To honor this pattern the previous methods such as get_post()
, get_user()
, get_term()
, etc. have been deprecated in favor of using get_object()
on each.
Call defined escape_cb
or sanitize_cb
arguments when accessing meta values.
When registering CMB2 fields we may specify escape_cb
for retrieval and sanitize_cb
for saving. Typically these callbacks are only called during meta box saving or retrieval.
The meta repo now honors both these callbacks during get
or update
. If a callback is not specified the meta repo continues without sanitization or escaping, like before.
Selectively enabling CMB2 fields in the REST API.
Leaving show_in_rest
set to false on a box and setting show_in_rest
to
any value besides false on a field now automatically sets the box to true
and false for any non-specified field. This allows selectively adding fields to the REST API responses.
A new complementary show_in_rest
method has been added to the Field
class for chaining, which accepts a single parameter to specify which methods the field is available for. E.G. WP_REST_Server::ALLMETHODS
Persistent memoize method
Simple persistent value caching using the object cache is now available via the Memoize
trait using the persistent
method.
Same functionality as the Memoize::memoize
with the difference of being persistent across runs. The data is automatically added to the object cache for the specified length of time and will pull from the cache if the arguments are the same for the life of the cache.
This replaces the boilerplate; check cache, if empty then retrieve data, then add to cache, then return cache.