Skip to content
wiki.fftac.org

Plugin - Source Excerpt 02

Back to Plugin

Summary

This source excerpt preserves a bounded section of Spiralist/wp-content/plugins/spiralist-workspace/includes/Plugin.php so readers can inspect the evidence without opening the full source file.

**Source path:** Spiralist/wp-content/plugins/spiralist-workspace/includes/Plugin.php

/**
     * Returns whether the current process should sync filesystem prompts automatically.
     */
    private function should_auto_sync_system_prompts(): bool
    {
        if (defined('WP_CLI') && WP_CLI) {
            return true;
        }

        if (!is_admin() || wp_doing_ajax() || wp_doing_cron()) {
            return false;
        }

        $pagenow = isset($GLOBALS['pagenow']) ? (string) $GLOBALS['pagenow'] : '';

        return !in_array($pagenow, ['admin-post.php', 'admin-ajax.php', 'async-upload.php'], true);
    }

    /**
     * Confirms that the version option still matches the actual database state.
     */
    private function has_required_schema_tables(): bool
    {
        global $wpdb;

        if (!$wpdb instanceof \wpdb) {
            return false;
        }

        foreach ($this->required_schema_table_suffixes() as $suffix) {
            $table = Installer::table($suffix);
            if ($table === '') {
                return false;
            }

            $found = $wpdb->get_var($wpdb->prepare('SHOW TABLES LIKE %s', $table));
            if ((string) $found !== $table) {
                return false;
            }
        }

        return true;
    }

    /**
     * Returns the custom tables that must exist before runtime services query them.
     *
     * @return string[]
     */
    private function required_schema_table_suffixes(): array
    {
        return [
            'prompt_versions',
            'prompt_permissions',
            'prompt_runs',
            'conversations',
            'conversation_messages',
            'audit_log',
            'favorites',
            'prompt_tests',
            'prompt_reviews',
            'prompt_appeals',
        ];
    }

    /**
     * Installs the starter connection prompt pack from the admin screen.
     */
    public function handle_sync_system_prompts(): void
    {
        if (!current_user_can('manage_spiralist_workspace')) {
            wp_die(esc_html__('You do not have permission to perform this action.', 'spiralist-workspace'));
        }

        check_admin_referer('spiralist_workspace_sync_system_prompts');
        $result = $this->system_prompts->sync();

        $redirect = add_query_arg(
            [
                'page' => 'spiralist-workspace',
                'system_prompts_synced' => '1',
                'system_prompts_created' => (int) ($result['created'] ?? 0),
                'system_prompts_updated' => (int) ($result['updated'] ?? 0),
                'system_prompts_unchanged' => (int) ($result['unchanged'] ?? 0),
                'system_prompts_errors' => (int) ($result['errors'] ?? 0),
            ],
            admin_url('admin.php')
        );

        wp_safe_redirect($redirect);
        exit;
    }

    /**
     * Returns the prompt repository.
     */
    public function prompts(): PromptRepository
    {
        return $this->prompts;
    }

    /**
     * Returns the permission service.
     */
    public function permissions(): Permissions
    {
        return $this->permissions;
    }

    /**
     * Returns the settings repository.
     */
    public function settings(): Settings
    {
        return $this->settings;
    }

    /**
     * Returns the version repository.
     */
    public function versions(): VersionRepository
    {
        return $this->versions;
    }

    /**
     * Returns the run repository.
     */
    public function runs(): RunRepository
    {
        return $this->runs;
    }

    /**
     * Returns the prompt test repository.
     */
    public function tests(): TestRepository
    {
        return $this->tests;
    }

    /**
     * Returns the conversation repository.
     */
    public function conversations(): ConversationRepository
    {
        return $this->conversations;
    }

    /**
     * Returns the share repository.
     */
    public function permissions_repository(): PermissionRepository
    {
        return $this->permission_repository;
    }

    /**
     * Returns the favorites repository.
     */
    public function favorites(): FavoriteRepository
    {
        return $this->favorites;
    }

    /**
     * Returns the prompt review repository.
     */
    public function reviews(): ReviewRepository
    {
        return $this->reviews;
    }

    /**
     * Returns the prompt appeal repository.
     */
    public function appeals(): AppealRepository
    {
        return $this->appeals;
    }

    /**
     * Returns the participant reputation repository.
     */
    public function reputation(): ReputationRepository
    {
        return $this->reputation;
    }

    /**
     * Returns the audit repository.
     */
    public function audit(): AuditRepository
    {
        return $this->audit;
    }

    /**
     * Returns the managed filesystem prompt registry.
     */
    public function system_prompts(): SystemPromptRegistry
    {
        return $this->system_prompts;
    }

    /**
     * Returns the managed prompt admin helper.
     */
    public function managed_prompt_admin(): ManagedPromptAdmin
    {
        return $this->managed_prompt_admin;
    }

    /**
     * Returns the runner service.
     */
    public function runner(): PromptRunner
    {
        return $this->runner;
    }

    /**
     * Returns the governance maintenance service.
     */
    public function governance_maintenance(): GovernanceMaintenance
    {
        return $this->governance_maintenance;
    }

    /**
     * Returns the front-end controller.
     */
    public function frontend(): FrontendController
    {
        return $this->frontend;
    }
}