$bp * @global $bpml * @global $sitepress * @global boolean $bpml_profiles_field_value_suppress_filter * @return */ function bpml_profiles_bp_after_profile_edit_content_hook() { global $bp, $bpml, $sitepress, $bpml_profiles_field_value_suppress_filter; $bpml_profiles_field_value_suppress_filter = TRUE; require_once dirname(__FILE__) . '/google-translate.php'; $default_language = $sitepress->get_default_language(); $langs = $sitepress->get_active_languages(); $group = BP_XProfile_Group::get(array( 'fetch_fields' => true, 'profile_group_id' => $bp->action_variables[1], 'fetch_field_data' => true )); echo ' 

Translate fields

'; foreach ($group[0]->fields as $field) { if (!isset($bpml['profiles']['fields'][$field->id]) || empty($field->data->value)) { continue; } echo '
' . $field->name . '
'; foreach ($langs as $lang) { if ($lang['code'] == $default_language) { continue; } echo ''; echo '
'; } echo '
'; } } /** * Processes AJAX call for updating field translation. * * @global $current_user */ function bpml_profiles_ajax() { if (isset($_POST['bpml_profiles_translate_field']) && is_user_logged_in()) { global $current_user; $field_id = $_POST['bpml_profiles_translate_field']; $lang = $_POST['bpml_profiles_translate_field_lang']; bpml_profile_update_translation($current_user->ID, $field_id, $lang, $_POST['content']); echo json_encode(array('output' => 'Done')); } } /** * Updates field translation. * * @global $wpdb $wpdb * @param $user_id * @param $field_id * @param $lang * @param $content */ function bpml_profile_update_translation($user_id, $field_id, $lang, $content) { global $wpdb; $exists = $wpdb->get_var($wpdb->prepare("SELECT id FROM {$wpdb->prefix}bp_xprofile_data_bpml WHERE field_id=%d AND user_id=%d AND lang=%s", $field_id, $user_id, $lang)); if (empty($exists)) { $wpdb->insert($wpdb->prefix . 'bp_xprofile_data_bpml', array( 'field_id' => $field_id, 'user_id' => $user_id, 'lang' => $lang, 'value' => $content, ), array('%d', '%d', '%s', '%s')); } else { $wpdb->update($wpdb->prefix . 'bp_xprofile_data_bpml', array( 'value' => $_POST['content'] ), array( 'user_id' => $user_id, 'field_id' => $field_id, 'lang' => $lang), array('%s'), array('%d', '%d', '%s')); } } /** * Fetches field translation. * * @global $wpdb $wpdb * @global $bpml * @global $sitepress * @global boolean $bpml_profiles_field_value_suppress_filter * @param $field_id * @param $lang * @param $value * @return */ function bpml_profiles_get_field_translation($field_id, $lang, $value = '') { global $wpdb, $bpml, $sitepress, $bpml_profiles_field_value_suppress_filter; if ($sitepress->get_default_language() == $lang) { return $value; } $translation = apply_filters('bp_get_the_profile_field_edit_value', $wpdb->get_var($wpdb->prepare("SELECT value FROM {$wpdb->prefix}bp_xprofile_data_bpml WHERE field_id=%d and lang=%s", $field_id, $lang))); if (empty($translation)) { bpml_debug('Missing tranlsation for field: ' . $field_id); if ($bpml['profiles']['translation']['user-missing'] && empty($bpml_profiles_field_value_suppress_filter)) { require_once dirname(__FILE__) . '/google-translate.php'; $value = bpml_google_translate(apply_filters('bp_get_the_profile_field_edit_value', $value), $sitepress->get_default_language(), $lang); bpml_debug('Fetching Google translation for field: ' . $field_id); } return $value; } else { return $translation; } } /** * Profilae field value filter. * * @global $sitepress * @global $bpml * @global boolean $bpml_profiles_field_value_suppress_filter * @param $value * @param $type * @param $field_id * @return */ function bpml_profiles_bp_get_the_profile_field_value_filter($value, $type, $field_id) { global $sitepress, $bpml, $bpml_profiles_field_value_suppress_filter; if (!empty($bpml_profiles_field_value_suppress_filter)) { return $value; } if (!isset($bpml['profiles']['fields'][$field_id])) { return $value; } $lang = $sitepress->get_current_language(); $value = bpml_profiles_get_field_translation($field_id, $lang, $value); return $value; } /** * Notices user about changed fields. * * @param $field */ function bpml_xprofile_data_before_save_hook($field) { bpml_store_frontend_notice('profile-field-updated', 'Check if fields need translation updated.'); } /** * Translates field names. * * @global $sitepress * @global $field * @staticvar array $cache * @param $name * @return array */ function bpml_bp_get_the_profile_field_name_filter($name) { global $sitepress, $field; if ($sitepress->get_default_language() == ICL_LANGUAGE_CODE) { return $name; } static $cache = NULL; if (is_null($cache)) { $cache = get_option('bpml_profile_fileds_names', array()); } if (isset($cache[$field->id][ICL_LANGUAGE_CODE])) { return $cache[$field->id][ICL_LANGUAGE_CODE]; } require_once dirname(__FILE__) . '/google-translate.php'; $name = bpml_google_translate($name, $sitepress->get_default_language(), ICL_LANGUAGE_CODE); $cache[$field->id][ICL_LANGUAGE_CODE] = $name; update_option('bpml_profile_fileds_names', $cache); return $name; }