Search code examples
wordpresswpmlcmb2

WordPress with CMB2 and WPML Media: file_list, how to get correct id in translations?


In WordPress I'm using the CMB2 plugins (framework for creating metabox and metafield) and WPML Media (WPML add-on avoids saving the same attachment for each language, creating translations of: title, alt, description and caption, to that only attachment); I'm creating a metafield like file_list that saves attachments in an array (id => url) like this:

$cmb = new_cmb2_box( array(
    'id'               => 'benny_metabox_photo',
    'title'            => esc_html__( 'My Photo Metabox', 'mydomain' ),
    'object_types'     => array( 'post' )
) );

$cmb->add_field( array(
    'name'         => esc_html__( 'My Photo', 'mydomain' ),
    'id'           => 'benny-file-list',
    'type'         => 'file_list',
    'preview_size' => array( 100, 100 ),
    'text'         => array(
        'add_upload_files_text' => esc_html__( 'Add Photos',   'mydomain' ),
        'remove_image_text'     => esc_html__( 'Remove Photo', 'mydomain' ),
        'file_text'             => esc_html__( 'Photo:',       'mydomain' ),
        'file_download_text'    => esc_html__( 'Download',     'mydomain' ),
        'remove_text'           => esc_html__( 'Remove',       'mydomain' )
    )
) );

So, when I create a post in the main language set in WPML everything works correctly. But when I create translations and insert attachments in the file_list field, the ids always refer to attachments in the main language and not to its translations, so in the frontend - when you display the site in the languages translated - the title, alt, description and caption for the images uploaded in the file_list field are in the main language. How could I do to view their translations and not the main language?


Solution

  • I have seen that CMB2 offers the possibility to create a custom sanitization function with which the values are checked before they're inserted in the database. So I set the sanitization_cb parameter in the metafield this way:

    $cmb->add_field( array(
        'name'            => esc_html__( 'My Photo', 'mydomain' ),
        'id'              => 'benny-file-list',
        'type'            => 'file_list',
        'preview_size'    => array( 100, 100 ),
        'text'            => array(
            'add_upload_files_text' => esc_html__( 'Add Photos',   'mydomain' ),
            'remove_image_text'     => esc_html__( 'Remove Photo', 'mydomain' ),
            'file_text'             => esc_html__( 'Photo:',       'mydomain' ),
            'file_download_text'    => esc_html__( 'Download',     'mydomain' ),
            'remove_text'           => esc_html__( 'Remove',       'mydomain' )
        ),
        'sanitization_cb' => 'benny_cmb_sanitize_file_list'
    ) );
    

    Then I created the callback function for sanitization, in order to check if the language of the uploaded attachment matches the language of the post; if it doesn't match, it looks for the translation id and if it has been translated it replaces it:

    function benny_cmb_sanitize_file_list( $value, $field_args, $field ) {
    
        if ( ! function_exists( 'icl_object_id' ) )
            return $value;
    
        if ( empty( $value ) )
            return $value;
    
        $main_lang = apply_filters( 'wpml_default_language', NULL );
    
        $post_id = $field->object_id;
    
        $post_lang = apply_filters( 'wpml_post_language_details', NULL, $post_id );
        $post_lang = $post_lang[ 'language_code' ];
    
        if ( $post_lang == $main_lang )
            return $value;
    
        $photo = $value;
    
        $trad_photo = array();
    
        foreach ( $photo as $id => $url ) {
    
            $photo_lang = apply_filters( 'wpml_post_language_details', NULL, $id );
            $photo_lang = $photo_lang[ 'language_code' ];
    
            $post_type = get_post_type( $id );
    
            if ( $photo_lang !== $post_lang ) {
    
                $trad_id = apply_filters( 'wpml_object_id', $id, $post_type, FALSE, $post_lang );
    
                if ( ! empty( $trad_id ) ) {
    
                    $trad_photo[ $trad_id ] = $url;
    
                }
            }
        }
    
        if ( ! empty( $trad_photo ) ) {
    
            return $trad_photo;
    
        }
    
        return $value;
    
    }
    

    Now it works great and in the front-end I visualize the image attributes (title, alt, etc.) in the correct language.

    P.S. I don't know if it's the right methodology or the most beautiful form, I'm open to every feedback :)