?
wpml-compat.php 0000666 00000011373 15126443553 0007536 0 ustar 00 <?php
/**
* WPML Compatibility class
* Defines some WPML constants
* Registers strings in a persistent way as done by WPML
*
* @since 1.0.2
*/
class PLL_WPML_Compat {
protected static $instance; // For singleton
protected static $strings; // Used for cache
public $api;
/**
* Constructor
*
* @since 1.0.2
*/
protected function __construct() {
// Load the WPML API
require_once PLL_MODULES_INC . '/wpml/wpml-legacy-api.php';
$this->api = new PLL_WPML_API();
self::$strings = get_option( 'polylang_wpml_strings', array() );
add_action( 'pll_language_defined', array( $this, 'define_constants' ) );
add_action( 'pll_no_language_defined', array( $this, 'define_constants' ) );
add_filter( 'pll_get_strings', array( $this, 'get_strings' ) );
}
/**
* Access to the single instance of the class
*
* @since 1.7
*
* @return object
*/
public static function instance() {
if ( empty( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Defines two WPML constants once the language has been defined
* The compatibility with WPML is not perfect on admin side as the constants are defined
* in 'setup_theme' by Polylang ( based on user info ) and 'plugins_loaded' by WPML ( based on cookie )
*
* @since 0.9.5
*/
public function define_constants() {
if ( ! empty( PLL()->curlang ) ) {
if ( ! defined( 'ICL_LANGUAGE_CODE' ) ) {
define( 'ICL_LANGUAGE_CODE', PLL()->curlang->slug );
}
if ( ! defined( 'ICL_LANGUAGE_NAME' ) ) {
define( 'ICL_LANGUAGE_NAME', PLL()->curlang->name );
}
} elseif ( ! PLL() instanceof PLL_Frontend ) {
if ( ! defined( 'ICL_LANGUAGE_CODE' ) ) {
define( 'ICL_LANGUAGE_CODE', 'all' );
}
if ( ! defined( 'ICL_LANGUAGE_NAME' ) ) {
define( 'ICL_LANGUAGE_NAME', '' );
}
}
}
/**
* Unlike pll_register_string, icl_register_string stores the string in database
* so we need to do the same as some plugins or themes may expect this
* we use a serialized option to do this
*
* @since 1.0.2
*
* @param string $context The group in which the string is registered.
* @param string $name A unique name for the string.
* @param string $string The string to register.
*/
public function register_string( $context, $name, $string ) {
// If a string has already been registered with the same name and context, let's replace it.
$exist_string = $this->get_string_by_context_and_name( $context, $name );
if ( $exist_string && $exist_string !== $string ) {
$languages = PLL()->model->get_languages_list();
// Assign translations of the old string to the new string, except for the default language.
foreach ( $languages as $language ) {
if ( pll_default_language() !== $language->slug ) {
$mo = new PLL_MO();
$mo->import_from_db( $language );
$mo->add_entry( $mo->make_entry( $string, $mo->translate( $exist_string ) ) );
$mo->export_to_db( $language );
}
}
$this->unregister_string( $context, $name );
}
// Registers the string if it does not exist yet (multiline as in WPML).
$to_register = array( 'context' => $context, 'name' => $name, 'string' => $string, 'multiline' => true, 'icl' => true );
if ( ! in_array( $to_register, self::$strings ) && $to_register['string'] ) {
self::$strings[] = $to_register;
update_option( 'polylang_wpml_strings', self::$strings );
}
}
/**
* Removes a string from the registered strings list
*
* @since 1.0.2
*
* @param string $context the group in which the string is registered, defaults to 'polylang'
* @param string $name a unique name for the string
*/
public function unregister_string( $context, $name ) {
foreach ( self::$strings as $key => $string ) {
if ( $string['context'] == $context && $string['name'] == $name ) {
unset( self::$strings[ $key ] );
update_option( 'polylang_wpml_strings', self::$strings );
}
}
}
/**
* Adds strings registered by icl_register_string to those registered by pll_register_string
*
* @since 1.0.2
*
* @param array $strings existing registered strings
* @return array registered strings with added strings through WPML API
*/
public function get_strings( $strings ) {
return empty( self::$strings ) ? $strings : array_merge( $strings, self::$strings );
}
/**
* Get a registered string by its context and name
*
* @since 2.0
*
* @param string $context the group in which the string is registered
* @param string $name a unique name for the string
* @return bool|string the registered string, false if none was found
*/
public function get_string_by_context_and_name( $context, $name ) {
foreach ( self::$strings as $string ) {
if ( $string['context'] == $context && $string['name'] == $name ) {
return $string['string'];
}
}
return false;
}
}
wpml-api.php 0000666 00000024577 15126443553 0007036 0 ustar 00 <?php
/**
* A class to handle the WPML API based on hooks, introduced since WPML 3.2
* It partly relies on the legacy API
*
* @see https://wpml.org/documentation/support/wpml-coding-api/wpml-hooks-reference/
*
* @since 2.0
*/
class PLL_WPML_API {
/**
* Constructor
*
* @since 2.0
*/
public function __construct() {
// Site Wide Language informations
add_filter( 'wpml_active_languages', array( $this, 'wpml_active_languages' ), 10, 2 );
add_filter( 'wpml_display_language_names', array( $this, 'wpml_display_language_names' ), 10, 5 );
// wpml_translated_language_name => not applicable
add_filter( 'wpml_current_language', 'pll_current_language', 10, 0 );
add_filter( 'wpml_default_language', 'pll_default_language', 10, 0 );
// wpml_add_language_selector => not implemented
// wpml_footer_language_selector => not applicable
add_action( 'wpml_add_language_form_field', array( $this, 'wpml_add_language_form_field' ) );
add_filter( 'wpml_language_is_active', array( $this, 'wpml_language_is_active' ), 10, 2 );
add_filter( 'wpml_is_rtl', array( $this, 'wpml_is_rtl' ) );
// wpml_language_form_input_field => See wpml_add_language_form_field
// wpml_language_has_switched => not implemented
// wpml_element_trid => not implemented
// wpml_get_element_translations => not implemented
// wpml_language_switcher => not implemented
// wpml_browser_redirect_language_params => not implemented
// wpml_enqueue_browser_redirect_language => not applicable
// wpml_enqueued_browser_redirect_language => not applicable
// wpml_encode_string => not applicable
// wpml_decode_string => not applicable
// Retrieving Language Information for Content
add_filter( 'wpml_post_language_details', 'wpml_get_language_information', 10, 2 );
// wpml_switch_language => not implemented
add_filter( 'wpml_element_language_code', array( $this, 'wpml_element_language_code' ), 10, 3 );
// wpml_element_language_details => not applicable
// Retrieving Localized Content
add_filter( 'wpml_home_url', 'pll_home_url', 10, 0 );
add_filter( 'wpml_element_link', 'icl_link_to_element', 10, 7 );
add_filter( 'wpml_object_id', 'icl_object_id', 10, 4 );
add_filter( 'wpml_translate_single_string', array( $this, 'wpml_translate_single_string' ), 10, 4 );
// wpml_translate_string => not applicable
// wpml_unfiltered_admin_string => not implemented
add_filter( 'wpml_permalink', array( $this, 'wpml_permalink' ), 10, 2 );
// wpml_elements_without_translations => not implemented
add_filter( 'wpml_get_translated_slug', array( $this, 'wpml_get_translated_slug' ), 10, 3 );
// Finding the Translation State of Content
// wpml_element_translation_type => not implemented
add_filter( 'wpml_element_has_translations', array( $this, 'wpml_element_has_translations' ), 10, 3 );
// wpml_master_post_from_duplicate => not applicable
// wpml_post_duplicates => not applicable
// Inserting Content
// wpml_admin_make_post_duplicates => not applicable
// wpml_make_post_duplicates => not applicable
add_action( 'wpml_register_single_string', 'icl_register_string', 10, 3 );
// wpml_register_string => not applicable
// wpml_register_string_packages => not applicable
// wpml_delete_package_action => not applicable
// wpml_show_package_language_ui => not applicable
// wpml_set_element_language_details => not implemented
// wpml_multilingual_options => not applicable
// Miscellaneous
// wpml_element_type => not applicable
// wpml_setting => not applicable
// wpml_sub_setting => not applicable
// wpml_editor_cf_to_display => not applicable
// wpml_tm_save_translation_cf => not implemented
// wpml_tm_xliff_export_translated_cf => not applicable
// wpml_tm_xliff_export_original_cf => not applicable
// wpml_duplicate_generic_string => not applicable
// wpml_translatable_user_meta_fields => not implemented
// wpml_cross_domain_language_data => not applicable
// wpml_get_cross_domain_language_data => not applicable
// wpml_loaded => not applicable
// wpml_st_loaded => not applicable
// wpml_tm_loaded => not applicable
// wpml_hide_management_column (3.4.1) => not applicable
// wpml_ls_directories_to_scan => not applicable
// wpml_ls_model_css_classes => not applicable
// wpml_ls_model_language_css_classes => not applicable
// wpml_tf_feedback_open_link => not applicable
// wpml_sync_custom_field => not implemented
// wpml_sync_all_custom_fields => not implemented
// wpml_is_redirected => not implemented
// Updating Content
// wpml_set_translation_mode_for_post_type => not implemented
}
/**
* Get a list of the languages enabled for a site
*
* @since 2.0
*
* @param mixed $null Not used
* @param array| string $args See arguments of icl_get_languages()
* @return array Array of arrays per language
*/
public function wpml_active_languages( $null, $args = '' ) {
return icl_get_languages( $args );
}
/**
* In WPML, get a language's native and translated name for display in a custom language switcher
* Since Polylang does not implement the translated name, always returns only the native name
*
* @since 2.2
*
* @param mixed $null Not used.
* @param string $native_name The language native name.
* @param string|bool $translated_name The language translated name. Not used.
* @param bool $native_hidden Whether to hide the language native name or not. Not used.
* @param bool $translated_hidden Whether to hide the language translated name or not. Not used.
* @return string
*/
public function wpml_display_language_names( $null, $native_name, $translated_name = false, $native_hidden = false, $translated_hidden = false ) {
return $native_name;
}
/**
* Returns an HTML hidden input field with name=”lang” and as value the current language
*
* @since 2.0
*/
public function wpml_add_language_form_field() {
$lang = pll_current_language();
$field = sprintf( '<input type="hidden" name="lang" value="%s" />', esc_attr( $lang ) );
$field = apply_filters( 'wpml_language_form_input_field', $field, $lang );
echo $field; // phpcs:ignore WordPress.Security.EscapeOutput
}
/**
* Find out if a specific language is enabled for the site
*
* @since 2.0
*
* @param mixed $null Not used
* @param string $slug Language code
* @return bool
*/
public function wpml_language_is_active( $null, $slug ) {
$language = PLL()->model->get_language( $slug );
return empty( $language->active ) || true === $language->active;
}
/**
* Find out whether the current language text direction is RTL or not
*
* @since 2.0
*
* @param mixed $null Not used
* @return bool
*/
public function wpml_is_rtl( $null ) {
return pll_current_language( 'is_rtl' );
}
/**
* Get the language code for a translatable element
*
* @since 2.0
*
* @param mixed $language_code
* @param array $args An array with two keys element_id => post_id or term_taxonomy_id, element_type => post type or taxonomy
* @return string
*/
public function wpml_element_language_code( $language_code, $args ) {
$type = $args['element_type'];
$id = $args['element_id'];
$pll_type = ( 'post' == $type || pll_is_translated_post_type( $type ) ) ? 'post' : ( 'term' == $type || pll_is_translated_taxonomy( $type ) ? 'term' : false );
if ( 'term' === $pll_type && $term = wpcom_vip_get_term_by( 'term_taxonomy_id', $id ) ) {
$id = $term->term_id;
}
return $pll_type ? call_user_func( "pll_get_{$pll_type}_language", $id ) : $language_code;
}
/**
* Translates a string
*
* @since 2.0
*
* @param string $string The string's original value
* @param string $context The string's registered context
* @param string $name The string's registered name
* @param null|string $lang Optional, return the translation in this language, defaults to current language
* @return string The translated string
*/
public function wpml_translate_single_string( $string, $context, $name, $lang = null ) {
$has_translation = null; // Passed by reference
return icl_translate( $context, $name, $string, false, $has_translation, $lang );
}
/**
* Converts a permalink to a language specific permalink
*
* @since 2.2
*
* @param string $url The url to filter
* @param null|string $lang Langage code, optional, defaults to the current language
* @return string
*/
public function wpml_permalink( $url, $lang = '' ) {
$lang = PLL()->model->get_language( $lang );
if ( empty( $lang ) && ! empty( PLL()->curlang ) ) {
$lang = PLL()->curlang;
}
return empty( $lang ) ? $url : PLL()->links_model->switch_language_in_link( $url, $lang );
}
/**
* Translates a post type slug
*
* @since 2.2
*
* @param string $slug Post type slug
* @param string $post_type Post type name
* @param string $lang Optional language code (defaults to current language)
* @return string
*/
public function wpml_get_translated_slug( $slug, $post_type, $lang = null ) {
if ( isset( PLL()->translate_slugs ) ) {
if ( empty( $lang ) ) {
$lang = pll_current_language();
}
$slug = PLL()->translate_slugs->slugs_model->get_translated_slug( $post_type, $lang );
}
return $slug;
}
/**
* Find out whether a post type or a taxonomy term is translated
*
* @since 2.0
*
* @param mixed $null
* @param int $id The post_id or term_id
* @param string $type The post type or taxonomy
* @return bool
*/
public function wpml_element_has_translations( $null, $id, $type ) {
$pll_type = ( 'post' == $type || pll_is_translated_post_type( $type ) ) ? 'post' : ( 'term' == $type || pll_is_translated_taxonomy( $type ) ? 'term' : false );
return ( $pll_type && $translations = call_user_func( "pll_get_{$pll_type}_translations", $id ) ) ? count( $translations ) > 1 : false;
}
}
wpml-legacy-api.php 0000666 00000035561 15126443553 0010273 0 ustar 00 <?php
/**
* Compatibility with WPML legacy API
* Deprecated since WPML 3.2 and no more documented
* But a lot of 3rd party plugins / themes are still using these functions
*/
if ( ! function_exists( 'icl_get_home_url' ) ) {
/**
* Link to the home page in the active language
*
* @since 0.9.4
*
* @return string
*/
function icl_get_home_url() {
return pll_home_url();
}
}
if ( ! function_exists( 'icl_get_languages' ) ) {
/**
* Used for building custom language selectors
* available only on frontend
*
* List of paramaters accepted in $args:
*
* skip_missing => whether to skip missing translation or not, 0 or 1, defaults to 0
* orderby => 'id', 'code', 'name', defaults to 'id'
* order => 'ASC' or 'DESC', defaults to 'ASC'
* link_empty_to => link to use when the translation is missing {$lang} is replaced by the language code
*
* List of parameters returned per language:
*
* id => the language id
* active => whether this is the active language or no, 0 or 1
* native_name => the language name
* missing => whether the translation is missing or not, 0 or 1
* translated_name => empty, does not exist in Polylang
* language_code => the language code ( slug )
* country_flag_url => the url of the flag
* url => the url of the translation
*
* @since 1.0
*
* @param string|array $args optional
* @return array array of arrays per language
*/
function icl_get_languages( $args = '' ) {
$args = wp_parse_args( $args, array( 'skip_missing' => 0, 'orderby' => 'id', 'order' => 'ASC' ) );
$orderby = ( isset( $args['orderby'] ) && 'code' == $args['orderby'] ) ? 'slug' : ( isset( $args['orderby'] ) && 'name' == $args['orderby'] ? 'name' : 'id' );
$order = ( ! empty( $args['order'] ) && 'desc' == $args['order'] ) ? 'DESC' : 'ASC';
$arr = array();
// NB: When 'skip_missing' is false, WPML returns all languages even if there is no content
$languages = PLL()->model->get_languages_list( array( 'hide_empty' => $args['skip_missing'] ) );
// FIXME: Backward compatibility with WP < 4.7
if ( function_exists( 'wp_list_sort' ) ) {
$languages = wp_list_sort( $languages, $orderby, $order ); // Since WP 4.7
}
foreach ( $languages as $lang ) {
// We can find a translation only on frontend once the global $wp_query object has been instantiated
if ( method_exists( PLL()->links, 'get_translation_url' ) && ! empty( $GLOBALS['wp_query'] ) ) {
$url = PLL()->links->get_translation_url( $lang );
}
// It seems that WPML does not bother of skip_missing parameter on admin side and before the $wp_query object has been filled
if ( empty( $url ) && ! empty( $args['skip_missing'] ) && ! is_admin() && did_action( 'parse_query' ) ) {
continue;
}
$arr[ $lang->slug ] = array(
'id' => $lang->term_id,
'active' => isset( PLL()->curlang->slug ) && PLL()->curlang->slug == $lang->slug ? 1 : 0,
'native_name' => $lang->name,
'missing' => empty( $url ) ? 1 : 0,
'translated_name' => '', // Does not exist in Polylang
'language_code' => $lang->slug,
'country_flag_url' => $lang->flag_url,
'url' => ! empty( $url ) ? $url :
( empty( $args['link_empty_to'] ) ? PLL()->links->get_home_url( $lang ) :
str_replace( '{$lang}', $lang->slug, $args['link_empty_to'] ) ),
);
}
// Apply undocumented WPML filter
$arr = apply_filters( 'icl_ls_languages', $arr );
return $arr;
}
}
if ( ! function_exists( 'icl_link_to_element' ) ) {
/**
* Used for creating language dependent links in themes
*
* @since 1.0
* @since 2.0 add support for arguments 6 and 7
*
* @param int $id object id
* @param string $type optional, post type or taxonomy name of the object, defaults to 'post'
* @param string $text optional, the link text. If not specified will produce the name of the element in the current language
* @param array $args optional, an array of arguments to add to the link, defaults to empty
* @param string $anchor optional, the anchor to add to the link, defaults to empty
* @param bool $echo optional, whether to echo the link, defaults to true
* @param bool $return_original_if_missing optional, whether to return a value if the translation is missing
* @return string a language dependent link
*/
function icl_link_to_element( $id, $type = 'post', $text = '', $args = array(), $anchor = '', $echo = true, $return_original_if_missing = true ) {
if ( 'tag' == $type ) {
$type = 'post_tag';
}
$pll_type = ( 'post' == $type || pll_is_translated_post_type( $type ) ) ? 'post' : ( 'term' == $type || pll_is_translated_taxonomy( $type ) ? 'term' : false );
if ( $pll_type && ( $lang = pll_current_language() ) && ( $tr_id = PLL()->model->$pll_type->get_translation( $id, $lang ) ) && ( 'term' === $pll_type || PLL()->model->post->current_user_can_read( $tr_id ) ) ) {
$id = $tr_id;
} elseif ( ! $return_original_if_missing ) {
return '';
}
if ( post_type_exists( $type ) ) {
$link = get_permalink( $id );
if ( empty( $text ) ) {
$text = get_the_title( $id );
}
} elseif ( taxonomy_exists( $type ) ) {
$link = wpcom_vip_get_term_link( $id, $type );
if ( empty( $text ) && ( $term = get_term( $id, $type ) ) && ! empty( $term ) && ! is_wp_error( $term ) ) {
$text = $term->name;
}
}
if ( empty( $link ) || is_wp_error( $link ) ) {
return '';
}
if ( ! empty( $args ) ) {
$link .= ( false === strpos( $link, '?' ) ? '?' : '&' ) . http_build_query( $args );
}
if ( ! empty( $anchor ) ) {
$link .= '#' . $anchor;
}
$link = sprintf( '<a href="%s">%s</a>', esc_url( $link ), esc_html( $text ) );
if ( $echo ) {
echo $link; // phpcs:ignore WordPress.Security.EscapeOutput
}
return $link;
}
}
if ( ! function_exists( 'icl_object_id' ) ) {
/**
* Used for calculating the IDs of objects (usually categories) in the current language
*
* @since 0.9.5
*
* @param int $id Object id
* @param string $type Optional, post type or taxonomy name of the object, defaults to 'post'
* @param bool $return_original_if_missing Optional, true if Polylang should return the original id if the translation is missing, defaults to false
* @param string $lang Optional, language code, defaults to current language
* @return int|null The object id of the translation, null if the translation is missing and $return_original_if_missing set to false
*/
function icl_object_id( $id, $type = 'post', $return_original_if_missing = false, $lang = false ) {
$lang = $lang ? $lang : pll_current_language();
if ( 'nav_menu' === $type ) {
$theme = get_option( 'stylesheet' );
if ( isset( PLL()->options['nav_menus'][ $theme ] ) ) {
foreach ( PLL()->options['nav_menus'][ $theme ] as $loc => $menu ) {
if ( array_search( $id, $menu ) && ! empty( $menu[ $lang ] ) ) {
$tr_id = $menu[ $lang ];
break;
}
}
}
} elseif ( $pll_type = ( 'post' === $type || pll_is_translated_post_type( $type ) ) ? 'post' : ( 'term' === $type || pll_is_translated_taxonomy( $type ) ? 'term' : false ) ) {
$tr_id = PLL()->model->$pll_type->get_translation( $id, $lang );
}
return ! empty( $tr_id ) ? $tr_id : ( $return_original_if_missing ? $id : null );
}
}
if ( ! function_exists( 'wpml_object_id_filter' ) ) {
/**
* Undocumented alias of `icl_object_id` introduced in WPML 3.2, used by Yith WooCommerce compare
*
* @since 2.2.4
*
* @param int $id object id
* @param string $type optional, post type or taxonomy name of the object, defaults to 'post'
* @param bool $return_original_if_missing optional, true if Polylang should return the original id if the translation is missing, defaults to false
* @param string $lang optional, language code, defaults to current language
* @return int|null the object id of the translation, null if the translation is missing and $return_original_if_missing set to false
*/
function wpml_object_id_filter( $id, $type = 'post', $return_original_if_missing = false, $lang = null ) {
return icl_object_id( $id, $type, $return_original_if_missing, $lang );
}
}
if ( ! function_exists( 'wpml_get_language_information' ) ) {
/**
* Undocumented function used by the theme Maya
* returns the post language
*
* @see original WPML code at https://wpml.org/forums/topic/canonical-urls-for-wpml-duplicated-posts/#post-52198
*
* @since 1.8
*
* @param null $empty optional, not used
* @param int $post_id optional, post id, defaults to current post
* @return array
*/
function wpml_get_language_information( $empty = null, $post_id = null ) {
if ( empty( $post_id ) ) {
$post_id = get_the_ID();
}
// FIXME WPML may return a WP_Error object
return false === ( $lang = PLL()->model->post->get_language( $post_id ) ) ? array() : array(
'language_code' => $lang->slug,
'locale' => $lang->locale,
'text_direction' => (bool) $lang->is_rtl,
'display_name' => $lang->name, // Seems to be the post language name displayed in the current language, not a feature in Polylang
'native_name' => $lang->name,
'different_language' => pll_current_language() !== $lang->slug,
);
}
}
if ( ! function_exists( 'icl_register_string' ) ) {
/**
* Registers a string for translation in the "strings translation" panel
*
* @since 0.9.3
*
* @param string $context the group in which the string is registered, defaults to 'polylang'
* @param string $name a unique name for the string
* @param string $string the string to register
* @param bool $allow_empty_value not used
* @param string $source_lang not used by Polylang
*/
function icl_register_string( $context, $name, $string, $allow_empty_value = false, $source_lang = '' ) {
PLL_WPML_Compat::instance()->register_string( $context, $name, $string );
}
}
if ( ! function_exists( 'icl_unregister_string' ) ) {
/**
* Removes a string from the "strings translation" panel
*
* @since 1.0.2
*
* @param string $context the group in which the string is registered, defaults to 'polylang'
* @param string $name a unique name for the string
*/
function icl_unregister_string( $context, $name ) {
PLL_WPML_Compat::instance()->unregister_string( $context, $name );
}
}
if ( ! function_exists( 'icl_t' ) ) {
/**
* Gets the translated value of a string ( previously registered with icl_register_string or pll_register_string )
*
* @since 0.9.3
* @since 1.9.2 argument 3 is optional
* @since 2.0 add support for arguments 4 to 6
*
* @param string $context the group in which the string is registered
* @param string $name a unique name for the string
* @param string $string the string to translate, optional for strings registered with icl_register_string
* @param bool|null $has_translation optional, not supported in Polylang
* @param bool $bool optional, not used
* @param string|null $lang optional, return the translation in this language, defaults to current language
* @return string the translated string
*/
function icl_t( $context, $name, $string = false, &$has_translation = null, $bool = false, $lang = null ) {
return icl_translate( $context, $name, $string, false, $has_translation, $lang );
}
}
if ( ! function_exists( 'icl_translate' ) ) {
/**
* Undocumented function used by NextGen Gallery
* used in PLL_Plugins_Compat for Jetpack with only 3 arguments
*
* @since 1.0.2
* @since 2.0 add support for arguments 5 and 6, strings are no more automatically registered
*
* @param string $context the group in which the string is registered
* @param string $name a unique name for the string
* @param string $string the string to translate, optional for strings registered with icl_register_string
* @param bool $bool optional, not used
* @param bool|null $has_translation optional, not supported in Polylang
* @param string|null $lang optional, return the translation in this language, defaults to current language
* @return string the translated string
*/
function icl_translate( $context, $name, $string = false, $bool = false, &$has_translation = null, $lang = null ) {
// FIXME WPML can automatically registers the string based on an option
if ( empty( $string ) ) {
$string = PLL_WPML_Compat::instance()->get_string_by_context_and_name( $context, $name );
}
return empty( $lang ) ? pll__( $string ) : pll_translate_string( $string, $lang );
}
}
if ( ! function_exists( 'wpml_get_copied_fields_for_post_edit' ) ) {
/**
* Undocumented function used by Types
* FIXME: tested only with Types
* probably incomplete as Types locks the custom fields for a new post, but not when edited
* This is probably linked to the fact that WPML has always an original post in the default language and not Polylang :)
*
* @since 1.1.2
*
* @return array
*/
function wpml_get_copied_fields_for_post_edit() {
if ( empty( $_GET['from_post'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
return array();
}
$arr['original_post_id'] = (int) $_GET['from_post']; // phpcs:ignore WordPress.Security.NonceVerification
// Don't know what WPML does but Polylang does copy all public meta keys by default
foreach ( $keys = array_unique( array_keys( get_post_custom( $arr['original_post_id'] ) ) ) as $k => $meta_key ) {
if ( is_protected_meta( $meta_key ) ) {
unset( $keys[ $k ] );
}
}
// Apply our filter and fill the expected output ( see /types/embedded/includes/fields-post.php )
/** This filter is documented in modules/sync/admin-sync.php */
$arr['fields'] = array_unique( apply_filters( 'pll_copy_post_metas', empty( $keys ) ? array() : $keys, false ) );
return $arr;
}
}
if ( ! function_exists( 'icl_get_default_language' ) ) {
/**
* Undocumented function used by Warp 6 by Yootheme
*
* @since 1.0.5
*
* @return string default language code
*/
function icl_get_default_language() {
return pll_default_language();
}
}
if ( ! function_exists( 'wpml_get_default_language' ) ) {
/**
* Undocumented function reported to be used by Table Rate Shipping for WooCommerce
*
* @see https://wordpress.org/support/topic/add-wpml-compatibility-function
*
* @since 1.8.2
*
* @return string default language code
*/
function wpml_get_default_language() {
return pll_default_language();
}
}
if ( ! function_exists( 'icl_get_current_language' ) ) {
/**
* Undocumented function used by Ultimate Member
*
* @since 2.2.4
*
* @return string Current language code
*/
function icl_get_current_language() {
return pll_current_language();
}
}
settings-wpml.php 0000666 00000001310 15126443553 0010101 0 ustar 00 <?php
/**
* A class to inform about the WPML compatibility module in Polylang settings
*
* @since 1.8
*/
class PLL_Settings_WPML extends PLL_Settings_Module {
/**
* Constructor
*
* @since 1.8
*
* @param object $polylang polylang object
*/
public function __construct( &$polylang ) {
parent::__construct(
$polylang,
array(
'module' => 'wpml',
'title' => __( 'WPML compatibility', 'polylang' ),
'description' => __( 'Polylang\'s WPML compatibility mode', 'polylang' ),
)
);
}
/**
* Tells if the module is active
*
* @since 1.8
*
* @return bool
*/
public function is_active() {
return ! defined( 'PLL_WPML_COMPAT' ) || PLL_WPML_COMPAT;
}
}
wpml-config.php 0000666 00000022625 15126443553 0007522 0 ustar 00 <?php
/**
* Reads and interprets the file wpml-config.xml
* See http://wpml.org/documentation/support/language-configuration-files/
* The language switcher configuration is not interpreted
*
* @since 1.0
*/
class PLL_WPML_Config {
protected static $instance; // For singleton
protected $xmls, $options;
/**
* Constructor
*
* @since 1.0
*/
public function __construct() {
if ( extension_loaded( 'simplexml' ) ) {
$this->init();
}
}
/**
* Access to the single instance of the class
*
* @since 1.7
*
* @return object
*/
public static function instance() {
if ( empty( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Finds the wpml-config.xml files to parse and setup filters
*
* @since 1.0
*/
public function init() {
$this->xmls = array();
// Plugins
// Don't forget sitewide active plugins thanks to Reactorshop http://wordpress.org/support/topic/polylang-and-yoast-seo-plugin/page/2?replies=38#post-4801829
$plugins = ( is_multisite() && $sitewide_plugins = get_site_option( 'active_sitewide_plugins' ) ) && is_array( $sitewide_plugins ) ? array_keys( $sitewide_plugins ) : array();
$plugins = array_merge( $plugins, get_option( 'active_plugins', array() ) );
foreach ( $plugins as $plugin ) {
if ( file_exists( $file = WP_PLUGIN_DIR . '/' . dirname( $plugin ) . '/wpml-config.xml' ) && false !== $xml = simplexml_load_file( $file ) ) {
$this->xmls[ dirname( $plugin ) ] = $xml;
}
}
// Theme
if ( file_exists( $file = ( $template = get_template_directory() ) . '/wpml-config.xml' ) && false !== $xml = simplexml_load_file( $file ) ) {
$this->xmls[ get_template() ] = $xml;
}
// Child theme
if ( ( $stylesheet = get_stylesheet_directory() ) !== $template && file_exists( $file = $stylesheet . '/wpml-config.xml' ) && false !== $xml = simplexml_load_file( $file ) ) {
$this->xmls[ get_stylesheet() ] = $xml;
}
// Custom
if ( file_exists( $file = PLL_LOCAL_DIR . '/wpml-config.xml' ) && false !== $xml = simplexml_load_file( $file ) ) {
$this->xmls['Polylang'] = $xml;
}
if ( ! empty( $this->xmls ) ) {
add_filter( 'pll_copy_post_metas', array( $this, 'copy_post_metas' ), 20, 2 );
add_filter( 'pll_copy_term_metas', array( $this, 'copy_term_metas' ), 20, 2 );
add_filter( 'pll_get_post_types', array( $this, 'translate_types' ), 10, 2 );
add_filter( 'pll_get_taxonomies', array( $this, 'translate_taxonomies' ), 10, 2 );
foreach ( $this->xmls as $context => $xml ) {
foreach ( $xml->xpath( 'admin-texts/key' ) as $key ) {
$attributes = $key->attributes();
$name = (string) $attributes['name'];
if ( PLL() instanceof PLL_Frontend ) {
$this->options[ $name ] = $key;
add_filter( 'option_' . $name, array( $this, 'translate_strings' ) );
} else {
$this->register_string_recursive( $context, get_option( $name ), $key );
}
}
}
}
}
/**
* Adds custom fields to the list of metas to copy when creating a new translation
*
* @since 1.0
*
* @param array $metas the list of custom fields to copy or synchronize
* @param bool $sync true for sync, false for copy
* @return array the list of custom fields to copy or synchronize
*/
public function copy_post_metas( $metas, $sync ) {
foreach ( $this->xmls as $xml ) {
foreach ( $xml->xpath( 'custom-fields/custom-field' ) as $cf ) {
$attributes = $cf->attributes();
if ( 'copy' == $attributes['action'] || ( ! $sync && in_array( $attributes['action'], array( 'translate', 'copy-once' ) ) ) ) {
$metas[] = (string) $cf;
} else {
$metas = array_diff( $metas, array( (string) $cf ) );
}
}
}
return $metas;
}
/**
* Adds term metas to the list of metas to copy when creating a new translation
*
* @since 2.6
*
* @param array $metas The list of term metas to copy or synchronize.
* @param bool $sync True for sync, false for copy.
* @return array The list of term metas to copy or synchronize.
*/
public function copy_term_metas( $metas, $sync ) {
foreach ( $this->xmls as $xml ) {
foreach ( $xml->xpath( 'custom-term-fields/custom-term-field' ) as $cf ) {
$attributes = $cf->attributes();
if ( 'copy' == $attributes['action'] || ( ! $sync && in_array( $attributes['action'], array( 'translate', 'copy-once' ) ) ) ) {
$metas[] = (string) $cf;
} else {
$metas = array_diff( $metas, array( (string) $cf ) );
}
}
}
return $metas;
}
/**
* Language and translation management for custom post types
*
* @since 1.0
*
* @param array $types list of post type names for which Polylang manages language and translations
* @param bool $hide true when displaying the list in Polylang settings
* @return array list of post type names for which Polylang manages language and translations
*/
public function translate_types( $types, $hide ) {
foreach ( $this->xmls as $xml ) {
foreach ( $xml->xpath( 'custom-types/custom-type' ) as $pt ) {
$attributes = $pt->attributes();
if ( 1 == $attributes['translate'] && ! $hide ) {
$types[ (string) $pt ] = (string) $pt;
} else {
unset( $types[ (string) $pt ] ); // The theme/plugin author decided what to do with the post type so don't allow the user to change this
}
}
}
return $types;
}
/**
* Language and translation management for custom taxonomies
*
* @since 1.0
*
* @param array $taxonomies list of taxonomy names for which Polylang manages language and translations
* @param bool $hide true when displaying the list in Polylang settings
* @return array list of taxonomy names for which Polylang manages language and translations
*/
public function translate_taxonomies( $taxonomies, $hide ) {
foreach ( $this->xmls as $xml ) {
foreach ( $xml->xpath( 'taxonomies/taxonomy' ) as $tax ) {
$attributes = $tax->attributes();
if ( 1 == $attributes['translate'] && ! $hide ) {
$taxonomies[ (string) $tax ] = (string) $tax;
} else {
unset( $taxonomies[ (string) $tax ] ); // the theme/plugin author decided what to do with the taxonomy so don't allow the user to change this
}
}
}
return $taxonomies;
}
/**
* Translates the strings for an option
*
* @since 1.0
*
* @param array|string $value Either a string to translate or a list of strings to translate
* @return array|string translated string(s)
*/
public function translate_strings( $value ) {
$option = substr( current_filter(), 7 );
return $this->translate_strings_recursive( $value, $this->options[ $option ] );
}
/**
* Recursively registers strings for a serialized option
*
* @since 1.0
*
* @param string $context the group in which the strings will be registered
* @param array $options
* @param object $key XML node
*/
protected function register_string_recursive( $context, $options, $key ) {
$children = $key->children();
if ( count( $children ) ) {
foreach ( $children as $child ) {
$attributes = $child->attributes();
$name = (string) $attributes['name'];
if ( '*' === $name && is_array( $options ) ) {
foreach ( $options as $n => $option ) {
$this->register_wildcard_options_recursive( $context, $option, $n );
}
} elseif ( isset( $options[ $name ] ) ) {
$this->register_string_recursive( $context, $options[ $name ], $child );
}
}
} else {
$attributes = $key->attributes();
pll_register_string( (string) $attributes['name'], $options, $context, true ); // Multiline as in WPML
}
}
/**
* Recursively registers strings with a wildcard
*
* @since 2.1
*
* @param string $context the group in which the strings will be registered
* @param array $options
* @param string $name Option name
*/
protected function register_wildcard_options_recursive( $context, $options, $name ) {
if ( is_array( $options ) ) {
foreach ( $options as $n => $option ) {
$this->register_wildcard_options_recursive( $context, $option, $n );
}
} else {
pll_register_string( $name, $options, $context );
}
}
/**
* Recursively translates strings for a serialized option
*
* @since 1.0
*
* @param array|string $values either a string to translate or a list of strings to translate
* @param object $key XML node
* @return array|string translated string(s)
*/
protected function translate_strings_recursive( $values, $key ) {
$children = $key->children();
if ( count( $children ) ) {
foreach ( $children as $child ) {
$attributes = $child->attributes();
$name = (string) $attributes['name'];
if ( '*' === $name && is_array( $values ) ) {
foreach ( $values as $n => $v ) {
$values[ $n ] = $this->translate_wildcard_options_recursive( $v, $n );
}
} elseif ( isset( $values[ $name ] ) ) {
$values[ $name ] = $this->translate_strings_recursive( $values[ $name ], $child );
}
}
} else {
$values = pll__( $values );
}
return $values;
}
/**
* Recursively translates strings registered by a wildcard
*
* @since 2.1
*
* @param array|string $options Either a string to translate or a list of strings to translate
* @param string $name Option name
* @return array|string Translated string(s)
*/
protected function translate_wildcard_options_recursive( $options, $name ) {
if ( is_array( $options ) ) {
foreach ( $options as $n => $option ) {
$options[ $n ] = $this->translate_wildcard_options_recursive( $option, $n );
}
} else {
$options = pll__( $options );
}
return $options;
}
}