Skip to content

Commit 3be5a7b

Browse files
committed
Fix Polylang incompatibility with multisite fields
1 parent 005f265 commit 3be5a7b

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

7+
### Fixed
8+
- Fix Polylang incompatibility with multisite fields.
9+
710
## [1.37.0] - 2021-11-30
811

912
### Changed

src/Fields/MultisitePostObject.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,22 @@ public function get_post_title( $post, $field, $post_id = 0, $is_search = 0 ) {
116116
*/
117117
public function render_field( $field ) {
118118
\switch_to_blog( $field['blog_id'] );
119-
parent::render_field( $field );
119+
120+
// If Polylang is active, we need to switch the global curlang manually
121+
// as switch_to_blog does not do that in admin views.
122+
// If this is not done, the field may not find its saved value if the taxonomy term IDs
123+
// do not match between all sites.
124+
if ( function_exists( 'pll_default_language' ) ) {
125+
$restore_curlang = \PLL()->curlang;
126+
$lang = \PLL()->model->get_language( $restore_curlang->slug );
127+
\PLL()->curlang = $lang ?: \PLL()->model->get_language( \pll_default_language() );
128+
parent::render_field( $field );
129+
\PLL()->curlang = $restore_curlang;
130+
}
131+
else {
132+
parent::render_field( $field );
133+
}
134+
120135
\restore_current_blog();
121136
}
122137

src/Fields/MultisiteRelationship.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,20 @@ public function get_post_title( $post, $field, $post_id = 0, $is_search = 0 ) {
124124
public function render_field( $field ) {
125125
\switch_to_blog( $field['blog_id'] );
126126

127-
parent::render_field( $field );
127+
// If Polylang is active, we need to switch the global curlang manually
128+
// as switch_to_blog does not do that in admin views.
129+
// If this is not done, the field may not find its saved value if the taxonomy term IDs
130+
// do not match between both sites.
131+
if ( function_exists( 'pll_default_language' ) ) {
132+
$restore_curlang = \PLL()->curlang;
133+
$lang = \PLL()->model->get_language( $restore_curlang->slug );
134+
\PLL()->curlang = $lang ?: \PLL()->model->get_language( \pll_default_language() );
135+
parent::render_field( $field );
136+
\PLL()->curlang = $restore_curlang;
137+
}
138+
else {
139+
parent::render_field( $field );
140+
}
128141

129142
\restore_current_blog();
130143
}

0 commit comments

Comments
 (0)