get_var($wpdb->prepare('SELECT id FROM %i WHERE memory_key = %s', $tables['memory'], $key)); if ($existing_id) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $wpdb->update( $tables['memory'], [ 'memory_value' => $encoded, 'memory_type' => sanitize_text_field($type), 'updated_at' => current_time('mysql'), ], ['id' => absint($existing_id)], ['%s', '%s', '%s'], ['%d'] ); return; } // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery $wpdb->insert( $tables['memory'], [ 'memory_key' => $key, 'memory_value' => $encoded, 'memory_type' => sanitize_text_field($type), 'created_at' => current_time('mysql'), 'updated_at' => current_time('mysql'), ], ['%s', '%s', '%s', '%s', '%s'] ); } public static function get($key, $default = null) { global $wpdb; $tables = AIWEST_Database::tables(); // Memory is read from a private plugin table and may be updated during the same request. // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $value = $wpdb->get_var($wpdb->prepare('SELECT memory_value FROM %i WHERE memory_key = %s', $tables['memory'], sanitize_key($key))); if ($value === null) return $default; $decoded = json_decode($value, true); return json_last_error() === JSON_ERROR_NONE ? $decoded : $value; } public static function all() { global $wpdb; $tables = AIWEST_Database::tables(); // The admin memory screen requires a current, uncached view of the private plugin table. // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching return $wpdb->get_results($wpdb->prepare('SELECT * FROM %i ORDER BY updated_at DESC', $tables['memory'])); } }