lucatume / cmb2-meta-split

A WordPress plugin that will take care of saving parallel meta when using the CMB2 plugin.
4 stars 1 forks source link

a more generic solution #1

Open lophas opened 5 years ago

lophas commented 5 years ago
class cmb2_fix {
    const TYPES = ['term', 'user'];

    static private $_instance;function instance() {if(!isset(self::$_instance)) self::$_instance =  new self();return self::$_instance;}

    function __construct() {

        add_filter('cmb2_override_meta_value', function($data, $object_id, $a, $instance) {
            if(!in_array($a['type'], self::TYPES) || !$a['repeat']) return $data;
            $data = get_metadata( $a['type'], $a['id'], $a['field_id'], false );
            return $data;
        }, 10, 4);

        add_filter( 'cmb2_override_meta_save', function($override, $a, Array $args, $instance ) {
            if(!in_array($a['type'], self::TYPES) || !$a['repeat']) return $override;
            $id = delete_metadata($a['type'], $a['id'], $a['field_id']);
            if(!empty($a['value'])) foreach($a['value'] as $value) $id = add_metadata( $a['type'], $a['id'], $a['field_id'], $value, false );
            return $id ? $id : false;
        }, 10, 4);

    } //__construct
} //cmb2_fix
cmb2_fix::instance();
lucatume commented 5 years ago

Hi,

could you clarify your intention here? Is this a PR? A suggestion? A problem?

lucatume commented 5 years ago

Closed by mistake.