| @@ -118,11 +118,20 @@ | ||
| 118 | 118 | // `<div class="wpdberror">…</div>` to the REST response body and |
| 119 | 119 | // break `await response.json()` on the client. `$wpdb->last_error` |
| 120 | 120 | // still holds the message, so genuine DB failures surface via the |
| 121 | 121 | // `WP_Error` we return when no existing row is found. |
| 122 | - $prev_suppress = $wpdb->suppress_errors( true ); | |
| 123 | - $ok = $wpdb->insert( $tables['placements'], $row, array( '%d', '%d', '%d', '%s', '%s', '%d', '%d', '%d', '%d', '%s' ) ); | |
| 124 | - $wpdb->suppress_errors( $prev_suppress ); | |
| 122 | + $insert = static function () use ( $tables, $row ) { | |
| 123 | + global $wpdb; | |
| 124 | + $prev_suppress = $wpdb->suppress_errors( true ); | |
| 125 | + $ok = $wpdb->insert( $tables['placements'], $row, array( '%d', '%d', '%d', '%s', '%s', '%d', '%d', '%d', '%d', '%s' ) ); | |
| 126 | + $wpdb->suppress_errors( $prev_suppress ); | |
| 127 | + return array( $ok, (int) $wpdb->insert_id ); | |
| 128 | + }; | |
| 129 | + $result = 'upload' === $type ? openstation_stored_files_place_insert( $ref, $insert ) : $insert(); | |
| 130 | + if ( is_wp_error( $result ) ) { | |
| 131 | + return $result; | |
| 132 | + } | |
| 133 | + list( $ok, $id ) = $result; | |
| 125 | 134 | if ( false === $ok ) { |
| 126 | 135 | // Disambiguate the two cases hidden behind a generic `false`: |
| 127 | 136 | // (a) The `placement_unique` index collided |
| 128 | 137 | // with an existing row for this (user, parent, type, |
| @@ -187,10 +196,8 @@ | ||
| 187 | 196 | } |
| 188 | 197 | |
| 189 | 198 | return $existing_id; |
| 190 | 199 | } |
| 191 | - $id = (int) $wpdb->insert_id; | |
| 192 | - | |
| 193 | 200 | $row['id'] = $id; |
| 194 | 201 | |
| 195 | 202 | /** |
| 196 | 203 | * Fires after a placement is created. |