Skip to content
wiki.fftac.org

Foundation - Source Excerpt 19

Back to Foundation

Summary

This source excerpt preserves a bounded section of Anti-Christ.org/wp-content/plugins/fftac-foundation-core/inc/foundation.php so readers can inspect the evidence without opening the full source file.

**Source path:** Anti-Christ.org/wp-content/plugins/fftac-foundation-core/inc/foundation.php

return (string) ($option[$label_key] ?? $option['name'] ?? $option['title'] ?? $value);
    }

    return (string) $option;
}

/**
 * Builds a normalized export row for one intake post.
 *
 * @param WP_Post $post       Intake post.
 * @param string  $source_key Intake source key.
 * @return array<string, scalar>
 */
function antichrist_engine_foundation_get_intake_export_row($post, $source_key)
{
    $post_id = (int) ($post->ID ?? 0);
    $sources = antichrist_engine_foundation_get_intake_sources();
    $source_label = (string) ($sources[$source_key]['label'] ?? $source_key);
    $row = array_fill_keys(antichrist_engine_foundation_get_intake_export_columns(), '');

    $row['intake_source'] = $source_label;
    $row['post_id']       = $post_id;
    $row['post_status']   = (string) ($post->post_status ?? '');
    $row['submitted_at']  = $post_id ? (string) get_post_time(DATE_ATOM, true, $post_id) : '';

    if ('signals' === $source_key) {
        $interest = sanitize_key((string) get_post_meta($post_id, '_antichrist_signals_interest', true));
        $row['name']  = (string) get_post_meta($post_id, '_antichrist_signals_name', true);
        $row['email'] = (string) get_post_meta($post_id, '_antichrist_signals_email', true);
        $row['lane']  = antichrist_engine_foundation_get_intake_option_label($interest, antichrist_engine_foundation_get_signal_interest_labels());
    } elseif ('contact' === $source_key) {
        $route = sanitize_key((string) get_post_meta($post_id, '_antichrist_contact_route', true));
        $row['name']    = (string) get_post_meta($post_id, '_antichrist_contact_name', true);
        $row['email']   = (string) get_post_meta($post_id, '_antichrist_contact_email', true);
        $row['lane']    = antichrist_engine_foundation_get_intake_option_label($route, antichrist_engine_foundation_contact_channels());
        $row['context'] = (string) get_post_meta($post_id, '_antichrist_contact_context', true);
        $row['subject'] = (string) get_post_meta($post_id, '_antichrist_contact_subject', true);
        $row['message'] = (string) get_post_meta($post_id, '_antichrist_contact_message', true);
    } elseif ('programs' === $source_key) {
        $program = sanitize_key((string) get_post_meta($post_id, '_antichrist_program_interest_program', true));
        $row['name']    = (string) get_post_meta($post_id, '_antichrist_program_interest_name', true);
        $row['email']   = (string) get_post_meta($post_id, '_antichrist_program_interest_email', true);
        $row['lane']    = antichrist_engine_foundation_get_intake_option_label($program, antichrist_engine_foundation_program_interest_options());
        $row['context'] = (string) get_post_meta($post_id, '_antichrist_program_interest_role', true);
        $row['notes']   = (string) get_post_meta($post_id, '_antichrist_program_interest_notes', true);
    }

    return $row;
}

/**
 * Returns normalized rows for the internal intake export.
 *
 * @param string $source Intake source key, post type, or "all".
 * @return array<int, array<string, scalar>>
 */
function antichrist_engine_foundation_get_intake_export_rows($source = 'all')
{
    $source = antichrist_engine_foundation_normalize_intake_source_key($source);
    $sources = antichrist_engine_foundation_get_intake_sources();
    $selected_sources = 'all' === $source ? $sources : array($source => $sources[$source]);
    $rows = array();

    foreach ($selected_sources as $source_key => $definition) {
        $posts = get_posts(array(
            'post_type'      => (string) ($definition['post_type'] ?? ''),
            'post_status'    => 'any',
            'posts_per_page' => -1,
            'orderby'        => 'date',
            'order'          => 'DESC',
            'no_found_rows'  => true,
        ));

        foreach ($posts as $post) {
            if ($post instanceof WP_Post) {
                $rows[] = antichrist_engine_foundation_get_intake_export_row($post, $source_key);
            }
        }
    }

    return $rows;
}

