Instantly share code, notes, and snippets.
Last active
August 10, 2020 03:27
-
Star
1
(1)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save srdjan-jcc/2bab3671ffd1295fe1a02751e55ead23 to your computer and use it in GitHub Desktop.
Ubermenu WPML menu sync
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Ubermenu Multilingual | |
* | |
* @package wpml | |
* @author OnTheGoSystems | |
* @copyright 2018 OTGS | |
* @license GPL-2.0+ | |
* | |
* @wordpress-plugin | |
* Plugin Name: Ubermenu Multilingual | |
* Plugin URI: https://wpml.org | |
* Description: This 'glue' plugin will sync menu properties between languages on Ubermenu save action. | |
* Version: 0.0.1 | |
* Author: OnTheGoSystems | |
* Author URI: https://wpml.org | |
* Text Domain: wpml-ubermenu | |
* License: GPL-2.0+ | |
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt | |
*/ | |
if ( ! defined( 'WPML_UBERMENU_ENABLE_STRING_TRANSLATION' ) ) { | |
define( 'WPML_UBERMENU_ENABLE_STRING_TRANSLATION', false ); | |
} | |
add_action( 'init', 'wpml_ubermenu_init', 20 ); | |
function wpml_ubermenu_init() { | |
if ( did_action( 'wpml_loaded' ) && defined( 'UBERMENU_VERSION' ) ) { | |
$wpml_ubermenu = new WPML_Ubermenu(); | |
$wpml_ubermenu->apply_filters(); | |
$wpml_ubermenu->add_hooks(); | |
} | |
} | |
class WPML_Ubermenu { | |
const CONTEXT = 'Ubermenu items'; | |
private $translatable_properties = array( | |
'custom_content', | |
'submenu_footer_content', | |
'empty_results_message', | |
'icon_title' | |
); | |
public function apply_filters() { | |
$this->translatable_properties = apply_filters( | |
'wpml_ubermenu_translatable_properties', | |
$this->translatable_properties | |
); | |
} | |
public function add_hooks() { | |
add_action( 'ubermenu_after_menu_item_save', array( $this, 'after_menu_item_save_action' ) ); | |
if ( ! is_admin() ) { | |
add_filter( 'ubermenu_item_settings', array( $this, 'item_settings_filter' ), 10, 2 ); | |
} | |
} | |
public function menu_sync_action( $data, ICLMenusSync $menu_sync ) { | |
foreach ( $menu_sync->menus as $menu_id => $menu ) { | |
foreach ( $menu['items'] as $menu_item_id => $menu_item ) { | |
$custom_item_type = get_post_meta( $menu_item_id, '_ubermenu_custom_item_type', true ); | |
$settings = get_post_meta( $menu_item_id, '_ubermenu_settings', true ); | |
$this->register_strings( $settings, $menu_item_id ); | |
foreach ( $menu_item['translations'] as $language_code => $menu_item_translation ) { | |
$this->sync_meta( $menu_item_translation['ID'], $custom_item_type, $settings ); | |
} | |
} | |
} | |
} | |
public function after_menu_item_save_action( $menu_item_id ) { | |
$translations = $this->get_translations( $menu_item_id ); | |
$original_id = $this->get_original( $translations ); | |
// Allow only original items from original menus | |
if ( $original_id == $menu_item_id && $this->in_original_menu( $original_id ) ) { | |
$custom_item_type = get_post_meta( $menu_item_id, '_ubermenu_custom_item_type', true ); | |
$settings = get_post_meta( $menu_item_id, '_ubermenu_settings', true ); | |
$this->register_strings( $settings, $original_id ); | |
foreach ( $translations as $language_code => $translation ) { | |
if ( $translation->element_id != $menu_item_id ) { | |
$this->sync_meta( $translation->element_id, $custom_item_type, $settings ); | |
} | |
} | |
} | |
} | |
private function get_translations( $menu_item_id ) { | |
$element_type = apply_filters( 'wpml_element_type', 'nav_menu_item' ); | |
$trid = apply_filters( 'wpml_element_trid', null, $menu_item_id, $element_type ); | |
$translations = apply_filters( 'wpml_get_element_translations', array(), $trid, $element_type ); | |
return $translations; | |
} | |
private function get_original( $translations ) { | |
$to_array = function ( $value ) { | |
return (array) $value; | |
}; | |
$original_id = array_search( '1', | |
array_column( | |
array_map( $to_array, $translations ), | |
'original', | |
'element_id' ) | |
); | |
return $original_id; | |
} | |
private function in_original_menu( $menu_item_id ) { | |
$current_language = apply_filters( 'wpml_current_language', null ); | |
$menu_item_language = apply_filters( 'wpml_element_language_code', null, | |
array( | |
'element_id' => $menu_item_id, | |
'element_type' => 'nav_menu_item' | |
) | |
); | |
do_action( 'wpml_switch_language', $menu_item_language ); | |
$terms = wp_get_object_terms( $menu_item_id, 'nav_menu' ); | |
$menu = reset( $terms ); | |
$element_type = apply_filters( 'wpml_element_type', 'nav_menu' ); | |
$trid = apply_filters( 'wpml_element_trid', null, $menu->term_id, $element_type ); | |
$translations = apply_filters( 'wpml_get_element_translations', array(), $trid, $element_type ); | |
$original_menu_id = $this->get_original( $translations ); | |
$is_original = $menu->term_id == $original_menu_id; | |
do_action( 'wpml_switch_language', $current_language ); | |
return $is_original; | |
} | |
private function sync_meta( $menu_item_id, $custom_item_type, $settings ) { | |
if ( $custom_item_type ) { | |
update_post_meta( | |
$menu_item_id, | |
'_ubermenu_custom_item_type', | |
$custom_item_type | |
); | |
} | |
if ( is_array( $settings ) ) { | |
$merged_settings = $settings; | |
$localized_settings = get_post_meta( | |
$menu_item_id, | |
UBERMENU_MENU_ITEM_META_KEY, | |
true | |
); | |
if ( is_array( $localized_settings ) ) { | |
foreach ( $this->translatable_properties as $property ) { | |
if ( ! empty( $localized_settings[ $property ] ) ) { | |
$merged_settings[ $property ] = $localized_settings[ $property ]; | |
} | |
} | |
} | |
update_post_meta( | |
$menu_item_id, | |
UBERMENU_MENU_ITEM_META_KEY, | |
$merged_settings | |
); | |
} | |
} | |
private function register_strings( $settings, $menu_item_id ) { | |
if ( WPML_UBERMENU_ENABLE_STRING_TRANSLATION ) { | |
foreach ( $this->translatable_properties as $property ) { | |
if ( ! empty( $settings[ $property ] ) ) { | |
do_action( | |
'wpml_register_single_string', | |
self::CONTEXT, | |
"{$property} {$menu_item_id}", | |
$settings[ $property ] | |
); | |
} | |
} | |
} | |
} | |
public function item_settings_filter( $settings, $menu_item_id ) { | |
if ( WPML_UBERMENU_ENABLE_STRING_TRANSLATION ) { | |
$translations = $this->get_translations( $menu_item_id ); | |
$original_id = $this->get_original( $translations ); | |
// Allow only original items from original menus | |
if ( $original_id && $this->in_original_menu( $original_id ) ) { | |
foreach ( $this->translatable_properties as $property ) { | |
if ( ! empty( $settings[ $property ] ) ) { | |
$settings[ $property ] = apply_filters( | |
'wpml_translate_single_string', | |
$settings[ $property ], | |
self::CONTEXT, | |
"{$property} {$original_id}" | |
); | |
} | |
} | |
} | |
} | |
return $settings; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment