'satisfaction', '18' => 'nps', '8' => 'priorities', '21' => 'site_design', '22' => 'site_range', '23' => 'site_prices', '24' => 'site_speed', '25' => 'site_functionality', '26' => 'site_payment', '27' => 'site_shipping', '29' => 'site_navigation', '30' => 'site_search', '31' => 'site_filters', '33' => 'site_descriptions', '47' => 'site_checkout', '32' => 'reco_helpful', '62' => 'rate_seminar', '63' => 'rate_blog', '64' => 'rate_finder', '35' => 'used_seminar', '38' => 'used_blog', '36' => 'used_finder', '51' => 'is_professional', '70' => 'will_followup', '12' => 'open_feedback', ); const DISCOVERY_18 = array( '44' => 'store', '45' => 'search', '73' => 'ad', '43' => 'word_of_mouth', ); const MAP_20 = array( '21' => 'delivery_speed', '101' => 'packaging', '103' => 'arrived_undamaged', '109' => 'will_repurchase', ); public static function install_map( $form_id ) { $form_id = (int) $form_id; $map = 18 === $form_id ? self::MAP_18 : ( 20 === $form_id ? self::MAP_20 : array() ); if ( ! $map ) { return 0; } $surface = AIEO_Form_Registry::surface( 'gravity', $form_id ); $stored = AIEO_Survey_Mapping::get( $surface ); $stored['blueprint'] = 18 === $form_id ? 'post_order' : 'post_delivery'; $stored['context'] = 18 === $form_id ? array( 'order' => '15', 'email' => '92', 'customer' => '16' ) : array( 'order' => '', 'email' => '17', 'customer' => '' ); $n = 0; foreach ( $map as $fid => $code ) { if ( ! AIEO_Survey_Blueprint::question( $code ) ) { continue; } $stored['fields'][ (string) $fid ] = array( 'question' => $code, 'choice' => '', 'status' => 'manual', 'score' => 1.0 ); $n++; } if ( 18 === $form_id ) { foreach ( self::DISCOVERY_18 as $fid => $choice ) { $stored['fields'][ (string) $fid ] = array( 'question' => 'discovery', 'choice' => $choice, 'status' => 'manual', 'score' => 1.0 ); $n++; } } AIEO_Survey_Mapping::save( $surface, $stored ); return $n; } public static function seed_register( $form_id ) { $form_id = (int) $form_id; $source = new AIEO_Survey_Source_Gravity( $form_id, true ); if ( ! $source->available() ) { return 0; } $catalog = $source->field_catalog(); $codes = AIEO_Survey_Mapping::resolved( $source->surface() ); $n = 0; foreach ( $codes as $fid => $code ) { $q = AIEO_Survey_Blueprint::question( $code ); if ( ! $q || ! isset( $catalog[ $fid ] ) ) { continue; } if ( 'scale' === $q['scored'] || ! $q['choices'] ) { continue; } $their = array_values( (array) $catalog[ $fid ]['choices'] ); $ours = array_keys( $q['choices'] ); if ( ! $their || count( $their ) !== count( $ours ) ) { continue; } foreach ( $their as $i => $label ) { if ( AIEO_Survey_Choice_Register::learn( $label, $ours[ $i ], $q['choices'][ $ours[ $i ] ], 'auto', $code ) ) { $n++; } } } return $n; } public static function target_surface( $form_id ) { return 18 === (int) $form_id ? 'aieo_post_order' : 'aieo_post_delivery'; } public static function can_migrate( $form_id ) { global $wpdb; $entries = AIEO_Form_CPT::entries_table(); $surface = self::target_surface( $form_id ); $clash = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$entries} WHERE surface = %s AND source NOT IN ('gravity', 'pods')", $surface ) ); if ( $clash > 0 ) { return new WP_Error( 'aieo_survey_migration_unsafe', sprintf( __( 'Refusing to migrate: %1$d native responses already exist on %2$s, and a migrated entry id could overwrite one of them. Migrate before the native survey goes live, or move those responses first.', 'ai-eshop-optimizer' ), $clash, $surface ) ); } return true; } public static function migrate( $form_id, $write = true, $chunk = 200 ) { $source = new AIEO_Survey_Source_Gravity( (int) $form_id, true ); $out = array( 'entries' => 0, 'rows' => 0, 'written' => 0, 'failed' => 0, 'error' => '' ); if ( ! $source->available() ) { $out['error'] = 'source unavailable'; return $out; } if ( $write ) { $safe = self::can_migrate( $form_id ); if ( is_wp_error( $safe ) ) { $out['error'] = $safe->get_error_message(); return $out; } } $target = self::target_surface( $form_id ); $after = 0; while ( true ) { $ids = $source->entry_ids( array( 'after_id' => $after, 'limit' => max( 1, (int) $chunk ) ) ); if ( ! $ids ) { break; } foreach ( $ids as $id ) { $after = max( $after, (int) $id ); $entry = $source->entry( $id ); if ( ! $entry || ! $entry['rows'] ) { continue; } $out['entries']++; $out['rows'] += count( $entry['rows'] ); if ( ! $write ) { continue; } $res = AIEO_Form_Entries::record_external( $target, $id, $entry['rows'], $entry['context'] ); if ( is_wp_error( $res ) ) { $out['failed']++; } else { $out['written']++; } } } return $out; } public static function verify( $form_id ) { global $wpdb; $form_id = (int) $form_id; $source = new AIEO_Survey_Source_Gravity( $form_id, true ); $surface = self::target_surface( $form_id ); $codes = AIEO_Survey_Mapping::resolved( $source->surface() ); $meta = $wpdb->prefix . 'gf_entry_meta'; $ent = $wpdb->prefix . 'gf_entry'; $entries = AIEO_Form_CPT::entries_table(); $values = AIEO_Form_CPT::entry_values_table(); $out = array(); foreach ( $codes as $fid => $code ) { $gravity = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(DISTINCT m.entry_id) FROM `{$meta}` m JOIN `{$ent}` e ON e.id = m.entry_id WHERE m.form_id = %d AND ( e.status = 'active' OR e.status IS NULL ) AND ( m.meta_key = %s OR m.meta_key LIKE %s ) AND m.meta_value <> ''", $form_id, (string) $fid, $wpdb->esc_like( (string) $fid . '.' ) . '%' ) ); $migrated = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(DISTINCT v.entry_id) FROM {$values} v JOIN {$entries} e ON e.id = v.entry_id WHERE e.surface = %s AND v.field_key = %s AND v.disposition = 'answered'", $surface, $code ) ); $multi = count( array_keys( $codes, $code, true ) ) > 1; $out[ (string) $fid ] = array( 'code' => $code, 'gravity' => $gravity, 'migrated' => $migrated, 'ok' => $multi ? ( $migrated > 0 ) : ( $gravity === $migrated ), 'multi' => $multi, ); } return $out; } }