/** * Delete a record and its associated change-tracker records. * @param string $pk_value The value of the primary key of the record to delete. * @return void * @throws Exception When the user doesn't have permission, or any error occurs deleting the record. */ public function delete_record($pk_value) { // Check permission. if (!Grants::current_user_can(Grants::DELETE, $this->get_name())) { throw new Exception('You do not have permission to delete data from this table.'); } $rec = $this->get_record($pk_value); $wpdb = $this->database->get_wpdb(); $wpdb->hide_errors(); $del = $wpdb->delete($this->get_name(), array($this->get_pk_column()->get_name() => $pk_value)); if (!$del) { throw new Exception($wpdb->last_error); } foreach ($rec->get_changes() as $change) { $where_1 = array('table_name' => $this->get_name(), 'record_ident' => $pk_value); $wpdb->delete(ChangeTracker::changes_name(), $where_1); $where_2 = array('id' => $change->changeset_id); $wpdb->delete(ChangeTracker::changesets_name(), $where_2); } $this->record_counter->clear(); }
/** * Get most recent changes. * @return array|string */ public function get_changes() { $wpdb = $this->table->get_database()->get_wpdb(); $sql = "SELECT cs.id AS changeset_id, c.id AS change_id, date_and_time, " . "user_nicename, table_name, record_ident, column_name, old_value, " . "new_value, comment " . "FROM " . ChangeTracker::changes_name() . " c " . " JOIN " . ChangeTracker::changesets_name() . " cs ON (c.changeset_id=cs.id) " . " JOIN {$wpdb->prefix}users u ON (u.ID=cs.user_id) " . "WHERE table_name = %s AND record_ident = %s" . "ORDER BY date_and_time DESC, cs.id DESC " . "LIMIT 15 "; $params = array($this->table->get_name(), $this->get_primary_key()); return $wpdb->get_results($wpdb->prepare($sql, $params)); }