__( 'From address', 'accessible-forms-by-accessible-org' ), 'mail_from_name' => __( 'From name', 'accessible-forms-by-accessible-org' ), 'spam_provider' => __( 'Spam verification', 'accessible-forms-by-accessible-org' ), 'max_links' => __( 'Maximum links per message', 'accessible-forms-by-accessible-org' ), 'rate_limit' => __( 'Submissions per hour, per IP address', 'accessible-forms-by-accessible-org' ), 'retention_days' => __( 'Keep received entries (days, 0 = until deleted)', 'accessible-forms-by-accessible-org' ), 'spam_retention_days' => __( 'Keep spam and held messages (days)', 'accessible-forms-by-accessible-org' ), ); } /** * Portable settings, grouped by owner: `core`, plus one group per add-on. * * @return array */ public static function export() { $stored = Settings::get(); $core = array(); foreach ( self::KEYS as $key ) { if ( ! Settings::is_constant( $key ) && array_key_exists( $key, $stored ) ) { $core[ $key ] = $stored[ $key ]; } } if ( isset( $core['spam_provider'] ) && ! in_array( $core['spam_provider'], self::PORTABLE_PROVIDERS, true ) ) { unset( $core['spam_provider'] ); } $settings = (array) Extensions::filter( 'accessible_forms_portable_settings', array( 'core' => $core ) ); Audit::record( 'settings_exported' ); return array_filter( $settings ); } /** * Incoming settings beside the current ones. * * @param array $incoming The file's `settings` section. * @return array[] Rows: id, group, label, current, incoming, value, changed, note. */ public static function review( array $incoming ) { $rows = array(); $current = Settings::get(); $labels = self::labels(); $core = is_array( $incoming['core'] ?? null ) ? $incoming['core'] : array(); foreach ( self::KEYS as $key ) { if ( ! array_key_exists( $key, $core ) || ! is_scalar( $core[ $key ] ) ) { continue; } $value = SettingsPage::sanitize_setting( $key, $core[ $key ], $current[ $key ] ?? '' ); $note = ''; if ( Settings::is_constant( $key ) ) { $note = __( 'Set in wp-config.php on this site, so it is not changed.', 'accessible-forms-by-accessible-org' ); } elseif ( 'spam_provider' === $key && ( ! in_array( (string) $core[ $key ], self::PORTABLE_PROVIDERS, true ) || ! isset( Spam::providers()[ (string) $core[ $key ] ] ) ) ) { $note = __( 'This method needs setting up on this site. Choose it under Settings, Spam protection.', 'accessible-forms-by-accessible-org' ); } $rows[] = array( 'id' => 'core.' . $key, 'group' => 'core', 'label' => $labels[ $key ], 'current' => self::display( $key, $current[ $key ] ?? '' ), 'incoming' => self::display( $key, $value ), 'value' => $value, 'changed' => (string) ( $current[ $key ] ?? '' ) !== (string) $value, 'note' => $note, ); } return (array) Extensions::filter( 'accessible_forms_settings_review', $rows, $incoming ); } /** * Apply the ticked rows. * * @param array $incoming The file's `settings` section. * @param string[] $selected Row IDs, such as `core.mail_from`. * @return int|\WP_Error Number of settings changed. */ public static function apply( array $incoming, array $selected ) { $selected = array_flip( array_filter( $selected, 'is_string' ) ); $changed = 0; $settings = Settings::get(); $previous = $settings; foreach ( self::review( $incoming ) as $row ) { if ( 'core' !== $row['group'] || ! isset( $selected[ $row['id'] ] ) || $row['note'] || ! $row['changed'] ) { continue; } $settings[ substr( $row['id'], 5 ) ] = $row['value']; ++$changed; } if ( $changed ) { $checked = Extensions::filter( 'accessible_forms_settings_before_save', $settings, $previous, 'import' ); if ( is_wp_error( $checked ) ) { return $checked; } if ( ! Settings::update( is_array( $checked ) ? $checked : $settings ) ) { return new \WP_Error( 'storage', __( 'The settings could not be saved. Nothing was changed.', 'accessible-forms-by-accessible-org' ) ); } } // Add-ons apply their own ticked rows and report how many changed. $changed += (int) Extensions::filter( 'accessible_forms_import_settings', 0, $incoming, array_keys( $selected ) ); if ( $changed ) { Audit::record( 'settings_imported' ); } return $changed; } private static function display( $key, $value ) { if ( 'spam_provider' === $key ) { $provider = Spam::providers()[ $value ] ?? null; return is_array( $provider ) ? (string) $provider[0] : (string) $value; } if ( 'retention_days' === $key && 0 === (int) $value ) { return __( 'Until deleted', 'accessible-forms-by-accessible-org' ); } return '' === (string) $value ? __( '(not set)', 'accessible-forms-by-accessible-org' ) : (string) $value; } }