From 4d73cf1e5edaaff756919e4168d08b6ac03e611f Mon Sep 17 00:00:00 2001 From: Jeremy Dormitzer Date: Sun, 11 Nov 2018 08:58:32 -0500 Subject: [PATCH] Refactor upsert_object function --- includes/server/objects.php | 46 +++++++++++-------------------------- 1 file changed, 13 insertions(+), 33 deletions(-) diff --git a/includes/server/objects.php b/includes/server/objects.php index 1d51d42..69cfd5b 100644 --- a/includes/server/objects.php +++ b/includes/server/objects.php @@ -85,46 +85,26 @@ function upsert_object( $object ) { if ( array_key_exists( 'url', $object ) ) { $object_url = $object['url']; } - $row = $wpdb->get_row( $wpdb->prepare( - "SELECT * FROM {$wpdb->prefix}pterotype_objects WHERE activitypub_id = %s", - $object['id'] + $res = $wpdb->query( $wpdb->prepare( + " + INSERT INTO {$wpdb->prefix}pterotype_objects (activitypub_id, type, object, url) + VALUES (%s, %s, %s, %s) ON DUPLICATE KEY UPDATE + id=LAST_INSERT_ID(id), + activitypub_id=VALUES(activitypub_id) + type=VALUES(type), + object=VALUES(object), + url=VALUES(url); + ", + $object['id'], $object['type'], wp_json_encode( $object ), $object_url ) ); - $res = true; - if ( $row === null ) { - $res = $wpdb->insert( - $wpdb->prefix . 'pterotype_objects', - array( - 'activitypub_id' => $object['id'], - 'type' => $object['type'], - 'object' => wp_json_encode( $object ), - 'url' => $object_url - ), - '%s' - ); - $row = new \stdClass(); - } else { - $res = $wpdb->update( - $wpdb->prefix . 'pterotype_objects', - array( - 'activitypub_id' => $object['id'], - 'type' => $object['type'], - 'object' => wp_json_encode( $object ), - 'url' => $object_url - ), - array( 'id' => $row->id ), - '%s', - '%d' - ); - $id = $row->id; - $row = new \stdClass(); - $row->id = $id; - } if ( $res === false ) { return new \WP_Error( 'db_error', __( 'Failed to upsert object row', 'pterotype' ) ); } + $row = new \stdClass(); $row->object = $object; + $row->id = $wpdb->insert_id; return $row; }