/**
* Ask for specific translations with arguments.
*
*
* @see prepare_translation_arguments()
* @param array $args {
*
* Optional. If left out, some magic happens.
*
* @type int $site_id Base site
* @type int $content_id post or term_taxonomy ID, *not* term ID
* @type string $type @see Mlp_Language_Api::get_request_type()
* @type bool $strict When TRUE (default) only matching exact
* translations will be included
* @type string $search_term If you want to translate a search
* @type string $post_type For post type archives
* @type bool $include_base Include the base site in returned list
*
* }
* @return array Array of Mlp_Translation instances, site IDs are the keys
*/
public function get_translations(array $args = array())
{
/** @type WP_Rewrite $wp_rewrite */
global $wp_rewrite;
$arguments = $this->prepare_translation_arguments($args);
$key = md5(serialize($arguments));
$content_relations = array();
$cached = wp_cache_get($key, 'mlp');
if (is_array($cached)) {
return $cached;
}
$sites = $this->get_related_sites($arguments['site_id'], $arguments['include_base']);
if (empty($sites)) {
return array();
}
if (!empty($arguments['content_id'])) {
// array with site_ids as keys, content_ids as values
$content_relations = $this->get_related_content_ids($arguments['site_id'], $arguments['content_id'], $arguments['type']);
if (empty($content_relations) && $arguments['strict']) {
return array();
}
}
$translations = array();
$languages = $this->get_all_language_data();
foreach ($sites as $site_id) {
if (!isset($languages[$site_id])) {
continue;
}
$translations[$site_id] = array('source_site_id' => $arguments['site_id'], 'target_site_id' => $site_id, 'type' => $arguments['type'], 'target_content_id' => 0, 'target_title' => '');
}
reset($translations);
foreach ($translations as $site_id => &$arr) {
$valid = TRUE;
if (!empty($content_relations[$site_id])) {
$content_id = $content_relations[$site_id];
$arr['target_content_id'] = $content_id;
if ('term' === $arguments['type']) {
$term_translation = new Mlp_Term_Translation($this->wpdb, $wp_rewrite);
$translation = $term_translation->get_translation($content_id, $site_id);
if (!$translation) {
$valid = FALSE;
} else {
$arr = array_merge($arr, $translation);
}
} elseif ('post' === $arguments['type']) {
switch_to_blog($site_id);
$translation = $this->get_post_translation($content_relations[$site_id], $arguments['strict']);
if (!$translation) {
$valid = FALSE;
} else {
$arr = array_merge($arr, $translation);
}
restore_current_blog();
}
} else {
switch_to_blog($site_id);
if ('search' === $arguments['type']) {
$url = get_search_link($arguments['search_term']);
$arr['target_url'] = new Mlp_Url($url);
} elseif ('post_type_archive' === $arguments['type'] && !empty($arguments['post_type'])) {
$translation = $this->get_post_type_archive_translation($arguments['post_type']);
$arr = array_merge($arr, $translation);
}
// Nothing found, use fallback if allowed
if (empty($arr['target_url']) && !$arguments['strict'] || 'front_page' === $arguments['type']) {
$arr['target_url'] = get_site_url($site_id, '/');
}
if (empty($arr['target_url'])) {
$valid = FALSE;
}
restore_current_blog();
}
if (!$valid) {
unset($translations[$site_id]);
continue;
}
$data = $languages[$site_id];
if (!isset($data['http_name'])) {
if (isset($data['lang'])) {
$data['http_name'] = $data['lang'];
} else {
$data['http_name'] = '';
}
}
if ('' !== $data['http_name']) {
$arr['icon'] = $this->get_flag_by_language($data['http_name'], $site_id);
}
$arr = new Mlp_Translation($arr, new Mlp_Language($data));
}
/**
* Filter list of translations before they are used.
*
* @param array $translations Prepared translations
* @param array $arguments Prepared arguments
*/
$translations = apply_filters('mlp_translations', $translations, $arguments);
wp_cache_set($key, $translations, 'mlp');
return $translations;
}