get_charset_collate(); $audit = $wpdb->prefix . 'fived_bridge_audit'; $sql_audit = "CREATE TABLE {$audit} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, created_at datetime NOT NULL, user_id bigint(20) unsigned NOT NULL DEFAULT 0, tool varchar(100) NOT NULL, action varchar(50) NOT NULL DEFAULT '', target text NULL, status varchar(20) NOT NULL DEFAULT 'success', details longtext NULL, PRIMARY KEY (id), KEY created_at (created_at), KEY tool (tool), KEY status (status) ) {$charset_collate};"; dbDelta( $sql_audit ); self::migrate_legacy_table( $wpdb->prefix . 'oraya_bridge_audit', $audit ); update_option( 'divi_powers_connection_db_version', self::DB_VERSION, false ); } /** * Copy data from the original single-site bridge tables when present. * Existing 5D Site Bridge tables are never overwritten. * * @param string $legacy_table Previous table name. * @param string $new_table Current table name. * @return void */ private static function migrate_legacy_table( $legacy_table, $new_table ) { global $wpdb; $found = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $legacy_table ) ) ); if ( $legacy_table !== $found ) { return; } $count = (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i', $new_table ) ); if ( 0 !== $count ) { return; } $wpdb->query( $wpdb->prepare( 'INSERT IGNORE INTO %i SELECT * FROM %i', $new_table, $legacy_table ) ); } /** * Resolve one of the plugin's table names. * * @param string $name audit. * @return string */ public function table( $name ) { global $wpdb; $allowed = array( 'audit' ); if ( ! in_array( $name, $allowed, true ) ) { throw new InvalidArgumentException( 'Unknown customer connection table.' ); } return $wpdb->prefix . 'fived_bridge_' . $name; } /** * Store an audit record without file contents or credentials. * * @param string $tool Tool name. * @param string $action Short action name. * @param string $target Human-readable target. * @param string $status success, denied, or error. * @param array $details Sanitized structured details. * @return int */ public function audit( $tool, $action = '', $target = '', $status = 'success', array $details = array() ) { global $wpdb; $inserted = $wpdb->insert( $this->table( 'audit' ), array( 'created_at' => current_time( 'mysql', true ), 'user_id' => get_current_user_id(), 'tool' => sanitize_key( $tool ), 'action' => sanitize_key( $action ), 'target' => sanitize_text_field( $target ), 'status' => sanitize_key( $status ), 'details' => empty( $details ) ? null : wp_json_encode( $details ), ), array( '%s', '%d', '%s', '%s', '%s', '%s', '%s' ) ); if ( false === $inserted ) { return 0; } return (int) $wpdb->insert_id; } /** * Read recent audit records. * * @param int $limit Maximum rows. * @param string $tool Optional tool filter. * @param string $status Optional status filter. * @return array */ public function get_audit( $limit = 50, $tool = '', $status = '' ) { global $wpdb; $limit = min( 200, max( 1, (int) $limit ) ); $rows = $wpdb->get_results( $wpdb->prepare( 'SELECT id, created_at, user_id, tool, action, target, status, details FROM %i WHERE (%d = 0 OR tool = %s) AND (%d = 0 OR status = %s) ORDER BY id DESC LIMIT %d', $this->table( 'audit' ), (int) ( '' !== $tool ), sanitize_key( $tool ), (int) ( '' !== $status ), sanitize_key( $status ), $limit ), ARRAY_A ); foreach ( $rows as &$row ) { $row['id'] = (int) $row['id']; $row['user_id'] = (int) $row['user_id']; $row['details'] = $row['details'] ? json_decode( $row['details'], true ) : array(); } return $rows; } }