'3', 'mapp_stage' => self::STAGE_STARTED, 'application_status' => '0', 'mapp_lang' => isset( $context['lang'] ) && 'es' === $context['lang'] ? 'es' : 'en', 'mapp_started_at' => time(), 'mapp_updated_at' => time(), // Keeps 2.6's hourly reminder (status 80, no notification_status) off 3.0 applications. 'notification_status' => 'v3', ); if ( ! empty( $context['route_email'] ) && is_email( $context['route_email'] ) ) { $meta['dynamic_email'] = sanitize_email( $context['route_email'] ); } if ( ! empty( $context['source'] ) && is_array( $context['source'] ) ) { foreach ( array( 'utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'gclid', 'fbclid', 'referrer', 'landing_page' ) as $key ) { if ( ! empty( $context['source'][ $key ] ) ) { $meta[ 'mapp_' . $key ] = mb_substr( sanitize_text_field( (string) $context['source'][ $key ] ), 0, 500 ); } } } if ( ! empty( $context['page_id'] ) ) { $meta['mapp_page_id'] = (int) $context['page_id']; } return ConnectorGuard::quietly( function () use ( $meta ) { $id = wp_insert_post( array( 'post_title' => __( 'Application in progress', '1003-mortgage-application' ), 'post_status' => 'publish', 'post_type' => self::POST_TYPE, 'meta_input' => $meta, ), true ); return is_wp_error( $id ) ? 0 : (int) $id; } ); } /** * @param int $id Post ID. * @return bool */ public static function exists( $id ) { $post = get_post( (int) $id ); return $post && self::POST_TYPE === $post->post_type && 'trash' !== $post->post_status; } /** * All answers for an application. Sensitive values are left out unless * $include_sensitive is true, in which case they are decrypted. * * @param int $id Post ID. * @param bool $include_sensitive Decrypt SSNs. * @return array */ public static function answers( $id, $include_sensitive = false ) { $answers = array(); foreach ( Schema::questions() as $key => $def ) { if ( ! isset( $def['type'] ) ) { continue; } if ( ! empty( $def['sensitive'] ) ) { if ( $include_sensitive ) { $plain = Crypto::read_ssn( (string) get_post_meta( $id, $key, true ) ); if ( '' !== $plain ) { $answers[ $key ] = $plain; } } continue; } if ( 'address' === $def['type'] ) { $formatted = get_post_meta( $id, $key, true ); if ( '' !== $formatted && null !== $formatted ) { $address = array( 'formatted' => (string) $formatted ); foreach ( $def['components'] as $part => $meta_key ) { $address[ $part ] = (string) get_post_meta( $id, $meta_key, true ); } $answers[ $key ] = $address; } continue; } $value = get_post_meta( $id, $key, true ); if ( '' !== $value && null !== $value && false !== $value ) { $answers[ $key ] = $value; } } return $answers; } /** * Which sensitive answers are on file (so the form can say "on file"). * * @param int $id Post ID. * @return string[] */ public static function sensitive_on_file( $id ) { $out = array(); foreach ( Schema::questions() as $key => $def ) { if ( ! empty( $def['sensitive'] ) && '' !== (string) get_post_meta( $id, $key, true ) ) { $out[] = $key; } } return $out; } /** * Save a set of answers. Unknown or legacy keys are ignored; invalid values * are returned as errors and not saved. * * @param int $id Post ID. * @param array $changes Key => raw value. * @return array [ 'saved' => string[], 'errors' => [ key => message ] ] */ public static function save_answers( $id, $changes ) { $saved = array(); $errors = array(); foreach ( (array) $changes as $key => $raw ) { $key = (string) $key; $def = Schema::question( $key ); if ( ! $def || ! Schema::is_asked( $def ) ) { continue; } $clean = Schema::sanitize( $key, $raw ); // The browser never gets SSNs back, so an empty one means "keep it". if ( ! empty( $def['sensitive'] ) && '' === $clean ) { continue; } if ( is_wp_error( $clean ) ) { $errors[ $key ] = $clean->get_error_message(); continue; } // The down payment has to be less than the price. if ( 'down_payment' === $key && '' !== $clean ) { $price = isset( $changes['purchase_price'] ) ? Schema::sanitize( 'purchase_price', $changes['purchase_price'] ) : get_post_meta( $id, 'purchase_price', true ); if ( ! is_wp_error( $price ) && '' !== (string) $price && (float) $clean >= (float) $price ) { $errors[ $key ] = __( 'The down payment has to be less than the price.', '1003-mortgage-application' ); continue; } } self::write( $id, $key, $def, $clean ); $saved[] = $key; } if ( ! empty( $saved ) ) { self::refresh_derived( $id ); update_post_meta( $id, 'mapp_updated_at', time() ); self::refresh_title( $id ); } return array( 'saved' => $saved, 'errors' => $errors, ); } /** * @param int $id Post ID. * @param string $key Key. * @param array $def Definition. * @param mixed $clean Sanitized value. */ private static function write( $id, $key, $def, $clean ) { if ( '' === $clean || array() === $clean ) { delete_post_meta( $id, $key ); if ( 'address' === $def['type'] ) { foreach ( $def['components'] as $meta_key ) { delete_post_meta( $id, $meta_key ); } } return; } if ( ! empty( $def['sensitive'] ) ) { update_post_meta( $id, $key, Crypto::encrypt( $clean ) ); return; } if ( 'address' === $def['type'] ) { update_post_meta( $id, $key, $clean['formatted'] ); foreach ( $def['components'] as $part => $meta_key ) { if ( isset( $clean[ $part ] ) && '' !== $clean[ $part ] ) { update_post_meta( $id, $meta_key, $clean[ $part ] ); } else { delete_post_meta( $id, $meta_key ); } } return; } update_post_meta( $id, $key, $clean ); } /** * Recompute derived fields from the stored answers. * * @param int $id Post ID. */ public static function refresh_derived( $id ) { $answers = self::answers( $id, true ); foreach ( Derive::compute( $answers ) as $key => $value ) { if ( '' === $value ) { delete_post_meta( $id, $key ); } else { update_post_meta( $id, $key, $value ); } } // Refinancing a primary residence: the property is the mailing address. if ( 'Yes' === get_post_meta( $id, 'mailing_same_as_property', true ) && isset( $answers['zip_code'] ) ) { $def = Schema::question( 'mailing_address' ); $src = Schema::question( 'zip_code' ); update_post_meta( $id, 'mailing_address', $answers['zip_code']['formatted'] ); foreach ( $def['components'] as $part => $meta_key ) { $from = isset( $src['components'][ $part ] ) ? get_post_meta( $id, $src['components'][ $part ], true ) : ''; update_post_meta( $id, $meta_key, $from ); } } } /** * Title the post "First Last email" once a name is known, as 2.6 does. * * @param int $id Post ID. */ private static function refresh_title( $id ) { $name = trim( get_post_meta( $id, 'first_name', true ) . ' ' . get_post_meta( $id, 'last_name', true ) ); $email = (string) get_post_meta( $id, 'email', true ); $title = trim( $name . ' ' . $email ); if ( '' === $title || get_post_field( 'post_title', $id ) === $title ) { return; } ConnectorGuard::quietly( function () use ( $id, $title ) { wp_update_post( array( 'ID' => $id, 'post_title' => $title, ) ); } ); } /** * @param int $id Post ID. * @return string */ public static function stage( $id ) { $stage = (string) get_post_meta( $id, 'mapp_stage', true ); return '' !== $stage ? $stage : self::STAGE_STARTED; } /** * Move forward to a stage (never backward). * * @param int $id Post ID. * @param string $stage Stage. * @return bool Whether the stage changed. */ public static function advance( $id, $stage ) { $order = array( self::STAGE_STARTED, self::STAGE_CONTACT, self::STAGE_SUBMITTED, self::STAGE_FULL_STARTED, self::STAGE_FULL_SUBMITTED ); $current = array_search( self::stage( $id ), $order, true ); $next = array_search( $stage, $order, true ); if ( false === $next || ( false !== $current && $next <= $current ) ) { return false; } update_post_meta( $id, 'mapp_stage', $stage ); update_post_meta( $id, 'mapp_' . $stage . '_at', time() ); return true; } /** * Progress percent for the 2.6 application_status field. 100 only once the * short app is submitted; reaching the contact step stores 80, which 2.6's * reminder email also keys on. * * @param int $id Post ID. * @param array $answers Answers. */ public static function refresh_progress( $id, $answers ) { $stage = self::stage( $id ); if ( in_array( $stage, array( self::STAGE_SUBMITTED, self::STAGE_FULL_STARTED, self::STAGE_FULL_SUBMITTED ), true ) ) { $percent = 100; } elseif ( self::STAGE_CONTACT === $stage ) { $percent = 80; } else { $short = Schema::questions_in( 'short' ); $visible = array_filter( array_keys( $short ), function ( $key ) use ( $answers ) { return Schema::is_visible( $key, $answers ); } ); $answered = array_filter( $visible, function ( $key ) use ( $answers ) { return isset( $answers[ $key ] ) && '' !== $answers[ $key ]; } ); $percent = $visible ? (int) floor( 70 * count( $answered ) / count( $visible ) ) : 0; } update_post_meta( $id, 'application_status', (string) $percent ); } }