Legacy_Upgrades::upgrade_legacy_form_meta( int $form_id )

Upgrades legacy form metadata to new schema.

Description

See also

Parameters

$form_id

(int) (Required) ID of the form for which to migrate data.

Source

File: src/components/legacy-upgrades.php

	public function upgrade_legacy_form_meta( $form_id ) {
		global $wpdb;

		$prefix = $this->get_prefix();

		$participants = $this->get_full_table_name( 'participants' );
		$email_notifications = $this->get_full_table_name( 'email_notifications' );

		$mappings = array(
			'access_controls' => array(
				'user_identification' => array(
					'prevent_multiple_submissions' => array( array( 'form_access_controls_allmembers_same_users', 'form_access_controls_selectedmembers_same_users' ), 'bool' ),
					'identification_modes'         => array(
						'ip_address' => 'form_access_controls_check_ip',
						'cookie'     => 'form_access_controls_check_cookie',
					),
					'already_submitted_message'    => array( 'already_entered_text', 'string' ),
				),
				'members'               => array(
					'allowed_users'          => 'PARTICIPANTS',
					'login_required_message' => array( 'to_be_logged_in_text', 'string' ),
				),
				'timerange'             => array(
					'start' => array( 'start_date', 'datetime' ),
					'end'   => array( 'end_date', 'datetime' ),
				),
			),
			'actions'         => array(
				'email_notifications' => array(
					'notifications' => 'EMAIL_NOTIFICATIONS',
				),
				'redirection'         => array(
					'type' => array( 'redirect_type', 'string' ),
					'page' => array( 'redirect_page', 'string' ),
					'url'  => array( 'redirect_url', 'string' ),
				),
			),
			'evaluators'      => array(),
			'form_settings'   => array(
				'' => array(
					'show_container_title'  => array( 'show_page_title', 'bool' ),
					'previous_button_label' => array( 'previous_button_text', 'string' ),
					'next_button_label'     => array( 'next_button_text', 'string' ),
					'submit_button_label'   => array( 'send_button_text', 'string' ),
					'success_message'       => array( 'redirect_text_content', 'string' ),
					'allow_get_params'      => array( 'allow_get_param', 'bool' ),
				),
			),
			'protectors'      => array(
				'honeypot'  => array(
					'enabled' => array( 'honeypot_enabled', 'bool' ),
				),
				'linkcount' => array(
					'enabled' => array( 'linkcount_enabled', 'bool' ),
				),
				'recaptcha' => array(
					'enabled' => array( 'recaptcha_enabled', 'bool' ),
					'type'    => array( 'recaptcha_type', 'string' ),
					'size'    => array( 'recaptcha_size', 'string' ),
					'theme'   => array( 'recaptcha_theme', 'string' ),
				),
				'timetrap'  => array(
					'enabled' => array( 'timetrap_enabled', 'bool' ),
				),
			),
		);

		$skip_enabled = array(
			'access_controls-user_identification',
			'access_controls-timerange',
			'actions-email_notifications',
			'actions-redirection',
			'protectors-honeypot',
			'protectors-linkcount',
			'protectors-recaptcha',
			'protectors-timetrap',
		);

		foreach ( $mappings as $module => $module_mappings ) {
			$metadata = get_post_meta( $form_id, $prefix . 'module_' . $module, true );
			if ( ! is_array( $metadata ) ) {
				$metadata = array();
			}

			foreach ( $module_mappings as $submodule => $submodule_mappings ) {
				$submodule_data_found = false;

				$form_option_prefix = ! empty( $submodule ) ? $submodule . '__' : '';

				foreach ( $submodule_mappings as $form_option => $mapping_data ) {
					if ( 'PARTICIPANTS' === $mapping_data ) {
						if ( get_option( $prefix . 'legacy_participants_table_installed' ) === 'true' ) {
							$user_ids = $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT user_id FROM $participants WHERE form_id = %d", $form_id ) );
							if ( ! empty( $user_ids ) ) {
								$submodule_data_found = true;

								// New values already set take precedence.
								if ( ! isset( $metadata[ $form_option_prefix . $form_option ] ) ) {
									$metadata[ $form_option_prefix . $form_option ] = $user_ids;
								}
							}
						}
						continue;
					}

					if ( 'EMAIL_NOTIFICATIONS' === $mapping_data ) {
						if ( get_option( $prefix . 'legacy_email_notifications_table_installed' ) === 'true' ) {
							$notifications = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $email_notifications WHERE form_id = %d", $form_id ) );
							if ( ! empty( $notifications ) ) {
								$submodule_data_found = true;

								// New values already set take precedence.
								if ( ! isset( $metadata[ $form_option_prefix . $form_option ] ) ) {
									$metadata[ $form_option_prefix . $form_option ] = array();
									foreach ( $notifications as $notification ) {
										$metadata[ $form_option_prefix . $form_option ][] = array(
											'from_name'   => $notification->from_name,
											'from_email'  => $notification->from_email,
											'reply_email' => $notification->reply_email,
											'to_email'    => $notification->to_email,
											'subject'     => $notification->subject,
											'message'     => wpautop( $notification->message ),
										);
									}
								}
							}
						}
						continue;
					}

					if ( ! isset( $mapping_data[0] ) ) {
						$new = array();
						foreach ( $mapping_data as $group_value => $old_form_option ) {
							$old = get_post_meta( $form_id, $old_form_option, true );
							if ( ! empty( $old ) && 'no' !== strtolower( $old ) ) {
								$new[] = $group_value;
							}
						}

						$metadata[ $form_option_prefix . $form_option ] = $new;
						continue;
					}

					$old = array();
					if ( is_array( $mapping_data[0] ) ) {
						foreach ( $mapping_data[0] as $old_form_option ) {
							$old = get_post_meta( $form_id, $old_form_option );
							if ( ! empty( $old ) ) {
								break;
							}
						}
					} else {
						$old = get_post_meta( $form_id, $mapping_data[0] );
					}

					if ( empty( $old ) ) {
						continue;
					}

					$submodule_data_found = true;

					// New values already set take precedence.
					if ( isset( $metadata[ $form_option_prefix . $form_option ] ) ) {
						continue;
					}

					$old = $old[0];

					switch ( $mapping_data[1] ) {
						case 'bool':
							$new = ! empty( $old ) && 'no' !== strtolower( $old ) ? true : false;
							break;
						case 'datetime':
							if ( ! is_numeric( $old ) ) {
								$old = strtotime( $old );
							}
							$new = date( 'Y-m-d H:i:s', $old );
							break;
						case 'string':
						default:
							$new = $old;
					}

					$metadata[ $form_option_prefix . $form_option ] = $new;
				}

				if ( ! empty( $form_option_prefix ) && ! in_array( $module . '-' . $submodule, $skip_enabled, true ) && $submodule_data_found ) {
					$metadata[ $form_option_prefix . 'enabled' ] = true;
				}
			}

			if ( ! empty( $metadata ) ) {
				update_post_meta( $form_id, $prefix . 'module_' . $module, $metadata );
			}
		}
	}

Changelog

Changelog
Version Description
1.0.0 Introduced.