function post_type_link_filter($post_link, $post, $leavename, $sample)
 {
     if (!$this->sitepress->is_translated_post_type($post->post_type) || !($ld = $this->sitepress->get_element_language_details($post->ID, 'post_' . $post->post_type))) {
         return $post_link;
     }
     if (isset($this->post_link_cache[$post->ID][$leavename . '#' . $sample])) {
         $post_link = $this->post_link_cache[$post->ID][$leavename . '#' . $sample];
     } else {
         $st_settings = $this->sitepress->get_setting('st');
         $strings_language = !empty($st_settings['strings_language']) ? $st_settings['strings_language'] : 'en';
         // fix permalink when object is not in the current language
         if ($ld->language_code != $strings_language) {
             $slug_settings = $this->sitepress->get_setting('posts_slug_translation');
             $slug_settings = !empty($slug_settings['types'][$post->post_type]) ? $slug_settings['types'][$post->post_type] : null;
             if ((bool) $slug_settings === true) {
                 $slug_this = $this->get_slug_by_type($post->post_type);
                 $slug_real = $this->get_translated_slug($slug_this, $ld->language_code);
                 if (empty($slug_real)) {
                     return $post_link;
                 }
                 global $wp_rewrite;
                 if (isset($wp_rewrite->extra_permastructs[$post->post_type])) {
                     $struct_original = $wp_rewrite->extra_permastructs[$post->post_type]['struct'];
                     $lslash = false !== strpos($struct_original, '/' . $slug_this) ? '/' : '';
                     $wp_rewrite->extra_permastructs[$post->post_type]['struct'] = preg_replace('@' . $lslash . $slug_this . '/@', $lslash . $slug_real . '/', $struct_original);
                     remove_filter('post_type_link', array($this, 'post_type_link_filter'), 1);
                     // high priority
                     $post_link = get_post_permalink($post->ID, $leavename, $sample);
                     add_filter('post_type_link', array($this, 'post_type_link_filter'), 1, 4);
                     // high priority
                     $wp_rewrite->extra_permastructs[$post->post_type]['struct'] = $struct_original;
                 } else {
                     $post_link = str_replace($slug_this . '=', $slug_real . '=', $post_link);
                 }
             }
             $this->post_link_cache[$post->ID][$leavename . '#' . $sample] = $post_link;
         }
     }
     return $post_link;
 }
Exemplo n.º 2
0
 function post_type_link_filter($post_link, $post, $leavename, $sample)
 {
     if ($this->ignore_post_type_link) {
         return $post_link;
     }
     if (!$this->sitepress->is_translated_post_type($post->post_type) || !($ld = $this->sitepress->get_element_language_details($post->ID, 'post_' . $post->post_type))) {
         return $post_link;
     }
     if (isset($this->post_link_cache[$post->ID][$leavename . '#' . $sample])) {
         $post_link = $this->post_link_cache[$post->ID][$leavename . '#' . $sample];
     } else {
         $slug_settings = $this->sitepress->get_setting('posts_slug_translation');
         $slug_settings = !empty($slug_settings['types'][$post->post_type]) ? $slug_settings['types'][$post->post_type] : null;
         if ((bool) $slug_settings === true) {
             $post_type_obj = get_post_type_object($post->post_type);
             $slug_this = isset($post_type_obj->rewrite['slug']) ? trim($post_type_obj->rewrite['slug'], '/') : false;
             $slug_real = $this->get_translated_slug($slug_this, $post->post_type, $ld->language_code);
             if (empty($slug_real) || empty($slug_this) || $slug_this == $slug_real) {
                 return $post_link;
             }
             global $wp_rewrite;
             if (isset($wp_rewrite->extra_permastructs[$post->post_type])) {
                 $struct_original = $wp_rewrite->extra_permastructs[$post->post_type]['struct'];
                 $lslash = false !== strpos($struct_original, '/' . $slug_this) ? '/' : '';
                 $wp_rewrite->extra_permastructs[$post->post_type]['struct'] = preg_replace('@' . $lslash . $slug_this . '/@', $lslash . $slug_real . '/', $struct_original);
                 $this->ignore_post_type_link = true;
                 $post_link = get_post_permalink($post->ID, $leavename, $sample);
                 $this->ignore_post_type_link = false;
                 $wp_rewrite->extra_permastructs[$post->post_type]['struct'] = $struct_original;
             } else {
                 $post_link = str_replace($slug_this . '=', $slug_real . '=', $post_link);
             }
         }
         $this->post_link_cache[$post->ID][$leavename . '#' . $sample] = $post_link;
     }
     return $post_link;
 }