Skip to content

Commit ab77c9a

Browse files
committed
Enhance location grid meta deletion logic
- Updated the delete_location_grid_meta method to include an optional parameter for location_meta_field_settings, allowing for better handling of shared dt rows. - Improved the logic to unlink fields and delete metadata only when necessary, enhancing performance and preventing unintended data loss. - Adjusted calls to delete_location_grid_meta throughout the codebase to accommodate the new parameter, ensuring consistent behavior across the application.
1 parent 279375b commit ab77c9a

2 files changed

Lines changed: 28 additions & 13 deletions

File tree

dt-mapping/location-grid-meta.php

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,9 @@ public static function add_location_grid_meta( $post_id, array $location_grid_me
215215
* @param int $value
216216
* @param array|null $existing_post
217217
* @param string $location_meta_field_key DT field key for postmeta pointers (default location_grid_meta).
218+
* @param array|null $location_meta_field_settings Field definitions for the post type; when set, shared dt rows are not removed while another location_meta field still references the grid_meta_id.
218219
*/
219-
public static function delete_location_grid_meta( int $post_id, $type, int $value, ?array $existing_post = null, string $location_meta_field_key = 'location_grid_meta' ) {
220+
public static function delete_location_grid_meta( int $post_id, $type, int $value, ?array $existing_post = null, string $location_meta_field_key = 'location_grid_meta', ?array $location_meta_field_settings = null ) {
220221
global $wpdb;
221222

222223
$status = false;
@@ -230,17 +231,31 @@ public static function delete_location_grid_meta( int $post_id, $type, int $valu
230231

231232
switch ( $type ) {
232233
case 'grid_meta_id':
233-
$postmeta_id_location_grid = $wpdb->get_var( $wpdb->prepare( "SELECT postmeta_id_location_grid FROM $wpdb->dt_location_grid_meta WHERE grid_meta_id = %d", $value ) );
234+
$grid_meta_id = (int) $value;
235+
if ( $grid_meta_id < 1 ) {
236+
break;
237+
}
234238

235-
delete_metadata_by_mid( 'post', $postmeta_id_location_grid );
236-
$wpdb->delete($wpdb->dt_location_grid_meta, [
237-
'post_id' => $post_id,
238-
'grid_meta_id' => $value
239-
]);
240-
delete_post_meta( $post_id, 'location_grid_meta', $value );
241-
if ( $location_meta_field_key !== 'location_grid_meta' ) {
242-
delete_post_meta( $post_id, $location_meta_field_key, $value );
239+
// Unlink this field only first; multiple location_meta fields can share one dt row (same lat/lng).
240+
delete_post_meta( $post_id, $location_meta_field_key, $grid_meta_id );
241+
242+
if ( is_array( $location_meta_field_settings )
243+
&& Disciple_Tools_Posts::post_references_grid_meta_id( $post_id, $grid_meta_id, $location_meta_field_settings ) ) {
244+
$status = true;
245+
break;
243246
}
247+
248+
$postmeta_id_location_grid = $wpdb->get_var( $wpdb->prepare( "SELECT postmeta_id_location_grid FROM $wpdb->dt_location_grid_meta WHERE grid_meta_id = %d", $grid_meta_id ) );
249+
250+
delete_metadata_by_mid( 'post', $postmeta_id_location_grid );
251+
$wpdb->delete(
252+
$wpdb->dt_location_grid_meta,
253+
[
254+
'post_id' => $post_id,
255+
'grid_meta_id' => $grid_meta_id,
256+
]
257+
);
258+
delete_post_meta( $post_id, 'location_grid_meta', $grid_meta_id );
244259
$status = true;
245260
break;
246261

dt-posts/posts.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1870,7 +1870,7 @@ public static function update_multi_select_fields( array $field_settings, int $p
18701870
/**
18711871
* Whether a grid_meta_id is still referenced by any location_meta field postmeta on this post.
18721872
*/
1873-
private static function post_references_grid_meta_id( int $post_id, int $grid_meta_id, array $field_settings ): bool {
1873+
public static function post_references_grid_meta_id( int $post_id, int $grid_meta_id, array $field_settings ): bool {
18741874
foreach ( $field_settings as $meta_key => $def ) {
18751875
if ( ( $def['type'] ?? '' ) !== 'location_meta' ) {
18761876
continue;
@@ -1955,7 +1955,7 @@ public static function update_location_grid_fields( array $field_settings, int $
19551955
continue;
19561956
}
19571957
if ( ! self::post_references_grid_meta_id( $post_id, $candidate_id, $field_settings ) ) {
1958-
Location_Grid_Meta::delete_location_grid_meta( $post_id, 'grid_meta_id', $candidate_id, $existing_post, $field_key );
1958+
Location_Grid_Meta::delete_location_grid_meta( $post_id, 'grid_meta_id', $candidate_id, $existing_post, $field_key, $field_settings );
19591959
}
19601960
}
19611961
$existing_post[ $field_key ] = [];
@@ -1966,7 +1966,7 @@ public static function update_location_grid_fields( array $field_settings, int $
19661966

19671967
// delete
19681968
if ( isset( $value['delete'] ) && $value['delete'] == true ) {
1969-
Location_Grid_Meta::delete_location_grid_meta( $post_id, 'grid_meta_id', $value['grid_meta_id'], $existing_post, $field_key );
1969+
Location_Grid_Meta::delete_location_grid_meta( $post_id, 'grid_meta_id', $value['grid_meta_id'], $existing_post, $field_key, $field_settings );
19701970
}
19711971

19721972
// Add by pre-defined meta grid values.

0 commit comments

Comments
 (0)