Skip to content

Commit

Permalink
fix: improve cache keys
Browse files Browse the repository at this point in the history
- this avoids having groups with the same field names from clashing
- in the case where you query via `wp-graphql-acf` and 
  you have multiple field groups with repeaters that resolve to the 
  same field name, then without this patch the first field group
  will load the field value and store it inside the cache
  with the wrong sub-field ids.
- for more info see 
   wp-graphql/wp-graphql-acf#170
  • Loading branch information
rburgst authored Nov 2, 2020
1 parent 70b15d5 commit c481289
Showing 1 changed file with 36 additions and 12 deletions.
48 changes: 36 additions & 12 deletions includes/acf-value-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,17 @@ function acf_get_value( $post_id = 0, $field ) {

// Get field name.
$field_name = $field['name'];

// RBA 20201029
$field_key = $field['key'];

// Check store.
$store = acf_get_store( 'values' );
if( $store->has( "$post_id:$field_name" ) ) {
return $store->get( "$post_id:$field_name" );
// RBA 20201029
// if( $store->has( "$post_id:$field_name" ) ) {
// return $store->get( "$post_id:$field_name" );
// }
if( $store->has( "$post_id:$field_name:$field_key" ) ) {
return $store->get( "$post_id:$field_name:$field_key" );
}

// Load value from database.
Expand All @@ -89,7 +95,9 @@ function acf_get_value( $post_id = 0, $field ) {
$value = apply_filters( "acf/load_value", $value, $post_id, $field );

// Update store.
$store->set( "$post_id:$field_name", $value );
// RBA 20201029
// $store->set( "$post_id:$field_name", $value );
$store->set( "$post_id:$field_name:$field_key", $value );

// Return value.
return $value;
Expand Down Expand Up @@ -121,11 +129,17 @@ function acf_format_value( $value, $post_id, $field ) {

// Get field name.
$field_name = $field['name'];

// RBA 20201029
$field_key = $field['key'];

// Check store.
$store = acf_get_store( 'values' );
if( $store->has( "$post_id:$field_name:formatted" ) ) {
return $store->get( "$post_id:$field_name:formatted" );
// RBA 20201029
// if( $store->has( "$post_id:$field_name:formatted" ) ) {
// return $store->get( "$post_id:$field_name:formatted" );
// }
if( $store->has( "$post_id:$field_name:$field_key:formatted" ) ) {
return $store->get( "$post_id:$field_name:$field_key:formatted" );
}

/**
Expand All @@ -141,7 +155,9 @@ function acf_format_value( $value, $post_id, $field ) {
$value = apply_filters( "acf/format_value", $value, $post_id, $field );

// Update store.
$store->set( "$post_id:$field_name:formatted", $value );
// RBA 20201029
// $store->set( "$post_id:$field_name:formatted", $value );
$store->set( "$post_id:$field_name:$field_key:formatted", $value );

// Return value.
return $value;
Expand Down Expand Up @@ -196,7 +212,9 @@ function acf_update_value( $value = null, $post_id = 0, $field ) {
acf_update_metadata( $post_id, $field['name'], $field['key'], true );

// Delete stored data.
acf_flush_value_cache( $post_id, $field['name'] );
// acf_flush_value_cache( $post_id, $field['name'] );
// RBA 20201029
acf_flush_value_cache( $post_id, $field['name'], $field['key'] );

// Return update status.
return $return;
Expand Down Expand Up @@ -244,12 +262,16 @@ function acf_update_values( $values = array(), $post_id = 0 ) {
* @param string $field_name The field name.
* @return void
*/
function acf_flush_value_cache( $post_id = 0, $field_name = '' ) {
function acf_flush_value_cache( $post_id = 0, $field_name = '', $field_key = '' ) {

// Delete stored data.
acf_get_store( 'values' )
->remove( "$post_id:$field_name" )
->remove( "$post_id:$field_name:formatted" );
->remove( "$post_id:$field_name:formatted" )
// RBA 20201029
->remove( "$post_id:$field_name:$field_key" )
->remove( "$post_id:$field_name:$field_key:formatted" )
;
}

/**
Expand Down Expand Up @@ -285,7 +307,9 @@ function acf_delete_value( $post_id, $field ) {
acf_delete_metadata( $post_id, $field['name'], true );

// Delete stored data.
acf_flush_value_cache( $post_id, $field['name'] );
// acf_flush_value_cache( $post_id, $field['name'] );
// RBA 20201029
acf_flush_value_cache( $post_id, $field['name'], $field['key'] );

// Return delete status.
return $return;
Expand Down

0 comments on commit c481289

Please sign in to comment.