Skip to content

Commit

Permalink
Pass more reference values to Pick save() method to avoid extra looku…
Browse files Browse the repository at this point in the history
…ps or lookups after delete has already occurred
  • Loading branch information
sc0ttkclark committed Sep 21, 2023
1 parent b904aba commit e77ce66
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
8 changes: 7 additions & 1 deletion classes/PodsAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -5750,8 +5750,14 @@ public function save_pod_item( $params ) {
$this->save_relationships( $params->id, $value_ids, $pod, $field_data );
}

$pick_save_params = $params;

$pick_save_params->current_ids = $related_ids;
$pick_save_params->value_ids = $value_ids;
$pick_save_params->remove_ids = $remove_ids;

// Run save function for field type (where needed).
PodsForm::save( $type, $field_save_values, $params->id, $field_name, $field_data, $all_fields, $pod, $params );
PodsForm::save( $type, $field_save_values, $params->id, $field_name, $field_data, $all_fields, $pod, $pick_save_params );
}

// Unset data no longer needed
Expand Down
27 changes: 22 additions & 5 deletions classes/fields/pick.php
Original file line number Diff line number Diff line change
Expand Up @@ -1885,16 +1885,28 @@ public function save( $value, $id = null, $name = null, $options = null, $fields
];
}

$value_ids = array_unique( array_filter( $value ) );
$current_ids = isset( $params->current_ids ) ? $params->current_ids : null;
$value_ids = isset( $params->value_ids ) ? $params->value_ids : null;
$remove_ids = isset( $params->remove_ids ) ? $params->remove_ids : null;

if ( null === $value_ids ) {
$value_ids = array_unique( array_filter( $value ) );
}

$related_data = pods_static_cache_get( $options['name'] . '/' . $options['id'], __CLASS__ . '/related_data' ) ?: [];

if ( ! empty( $related_data ) && isset( $related_data['current_ids_' . $id ], $related_data['remove_ids_' . $id ] ) ) {
$related_pod = $related_data['related_pod'];
$related_field = $related_data['related_field'];
$related_pick_limit = $related_data['related_pick_limit'];
$current_ids = $related_data[ 'current_ids_' . $id ];
$remove_ids = $related_data[ 'remove_ids_' . $id ];

if ( null === $current_ids ) {
$current_ids = $related_data[ 'current_ids_' . $id ];
}

if ( null === $remove_ids ) {
$remove_ids = $related_data[ 'remove_ids_' . $id ];
}
} elseif ( $options instanceof Field || $options instanceof Value_Field ) {
$related_field = $options->get_bidirectional_field();

Expand All @@ -1904,10 +1916,15 @@ public function save( $value, $id = null, $name = null, $options = null, $fields

$related_pod = $related_field->get_parent_object();
$related_pick_limit = $related_field->get_limit();
$current_ids = self::$api->lookup_related_items( $options['id'], $pod['id'], $id, $options, $pod );

if ( null === $current_ids ) {
$current_ids = self::$api->lookup_related_items( $options['id'], $pod['id'], $id, $options, $pod );
}

// Get ids to remove.
$remove_ids = array_diff( $current_ids, $value_ids );
if ( null === $remove_ids ) {
$remove_ids = array_diff( $current_ids, $value_ids );
}
}

if ( empty( $related_field ) || empty( $related_pod ) ) {
Expand Down

0 comments on commit e77ce66

Please sign in to comment.