Support

Account

Home Forums ACF PRO Trying to return a user dependent language

Helping

Trying to return a user dependent language

    • Beee

    • June 11, 2019 at 4:11 am

    I have created an email function which sends custom emails to users.
    This function retrieves some acf fields from the current language, but I tried to do it in a batch instead of getting the email templates just once instead of again for each user.

    I wanted to do this by language code. The steps are

    1. query all active languages
    2. foreach through these languages
    3. get all users which are about to expire and have set language (in loop item) as preferred
    4. change current language to language (in loop item)
    5. get email template for this language (in loop item)
    6. loop through users and send email
    7. change back to original language after (languages) loop
    $eot_benchmark = date( 'U', strtotime( '+8 days', current_time( 'timestamp' ) ) );
    $users         = [];
    
    // get active countries
    $all_languages    = apply_filters( 'wpml_active_languages', null, [] );
    $hidden_languages = apply_filters( 'wpml_setting', [], 'hidden_languages' );
    foreach ( $all_languages as $key => $values ) {
        if ( ! array_key_exists( $key, $hidden_languages ) ) {
    
            // get all users which have set $key as preferred language
            $user_args = array(
                'role__in'     => array( 'independent', 'company' ),
                'role__not_in' => array(),
                'meta_query'   => array(
                    array(
                        'key'     => 'sd8_vip_eot_date',
                        'value'   => $eot_benchmark,
                        'compare' => '<',
                    ),
                    array(
                        'key'     => 'sd8_vip_eot_date',
                        'value'   => date( 'U', current_time( 'timestamp' ) ),
                        'compare' => '>',
                    ),
                    array(
                        'key'     => 'sd8_preferred_language',
                        'value'   => $key,
                        'compare' => '=',
                    ),
                ),
                'number'       => -1,
            );
            $users     = get_users( $user_args );
    
            
            if ( class_exists( 'SitePress' ) ) {
                // if this language has any users who set $key as preferred language
                if ( count( $users ) > 0 ) {
                    add_filter( 'acf/settings/current_language', function() {
                        return $key;
                    } );
                }
            }
            
            // do email stuff
    
            if ( class_exists( 'SitePress' ) ) {
                add_filter( 'acf/settings/current_language', function () {
                    return ICL_LANGUAGE_CODE;
                } );
            }
        }       
    }

    4 is the one I am struggling with. I can’t get $key to return the correct language (code). I know this isn’t the proper way to do it, but it illustrates what I’d like to achieve.

    • Beee

    • June 11, 2019 at 5:13 am

    I have a way to do it, but it isn’t the most ideal one, because it’s not DRY.

    if ( 'nl' == $key ) {
        add_filter( 'acf/settings/current_language', function() {
            return 'nl';
        } );
    } elseif ( 'en' == $key ) {
        add_filter( 'acf/settings/current_language', function() {
            return 'en';
        } );
    } else {
        add_filter( 'acf/settings/current_language', function () {
            global $sitepress;
            return $sitepress->get_default_language();
        } );
    }
    
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.