• Resolved crossi72

    (@crossi72)


    I use CMB2 to generate a set of custom metaboxes but sometimes (I can’t reproduce the condition that causes issue) those data are deleted from database.

    When this occurs the data are deleted in every post, not only in the post being edited, sometimes the issue occurs even without any user logged.

    I post my code:

    <?php

    //support function for CMB2 metaboxes
    function AddFieldToMetabox($box, $prefix, $label, $name, $type, $repeatable = false, $previewSize = ”, $sanitize_callback = ”){
    $args = array(
    ‘name’ => __( $label, ‘cmb2’ ),
    ‘id’ => ‘_esl_’ . $prefix . $name,
    ‘type’ => $type,
    ‘repeatable’ => $repeatable,
    );

    if ( $previewSize != ”){
    $args[‘preview_size’] = $previewSize;
    }

    if ( $sanitize_callback != ”){
    $args[‘sanitization_cb’] = $sanitize_callback;
    } else {
    $args[‘sanitization_cb’] = ‘sanitize_text_field’;
    }

    $box->add_field( $args );
    }

    add_filter( ‘cmb2_sanitize_gmap’, ‘cmb2_sanitize_map_callback’, 10, 2 );
    add_action( ‘cmb2_render_gmap’, ‘sm_cmb_render_gmap’, 10, 5 );
    add_filter( ‘cmb2_sanitize_smartslider’, ‘cmb2_sanitize_smartslider_callback’, 10, 2 );
    add_action( ‘cmb2_render_smartslider’, ‘sm_cmb_render_smartslider’, 10, 5 );

    //removes iframe tag and google classes from Gmap
    function cmb2_sanitize_map_callback( $override_value, $value ) {
    // check if is gmaps iframe
    if ( substr ( $value, 0 , 13 ) == ‘<iframe src=”‘) {
    // remove iframe code
    $value = substr ( $value, 13 , strlen($value) );
    $value = substr ( $value, 0, strpos( $value, ‘”‘));
    }
    return $value;
    }

    function sm_cmb_render_gmap( $field, $escaped_value, $object_id, $object_type, $field_type_object ) {
    echo $field_type_object->input( array( ‘class’ => ‘regular-text’, ‘type’ => ‘text’ ) );
    }

    // Metabox definition functions association to WordPress hook
    add_action( ‘cmb2_admin_init’, ‘categorie_meta_box_taxonomy’ );
    add_action( ‘cmb2_admin_init’, ‘add_metabox_contatti’ );
    add_action( ‘cmb2_admin_init’, ‘add_metabox_galleria’ );
    add_action( ‘cmb2_admin_init’, ‘add_metabox_mappa’ );
    add_action( ‘cmb2_admin_init’, ‘add_metabox_slideshow’ );
    add_action( ‘cmb2_admin_init’, ‘add_metabox_offerte’ );
    add_action( ‘cmb2_admin_init’, ‘add_metabox_attrazione_home’ );

    function categorie_meta_box_taxonomy() {
    $prefix = ‘_categoria’;

    $cmb_term = new_cmb2_box( array(
    ‘id’ => $prefix . ‘edit’,
    ‘title’ => __( ‘Immagine Categoria’, ‘cmb2’ ),
    ‘object_types’ => array( ‘term’ ),
    ‘taxonomies’ => array( ‘categoria’ )
    ) );

    AddFieldToMetabox($cmb_term, $prefix, ‘Immagine categoria’, ‘immagine’, ‘file’);
    }

    function add_metabox_contatti() {
    $prefix = ”;

    $cmb = new_cmb2_box(
    array(
    ‘id’ => $prefix . ‘contatti’,
    ‘title’ => __( ‘Contatti’, ‘cmb2’ ),
    ‘object_types’ => array(‘hotel’, ‘residence’),
    ‘show_names’ => true
    )
    );

    $group_field_id = $cmb->add_field( array(
    ‘id’ => $prefix . ‘_field_indirizzo’,
    ‘type’ => ‘group’,
    ‘description’ => __( ‘Indirizzo’, ‘cmb2’ ),
    ‘repeatable’ => false,
    ‘options’ => array( ‘group_title’ => __( ‘Indirizzo’, ‘cmb2’ ) ),
    ) );

    // Id’s for group’s fields only need to be unique for the group. Prefix is not needed.
    $cmb->add_group_field( $group_field_id, array(
    ‘name’ => ‘Via’,
    ‘id’ => ‘via’,
    ‘type’ => ‘text’
    ) );

    $cmb->add_group_field( $group_field_id, array(
    ‘name’ => ‘CAP’,
    ‘id’ => ‘cap’,
    ‘type’ => ‘text’
    ) );

    $cmb->add_group_field( $group_field_id, array(
    ‘name’ => ‘Citta’,
    ‘id’ => ‘citta’,
    ‘type’ => ‘text’
    ) );

    $cmb->add_group_field( $group_field_id, array(
    ‘name’ => ‘Prov’,
    ‘id’ => ‘provincia’,
    ‘type’ => ‘text_small’
    ) );

    $group_field_id = $cmb->add_field( array(
    ‘id’ => $prefix . ‘_field_consigliato’,
    ‘type’ => ‘group’,
    ‘description’ => __( ‘Dati per mostrare la struttura prima delle altre nelle ricerche’, ‘cmb2’ ),
    ‘repeatable’ => false,
    ‘options’ => array( ‘group_title’ => __( ‘Consigliato’, ‘cmb2’ ) ),
    ) );

    // Id’s for group’s fields only need to be unique for the group. Prefix is not needed.
    $cmb->add_group_field( $group_field_id, array(
    ‘name’ => ‘Consigliato’,
    ‘id’ => ‘consigliato’,
    ‘type’ => ‘checkbox’
    ) );

    $cmb->add_group_field( $group_field_id, array(
    ‘name’ => ‘Dal’,
    ‘id’ => ‘dal’,
    ‘type’ => ‘text_date’
    ) );

    $cmb->add_group_field( $group_field_id, array(
    ‘name’ => ‘Al’,
    ‘id’ => ‘al’,
    ‘type’ => ‘text_date’
    ) );

    $group_field_id = $cmb->add_field( array(
    ‘id’ => $prefix . ‘_field_contatti’,
    ‘type’ => ‘group’,
    ‘description’ => __( ‘Dati di contatto’, ‘cmb2’ ),
    ‘repeatable’ => false,
    ‘options’ => array( ‘group_title’ => __( ‘Contatti’, ‘cmb2’ ) ),
    ) );

    // Id’s for group’s fields only need to be unique for the group. Prefix is not needed.
    $cmb->add_group_field( $group_field_id, array(
    ‘name’ => ‘Telefono’,
    ‘id’ => ‘telefono’,
    ‘type’ => ‘text_small’
    ) );

    $cmb->add_group_field( $group_field_id, array(
    ‘name’ => ‘Email’,
    ‘id’ => ’email’,
    ‘type’ => ‘text_email’
    ) );

    $cmb->add_group_field( $group_field_id, array(
    ‘name’ => ‘Dal’,
    ‘id’ => ‘dal’,
    ‘type’ => ‘text_date’
    ) );

    $cmb->add_group_field( $group_field_id, array(
    ‘name’ => ‘Al’,
    ‘id’ => ‘al’,
    ‘type’ => ‘text_date’
    ) );
    }

    function add_metabox_offerte(){
    $prefix = ”;

    $cmb = new_cmb2_box(
    array(
    ‘id’ => $prefix . ‘offerte’,
    ‘title’ => __( ‘Offerte’, ‘cmb2’ ),
    ‘object_types’ => array(‘hotel’, ‘residence’),
    ‘show_names’ => true
    )
    );

    AddFieldToMetabox($cmb, $prefix, ‘Fascia di prezzo’, ‘fascia_prezzo’, ‘text’);

    $group_field_id = $cmb->add_field( array(
    ‘id’ => $prefix . ‘_field_offerta’,
    ‘type’ => ‘group’,
    ‘description’ => __( ‘Offerte’, ‘cmb2’ ),
    ‘repeatable’ => true,
    ‘options’ => array(
    ‘group_title’ => __( ‘Offerta {#}’, ‘cmb2’ ),
    ‘add_button’ => __( ‘Aggiungi’, ‘cmb2’ ),
    ‘remove_button’ => __( ‘Rimuovi’, ‘cmb2’ )
    ),
    ) );

    // Id’s for group’s fields only need to be unique for the group. Prefix is not needed.
    $cmb->add_group_field( $group_field_id, array(
    ‘name’ => ‘Titolo’,
    ‘id’ => ‘titolo’,
    ‘type’ => ‘text’
    ) );

    $cmb->add_group_field( $group_field_id, array(
    ‘name’ => ‘Descrizione’,
    ‘id’ => ‘descrizione’,
    ‘type’ => ‘text’
    ) );

    $cmb->add_group_field( $group_field_id, array(
    ‘name’ => ‘Prezzo (€)’,
    ‘id’ => ‘prezzo’,
    ‘type’ => ‘text_small’
    ) );

    $cmb->add_group_field( $group_field_id, array(
    ‘name’ => ‘Trattamento’,
    ‘id’ => ‘trattamento’,
    ‘type’ => ‘text’
    ) );

    $cmb->add_group_field( $group_field_id, array(
    ‘name’ => ‘Dal’,
    ‘id’ => ‘dal’,
    ‘type’ => ‘text_date’
    ) );

    $cmb->add_group_field( $group_field_id, array(
    ‘name’ => ‘Al’,
    ‘id’ => ‘al’,
    ‘type’ => ‘text_date’
    ) );

    $group_field_id = $cmb->add_field( array(
    ‘id’ => $prefix . ‘_field_lastminute’,
    ‘type’ => ‘group’,
    ‘description’ => __( ‘Last minute’, ‘cmb2’ ),
    ‘repeatable’ => true,
    ‘options’ => array(
    ‘group_title’ => __( ‘Last minute {#}’, ‘cmb2’ ),
    ‘add_button’ => __( ‘Aggiungi’, ‘cmb2’ ),
    ‘remove_button’ => __( ‘Rimuovi’, ‘cmb2’ )
    ),
    ) );

    // Id’s for group’s fields only need to be unique for the group. Prefix is not needed.
    $cmb->add_group_field( $group_field_id, array(
    ‘name’ => ‘Titolo’,
    ‘id’ => ‘titolo’,
    ‘type’ => ‘text’
    ) );

    $cmb->add_group_field( $group_field_id, array(
    ‘name’ => ‘Descrizione’,
    ‘id’ => ‘descrizione’,
    ‘type’ => ‘text’
    ) );

    $cmb->add_group_field( $group_field_id, array(
    ‘name’ => ‘Prezzo (€)’,
    ‘id’ => ‘prezzo’,
    ‘type’ => ‘text_small’
    ) );

    $cmb->add_group_field( $group_field_id, array(
    ‘name’ => ‘Trattamento’,
    ‘id’ => ‘trattamento’,
    ‘type’ => ‘text’
    ) );

    $cmb->add_group_field( $group_field_id, array(
    ‘name’ => ‘Dal’,
    ‘id’ => ‘dal’,
    ‘type’ => ‘text_date’
    ) );

    $cmb->add_group_field( $group_field_id, array(
    ‘name’ => ‘Al’,
    ‘id’ => ‘al’,
    ‘type’ => ‘text_date’
    ) );
    }

    function add_metabox_galleria() {
    $prefix = ”;

    $cmb = new_cmb2_box(
    array(
    ‘id’ => $prefix . ‘galleria’,
    ‘title’ => __( ‘Galleria’, ‘cmb2’ ),
    ‘object_types’ => array(‘hotel’, ‘residence’),
    ‘show_names’ => true
    )
    );

    AddFieldToMetabox($cmb, $prefix, ‘Galleria’, ‘galleria’, ‘file_list’, false, array( 150, 150 ));
    }

    function add_metabox_slideshow(){
    global $wpdb;

    $prefix = ‘_esl_’;

    $cmb = new_cmb2_box(
    array(
    ‘id’ => $prefix . ‘slideshow’,
    ‘title’ => __( ‘Slideshow’, ‘cmb2’ ),
    ‘object_types’ => array(‘hotel’, ‘residence’),
    ‘show_names’ => true
    )
    );

    //load slider from Smart Slider 3 table
    $sliders = $wpdb->get_results( ‘SELECT * FROM hot_nextend2_smartslider3_sliders ORDER BY title’, ARRAY_N );

    $options = array();

    if ( count( $sliders) > 0){
    //slider found
    for ( $i = 0; $i < count($sliders); $i++ ){
    $options[$sliders[$i][0]] = $sliders[$i][1];
    }
    }

    //output Select
    $cmb->add_field( array(
    ‘name’ => ‘slideshow’,
    ‘desc’ => ‘Seleziona uno slider’,
    ‘id’ => ‘_slideshow’,
    ‘type’ => ‘select’,
    ‘show_option_none’ => true,
    ‘default’ => ”,
    ‘options’ => $options,
    ) );

    }

    function add_metabox_mappa(){
    $prefix = ”;

    $cmb = new_cmb2_box(
    array(
    ‘id’ => $prefix . ‘mappa’,
    ‘title’ => __( ‘Mappa’, ‘cmb2’ ),
    ‘object_types’ => array(‘hotel’, ‘residence’),
    ‘show_names’ => true
    )
    );

    AddFieldToMetabox($cmb, $prefix, ‘Mappa’, ‘mappa’, ‘gmap’, ”);
    }

    function add_metabox_attrazione_home(){
    $prefix = ”;

    $cmb = new_cmb2_box(
    array(
    ‘id’ => $prefix . ‘in_home’,
    ‘title’ => __( ‘In home’, ‘cmb2’ ),
    ‘object_types’ => array(‘attrazioni’, ‘eventi’),
    ‘show_names’ => true
    )
    );

    AddFieldToMetabox($cmb, $prefix, ‘In home’, ‘in_home’, ‘checkbox’);
    }

    ?>

Viewing 15 replies - 1 through 15 (of 23 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    There is definitely a lot going on there, which also makes it harder to mentally work out. I know I see a number of field groups which may be contributing to the issues. I also see some custom functions like AddFieldToMetabox which I’m not currently sure of the need for.

    There is also a fair amount of dependency on your own website via the post types and taxonomies, so it’s not quite ready for me to use locally and try to recreate. Lastly, it seems that it’s not a permanent thing, only occurring part of the time.

    Perhaps an idea of what the data stored looks like when it’s working would help, compared to what it looks like again when lost.

    Thread Starter crossi72

    (@crossi72)

    Hi Michael,
    I have lot of code because I have lot of metadata ??

    I use metaboxes for:
    – address
    – Google map (with a custom sanitization to remove html markup)
    – hotel special prices
    – custom image galley
    – slideshow selection

    I wrote function AddFieldToMetabox to reduce the code needed for add a control to a metabox, I usually write lot of support functions to divide my code so I used this kind of approach also with CMB2 but I can remove my function to make some test.

    This is an example of metadata stored in DB (field _esl_indirizzo):
    a:1:{i:0;a:4:{s:3:”via”;s:28:”Viale Alessandro Manzoni, 19″;s:3:”cap”;s:5:”47900″;s:5:”citta”;s:6:”Rimini”;s:9:”provincia”;s:2:”RN”;}}

    Every custom field name starts with _esl_ to make easier to find.

    When the problem occurs every metadata with the name that starts with “_esl_” disappears and I have to restore a backup.

    If you want I can give you an access to the site or a full backup.

    thanks for your support
    Best regards
    Claudio

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    If you could link me to a backup via michael @ webdevstudios.com, that’d help make sure I have content and config. Thanks.

    Offhand, it’s extremely weird that it would be going through and deleting everything, and I can’t think of anything at the moment that would cause that to happen.

    Thread Starter crossi72

    (@crossi72)

    Done, I shared the file directly from cloud backup because sending you some gigabyte of email attachment is not a good idea ^^

    Thanks
    C.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Thanks. I’ll try to get to it soon, but I can’t make promises on speed. Feel free to nudge me over the next couple days if you don’t hear back much.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Just a quick initial followup, I only downloaded the themes/plugins/db, I left the uploads zips alone. Going for minimal needs to recreate ??

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Update: I have set up a local install that imported the database, and added the plugins/themes.

    At the moment, I’m wondering if you noticed any consistent patterns that may or may not have triggered content deletion. For example, when you update a post of post type X, suddenly everything is lost. Or you do some other thing and suddenly everything is lost.

    I am wondering/curious if the reliance on so many groups/repeatable fields is possibly playing a part and causing serialized data mismatch, thus dropping the saved values. However, possibly knowing triggers will help debug.

    Thread Starter crossi72

    (@crossi72)

    Hi Michael,
    I tried for a month every pattern but I didn’t find any repeatable sequence of operation that causes the issue.

    A couple of days ago I had the same issue on another site (I have five or six sites based on CMB2) but even in that case I’ve not been able to reproduce the error.

    On the site i gave you the issue happen more ore less every day, even when no user is logged.

    Thanks for your help.

    Regards.
    C.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    I filled in some meta for one of the posts, and will wait to see if it automagically clears itself out throughout the day.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Something I did notice is that the meta_id columns for some of the meta data set is “0”

    https://cloudup.com/cuvjQjkHX_K

    Curious if that’s happening for you as well.

    Thread Starter crossi72

    (@crossi72)

    Yes, in my DB there are a lot of record with meta_id = 0, it is very strange.

    I’ll try to fix setting a progressive integer to see if something happens.

    Have you changed “_custom_field_mappa” and “_custom_field_slideshow”? Those field have old meta_key, I changed the names leaving old records to see if they disappears (never happened, only meta_key managed by code are deleted) but the data I see in your screenshot are not sanitized.

    C.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Nope, I left it all as is from the provided backup. For the post/fields, I just typed in random stuff.

    That meta ID part has me the most curious, because it should be getting a unique value.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    At least thus far, the post meta has persisted and remained in my local dev site that I set up for this.

    Thread Starter crossi72

    (@crossi72)

    I think that those Meta_ID = 0 are due to a query I used to remove some duplicates:

    set session old_alter_table=1

    ALTER IGNORE TABLE hot_postmeta

    ADD UNIQUE INDEX remove_duplicate (meta_key(50) ASC, meta_value(250) ASC, post_id ASC);

    C.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    can definitely make sense, but would it be doing that for new meta that I added after you ran such queries?

Viewing 15 replies - 1 through 15 (of 23 total)
  • The topic ‘metadata generated from CMB2 disappears from DB’ is closed to new replies.