Releases: lipemat/wordpress-libs
CMB2 2.6.0 Support
Important! If you are going to use CMB2 2.6.0+ you must update to this version or newer or the group layouts will break.
Misc Improvments
1. Introduce a once
method to the Memoize
trait for methods that require constant memoized results.
2. Improve the field attributes for text_url
fields.
3. Support the full range of labels when using Taxonomy_Extended
Memoize Trait
Brand new way to memorize method returns for methods which have rotating arguments.
- Use the trait in your class.
- Call
$this->memoize
- Enjoy the free caching.
$test = new class {
use \Lipe\Lib\Traits\Memoize;
public function heavy( $text ) {
return $this->memoize( function ( $text ) {
echo 'called' . "\n";
return $text;
}, __METHOD__, $text );
}
};
$test->heavy( 'as can be' . "\n" ); //called
$test->heavy( 'as can be x2' . "\n" ); //called
$test->heavy( 'aas can be X3' . "\n" ); // called
$test->heavy( 'as can be x2' . "\n" ); //Not called
ArrayAcesss Mutators For Object Types
Introducing delete_value
and update_value
from the Meta\Repo
.
The full range of getting, setting, and deleting is now available from the Meta\Repo
. class instead of previously only having a get_value
method. Each method automatically translates the data into the correct format before saving as well as deleting all appropriate date when deleting.
Object traits now have delete_meta
and update_meta
methods available.
These method call the appropriate methods from the Meta\Repo
to translate the data automatically.
$o = new Post_Object_Trait::factory( 1 );
$o->update_meta( 'test', 'ok' );
($o->get_meta('test') === 'ok' ) //true
$o->delete_meta('test');
($o->get_meta('test') === 'ok' ) //false
ArrayAccess
Available for Object traits.
An optional feature has been added to all Object traits with support enabling ArrayAccess
modifiers. You may opt into this functionality by implementing the \ArrayAccess
interface on your classes which use the Object traits.
class Post implements \ArrayAccess {
use Post_Object_Trait;
}
$o = Post::factory(1);
$o['test'] = 'ok'; //updates the post meta
echo $o['test']; //echos 'ok
unset($o['test']); //deletes the post meta
** Note: The ArrayAccess
modifiers do use the same methods as the object trait to interact with Meta\Repo
so all fields still need to be registered via the CMB2\Box
. **
Fix Saving Of Meta Box Schema
When using the Schema/Meta_Box
abstract the values were not saving correctly.
This fixes that issue.
Network Options And Singular Term Fields
This version enhances the handling of options and terms.
New network
method on the CMB2\Options_Page
class.
Calls this method to make the settings appear as network settings. Works the same as setting the admin_menu_hook
property to network_admin_menu
.
$box = new Options_Page( 'options-page-id', __( 'Theme Settings', 'lipe' ) );
$box->network();
Now objects using the Settings_Trait
with automatically pull from the stored networks settings when calling get_option
or update_option
.
class Class_Using_Settings_Trait {
use Settings_Trait;
public const NAME = 'options-page-id';
}
$settings = new Class_Using_Settings_Trait();
$settings->get_option();
Taxonomy field improvements
Now returned values will be consistent whether the taxonomy field is within options or a meta box.
- All returned values will the full term objects.
- Singular term fields will return a single term or false.
Misc Enhancements
This release included a few improvements and enhancements
- Improved handling of
flush_rewrite_rules
when registering post_types. - Bring back the
Util\Url
class. - Support setting Gutenberg templates when registering post_types.
Misc Enhancements
A few improvements
- Notify with an error if trying to make a field repeatable which does not support repeating.
- Phpdoc improvements on
Cron\Cron_Abstract
. - Modernize the
Api\Api
class. - Support
rest_value_cb
inCMB2\Field
. - Add methods to
CMB2\Field
forsanitization_cb
andescape_cb
.
Array Utils
New Class Utils\Arrays
Beginnings if a central place to store specialize array handling. Starting with the first method array_merge_recursive
which works like the native array_merge_recursive
but it preserves data types. Think of it like wp_parse_args_recursive
.
/**
* Works the same as `array_merge_recursive` except instead of turning
* duplicate array keys into arrays, this will favor the $args over
* the $defaults and clobber identical $default keys.
*
* @param array $args
* @param array $defaults
*
* @since 2.1.0
*
* @return array
*/
public function array_merge_recursive( array $args, array $defaults ) : array {
Improvements to Field_Type::textarea_code()
- Pass
language
as a parameter. - Pass
code_editor_arguments
as a parameter.
/**
* Code textarea.
*
* The defaults are most likely what you want to use, but just in case
* there are arguments for specialize fine tuning
*
* @link https://github.com/CMB2/CMB2/wiki/Field-Types#textarea_code
* @link https://www.ibenic.com/wordpress-code-editor#file-code-editor-js
*
* @param bool $disable_codemirror - disable code mirror handling in favor or a basic textbox
* @param string $language - [clike, css, diff, htmlmixed, http, javascript, jsx, markdown, gfm,
* nginx, php, sass, shell, sql, xml, yaml]
* @param array $code_editor_arguments - The arguments are then passed to `wp.codeEditor.initialize` method.
*
* @return Field
*/
public function textarea_code( bool $disable_codemirror = false, ?string $language = null, array $code_editor_arguments = [] ) : Field {
Version 2!
CAUTION! BREAKING CHANGES
A lot of structure has been re-done with the release:
- Some classes have been removed.
- Some classes have been moved.
- Some methods have changed.
- Some scopes have changed.
All of the basics still exist from version 1, it just may take a different path to use the functionality.
If you are going to switch an existing project to version 2, make sure you give yourself plenty of time to update and run your unit tests.
The most exciting change in this version is the new Meta\Repo
I was never quite satisfied with the old structure. It had too many layers and do many ways to extend the simple task of retrieving meta for an object. Not to mention, you had to track your own meta types and use custom methods to massage CMB2 data.
not any more
The new Meta\Repo
now automatically tracks the fields that are registered via the CMB2\Field
class. The type of fields is also tracked and the proper data type is automatically returned!
$value = Repo::instance()->get_value( $post_id, $key );