Saltar navegación

[Resuelto] Allow multiple instances of this field – not showing on Pages to update

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Zona horaria del colaborador: Asia/Kolkata (GMT+05:30)

Este tema contiene 15 respuestas, tiene 1 mensaje.

Última actualización por Minesh 2 weeks, 3 days ago.

Asistido por: Minesh.

Autor
Mensajes
#2866869

Minesh
Colaborador

Idiomas: Inglés (English )

Zona horaria: Asia/Kolkata (GMT+05:30)

After having deep look at the code, it looks like due to complex structure and number of conditoinal fields you have in admin, it seems there is some shortcircuit that cause the issue.

However - I've good news for you that I've added the following code to "Custom Code" section offered by Toolset with the code snippet namely "toolset-custom-code":
=> enlace oculto

function toolset_conditional_repair(array $fieldSlugs)
{
    if (!is_admin()) {
        return;
    }

    $screen = get_current_screen();

    if (!$screen || $screen->base !== 'post') {
        return;
    }

?>
<script>

(function($){

const repairFields=<?php echo wp_json_encode(array_values($fieldSlugs)); ?>;

function getTriggerValue(id){

    id=id.replace(/^wpcf-/,'');

    let el=$('#post_'+id);

    if(!el.length)
        el=$('[name="wpcf['+id+']"]');

    if(!el.length)
        el=$('[name="wpcf['+id+'][]"]:checked');

    if(!el.length)
        return null;

    return el.val();

}

function compare(current,operator,args){

    let expected=args[0];

    switch(operator){

        case '=':
            return String(current)==String(expected);

        case '!=':
            return String(current)!=String(expected);

        case '>':
            return Number(current)>Number(expected);

        case '<':
            return Number(current)<Number(expected);

        case '>=':
            return Number(current)>=Number(expected);

        case '<=':
            return Number(current)<=Number(expected);

        default:
            return false;

    }

}

function evaluate(cond){

    if(!cond)
        return true;

    let relation=(cond.relation||'AND').toUpperCase();

    let result=(relation==='AND');

    $.each(cond.conditions,function(i,c){

        let current=getTriggerValue(c.id);

        let ok=compare(current,c.operator,c.args);

        if(relation==='AND')
            result=result && ok;
        else
            result=result || ok;

    });

    return result;

}

function needsRepair(field){

    if(!field.length)
        return false;

    if(field.css('display')==='none')
        return true;

    if(field.closest(':hidden').length)
        return true;

    if(field.find(':disabled').length)
        return true;

    return false;

}

function repair(slug){

    let key='wpcf-'+slug;

    if(
        typeof wptCondFields==='undefined' ||
        !wptCondFields['#post'] ||
        !wptCondFields['#post'][key]
    ){
        console.warn('No Toolset condition for',slug);
        return;
    }

    let condition=wptCondFields['#post'][key];

    if(!evaluate(condition))
        return;

    let field=$('.js-wpt-field[data-wpt-id="'+key+'"]');

    if(!field.length)
        return;

    if(!needsRepair(field))
        return;

    console.log('Repairing',slug);

    field.parents().each(function(){

        $(this)
            .removeClass('hidden wpt-hidden toolset-hidden js-wpt-validation-ignore wpt-conditional-hidden')
            .css({
                display:'',
                visibility:'',
                opacity:''
            });

    });

    field
        .removeClass('hidden wpt-hidden toolset-hidden js-wpt-validation-ignore wpt-conditional-hidden')
        .css({
            display:'',
            visibility:'',
            opacity:''
        });

    field.find(':input')
        .prop('disabled',false)
        .prop('readonly',false)
        .removeAttr('disabled')
        .removeAttr('readonly');

    field.find('.js-wpt-repadd')
        .show()
        .prop('disabled',false);

    field.find('.js-wpt-repdelete')
        .show()
        .prop('disabled',false);

    field.find(':input').trigger('change').trigger('input');

}

function runRepair(){

    repairFields.forEach(repair);

}

$(window).on('load',function(){

    setTimeout(runRepair,400);
    setTimeout(runRepair,1000);
    setTimeout(runRepair,2000);

});

$(document).on('change input','select,input,textarea',function(){

    runRepair();

});

new MutationObserver(function(){

    clearTimeout(window.toolsetRepairTimer);

    window.toolsetRepairTimer=setTimeout(runRepair,200);

}).observe(document.body,{
    childList:true,
    subtree:true,
    attributes:true
});

})(jQuery);

</script>
<?php
 
}


add_action('admin_footer', function () {

    toolset_conditional_repair([
        'sidebar-panel-list'
    ]);

});

Can you please cofnrim it works as expected now.

More info:
- https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/#benefits-of-adding-custom-code-using-toolset