Skip to content

Commit e21258d

Browse files
committed
Merge pull request #268 from Automattic/object-cache-translation-ids-236
Object cache just the post's translations IDs rather than objects
2 parents 072adb2 + 2bb6567 commit e21258d

File tree

2 files changed

+52
-23
lines changed

2 files changed

+52
-23
lines changed

class-post-public.php

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ public function the_posts( array $posts, WP_Query & $wp_query ) {
541541
$subs_index = array();
542542
foreach ( $posts as & $post ) {
543543
if ( empty( $post->post_title ) || empty( $post->post_excerpt ) || empty( $post->post_content ) ) {
544-
if ( $default_post = bbl_get_default_lang_post( $post->ID ) )
544+
if ( $default_post = $this->get_default_lang_post( $post->ID ) )
545545
$subs_index[ $post->ID ] = $default_post->ID;
546546
}
547547
if ( ! $this->get_transid( $post ) && bbl_get_default_lang_code() == bbl_get_post_lang_code( $post ) )
@@ -780,6 +780,7 @@ function clean_post_cache( $post_id ) {
780780
return;
781781
}
782782
wp_cache_delete( $transid, 'bbl_post_translations' );
783+
wp_cache_delete( $transid, 'bbl_post_translation_ids' );
783784
}
784785

785786
/**
@@ -1186,7 +1187,7 @@ public function get_new_post_translation_url( $default_post, $lang_code ) {
11861187
**/
11871188
public function get_default_lang_post( $post ) {
11881189
$post = get_post( $post );
1189-
$translations = bbl_get_post_translations( $post->ID );
1190+
$translations = $this->get_post_translations( $post->ID );
11901191
if ( isset( $translations[ bbl_get_default_lang_code() ] ) )
11911192
return $translations[ bbl_get_default_lang_code() ];
11921193
return false;
@@ -1205,8 +1206,10 @@ public function get_post_translations( $post ) {
12051206
// @FIXME: Is it worth caching here, or can we just rely on the caching in get_objects_in_term and get_posts?
12061207
$transid = $this->get_transid( $post );
12071208

1208-
if ( $translations = wp_cache_get( $transid, 'bbl_post_translations' ) ) {
1209-
return $translations;
1209+
$translations = wp_cache_get( $transid, 'bbl_post_translation_ids' );
1210+
1211+
if ( false !== $translations ) {
1212+
return array_map( 'get_post', $translations );
12101213
}
12111214

12121215
# @TODO A transid should never be a wp_error. Check and fix.
@@ -1215,28 +1218,14 @@ public function get_post_translations( $post ) {
12151218
}
12161219
$post_ids = get_objects_in_term( $transid, 'post_translation' );
12171220

1218-
// Work out all the translated equivalent post types
1219-
$post_types = array();
1220-
$langs = bbl_get_active_langs();
1221-
foreach ( $langs as $lang )
1222-
$post_types[] = bbl_get_post_type_in_lang( $post->post_type, $lang->code );
1223-
1224-
// Get all the translations in one cached DB query
1225-
$args = array(
1226-
// We want a clean listing, without any particular language
1227-
'bbl_translate' => false,
1228-
'include' => $post_ids,
1229-
'post_type' => $post_types,
1230-
'post_status' => array( 'publish', 'pending', 'draft', 'future' ),
1231-
);
1232-
$posts = get_posts( $args );
12331221
$translations = array();
1234-
foreach ( $posts as $post )
1235-
$translations[ $this->get_post_lang_code( $post ) ] = $post;
1222+
$post_ids = array_filter( $post_ids );
1223+
foreach ( $post_ids as $post_id )
1224+
$translations[ $this->get_post_lang_code( $post_id ) ] = $post_id;
12361225

1237-
wp_cache_set( $transid, $translations, 'bbl_post_translations' );
1226+
wp_cache_set( $transid, $translations, 'bbl_post_translation_ids' );
12381227

1239-
return $translations;
1228+
return array_map( 'get_post', $translations );
12401229
}
12411230

12421231
/**
@@ -1515,6 +1504,7 @@ function set_transid( $post, $transid = false ) {
15151504
}
15161505
// Delete anything in there currently
15171506
wp_cache_delete( $transid, 'bbl_post_translations' );
1507+
wp_cache_delete( $transid, 'bbl_post_translation_ids' );
15181508
}
15191509
$result = wp_set_object_terms( $post->ID, $transid, 'post_translation' );
15201510
if ( is_wp_error( $result ) ) {

tests/test-translations.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
class Test_Translations extends Babble_UnitTestCase {
4+
5+
public function setUp() {
6+
$this->install_languages();
7+
8+
parent::setUp();
9+
}
10+
11+
public function test_post_translations() {
12+
13+
$this->assertSame( 'en_US', get_locale() );
14+
15+
$en = $this->factory->post->create_and_get();
16+
$uk = $this->create_post_translation( $en, 'en_GB' );
17+
$fr = $this->create_post_translation( $en, 'fr_FR' );
18+
19+
// Ensure translations are correctly fetched
20+
$translations = bbl_get_post_translations( $en->ID );
21+
22+
$this->assertEquals( array(
23+
'en_US' => get_post( $en->ID ),
24+
'en_GB' => get_post( $uk->ID ),
25+
'fr_FR' => get_post( $fr->ID ),
26+
), $translations );
27+
28+
// Test it again to ensure translation caching is correct
29+
$translations = bbl_get_post_translations( $en->ID );
30+
31+
$this->assertEquals( array(
32+
'en_US' => get_post( $en->ID ),
33+
'en_GB' => get_post( $uk->ID ),
34+
'fr_FR' => get_post( $fr->ID ),
35+
), $translations );
36+
37+
}
38+
39+
}

0 commit comments

Comments
 (0)