Skip to content
wiki.fftac.org

Participants Database - Source Excerpt 04

Back to Participants Database

Summary

This source excerpt preserves a bounded section of Anti-Christ.org/wp-content/plugins/fftac-foundation-core/inc/participants-database.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/participants-database.php

if (!$saved_id) {
        update_post_meta($post_id, '_antichrist_participants_database_status', 'failed');
        update_post_meta($post_id, '_antichrist_participants_database_error', 'Participants Database write_participant returned false.');

        return array('status' => 'failed', 'message' => 'Participants Database write failed.');
    }

    update_post_meta($post_id, '_antichrist_participants_database_id', (int) $saved_id);
    update_post_meta($post_id, '_antichrist_participants_database_record_key', $source_key . ':' . $post_id);
    update_post_meta($post_id, '_antichrist_participants_database_status', 'synced');
    update_post_meta($post_id, '_antichrist_participants_database_error', '');
    update_post_meta($post_id, '_antichrist_participants_database_synced_at', gmdate(DATE_ATOM));

    return array('status' => 'synced', 'record_id' => (int) $saved_id);
}

/**
 * Mirrors all existing local private intake into Participants Database.
 *
 * @return array<string, int>
 */
function antichrist_engine_foundation_participants_database_sync_all_intake()
{
    $totals = array(
        'synced'      => 0,
        'failed'      => 0,
        'skipped'     => 0,
        'unavailable' => 0,
    );

    foreach (antichrist_engine_foundation_get_intake_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) {
                $totals['skipped']++;
                continue;
            }

            $result = antichrist_engine_foundation_participants_database_sync_intake_post((int) $post->ID, $source_key);
            $status = sanitize_key((string) ($result['status'] ?? 'skipped'));

            if (!isset($totals[$status])) {
                $totals[$status] = 0;
            }

            $totals[$status]++;
        }
    }

    return $totals;
}

/**
 * Mirrors existing WordPress users into Participants Database member records.
 *
 * @return array<string, int>
 */
function antichrist_engine_foundation_participants_database_sync_all_members()
{
    $totals = array(
        'synced'      => 0,
        'failed'      => 0,
        'skipped'     => 0,
        'unavailable' => 0,
    );

    if (!function_exists('get_users')) {
        return $totals;
    }

    $users = get_users(array(
        'fields' => 'all',
    ));

    foreach ($users as $user) {
        if (!$user instanceof WP_User) {
            $totals['skipped']++;
            continue;
        }

        $result = antichrist_engine_foundation_participants_database_sync_member($user);
        $status = sanitize_key((string) ($result['status'] ?? 'skipped'));

        if (!isset($totals[$status])) {
            $totals[$status] = 0;
        }

        $totals[$status]++;
    }

    return $totals;
}

/**
 * Handles the admin backfill action.
 *
 * @return void
 */
function antichrist_engine_foundation_participants_database_handle_sync()
{
    if (!current_user_can('manage_options')) {
        wp_die(esc_html__('You are not allowed to sync Participants Database records.', 'antichrist-engine'));
    }

    check_admin_referer('antichrist_engine_sync_participants_database');

    $intake_totals = antichrist_engine_foundation_participants_database_sync_all_intake();
    $member_totals = antichrist_engine_foundation_participants_database_sync_all_members();
    $synced_total = (int) ($intake_totals['synced'] ?? 0) + (int) ($member_totals['synced'] ?? 0);
    $failed_total = (int) ($intake_totals['failed'] ?? 0)
        + (int) ($intake_totals['unavailable'] ?? 0)
        + (int) ($member_totals['failed'] ?? 0)
        + (int) ($member_totals['unavailable'] ?? 0);
    $redirect = add_query_arg(
        array(
            'pdb_sync' => 'complete',
            'pdb_ok'   => $synced_total,
            'pdb_fail' => $failed_total,
        ),
        admin_url('tools.php?page=fftac-launch-readiness')
    );

    wp_safe_redirect($redirect);
    exit;
}
add_action('admin_post_antichrist_engine_sync_participants_database', 'antichrist_engine_foundation_participants_database_handle_sync');

/**
 * Counts local intake posts already linked to Participants Database records.
 *
 * @return int
 */
function antichrist_engine_foundation_participants_database_get_synced_count()
{
    $count = 0;

    foreach (antichrist_engine_foundation_get_intake_sources() as $definition) {
        $posts = get_posts(array(
            'post_type'      => (string) ($definition['post_type'] ?? ''),
            'post_status'    => 'any',
            'posts_per_page' => -1,
            'no_found_rows'  => true,
        ));

        foreach ($posts as $post) {
            if ($post instanceof WP_Post && absint(get_post_meta((int) $post->ID, '_antichrist_participants_database_id', true))) {
                $count++;
            }
        }
    }

    return $count;
}

/**
 * Counts WordPress member profiles already linked to Participants Database records.
 *
 * @return int
 */
function antichrist_engine_foundation_participants_database_get_synced_member_count()
{
    if (!function_exists('get_users')) {
        return 0;
    }

    $count = 0;
    $users = get_users(array(
        'fields' => 'all',
    ));

    foreach ($users as $user) {
        if ($user instanceof WP_User && absint(get_user_meta((int) $user->ID, '_antichrist_member_participants_database_id', true))) {
            $count++;
        }
    }

    return $count;
}

/**
 * Returns status details for the Participants Database bridge.
 *
 * @return array<string, mixed>
 */
function antichrist_engine_foundation_participants_database_get_status()
{
    $available = antichrist_engine_foundation_participants_database_is_available();
    $installed = antichrist_engine_foundation_participants_database_is_installed();
    $missing_fields = array_keys(antichrist_engine_foundation_participants_database_expected_fields());
    $shortcodes = array();

    if ($available) {
        antichrist_engine_foundation_participants_database_ensure_schema();
        $missing_fields = array();

        foreach (array_keys(antichrist_engine_foundation_participants_database_expected_fields()) as $field_name) {
            if (!antichrist_engine_foundation_participants_database_field_exists($field_name)) {
                $missing_fields[] = $field_name;
            }
        }

        if (method_exists('Participants_Db', 'plugin_shortcode_list')) {
            $shortcodes = array_values(array_filter((array) Participants_Db::plugin_shortcode_list(), static function ($shortcode) {
                return 0 === strpos((string) $shortcode, 'pdb_');
            }));
        }
    }

    $status = $available && !$missing_fields ? 'ready' : 'attention';
    $summary = __('Participants Database is active; FFTAC intake and member mirror fields are available.', 'antichrist-engine');
    $action = __('Use the backfill button after importing local intake records or creating member accounts outside the public forms.', 'antichrist-engine');

    if (!$available) {
        $summary = $installed
            ? __('Participants Database files are present, but the plugin is not active for this site.', 'antichrist-engine')
            : __('Participants Database is not installed in this WordPress tree.', 'antichrist-engine');
        $action = __('Install and activate Participants Database; Foundation Core will create FFTAC mirror fields and sync future intake submissions and member profile updates.', 'antichrist-engine');
    } elseif ($missing_fields) {
        $summary = sprintf(
            /* translators: %d is the number of missing Participants Database fields. */
            _n('%d FFTAC mirror field is missing.', '%d FFTAC mirror fields are missing.', count($missing_fields), 'antichrist-engine'),
            count($missing_fields)
        );
        $action = __('Open Tools -> FFTAC Launch Readiness and run the Participants Database backfill after the field schema is repaired.', 'antichrist-engine');
    }