$args
* @param string $id_prefix
* @param string|int $value
* @param string $caption
*
* @return string
*/
function wpml_translation_preference_input_helper( $args, $id_prefix, $value, $caption ) {
$output = '';
$input_attributes = $args['input_attributes'];
$label_attributes = $args['label_attributes'];
$id = $args['id'];
$action = $args['action'];
$input_attributes['id'] = $id_prefix . $id;
$input_attributes['value'] = $value;
$label_attributes['for'] = $input_attributes['id'];
$output .= wpml_input_field_helper( $input_attributes, ( $value === $action ), $args['disabled'] );
$output .= wpml_label_helper( $label_attributes, $caption );
return $output;
}
/**
* @todo: [WPML 3.3] refactor in 3.3
*
* @param string $id
* @param bool $custom_field
* @param string $class
* @param bool $ajax
* @param string $default_value
* @param bool $fieldset
* @param bool $suppress_error
*
* @return string
*/
function wpml_cf_translation_preferences( $id, $custom_field = false, $class = 'wpml', $ajax = false, $default_value = 'ignore', $fieldset = false, $suppress_error = false ) {
global $iclTranslationManagement;
$output = '';
if ( isset( $iclTranslationManagement ) ) {
$section = 'custom_fields';
$config_section = $iclTranslationManagement->get_translation_setting_name( $section );
$readonly_config_section = $iclTranslationManagement->get_readonly_translation_setting_name( $section );
if ( $custom_field ) {
$custom_field = @strval( $custom_field );
}
$class = @strval( $class );
if ( $fieldset ) {
$output .= '
';
}
}
return $output;
}
/**
* @todo: [WPML 3.3] refactor in 3.3
* wpml_get_copied_fields_for_post_edit
* return a list of fields that are marked for copying and the
* original post id that the fields should be copied from
* This should be used to populate any custom field controls when
* a new translation is selected and the field is marked as "copy" (sync)
*
* @param array $fields
*
* @return array
*/
function wpml_get_copied_fields_for_post_edit( $fields = array() ) {
global $sitepress, $wpdb, $sitepress_settings, $pagenow;
$copied_cf = array( 'fields' => array() );
$translations = null;
if ( defined( 'WPML_TM_VERSION' ) ) {
if ( ( $pagenow == 'post-new.php' || $pagenow == 'post.php' ) ) {
$lang_details = null;
$source_lang = null;
if ( isset( $_GET['trid'] ) ) {
$post_type = isset( $_GET['post_type'] ) ? $_GET['post_type'] : 'post';
$translations = $sitepress->get_element_translations( $_GET['trid'], 'post_' . $post_type );
$source_lang = isset( $_GET['source_lang'] ) ? $_GET['source_lang'] : $sitepress->get_default_language();
$lang_details = $sitepress->get_language_details( $source_lang );
} else {
$post_id = filter_input( INPUT_GET, 'post', FILTER_SANITIZE_NUMBER_INT );
if ( $post_id ) {
$post_type = $wpdb->get_var( $wpdb->prepare( "SELECT post_type FROM {$wpdb->posts} WHERE ID = %d", $post_id ) );
$trid = $sitepress->get_element_trid( $post_id, 'post_' . $post_type );
$original_id = $wpdb->get_var( $wpdb->prepare( "SELECT element_id FROM {$wpdb->prefix}icl_translations WHERE source_language_code IS NULL AND trid=%d", $trid ) );
if ( $original_id != $post_id ) {
// Only return information if this is not the source language post.
$translations = $sitepress->get_element_translations( $trid, 'post_' . $post_type );
$source_lang = $wpdb->get_var( $wpdb->prepare( "SELECT language_code FROM {$wpdb->prefix}icl_translations WHERE source_language_code IS NULL AND trid=%d", $trid ) );
$lang_details = $sitepress->get_language_details( $source_lang );
}
}
}
if ( $lang_details && $translations && $source_lang ) {
$original_custom = get_post_custom( $translations[ $source_lang ]->element_id );
$copied_cf['_wpml_original_post_id'] = $translations[ $source_lang ]->element_id;
$ccf_note = '
';
$copied_cf['copy_message'] = $ccf_note . sprintf( __( 'WPML will copy this field from %s when you save this post.', 'sitepress' ), $lang_details['display_name'] );
foreach ( (array) $sitepress_settings['translation-management']['custom_fields_translation'] as $key => $sync_opt ) {
/*
* Added parameter $fields so except checking if field exist in DB,
* it can be checked if set in pre-defined fields.
* Noticed when testing checkbox field that does not save
* value to DB if not checked (omitted from list of copied fields).
* https://icanlocalize.basecamphq.com/projects/2461186-wpml/todo_items/169933388/comments
*/
if ( $sync_opt == 1 && ( isset( $original_custom[ $key ] ) || in_array( $key, $fields ) ) ) {
$copied_cf['fields'][] = $key;
}
}
}
}
}
return $copied_cf;
}
/**
* Retrieve language information of a post by its ID
* The language information includes
* the post locale,
* the language text direction (True for RTL, False for LTR),
* the post language translated name and native name and
* whether the current language is different to the post language (True/False)
*
* @param mixed $empty_value
*
* @see \wpml_get_active_languages_filter
*
* @param int $post_id Optional The post id to retrieve information of (post, page, attachment, custom) Defaults to current post ID.
*
* @return array|WP_Error
* @uses \SitePress::api_hooks
*/
function wpml_get_language_information( $empty_value = null, $post_id = null ) {
global $sitepress;
if ( is_null( $post_id ) ) {
$post_id = get_the_ID();
}
if ( empty( $post_id ) ) {
return new WP_Error( 'missing_id', __( 'Missing post ID', 'sitepress' ) );
}
$post = get_post( $post_id );
if ( empty( $post ) ) {
// translators: Post id.
return new WP_Error( 'missing_post', sprintf( __( 'No such post for ID = %d', 'sitepress' ), $post_id ) );
}
$language = $sitepress->get_language_for_element( $post_id, 'post_' . $post->post_type );
$language_information = $sitepress->get_language_details( $language );
$current_language = $sitepress->get_current_language();
$info = [
'language_code' => $language,
'locale' => $sitepress->get_locale( $language ),
'text_direction' => $sitepress->is_rtl( $language ),
'display_name' => $sitepress->get_display_language_name( $language, $current_language ),
'native_name' => isset( $language_information['display_name'] ) ? $language_information['display_name'] : '',
'different_language' => $language !== $current_language,
];
return $info;
}
/** This action is documented in */
add_filter( 'wpcf_meta_box_post_type', 'wpml_wpcf_meta_box_order_defaults' );
/**
* Add metabox definition to edit post type in Types
*
* @since x.x.x
*
* @param array $boxes Meta boxes in Types.
*
* @return array Meta boxes in Types.
*/
function wpml_wpcf_meta_box_order_defaults( $boxes ) {
$boxes['wpml'] = array(
'callback' => 'wpml_custom_post_translation_options',
'title' => __( 'Translation', 'sitepress' ),
'default' => 'normal',
'priority' => 'low',
);
return $boxes;
}
/**
* @todo: [WPML 3.3] refactor in 3.3
*
* @return string
*/
function wpml_custom_post_translation_options() {
global $sitepress;
$type_id = isset( $_GET['wpcf-post-type'] ) ? $_GET['wpcf-post-type'] : '';
$out = '';
$type = get_post_type_object( $type_id );
$translated = $sitepress->is_translated_post_type( $type_id );
$link = WPML_Admin_URL::multilingual_setup( 7 );
$link2 = WPML_Admin_URL::multilingual_setup( 4 );
if ( $translated ) {
$out .= sprintf( __( '%1$s is translated via WPML. %2$sClick here to change translation options.%3$s', 'sitepress' ), '' . $type->labels->singular_name . '', '', '' );
if ( $type->rewrite['enabled'] && class_exists( 'WPML_ST_Post_Slug_Translation_Settings' ) ) {
$settings = new WPML_ST_Post_Slug_Translation_Settings( $sitepress );
if ( $settings->is_enabled() ) {
if ( ! $settings->is_translated( $type_id ) ) {
$out .= '- ' . __( 'Slugs are currently not translated.', 'sitepress' ) . '
';
} else {
$out .= '- ' . __( 'Slugs are currently translated. Click the link above to edit the translations.', 'sitepress' ) . '
';
}
} else {
$out .= '- ' . sprintf( __( 'Slug translation is currently disabled in WPML. %1$sClick here to enable.%2$s', 'sitepress' ), '', '' ) . '
';
}
}
} else {
$out .= sprintf( __( '%1$s is not translated. %2$sClick here to make this post type translatable.%3$s', 'sitepress' ), '' . $type->labels->singular_name . '', '', '' );
}
return $out;
}
/**
* @since unknown
* @deprecated 3.2 use 'wpml_add_language_selector' filter instead
*/
function icl_language_selector() {
ob_start();
do_action( 'wpml_add_language_selector' );
$output = ob_get_contents();
ob_end_clean();
return $output;
}
/**
* Display the drop down language selector
*
* @since 3.2
* Will use the language selector settings from "Language switcher as shortcode or action"
* @uses \SitePress::api_hooks
* example: do_action( 'wpml_add_language_selector' );
*/
function wpml_add_language_selector_action() {
do_action( 'wpml_add_language_selector' );
}
/**
* @since unknown
* @deprecated 3.2 use 'wpml_footer_language_selector' filter instead
*/
function icl_language_selector_footer() {
ob_start();
do_action( 'wpml_footer_language_selector' );
$output = ob_get_contents();
ob_end_clean();
return $output;
}
/**
* Display the footer language selector
*
* @since 3.2
* Will use the language selector include configuration from the WPML -> Language admin screen
* @uses \SitePress::api_hooks
* example: do_action('wpml_footer_language_selector');
*/
function wpml_footer_language_selector_action() {
do_action( 'wpml_footer_language_selector' );
}
/**
* Returns an HTML hidden input field with name="lang" and value of current language
* This is for theme authors, to make their themes compatible with WPML when using the search form.
* In order to make the search form work properly, they should use standard WordPress template tag get_search_form()
* In this case WPML will handle the the rest.
* If for some reasons the template function can't be used and form is created differently,
* authors must the following code between inside the form
* get_current_language() ) . "' />";
}
return null;
}
/**
* Echoes the value returned by \wpml_get_language_input_field
*
* @since 3.1.7.3
* @deprecated 3.2 use 'wpml_add_language_form_field' filter instead
*/
function wpml_the_language_input_field() {
echo wpml_get_language_input_field();
}
/**
* @since 3.2
* Returns an HTML hidden input field with name="lang" and as value the current language
* In order to add a search form to your theme you would normally use the standard WordPress template tag: get_search_form()
* If you are making use of the default WordPress search form, you do not need to edit anything. WPML will handle the rest.
* However, there may be times when get_search_form() can't be used.
* If you are creating a custom search form and you need to make it WPML compatible then this action hook is what you need.
* Add the action hook inside the form:
*
* @global SitePress $sitepress
* @uses \SitePress::api_hooks
*/
function wpml_add_language_form_field_action() {
echo wpml_get_language_form_field();
}
function wpml_language_form_field_shortcode() {
return wpml_get_language_form_field();
}
function wpml_get_language_form_field() {
$language_form_field = '';
global $sitepress;
if ( isset( $sitepress ) ) {
$current_language = $sitepress->get_current_language();
$language_form_field = "";
$language_form_field = apply_filters( 'wpml_language_form_input_field', $language_form_field, $current_language );
}
return $language_form_field;
}
/**
* @since unknown
* @deprecated 3.2 use 'wpml_element_translation_type' filter instead
*
* @param int $id
* @param string $type
*
* @return bool|int
*/
function wpml_get_translation_type( $id, $type = 'post' ) {
$translation_type = WPML_ELEMENT_IS_NOT_TRANSLATED;
if ( $type == 'post' ) {
$translation_type = wpml_post_has_translations( $id );
}
// TODO: [WPML 3.3] handle other element types (e.g. taxonomies, strings, etc.)
return $translation_type;
}
/**
* @since 3.2
* Accepts the ID and type of an element and returns its translation type.
* Values will be one of these:
* WPML_ELEMENT_IS_NOT_TRANSLATED = 0
* WPML_ELEMENT_IS_TRANSLATED = 1
* WPML_ELEMENT_IS_DUPLICATED = 2
* WPML_ELEMENT_IS_A_DUPLICATE = 3
*
* @param mixed $empty_value
*
* @see \wpml_get_active_languages_filter
*
* @param int $element_id The element id to retrieve the information of. Use term_id for taxonomies, post_id for posts
* @param string $element_type Can be a post type: post, page, attachment, nav_menu_item, {custom post key}
* or taxonomy: category, post_tag, nav_menu {custom taxonomy key}
*
* @return int
* @uses \SitePress::api_hooks
*/
function wpml_get_element_translation_type_filter( $empty_value, $element_id, $element_type ) {
$translation_type = WPML_ELEMENT_IS_NOT_TRANSLATED;
$element_has_translations = apply_filters( 'wpml_element_has_translations', null, $element_id, $element_type );
$element_is_master = apply_filters( 'wpml_master_post_from_duplicate', $element_id );
$element_is_duplicate = apply_filters( 'wpml_post_duplicates', $element_id );
if ( $element_has_translations ) {
$translation_type = WPML_ELEMENT_IS_TRANSLATED;
if ( $element_is_master ) {
$translation_type = WPML_ELEMENT_IS_A_DUPLICATE;
} elseif ( $element_is_duplicate ) {
$translation_type = WPML_ELEMENT_IS_DUPLICATED;
}
}
return $translation_type;
}
/**
* Accepts the ID of a post and returns its translation type.
* Values will be one of these:
* WPML_ELEMENT_IS_NOT_TRANSLATED = 0
* WPML_ELEMENT_IS_TRANSLATED = 1
* WPML_ELEMENT_IS_DUPLICATED = 2
* WPML_ELEMENT_IS_A_DUPLICATE = 3
*
* @param int $post_id The ID of the post from which to get translation information
*
* @return int
* @internal param string $post_type
* @since 3.2
* @deprecated 3.2 use 'wpml_element_translation_type' filter instead
*/
function wpml_get_post_translation_type( $post_id ) {
$translation_type = WPML_ELEMENT_IS_NOT_TRANSLATED;
$post_type = get_post_type( $post_id );
if ( $post_type && wpml_post_has_translations( $post_id, $post_type ) ) {
$translation_type = WPML_ELEMENT_IS_TRANSLATED;
if ( wpml_get_master_post_from_duplicate( $post_id ) ) {
$translation_type = WPML_ELEMENT_IS_A_DUPLICATE;
} elseif ( wpml_get_post_duplicates( $post_id ) ) {
$translation_type = WPML_ELEMENT_IS_DUPLICATED;
}
}
return $translation_type;
}
/**
* @param int $post_id
* @param string $post_type
*
* @return bool
* @since 3.2
* @deprecated 3.2 use 'wpml_element_has_translations' filter instead
*/
function wpml_post_has_translations( $post_id, $post_type = 'post' ) {
$has_translations = false;
global $sitepress;
if ( isset( $sitepress ) ) {
$trid = $sitepress->get_element_trid( $post_id, 'post_' . $post_type );
$translations = $sitepress->get_element_translations( $trid );
if ( $translations && count( $translations ) > 1 ) {
$has_translations = true;
}
}
return $has_translations;
}
/**
* Checks if an element has translations
* A translation can be a manual translation or a duplication.
*
* @since 3.2
*
* @param mixed $empty_value
*
* @see \wpml_get_active_languages_filter
*
* @param int $element_id Use term_id for taxonomies, post_id for posts
* @param string $element_type Can be a post type: post, page, attachment, nav_menu_item, {custom post key}
* or taxonomy: category, post_tag, nav_menu {custom taxonomy key}
*
* @return bool
* @uses \SitePress::api_hooks
*/
function wpml_element_has_translations_filter( $empty_value, $element_id, $element_type = 'post' ) {
$has_translations = false;
global $sitepress;
if ( isset( $sitepress ) ) {
$wpml_element_type = apply_filters( 'wpml_element_type', $element_type );
if ( strpos( $wpml_element_type, 'tax_' ) === 0 ) {
/** @var WPML_Term_Translation $wpml_term_translations*/
global $wpml_term_translations;
$element_id = $wpml_term_translations->adjust_ttid_for_term_id( $element_id );
}
$trid = $sitepress->get_element_trid( $element_id, $wpml_element_type );
$translations = apply_filters( 'wpml_get_element_translations_filter', '', $trid, $wpml_element_type );
if ( $translations && count( $translations ) > 1 ) {
$has_translations = true;
}
}
return $has_translations;
}
function wpml_get_content_translations_filter( $empty, $post_id, $content_type = 'post' ) {
global $sitepress;
$translations = array();
if ( isset( $sitepress ) ) {
$wpml_element_type = apply_filters( 'wpml_element_type', $content_type );
$trid = $sitepress->get_element_trid( $post_id, $wpml_element_type );
$translations = apply_filters( 'wpml_get_element_translations', null, $trid, $wpml_element_type );
}
return $translations;
}
/**
* @param int $post_id
*
* @return mixed
* @since 3.2
* @deprecated 3.2 use 'wpml_master_post_from_duplicate' filter instead
*/
function wpml_get_master_post_from_duplicate( $post_id ) {
return get_post_meta( $post_id, '_icl_lang_duplicate_of', true );
}
/**
* Get the original post from the duplicated post
*
* @param int $post_id The duplicated post ID
*
* @return int or empty string if there is nothing to return
* @uses \SitePress::api_hooks
*/
function wpml_get_master_post_from_duplicate_filter( $post_id ) {
return get_post_meta( $post_id, '_icl_lang_duplicate_of', true );
}
/**
* @param int $master_post_id
*
* @return mixed
* @since 3.2
* @deprecated 3.2 use 'wpml_post_duplicates' filter instead
*/
function wpml_get_post_duplicates( $master_post_id ) {
global $sitepress;
if ( isset( $sitepress ) ) {
return $sitepress->get_duplicates( $master_post_id );
}
return array();
}
/**
* Get the duplicated post ids
* Will return an associative array with language codes as indexes and post_ids as values
*
* @param int $master_post_id The original post id from which duplicates exist
*
* @return array
* @uses \SitePress::api_hooks
*/
function wpml_get_post_duplicates_filter( $master_post_id ) {
global $sitepress;
if ( isset( $sitepress ) ) {
return $sitepress->get_duplicates( $master_post_id );
}
return array();
}
/**
* Filters a WordPress element by adding the WPML prefix 'post_', 'tax_', or nothing for 'comment' as used in icl_translations db table
*
* @since 3.2
*
* @param string $element_type Accepts comment, post, page, attachment, nav_menu_item, {custom post key},
* nav_menu, category, post_tag, {custom taxonomy key}
*
* @return string
* @uses \SitePress::api_hooks
*/
function wpml_element_type_filter( $element_type ) {
global $wp_post_types, $wp_taxonomies;
$post_types = array_keys( (array) $wp_post_types );
$taxonomies = array_keys( (array) $wp_taxonomies );
if ( in_array( $element_type, $taxonomies ) ) {
$wpml_element_type = 'tax_' . $element_type;
} elseif ( in_array( $element_type, $post_types ) ) {
$wpml_element_type = 'post_' . $element_type;
} else {
$wpml_element_type = $element_type;
}
return $wpml_element_type;
}
/**
* Retrieves language information for a translatable element
* Checks icl_translations db table and returns an object with the element's
* trid, source language code and language code
*
* @since 3.2.2
*
* @param mixed $element_object A WordPress object.
* @param array $args {
* Required An array of arguments to be used
*
* @type int $element_id Use term_taxonomy_id for taxonomies, post_id for posts
* @type string $element_type Can be a post type: post, page, attachment, nav_menu_item, {custom post key}
* or taxonomy: category, post_tag, nav_menu {custom taxonomy key}
* }
* @return object
* @uses \SitePress::api_hooks
*/
function wpml_element_language_details_filter( $element_object, $args ) {
global $sitepress;
if ( isset( $sitepress ) ) {
$element_type = apply_filters( 'wpml_element_type', $args['element_type'] );
$element_object = $sitepress->get_element_language_details( $args['element_id'], $element_type );
}
return $element_object;
}
/**
* Retrieves the language code for a translatable element
* Checks icl_translations db table and returns the element's language code
*
* @since 3.2.2
*
* @param mixed $language_code A 2-letter language code.
* @param array $args {
* Required An array of arguments to be used
*
* @type int $element_id Use term_taxonomy_id for taxonomies, post_id for posts
* @type string $element_type Can be a post type: post, page, attachment, nav_menu_item, {custom post key}
* or taxonomy: category, post_tag, nav_menu {custom taxonomy key}
* }
* @return string
* @uses \SitePress::api_hooks
*/
function wpml_element_language_code_filter( $language_code, $args ) {
global $sitepress;
if ( isset( $sitepress ) ) {
$element_type = apply_filters( 'wpml_element_type', $args['element_type'] );
$language_code = $sitepress->get_language_for_element( $args['element_id'], $element_type );
}
return $language_code;
}
/**
* Retrieves the elements without translations
* Queries the database and returns an array with ids
*
* @since 3.2.2
*
* @param array $element_ids An array of element ids.
* @param array $args {
* Required An array of arguments to be used
*
* @type string $target_language The target language code
* @type string $source_language The source language code
* @type string $element_type Can be a post type: post, page, attachment, nav_menu_item, {custom post key}
* or taxonomy: category, post_tag, nav_menu {custom taxonomy key}
* }
* @return array
* @uses \SitePress::api_hooks
*/
function wpml_elements_without_translations_filter( $element_ids, $args ) {
global $sitepress;
if ( isset( $sitepress ) ) {
$element_type = apply_filters( 'wpml_element_type', $args['element_type'] );
$element_ids = $sitepress->get_elements_without_translations( $element_type, $args['target_language'], $args['source_language'] );
}
return $element_ids;
}
/**
* @deprecated Use the filter hook `wpml_permalink` instead
*
* Filters a WordPress permalink and converts it to a language specific permalink based on plugin settings
*
* @since 3.2.2
*
* @param string $url The WordPress generated url to filter
* @param null|string $language_code if null, it falls back to default language for root page,
* or current language in all other cases.
*
* @return string
*/
function wpml_permalink_filter( $url, $language_code = null ) {
return apply_filters( 'wpml_permalink', $url, $language_code );
}
/**
* Switches WPML's query language
*
* @since 3.2.2
* @type null|string $language_code The language code to switch to
* If set to null it restores the original language
* If set to 'all' it will query content from all active languages
* Defaults to null
* @uses \SitePress::api_hooks
*/
function wpml_switch_language_action( $language_code = null ) {
global $sitepress;
$sitepress->switch_lang( $language_code, true );
}