wpdb->replace( "{$this->wpdb->prefix}senti_relationships", [ 'source_id' => $source_id, 'target_id' => $target_id, 'relationship' => $relationship, 'position' => $position, 'created_gmt' => current_time( 'mysql', true ), ], [ '%d', '%d', '%s', '%d', '%s' ] ); return $result !== false; } /** * Removes all relationships matching the given target ID and relationship type. */ public function delete_by_target( int $target_id, string $relationship ): bool { $result = $this->wpdb->delete( "{$this->wpdb->prefix}senti_relationships", [ 'target_id' => $target_id, 'relationship' => $relationship, ], [ '%d', '%s' ] ); return $result !== false; } /** * Finds all source IDs linked to a specific target ID for a relationship type. * * @return int[] */ public function find_source_ids( int $target_id, string $relationship, string $source_post_type ): array { $query = $this->wpdb->prepare( 'SELECT relationship.source_id FROM %i relationship INNER JOIN %i post ON post.ID = relationship.source_id WHERE relationship.target_id = %d AND relationship.relationship = %s AND post.post_type = %s ORDER BY relationship.position, relationship.source_id', $this->wpdb->prefix . 'senti_relationships', $this->wpdb->posts, $target_id, $relationship, $source_post_type ); if ( ! is_string( $query ) ) { return []; } // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- $query is prepared immediately above. return array_map( 'intval', $this->wpdb->get_col( $query ) ); } /** * Finds all target IDs linked to a specific source ID for a relationship type. * * @return int[] */ public function find_target_ids( int $source_id, string $relationship, string $target_post_type ): array { $query = $this->wpdb->prepare( 'SELECT relationship.target_id FROM %i relationship INNER JOIN %i post ON post.ID = relationship.target_id WHERE relationship.source_id = %d AND relationship.relationship = %s AND post.post_type = %s ORDER BY relationship.position, relationship.target_id', $this->wpdb->prefix . 'senti_relationships', $this->wpdb->posts, $source_id, $relationship, $target_post_type ); if ( ! is_string( $query ) ) { return []; } // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- $query is prepared immediately above. return array_map( 'intval', $this->wpdb->get_col( $query ) ); } }