Sindbad~EG File Manager
<?php
namespace RealPress\Controllers\Translation;
use RealPress\Helpers\Config;
use RealPress\Helpers\Settings;
use RealPress\Helpers\Translation\WPML;
class WPMLController {
public function __construct() {
add_filter( 'realpress/filter/frontend-script/global-object', array( $this, 'add_localize_script' ) );
add_filter( 'realpress/filter/admin-script/global-object', array( $this, 'add_localize_script' ) );
add_filter( 'realpress/filter/set-up/script/global-object', array( $this, 'add_localize_script' ) );
add_action( 'icl_ls_languages', array( $this, 'get_property_archive_url' ) );
add_filter( 'pre_term_link', array( $this, 'get_link_property_term' ), 10, 2 );
add_filter( 'realpress/filter/list-property/args', array( $this, 'property_query_arg' ), 10, 2 );
add_filter( 'realpress/filter/wishlist/args', array( $this, 'property_query_arg' ), 10, 2 );
add_filter( 'realpress/filter/compare/args', array( $this, 'property_query_arg' ), 10, 2 );
add_action( 'realpress/insert-page/after', array( $this, 'set_page_language' ), 10, 2 );
}
/**
* @param $w_active_languages
*
* @return array|mixed
*/
public function get_property_archive_url( $w_active_languages ) {
if ( ! WPML::is_active() ) {
return $w_active_languages;
}
if ( ! is_post_type_archive( REALPRESS_PROPERTY_CPT ) ) {
return $w_active_languages;
}
$current_lang = WPML::get_current_language();
$current_lang_property_slug = Settings::get_setting_detail( 'group:slug:fields:property', $current_lang );
foreach ( $w_active_languages as $k => $v ) {
$switch_lang_property_slug = Settings::get_setting_detail( 'group:slug:fields:property', $k );
$w_active_languages[ $k ]['url'] = str_replace( '/' . $current_lang_property_slug . '/', '/' . $switch_lang_property_slug . '/', $v['url'] );
}
return $w_active_languages;
}
/**
* @param $term_link
* @param $term
*
* @return array|mixed|string|string[]
*/
public function get_link_property_term( $term_link, $term ) {
if ( ! WPML::is_active() ) {
return $term_link;
}
$taxonomies = Config::instance()->get( 'property-type:taxonomies' );
if ( ! isset( $taxonomies[ $term->taxonomy ] ) ) {
return $term_link;
}
$lang = WPML::get_element_language_code( array(
'element_id' => $term->term_id,
'element_type' => $term->taxonomy
) );
$current_lang = WPML::get_current_language();
if ( $lang === $current_lang ) {
return $term_link;
}
$current_lang_term_slug = Settings::get_setting_detail( 'group:slug:fields:' . $term->taxonomy, $current_lang );
$switch_lang_term_slug = Settings::get_setting_detail( 'group:slug:fields:' . $term->taxonomy, $lang );
return str_replace( '/' . $current_lang_term_slug . '/', '/' . $switch_lang_term_slug . '/', $term_link );
}
/**
* @param $args
*
* @return mixed
*/
public function add_localize_script( $args ) {
if ( ! WPML::is_active() ) {
return $args;
}
$args['wpml_current_lang'] = WPML::get_current_language();
return $args;
}
/**
* @param $args
* @param $params
*
* @return mixed
*/
public function property_query_arg( $args, $params ) {
if ( isset( $params['wpml_current_lang'] ) && ! empty( $params['wpml_current_lang'] ) ) {
$args['lang'] = $params['wpml_current_lang'];
}
return $args;
}
/**
* @param $id
* @param $params
*
* @return void
*/
public function set_page_language( $id, $params ) {
if ( empty( $id ) || is_wp_error( $id ) ) {
return;
}
if ( isset( $params['wpml_current_lang'] ) && ! empty( $params['wpml_current_lang'] ) ) {
$type = get_post_type( $id );
$set_language_args = array(
'element_id' => $id,
'element_type' => 'post_' . $type,
'language_code' => $params['wpml_current_lang'],
);
WPML::set_element_language_details( $set_language_args );
}
}
}
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists