• Hi Chouby,

    I recently moved to php 5.4 and noticed the following:

    Warning: Invalid argument supplied for foreach() in [...]/plugins/polylang/include/auto-translate.php on line 141

    The piece of code in question is this:

    function get_terms_args($args, $taxonomies) {
    	global $polylang;
    
    	if (!empty($args['include']) && isset($polylang->taxonomies) && is_array($polylang->taxonomies) && array_intersect($taxonomies, $polylang->taxonomies)) {
    		foreach($args['include'] as $id)
    			$arr[] = ($tr = pll_get_term($id)) ? $tr : $id;
    
    		$args['include'] = $arr;
    	}
    	return $args;
    }

    I’m not a PHP expert, but could it be that PHP version before 5.4 were more relaxed in accepting arguments to foreach and would perhaps assume a string to be a one element array of string? Or even better: perhaps a comma separated list would be automagically cast to an array of string?

    Anyway – PHP documentation suggests that foreach can only be passed an array and I’m under the impression that the function argument ($args) above is from get_categories and as such a comma separated list.

    I have changed your function to read as below for now and that seems to have resolved the issue:

    function get_terms_args($args, $taxonomies) {
    	global $polylang;
    
    	if (!empty($args['include']) && isset($polylang->taxonomies) && is_array($polylang->taxonomies) && array_intersect($taxonomies, $polylang->taxonomies)) {
    		foreach(explode(",", $args['include']) as $id) // added explode to convert cs list to array
    			$arr[] = ($tr = pll_get_term($id)) ? $tr : $id;
    
    		$args['include'] = $arr;
    	}
    	return $args;
    }

    Let me know if you will be including this in a future release please.

    Thanks and regards,
    Ger Reuvekamp

  • The topic ‘[Plugin: Polylang] foreach datatype warning after php upgrade to 5.4’ is closed to new replies.