Legacy_Upgrades::upgrade_legacy_settings()

Upgrades legacy settings to new schema.

Description

See also

Source

File: src/components/legacy-upgrades.php

	public function upgrade_legacy_settings() {
		$prefix = $this->get_prefix();

		$general_mappings = array(
			'modules'        => array( $prefix . 'settings_general_modules', 'modules' ),
			'slug'           => array( $prefix . 'settings_general_slug', 'string' ),
			'frontend_css'   => array( $prefix . 'settings_general_frontend_css', 'bool' ),
			'hard_uninstall' => array( $prefix . 'settings_general_hard_uninstall', 'bool' ),
		);

		$general = get_option( $prefix . 'general_settings', array() );

		foreach ( $general_mappings as $option => $mapping_data ) {
			// New values already set take precedence.
			if ( isset( $general[ $option ] ) ) {
				continue;
			}

			$old = get_option( $mapping_data[0] );
			if ( false === $old ) {
				continue;
			}

			switch ( $mapping_data[1] ) {
				case 'bool':
					$new = ! empty( $old ) && 'no' !== strtolower( $old ) ? true : false;
					break;
				case 'modules':
					$old = (array) $old;
					$new = array();
					if ( in_array( 'actions', $old, true ) ) {
						$new[] = 'actions';
					}
					if ( in_array( 'results', $old, true ) ) {
						$new[] = 'evaluators';
					}
					if ( in_array( 'form-settings', $old, true ) ) {
						$new[] = 'form_settings';
						$new[] = 'access_controls';
						$new[] = 'protectors';
					}
					break;
				case 'string':
				default:
					$new = $old;
			}

			$general[ $option ] = $new;
		}

		if ( ! empty( $general ) ) {
			update_option( $prefix . 'general_settings', $general );
		}

		$mappings = array(
			'access_controls' => array(
				'members' => array(
					'invitation_from_name'      => array( $prefix . 'settings_visitors_selectedmembers_invite_from_name', 'string' ),
					'invitation_from_email'     => array( $prefix . 'settings_visitors_selectedmembers_invite_from', 'string' ),
					'invitation_from_subject'   => array( $prefix . 'settings_visitors_selectedmembers_invite_from_subject', 'string' ),
					'invitation_from_message'   => array( $prefix . 'settings_visitors_selectedmembers_invite_from_text', 'string' ),
					'reinvitation_from_name'    => array( $prefix . 'settings_visitors_selectedmembers_reinvite_from_name', 'string' ),
					'reinvitation_from_email'   => array( $prefix . 'settings_visitors_selectedmembers_reinvite_from', 'string' ),
					'reinvitation_from_subject' => array( $prefix . 'settings_visitors_selectedmembers_reinvite_from_subject', 'string' ),
					'reinvitation_from_message' => array( $prefix . 'settings_visitors_selectedmembers_reinvite_from_text', 'string' ),
				),
			),
			'protectors'      => array(
				'recaptcha' => array(
					'site_key'   => array( $prefix . 'settings_form_settings_spam_protection_recaptcha_sitekey', 'string' ),
					'secret_key' => array( $prefix . 'settings_form_settings_spam_protection_recaptcha_secret', 'string' ),
				),
			),
		);

		foreach ( $mappings as $module => $module_mappings ) {
			$settings = get_option( $prefix . 'module_' . $module, array() );

			foreach ( $module_mappings as $submodule => $submodule_mappings ) {
				$option_prefix = ! empty( $submodule ) ? $submodule . '__' : '';

				foreach ( $submodule_mappings as $option => $mapping_data ) {
					// New values already set take precedence.
					if ( isset( $settings[ $option_prefix . $option ] ) ) {
						continue;
					}

					$old = get_option( $mapping_data[0] );
					if ( false === $old ) {
						continue;
					}

					switch ( $mapping_data[1] ) {
						case 'string':
						default:
							$new = $old;
					}

					$settings[ $option_prefix . $option ] = $new;
				}
			}

			if ( ! empty( $settings ) ) {
				update_option( $prefix . 'module_' . $module, $settings );
			}
		}
	}

Changelog

Changelog
Version Description
1.0.0 Introduced.