• Resolved Surbma

    (@surbma)


    Hi,

    the DISALLOW_FILE_MODS is set to true for my install for other security reasons. But I do want to allow administrators to edit and save translations with Loco Translate. How can I enable it for them?

    I have found this filter, but don’t know, how to use it: loco_file_mod_allowed_context

    Can you please help me in this with an example code?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Tim W

    (@timwhitlock)

    Hi. Yes, I should document that hook…

    It’s actually the WordPress file_mod_allowed filter that ultimately controls whether DISALLOW_FILE_MODS is observed.

    This plugin’s loco_file_mod_allowed_context filter changes the context that gets passed to WordPress’s wp_is_file_mod_allowed function. By default the value is "download_language_pack" but depending on your needs you might want to change that.

    So, putting them both together you can achieve your goal with something like this:

    /* define a context value to allow translation file mods */
    add_filter('loco_file_mod_allowed_context', function(){
    return 'my_file_mod_context';
    } );

    /* let our file context override the default */
    add_filter('file_mod_allowed', function($default,$context){
    if( 'my_file_mod_context' === $context ){
    return current_user_can('loco_admin');
    }
    return $default;
    },10,2);

    Note that this will operate on ALL file modifications initiated by Loco Translate, not just MO file compilation as per your original question.

    Please note that general coding help is beyond the remit of my support, so I suggest you familiarize yourself with WordsPress filters in general, and where to place them so they run when you need them.

    Thread Starter Surbma

    (@surbma)

    Thank you for your reply and for the code example!

    Meanwhile I also have added my own code:

    // Modify context to 'pwp_control_allow_loco_translation', if translation location is set to Custom
    add_filter( 'loco_file_mod_allowed_context', function( $context, $file ) {
    return strpos( $file->dirname(), 'languages/loco' ) !== false ? 'pwp_control_allow_loco_translation' : $context;
    }, 10, 2 );

    // Always allow file modification, if context is 'pwp_control_allow_loco_translation'
    add_filter( 'file_mod_allowed', function( $allow, $context ) {
    return 'pwp_control_allow_loco_translation' === $context ? true : $allow;
    }, 10, 2 );

    Maybe this code could help others too.

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.