current_language = $current_language; $this->element_factory = $element_factory; } public function add_hooks() { add_filter( 'content_edit_pre', array( $this, 'translate_global_element_ids' ), self::BEFORE_ADD_GLOBAL_ELEMENTS_PRIORITY ); } public function translate_global_element_ids( $content ) { $pattern = '/' . preg_quote( self::GLOBAL_SHORTCODE_START, '[' ) . '([\d]+)"\]/'; return preg_replace_callback( $pattern, array( $this, 'replace_global_id' ), $content ); } private function replace_global_id( array $matches ) { $global_id = (int) $matches[1]; $element = $this->element_factory->create( $global_id, 'post' ); $translation = $element->get_translation( $this->current_language->get_current_language() ); if ( $translation ) { $global_id = $translation->get_element_id(); } return self::GLOBAL_SHORTCODE_START . $global_id . '"]'; } }