/**
 * Converts intake export rows into CSV text with a stable header row.
 *
 * @param array<int, array<string, scalar>> $rows Intake rows.
 * @return string
 */
function antichrist_engine_foundation_convert_intake_rows_to_csv($rows)
{
    $headers = antichrist_engine_foundation_get_intake_export_columns();
    $lines = array();

    $lines[] = implode(',', array_map(static function ($header) {
        return '"' . str_replace('"', '""', (string) $header) . '"';
    }, $headers));

    foreach ($rows as $row) {
        $line = array();

        foreach ($headers as $header) {
            $value = $row[$header] ?? '';
            $value = is_scalar($value) || null === $value ? (string) $value : wp_json_encode($value);
            $value = str_replace(array("\r\n", "\r"), "\n", $value);

            $line[] = '"' . str_replace('"', '""', $value) . '"';
        }

        $lines[] = implode(',', $line);
    }

    return implode("\n", $lines) . "\n";
}

/**
 * Returns a generated CSV payload for private intake export.
 *
 * @param string $source Intake source key, post type, or "all".
 * @return array<string, string>
 */
function antichrist_engine_foundation_get_intake_export_payload($source = 'all')
{
    $source = antichrist_engine_foundation_normalize_intake_source_key($source);
    $rows = antichrist_engine_foundation_get_intake_export_rows($source);
    $filename_source = 'all' === $source ? 'all' : $source;

    return array(
        'filename'     => 'fftac-launch-intake-' . $filename_source . '-' . gmdate('Ymd-His') . '.csv',
        'content_type' => 'text/csv; charset=utf-8',
        'content'      => antichrist_engine_foundation_convert_intake_rows_to_csv($rows),
    );
}

/**
 * Returns counts and export links for the internal intake operations panel.
 *
 * @return array<string, array<string, mixed>>
 */
function antichrist_engine_foundation_get_intake_operations_summary()
{
    $summary = array();
    $total = 0;

    foreach (antichrist_engine_foundation_get_intake_sources() as $source_key => $definition) {
        $counts = wp_count_posts((string) ($definition['post_type'] ?? ''));
        $count = isset($counts->private) ? (int) $counts->private : 0;
        $total += $count;

        $summary[$source_key] = array(
            'label'       => (string) ($definition['plural_label'] ?? $definition['label'] ?? $source_key),
            'description' => (string) ($definition['description'] ?? ''),
            'post_type'   => (string) ($definition['post_type'] ?? ''),
            'count'       => $count,
            'export_url'  => wp_nonce_url(
                add_query_arg(
                    array(
                        'action' => 'antichrist_engine_export_intake',
                        'intake' => $source_key,
                    ),
                    admin_url('admin-post.php')
                ),
                'antichrist_engine_export_intake'
            ),
        );
    }

    $summary['all'] = array(
        'label'       => __('All launch intake', 'antichrist-engine'),
        'description' => __('Combined CSV export for migration into future newsletter, support, or program operations tooling.', 'antichrist-engine'),
        'post_type'   => '',
        'count'       => $total,
        'export_url'  => wp_nonce_url(
            add_query_arg(
                array(
                    'action' => 'antichrist_engine_export_intake',
                    'intake' => 'all',
                ),
                admin_url('admin-post.php')
            ),
            'antichrist_engine_export_intake'
        ),
    );

    return $summary;
}

/**
 * Streams an admin-only CSV export of private launch intake records.
 *
 * @return void
 */
function antichrist_engine_foundation_handle_intake_export()
{
    if (!current_user_can('manage_options')) {
        wp_die(esc_html__('You do not have permission to export FFTAC launch intake.', 'antichrist-engine'));
    }

    check_admin_referer('antichrist_engine_export_intake');

    $source = isset($_GET['intake']) ? sanitize_key(wp_unslash($_GET['intake'])) : 'all';
    $payload = antichrist_engine_foundation_get_intake_export_payload($source);
    $filename = preg_replace('/[^A-Za-z0-9._-]/', '-', (string) ($payload['filename'] ?? 'fftac-launch-intake.csv'));

    if (function_exists('nocache_headers')) {
        nocache_headers();
    }

    header('Content-Type: ' . (string) ($payload['content_type'] ?? 'text/csv; charset=utf-8'));
    header('Content-Disposition: attachment; filename="' . $filename . '"');
    header('X-Content-Type-Options: nosniff');
    echo (string) ($payload['content'] ?? '');
    exit;
}