table = $wpdb->prefix . 'subtleforms_forms'; $this->schemas_table = $wpdb->prefix . 'subtleforms_form_schemas'; // Verify tables exist on first instantiation $this->ensureTablesExist(); } /** * Verify required database tables exist. * * @throws \RuntimeException If tables are missing */ private function ensureTablesExist(): void { global $wpdb; // phpcs:ignore PluginCheck.Security.DirectDB.UnescapedDBParameter, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Table names are safe, set in constructor from $wpdb->prefix. $forms_exists = $wpdb->get_var( "SHOW TABLES LIKE '{$this->table}'" ); // phpcs:ignore PluginCheck.Security.DirectDB.UnescapedDBParameter, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Table names are safe, set in constructor from $wpdb->prefix. $schemas_exists = $wpdb->get_var( "SHOW TABLES LIKE '{$this->schemas_table}'" ); $missing = array(); if ( $forms_exists !== $this->table ) { $missing[] = $this->table; } if ( $schemas_exists !== $this->schemas_table ) { $missing[] = $this->schemas_table; } if ( ! empty( $missing ) ) { $error = sprintf( 'SubtleForms database tables are missing: %s. Please deactivate and reactivate the plugin to create them.', implode( ', ', $missing ) ); Logger::error( 'Repository Error: ' . $error ); throw new \RuntimeException( $error ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped } } /** * Get a form by ID. */ public function find( int $id ): ?array { $cache_key = "subtleforms_form_{$id}"; $cached = wp_cache_get( $cache_key, 'subtleforms' ); if ( $cached !== false ) { return $cached; } global $wpdb; // phpcs:disable PluginCheck.Security.DirectDB.UnescapedDBParameter, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Table name is safe; direct query needed for dynamic form data. $result = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$this->table} WHERE id = %d", $id ), ARRAY_A ); // phpcs:enable PluginCheck.Security.DirectDB.UnescapedDBParameter, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching if ( ! $result ) { return null; } // Decode JSON config $result['config'] = Helpers::safe_json_decode( Helpers::safe_array_get( $result, 'config', '{}' ), true, array() ); wp_cache_set( $cache_key, $result, 'subtleforms', 60 ); return $result; } /** * Get all forms. */ public function all( array $args = array() ): array { global $wpdb; $start = \SubtleForms\Support\QueryInstrumentation::start( __METHOD__ ); $defaults = array( 'status' => null, 'search' => null, 'limit' => 20, 'offset' => 0, 'orderby' => 'created_at', 'order' => 'DESC', ); $args = wp_parse_args( $args, $defaults ); $where = array(); $params = array(); if ( $args['status'] ) { // 'trash' is a real status; any other status filters exactly. // An empty status (the "All" tab) excludes trashed forms so they // don't pollute the active list (Part B10). $where[] = 'status = %s'; $params[] = $args['status']; } else { $where[] = "status <> 'trash'"; } if ( $args['search'] ) { $where[] = 'title LIKE %s'; $params[] = '%' . $wpdb->esc_like( $args['search'] ) . '%'; } $where_clause = empty( $where ) ? '' : ' WHERE ' . implode( ' AND ', $where ); // Validate orderby (whitelist, safe to interpolate) $allowed_orderby = array( 'id', 'title', 'status', 'created_at', 'updated_at' ); $orderby = in_array( $args['orderby'], $allowed_orderby, true ) ? $args['orderby'] : 'created_at'; $order = strtoupper( $args['order'] ) === 'ASC' ? 'ASC' : 'DESC'; // Phase B3: Only fetch needed columns for list views (skip large config JSON). // Extract form_type and template_id: prefer draft_schema (in-progress edits) then fall back // to config (published schema). draft_schema is NULL until a builder session saves a draft. // JSON_EXTRACT / JSON_UNQUOTE need MySQL 5.7+ or MariaDB 10.2+. WordPress still // supports older engines, so fall back to decoding the JSON in PHP there. $supports_json = \SubtleForms\Support\DatabaseFeatures::supportsJson(); if ( $supports_json ) { $columns = "id, title, status, created_at, updated_at, COALESCE(JSON_UNQUOTE(JSON_EXTRACT(draft_schema, '$.metadata.type')), JSON_UNQUOTE(JSON_EXTRACT(config, '$.metadata.type'))) AS form_type, COALESCE(JSON_UNQUOTE(JSON_EXTRACT(draft_schema, '$.metadata.template')), JSON_UNQUOTE(JSON_EXTRACT(config, '$.metadata.template'))) AS template_id, CASE WHEN draft_schema IS NOT NULL AND draft_schema <> config THEN 1 ELSE 0 END AS has_unpublished_changes"; } else { $columns = 'id, title, status, created_at, updated_at, draft_schema, config'; } // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name, orderby and order are whitelisted. $sql = "SELECT {$columns} FROM {$this->table}{$where_clause} ORDER BY {$orderby} {$order} LIMIT %d OFFSET %d"; $params[] = intval( $args['limit'] ); $params[] = intval( $args['offset'] ); $results = $wpdb->get_results( $wpdb->prepare( $sql, ...$params ), ARRAY_A ); // phpcs:ignore PluginCheck.Security.DirectDB.UnescapedDBParameter, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- $sql built from safe table name; prepared params used. if ( ! $supports_json && is_array( $results ) ) { $results = array_map( array( $this, 'extractListMetadata' ), $results ); } \SubtleForms\Support\QueryInstrumentation::end( $start, __METHOD__, $sql ); return $results; } /** * Derive form_type and template_id in PHP for engines without JSON functions. * * Mirrors the SQL COALESCE above: the in-progress draft wins over the * published config. The raw JSON columns are dropped so the row shape matches * the JSON-capable path exactly. * * @param array $row Raw database row. * @return array Row with form_type/template_id resolved. */ private function extractListMetadata( array $row ): array { $draft_raw = is_string( $row['draft_schema'] ?? null ) ? trim( $row['draft_schema'] ) : ''; $config_raw = is_string( $row['config'] ?? null ) ? trim( $row['config'] ) : ''; $draft = \SubtleForms\Support\Helpers::safe_json_decode( $draft_raw, true, array() ); $config = \SubtleForms\Support\Helpers::safe_json_decode( $config_raw, true, array() ); $draft_meta = is_array( $draft ) ? ( $draft['metadata'] ?? array() ) : array(); $config_meta = is_array( $config ) ? ( $config['metadata'] ?? array() ) : array(); $row['form_type'] = $draft_meta['type'] ?? $config_meta['type'] ?? null; $row['template_id'] = $draft_meta['template'] ?? $config_meta['template'] ?? null; $row['has_unpublished_changes'] = ( $draft_raw !== '' && $config_raw !== '' && $draft_raw !== $config_raw ) ? 1 : 0; unset( $row['draft_schema'], $row['config'] ); return $row; } /** * Count forms based on criteria. */ public function count( array $args = array() ): int { global $wpdb; $where = array(); $params = array(); if ( ! empty( $args['status'] ) ) { $where[] = 'status = %s'; $params[] = $args['status']; } else { // "All" excludes trashed forms (Part B10). $where[] = "status <> 'trash'"; } if ( ! empty( $args['search'] ) ) { $where[] = 'title LIKE %s'; $params[] = '%' . $wpdb->esc_like( $args['search'] ) . '%'; } $where_clause = empty( $where ) ? '' : ' WHERE ' . implode( ' AND ', $where ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name is safe. $sql = "SELECT COUNT(*) FROM {$this->table}{$where_clause}"; if ( ! empty( $params ) ) { // phpcs:ignore PluginCheck.Security.DirectDB.UnescapedDBParameter, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- $sql built from whitelisted table name; values are prepared. return (int) $wpdb->get_var( $wpdb->prepare( $sql, ...$params ) ); } // phpcs:ignore PluginCheck.Security.DirectDB.UnescapedDBParameter, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- No user-supplied params; table name is controlled. return (int) $wpdb->get_var( $sql ); } /** * Find multiple forms by IDs in a single query (v1.8.2). * Returns a map of form_id => form data. * Only fetches id and title columns for performance. */ public function findMultiple( array $ids ): array { global $wpdb; if ( empty( $ids ) ) { return array(); } // Sanitize IDs $ids = array_map( 'intval', $ids ); $ids = array_filter( $ids ); if ( empty( $ids ) ) { return array(); } $placeholders = implode( ', ', array_fill( 0, count( $ids ), '%d' ) ); // phpcs:disable PluginCheck.Security.DirectDB.UnescapedDBParameter, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- $sql built from $wpdb->prefix table name; values are prepared. $sql = "SELECT id, title FROM {$this->table} WHERE id IN ($placeholders)"; $results = $wpdb->get_results( $wpdb->prepare( $sql, ...$ids ), ARRAY_A ); // phpcs:enable PluginCheck.Security.DirectDB.UnescapedDBParameter, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching // Return as map: form_id => form $map = array(); foreach ( $results as $form ) { $map[ $form['id'] ] = $form; } return $map; } /** * Create a new form. */ public function create( array $data ): int { global $wpdb; $defaults = array( 'title' => '', 'config' => array(), 'status' => 'draft', 'schema' => null, ); $data = wp_parse_args( $data, $defaults ); // Inject default schema if missing or empty if (empty($data['schema']) || !is_array($data['schema']) || empty($data['schema']['fields'])) { $data['schema'] = array( 'metadata' => array( 'type' => 'regular', 'name' => $data['title'] ?: 'Untitled Form', ), 'schema_version' => 1, 'fields' => array( array( 'type' => 'text', 'name' => 'field_1', 'label' => 'Text', 'required' => false, 'placeholder' => '', 'options' => array(), 'defaultValue' => '', 'validation' => array(), 'attributes' => array(), ), ), ); } // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Direct insert; no caching needed for writes. $wpdb->insert( $this->table, array( 'title' => Helpers::safe_sanitize_text( Helpers::safe_string_get( $data, 'title' ) ), 'config' => wp_json_encode( $data['config'] ), 'status' => $data['status'], 'created_at' => current_time( 'mysql', true ), 'updated_at' => current_time( 'mysql', true ), ), array( '%s', '%s', '%s', '%s', '%s' ) ); // phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching return $wpdb->insert_id; } /** * Save a new schema version for a form. * * @return int Version number saved * @throws \InvalidArgumentException If schema validation fails * @throws \RuntimeException If database operation fails */ /** * Sanitise every appearance object in a schema. * * @param array $schema Form schema. * @return array Schema with appearance values checked. */ private static function sanitizeAppearance( array $schema ): array { if ( isset( $schema['metadata']['appearance'] ) ) { $clean = \SubtleForms\Support\Appearance::sanitizeForm( $schema['metadata']['appearance'] ); if ( empty( $clean ) ) { unset( $schema['metadata']['appearance'] ); } else { $schema['metadata']['appearance'] = $clean; } } if ( ! empty( $schema['fields'] ) && is_array( $schema['fields'] ) ) { $schema['fields'] = self::sanitizeFieldAppearance( $schema['fields'] ); } return $schema; } /** * Run the schema_before_save filter (2.4.1). * * Every path that stores a schema a person made goes through this: builder * saves, new forms created with a schema, and forms imported in Tools. * Add-ons rely on it, for example to give a signed webhook its secret or to * drop logic rules that point at missing fields. * * @param array $schema Schema about to be saved. * @param int $formId Form ID. * @param bool $activate Whether this save publishes the schema. * @return array */ public static function beforeSave( array $schema, int $formId, bool $activate ): array { /** * Filter a form schema before it is saved (draft or published). Add-ons * use this to store data they need at render time. * * @param array $schema Schema about to be saved. * @param int $formId Form ID. * @param bool $activate Whether this save publishes the schema. * @since 2.1.0 */ $filtered = apply_filters( 'subtleforms/forms/schema_before_save', $schema, $formId, $activate ); return is_array( $filtered ) ? $filtered : $schema; } /** * Walk a field list, sanitising each field's appearance override. * * Mirrors the nesting SchemaCompiler normalises: fields, children and the * per-column lists of a column container. * * @param array $fields Field list. * @return array Field list with appearance values checked. */ private static function sanitizeFieldAppearance( array $fields ): array { foreach ( $fields as &$field ) { if ( ! is_array( $field ) ) { continue; } // Input mask settings (2.8.0): only known masks, a short plain pattern. $field = \SubtleForms\Fields\InputMask::sanitizeSettings( $field ); if ( isset( $field['config'] ) && is_array( $field['config'] ) ) { $field['config'] = \SubtleForms\Fields\InputMask::sanitizeSettings( $field['config'] ); } if ( isset( $field['config']['appearance'] ) ) { $clean = \SubtleForms\Support\Appearance::sanitizeField( $field['config']['appearance'] ); if ( empty( $clean ) ) { unset( $field['config']['appearance'] ); } else { $field['config']['appearance'] = $clean; } } // The builder saves flat nodes, so the override usually sits on the // node itself; it gets the same checks. if ( isset( $field['appearance'] ) ) { $clean = is_array( $field['appearance'] ) ? \SubtleForms\Support\Appearance::sanitizeField( $field['appearance'] ) : array(); if ( empty( $clean ) ) { unset( $field['appearance'] ); } else { $field['appearance'] = $clean; } } foreach ( array( 'fields', 'children' ) as $nested ) { if ( ! empty( $field[ $nested ] ) && is_array( $field[ $nested ] ) ) { $field[ $nested ] = self::sanitizeFieldAppearance( $field[ $nested ] ); } } if ( ! empty( $field['columns'] ) && is_array( $field['columns'] ) ) { foreach ( $field['columns'] as &$column ) { if ( is_array( $column ) ) { $column = self::sanitizeFieldAppearance( $column ); } } unset( $column ); } } unset( $field ); return $fields; } public function saveSchemaVersion( int $formId, array $schema, bool $activate = false ): int { // Ensure schema_version exists, default to 1 if not present if ( ! isset( $schema['schema_version'] ) ) { $schema['schema_version'] = 1; } // Ensure metadata and metadata.name exist with a safe default if ( ! isset( $schema['metadata'] ) || ! is_array( $schema['metadata'] ) ) { $schema['metadata'] = array(); } if ( empty( $schema['metadata']['name'] ) || ! is_string( $schema['metadata']['name'] ) ) { $schema['metadata']['name'] = 'form_schema'; Logger::debug( 'Injected default metadata.name="%s" for form %d in saveSchemaVersion', $schema['metadata']['name'], $formId ); } // Ensure fields array exists (allow empty drafts) if ( ! isset( $schema['fields'] ) || ! is_array( $schema['fields'] ) ) { $schema['fields'] = array(); Logger::debug( 'Injected default empty fields array for form %d in saveSchemaVersion', $formId ); } // Two spellings of the multi-step type are in circulation: templates and // some builder paths write `multistep`, the schema and renderers expect // `multi-step`. Forms created from those templates could not save at all. // Canonicalise here so storage has one spelling whatever the client sent. if ( isset( $schema['metadata']['type'] ) && $schema['metadata']['type'] === 'multistep' ) { $schema['metadata']['type'] = 'multi-step'; } // The type follows the structure (2.3.2), whatever the client sent. list( $schema ) = \SubtleForms\Support\SchemaRepair::syncType( $schema ); // A field saved without a key gets one rather than breaking the form. list( $schema ) = \SubtleForms\Support\SchemaRepair::assignMissingKeys( $schema ); // Appearance values are written into CSS custom properties on the rendered // form, so they are checked here, at the point of storage, rather than // escaped at output. Every save path goes through this method. $schema = self::sanitizeAppearance( $schema ); // Validate schema before saving $validator = ( new \SubtleForms\Support\SchemaValidator() )->keepUnregisteredTypes(); try { $validator->validate( $schema ); } catch ( \InvalidArgumentException $e ) { throw $e; } global $wpdb; // Determine next version // phpcs:ignore PluginCheck.Security.DirectDB.UnescapedDBParameter, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Table name is safe. Direct query needed for versioning. $max = (int) $wpdb->get_var( $wpdb->prepare( "SELECT MAX(version) FROM {$this->schemas_table} WHERE form_id = %d", $formId ) ); if ( $wpdb->last_error ) { $error = sprintf( 'Database error determining next schema version for form %d: %s', $formId, $wpdb->last_error ); Logger::error( '' . $error ); throw new \RuntimeException( $error ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped } $next = $max ? $max + 1 : 1; // Development: Uncomment to debug schema versioning // if ($activate) { // error_log(sprintf( // 'SubtleForms: Activating schema version %d for form %d', // $next, // $formId // )); // } // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Direct insert; no caching for write operations. $inserted = $wpdb->insert( $this->schemas_table, array( 'form_id' => $formId, 'version' => $next, 'schema_data' => wp_json_encode( $schema ), 'active' => $activate ? 1 : 0, 'created_at' => current_time( 'mysql', true ), ), array( '%d', '%d', '%s', '%d', '%s' ) ); // phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching if ( $inserted === false || $wpdb->last_error ) { $error = sprintf( 'Failed to save schema version %d for form %d: %s', $next, $formId, $wpdb->last_error ?: 'Unknown database error' ); Logger::error( '' . $error ); throw new \RuntimeException( $error ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped } // If activating, mark others inactive and update forms table if ( $activate ) { try { $this->setActiveSchemaVersion( $formId, $next ); } catch ( \RuntimeException $e ) { // Log but don't fail - the schema was saved successfully Logger::error( 'Failed to activate schema version ' . $next . ': ' . $e->getMessage() ); } } return $next; } /** * Load a specific schema version or the active one when version is null. * If no active version exists, returns the latest version as a fallback. * * @throws \RuntimeException If database query fails or schema data is corrupt */ public function loadSchemaVersion( int $formId, ?int $version = null ): ?array { global $wpdb; if ( $version === null ) { // Serve the active schema from object cache when no specific version is requested. $cache_key = "subtleforms_schema_{$formId}"; $cached_schema = wp_cache_get( $cache_key, 'subtleforms' ); if ( $cached_schema !== false ) { return $cached_schema; } // Try to get active version first // phpcs:ignore PluginCheck.Security.DirectDB.UnescapedDBParameter, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Table name is safe, set in constructor from $wpdb->prefix. $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$this->schemas_table} WHERE form_id = %d AND active = 1 ORDER BY version DESC LIMIT 1", $formId ), ARRAY_A ); // If no active version, fall back to the latest version if ( ! $row && ! $wpdb->last_error ) { Logger::error( "SubtleForms: No active schema found for form {$formId}, using latest version as fallback" ); // phpcs:ignore PluginCheck.Security.DirectDB.UnescapedDBParameter, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Table name is safe, set in constructor from $wpdb->prefix. $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$this->schemas_table} WHERE form_id = %d ORDER BY version DESC LIMIT 1", $formId ), ARRAY_A ); } } else { // phpcs:ignore PluginCheck.Security.DirectDB.UnescapedDBParameter, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Table name is safe, set in constructor from $wpdb->prefix. $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$this->schemas_table} WHERE form_id = %d AND version = %d", $formId, $version ), ARRAY_A ); } // Check for database errors if ( $wpdb->last_error ) { $error = sprintf( 'Database error loading schema for form %d: %s', $formId, $wpdb->last_error ); Logger::error( '' . $error ); throw new \RuntimeException( $error ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped } if ( ! $row ) { return null; } // Validate schema_data exists if ( ! isset( $row['schema_data'] ) ) { $error = sprintf( 'Schema data column missing for form %d version %d. Database schema may be outdated.', $formId, $row['version'] ?? 'unknown' ); Logger::error( '' . $error ); throw new \RuntimeException( $error ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped } // Decode and validate JSON $decodedSchema = Helpers::safe_json_decode( Helpers::safe_array_get( $row, 'schema_data', '{}' ), true, null ); if ( $decodedSchema === null ) { $error = sprintf( 'Failed to decode schema JSON for form %d version %d: %s', $formId, Helpers::safe_array_get( $row, 'version', 0 ), json_last_error_msg() ); Logger::error( '' . $error ); throw new \RuntimeException( $error ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped } // Ensure schema_version exists in decoded schema, default to 1 for legacy schemas if ( ! isset( $decodedSchema['schema_version'] ) ) { $decodedSchema['schema_version'] = 1; } // Run migrations $migrator = new \SubtleForms\Support\SchemaMigrator(); $decodedSchema = $migrator->migrate( $decodedSchema ); $schemaResult = array( 'version' => (int) $row['version'], 'schema' => $decodedSchema, 'created_at' => $row['created_at'], 'active' => (bool) $row['active'], ); // Cache active-version lookups (version === null path, set above). if ( isset( $cache_key ) ) { wp_cache_set( $cache_key, $schemaResult, 'subtleforms', 60 ); } return $schemaResult; } /** * Mark a given schema version active for a form. * * @throws \RuntimeException If database operations fail */ public function setActiveSchemaVersion( int $formId, int $version ): bool { global $wpdb; // Unset other active flags // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Direct update for schema version flag; no caching for writes. $wpdb->update( $this->schemas_table, array( 'active' => 0 ), array( 'form_id' => $formId ), array( '%d' ), array( '%d' ) ); if ( $wpdb->last_error ) { $error = sprintf( 'Failed to deactivate other schema versions for form %d: %s', $formId, $wpdb->last_error ); Logger::error( '' . $error ); throw new \RuntimeException( $error ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped } // Set requested version active // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Direct update for schema version activation; no caching for writes. $updated = $wpdb->update( $this->schemas_table, array( 'active' => 1 ), array( 'form_id' => $formId, 'version' => $version, ), array( '%d' ), array( '%d', '%d' ) ); // phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching if ( $updated === false || $wpdb->last_error ) { $error = sprintf( 'Failed to activate schema version %d for form %d: %s', $version, $formId, $wpdb->last_error ?: 'Version may not exist' ); Logger::error( '' . $error ); throw new \RuntimeException( $error ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped } // Update forms table config and active_version to keep in sync // phpcs:ignore PluginCheck.Security.DirectDB.UnescapedDBParameter, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Table name is safe, set in constructor from $wpdb->prefix. $schemaRow = $wpdb->get_row( $wpdb->prepare( "SELECT schema_data FROM {$this->schemas_table} WHERE form_id = %d AND version = %d", $formId, $version ), ARRAY_A ); if ( $wpdb->last_error ) { $error = sprintf( 'Failed to fetch schema data for form %d version %d: %s', $formId, $version, $wpdb->last_error ); Logger::error( '' . $error ); throw new \RuntimeException( $error ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped } if ( $schemaRow ) { // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Direct update for form sync; no caching for writes. $result = $wpdb->update( $this->table, array( 'config' => $schemaRow['schema_data'], 'active_version' => $version, // The same bytes as the live form, so reopening is not // "changes not live" when nothing was edited (FF2-10). 'draft_schema' => $schemaRow['schema_data'], ), array( 'id' => $formId ), array( '%s', '%d', '%s' ), array( '%d' ) ); // phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching if ( $result === false && $wpdb->last_error ) { $error = sprintf( 'Failed to update forms table for form %d: %s', $formId, $wpdb->last_error ); Logger::error( '' . $error ); // Don't throw - schema is already activated, this is just sync } } if ( $updated !== false ) { // Invalidate both the form row and the cached active schema. wp_cache_delete( "subtleforms_form_{$formId}", 'subtleforms' ); wp_cache_delete( "subtleforms_schema_{$formId}", 'subtleforms' ); } return $updated !== false; } /** * Get list of schema versions for a form. */ public function getSchemaVersions( int $formId ): array { global $wpdb; // phpcs:ignore PluginCheck.Security.DirectDB.UnescapedDBParameter, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Table name is safe, set in constructor from $wpdb->prefix. $rows = $wpdb->get_results( $wpdb->prepare( "SELECT version, active, created_at FROM {$this->schemas_table} WHERE form_id = %d ORDER BY version DESC", $formId ), ARRAY_A ); return array_map( function ( $r ) { return array( 'version' => (int) $r['version'], 'active' => (bool) $r['active'], 'created_at' => $r['created_at'], ); }, $rows ?: array() ); } /** * Update a form. */ public function update( int $id, array $data ): bool { global $wpdb; $update_data = array(); $format = array(); if ( isset( $data['title'] ) ) { $update_data['title'] = Helpers::safe_sanitize_text( $data['title'] ); $format[] = '%s'; } if ( isset( $data['config'] ) ) { $update_data['config'] = wp_json_encode( $data['config'] ); $format[] = '%s'; } if ( isset( $data['status'] ) ) { $update_data['status'] = $data['status']; $format[] = '%s'; } if ( empty( $update_data ) ) { return false; } // UTC, like created_at (the column default follows the MySQL server zone). $update_data['updated_at'] = current_time( 'mysql', true ); $format[] = '%s'; // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Direct update; no caching for writes. $result = $wpdb->update( $this->table, $update_data, array( 'id' => $id ), $format, array( '%d' ) ); // phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching if ( $result !== false ) { wp_cache_delete( "subtleforms_form_{$id}", 'subtleforms' ); } return $result !== false; } /** * Delete a form for good, with everything that belongs to it: its entries * (and their uploaded files and activity logs), its schema versions and * its daily view totals (FA-19, FF2-21, FF-27). */ public function delete( int $id ): bool { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Direct delete; no caching for writes. $result = $wpdb->delete( $this->table, array( 'id' => $id ), array( '%d' ) ); if ( $result ) { $this->deleteFormData( $id ); } if ( $result !== false ) { wp_cache_delete( "subtleforms_form_{$id}", 'subtleforms' ); wp_cache_delete( "subtleforms_schema_{$id}", 'subtleforms' ); } return $result !== false; } /** * Remove the entries, files, logs, schema versions and view totals of a * deleted form. * * @param int $id Form ID. */ private function deleteFormData( int $id ): void { global $wpdb; $submissions = $wpdb->prefix . 'subtleforms_submissions'; $logs = $wpdb->prefix . 'subtleforms_logs'; // phpcs:disable PluginCheck.Security.DirectDB.UnescapedDBParameter, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange -- Table names are from $wpdb->prefix; values are prepared. // Uploaded files first: only entries that carry a file record are read. $with_files = $wpdb->get_col( $wpdb->prepare( "SELECT payload FROM {$submissions} WHERE form_id = %d AND payload LIKE %s", $id, '%' . $wpdb->esc_like( '"size":' ) . '%' ) ); foreach ( (array) $with_files as $payload ) { \SubtleForms\Uploads\Uploads::deleteForPayload( (string) $payload ); } $wpdb->query( $wpdb->prepare( "DELETE FROM {$logs} WHERE submission_id IN ( SELECT id FROM {$submissions} WHERE form_id = %d )", $id ) ); $wpdb->query( $wpdb->prepare( "DELETE FROM {$submissions} WHERE form_id = %d", $id ) ); $wpdb->query( $wpdb->prepare( "DELETE FROM {$this->schemas_table} WHERE form_id = %d", $id ) ); // phpcs:enable PluginCheck.Security.DirectDB.UnescapedDBParameter, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange // Count caches of the form's entries. $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s", '_transient_subtleforms:%:counts:%' ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching /** * A form and all its data were deleted for good. * * @since 2.3.9 * * @param int $id Form ID. */ do_action( 'subtleforms/form/deleted', $id ); } /** * Move a form to the Trash (soft delete). * * Sets status to 'trash'; the row and all schema versions are kept so the * form can be restored or permanently deleted later (Part B10). * * @param int $id Form ID. * @return bool */ public function trash( int $id ): bool { global $wpdb; $form = $this->find( $id ); if ( ! $form ) { return false; } if ( ( $form['status'] ?? '' ) === 'trash' ) { return true; } // Remember the status so Restore can return to it (2.3.1). // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Write path; no caching. $result = $wpdb->update( $this->table, array( 'status' => 'trash', 'trashed_from' => (string) ( $form['status'] ?? 'draft' ), ), array( 'id' => $id ), array( '%s', '%s' ), array( '%d' ) ); if ( $result !== false ) { wp_cache_delete( "subtleforms_form_{$id}", 'subtleforms' ); } return $result !== false; } /** * Restore a trashed form to the status it had before (2.3.1). * * Forms trashed by older versions have no recorded status and come back * as drafts, so restoring never publishes a form by surprise. * * @param int $id Form ID. * @return string|false The restored status, or false. */ public function restore( int $id ) { global $wpdb; $form = $this->find( $id ); if ( ! $form || ( $form['status'] ?? '' ) !== 'trash' ) { return false; } $previous = (string) ( $form['trashed_from'] ?? '' ); $status = in_array( $previous, array( 'draft', 'published', 'archived' ), true ) ? $previous : 'draft'; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Write path; no caching. $result = $wpdb->update( $this->table, array( 'status' => $status, 'trashed_from' => null, ), array( 'id' => $id, 'status' => 'trash' ), array( '%s', '%s' ), array( '%d', '%s' ) ); if ( $result !== false ) { wp_cache_delete( "subtleforms_form_{$id}", 'subtleforms' ); } return $result !== false ? $status : false; } /** * Count forms grouped by status, excluding nothing. * * Used by the All Forms tab bar to show All / Published / Draft / Trash * counts (Part B10). "All" here means non-trash. * * @return array{all:int,published:int,draft:int,trash:int} */ public function count_by_status(): array { global $wpdb; // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter -- Aggregate read with no user input; the table name comes from $wpdb->prefix. $rows = $wpdb->get_results( "SELECT status, COUNT(*) AS c FROM {$this->table} GROUP BY status", ARRAY_A ); // phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter $counts = array( 'all' => 0, 'published' => 0, 'draft' => 0, 'trash' => 0, ); if ( is_array( $rows ) ) { foreach ( $rows as $row ) { $status = $row['status'] ?? ''; $c = (int) ( $row['c'] ?? 0 ); if ( isset( $counts[ $status ] ) ) { $counts[ $status ] = $c; } if ( $status !== 'trash' ) { $counts['all'] += $c; } } } return $counts; } /** * The schema as it is stored. Appearance keys are written in one order so * a draft of an unchanged form matches the published copy. * * @param array $schema Schema. * @return array */ public static function prepareStoredSchema( array $schema ): array { list( $schema ) = \SubtleForms\Support\SchemaRepair::syncType( $schema ); list( $schema ) = \SubtleForms\Support\SchemaRepair::assignMissingKeys( $schema ); return self::sanitizeAppearance( $schema ); } /** * Save draft schema (non-versioned, unpublished). * * Draft schemas are stored directly in the forms table and do NOT create versions. * This is used for autosave and any edits that haven't been published yet. * * @param int $formId Form ID * @param array $schema Schema data * @return bool Success status * @throws \InvalidArgumentException If schema validation fails */ public function saveDraftSchema( int $formId, array $schema ): bool { // The type follows the structure: a Page Break makes a Standard form // Multi-step and removing the last one makes it Standard again (2.3.2). $schema = self::prepareStoredSchema( $schema ); // Validate schema before saving $validator = ( new \SubtleForms\Support\SchemaValidator() )->keepUnregisteredTypes(); try { $validator->validate( $schema ); } catch ( \InvalidArgumentException $e ) { throw $e; } global $wpdb; // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Direct update for draft schema; no caching for writes. $result = $wpdb->update( $this->table, array( 'draft_schema' => wp_json_encode( $schema ) ), array( 'id' => $formId ), array( '%s' ), array( '%d' ) ); // phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching if ( $result === false ) { Logger::error( sprintf( 'SubtleForms: Failed to save draft schema for form %d: %s', $formId, $wpdb->last_error ) ); return false; } return true; } /** * Get draft schema for a form. * * @param int $formId Form ID * @return array|null Draft schema or null if none exists */ public function getDraftSchema( int $formId ): ?array { global $wpdb; // phpcs:disable PluginCheck.Security.DirectDB.UnescapedDBParameter, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Table name is safe; values prepared. $draft = $wpdb->get_var( $wpdb->prepare( "SELECT draft_schema FROM {$this->table} WHERE id = %d", $formId ) ); // phpcs:enable PluginCheck.Security.DirectDB.UnescapedDBParameter, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching if ( ! $draft ) { return null; } $decoded = json_decode( $draft, true ); return is_array( $decoded ) ? $decoded : null; } /** * Promote draft schema to active version. * * This creates a new versioned schema from the current draft and activates it. * Used when publishing a form or manually saving with activation. * * @param int $formId Form ID * @return int New version number * @throws \InvalidArgumentException If draft schema is invalid * @throws \RuntimeException If no draft schema exists or save fails */ public function promoteDraftToActive( int $formId ): int { $draftSchema = $this->getDraftSchema( $formId ); if ( ! $draftSchema ) { throw new \RuntimeException( 'No draft schema to promote' ); } // Create versioned schema and activate it $version = $this->saveSchemaVersion( $formId, $draftSchema, true ); // Development: Uncomment to debug schema promotion // error_log(sprintf( // 'SubtleForms: Promoted draft schema to version %d for form %d', // $version, // $formId // )); return $version; } }