$issues ) ); } if ( $publish && true !== $reviewed ) { return new \WP_Error( 'accessibility_review', __( 'Before publishing, review the questions and instructions, then confirm the accessibility review in Review & publish.', 'accessible-forms-by-accessible-org' ), array( 'setting' => 'accessibility_review' ) ); } $existing_fields = array_column( ( Schema::get( $id, true ) ?: array( 'fields' => array() ) )['fields'], null, 'uid' ); foreach ( $schema['fields'] as $field ) { if ( isset( $existing_fields[ $field['uid'] ] ) && $existing_fields[ $field['uid'] ]['name'] !== $field['name'] ) { return new \WP_Error( 'identity', __( 'A saved field’s key cannot change. Edit its visible label, or create an independent field instead.', 'accessible-forms-by-accessible-org' ) ); } } if ( ! $publish && in_array( get_post_status( $id ), array( 'publish', 'future' ), true ) ) { if ( ! Persistence::meta( $id, self::DRAFT, wp_json_encode( $schema ) ) ) { return self::storage_error(); } Audit::record( 'draft_saved', $id ); return true; } $old = Schema::get_live( $id ); if ( ! Schema::save( $id, $schema ) ) { return self::storage_error(); } delete_post_meta( $id, self::DRAFT ); if ( self::has_draft( $id ) ) { return new \WP_Error( 'draft_cleanup', __( 'Your form was saved, but its working copy could not be cleared. Your edits are kept. Reload and try again.', 'accessible-forms-by-accessible-org' ) ); } Audit::record( $publish ? 'form_published' : 'form_saved', $id ); if ( $old && $old !== $schema ) { // Add-ons keep a history of replaced versions. Extensions::notify( 'accessible_forms_form_published', (int) $id, $old, $schema ); } return true; } finally { Locks::release( $lock ); } } private static function storage_error() { return new \WP_Error( 'storage', __( 'Your changes could not be saved. The saved version and your edits are kept. Please try again.', 'accessible-forms-by-accessible-org' ) ); } }