# kreatifsayfa/wordpress-twitter-auto-shared architecture

> Architecture diagram of the GitHub repository kreatifsayfa/wordpress-twitter-auto-shared, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-13). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/kreatifsayfa/wordpress-twitter-auto-shared
- Repository: https://github.com/kreatifsayfa/wordpress-twitter-auto-shared

## Overview

##### Project type and purpose
- This is a WordPress plugin (“TurkNews Twitter Auto”) whose job is to detect new posts on a WordPress site and automatically share them on Twitter with customizable templates, hashtags and media attachments.

##### File structure
- Top‐level plugin file: turknews-twitter-auto.php (bootstrap & hook registrations)
- includes/ directory—each file is a class representing a distinct feature or subsystem:
- class-twitter-api.php (abstracts Twitter REST calls)
- class-tweet-retweeter.php (orchestrates tweet creation & dispatch)
- class-scheduler.php (uses WP Cron to schedule posting)
- class-error-manager.php (central error logging & retry logic)
- class-analytics.php (collects & reports usage metrics)
- class-ab-testing.php (A/B tests of tweet templates)
- class-content-optimizer.php (applies template tokens, category hashtags, image inclusion)
- class-engagement.php (handles replies/likes tracking)
- class-multi-account.php (supports multiple Twitter credentials)
- class-smart-features.php (e.g. link shortening, smart scheduling)

##### Extract architectural hints from README
- Hooks into WordPress publish‐post event (new post → automatic tweet)
- Settings stored in WP admin (encrypted tokens in DB)
- SSL‐secured API calls, rate‐limiting checks
- Debug mode, admin UI

##### Main components
a. WordPress Core (event dispatcher, cron engine, options & metadata database)
b. Plugin Layer, subdivided into:
- Admin/UI Module (Settings page, input of auth & CSRF tokens, toggle switches)
- Scheduler Module (class-scheduler.php; WP Cron hooks)
- Tweet Orchestration Layer:
– Content Optimizer (templates, hashtags, featured image)
– A/B Testing Engine
– Multi-Account Selector
– Smart Features (link shortener, scheduling heuristics)
- Twitter API Adapter (REST client over SSL)
- Error Manager & Analytics (logging, retry, metrics)
c. External Service: Twitter REST API

##### Relationships & data flow
1. Admin user sets credentials & templates → stored in WP options table
2. A new post is published → WordPress “publish_post” hook triggers Scheduler
3. Scheduler invokes Tweet Orchestration Layer
4. Content Optimizer + A/B Testing + Multi-Account + Smart Features prepare the final tweet payload
5. Twitter API Adapter sends request over HTTPS → receives response
6. Error Manager handles failures, enqueues retries, logs to Analytics
7. Analytics Engine records success/failure, engagement data
8. (Optional) Engagement Module polls Twitter API for metrics on posted tweets

##### Architectural patterns & design principles
- Hook‐based plugin bootstrap (inversion of control via WP hooks)
- Modular single‐responsibility classes (each class does one thing)
- Layered approach: UI → scheduling → business logic → external API
- Dependency injection (classes depend on an HTTP client or WP database abstraction)
- Retry/circuit‐breaker pattern in Error Manager
- Cron‐driven asynchronous processing

##### Elements
- WordPress Core (event dispatcher, cron, DB)
- Plugin bootstrap file (initial hook registrations)
- Admin Settings UI
- Scheduler module
- Business logic components (optimizer, ab-testing, multi-account, smart-features)
- Twitter API adapter
- Error Manager & Analytics
- WP database (options/encrypted tokens)
- Twitter API (external)

##### Detail vs. over‐segmentation
- How settings flow into modules at runtime
- Full end‐to‐end flow from “new post published” to “tweet delivered and logged”
- Don’t omit retry logic or analytics, even if they run in the background

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% WordPress Core container
    subgraph "WordPress Core"
        direction TB
        WP_EventDispatcher["Event Dispatcher"]:::hooks
        WP_CronEngine["Cron Engine"]:::hooks
        WP_DB["Options & Metadata DB"]:::data
    end

    %% Plugin Layer container
    subgraph "TurkNews Twitter Auto Plugin"
        direction TB

        %% Bootstrap
        Bootstrap["Plugin Bootstrap & Hook Registrations"]:::bootstrap

        %% Admin UI
        subgraph "Admin Settings UI"
            direction TB
            AdminUI["Settings Page (credentials, templates)"]:::admin
        end

        %% Scheduler & Hooks
        subgraph "Scheduler & Hooks"
            direction TB
            Scheduler["Scheduler<br>(class-scheduler.php)"]:::scheduler
        end

        %% Business Logic
        subgraph "Tweet Orchestration Layer"
            direction TB
            Retweeter["Core Orchestrator<br>(class-tweet-retweeter.php)"]:::business
            ContentOptimizer["Content Optimizer<br>(class-content-optimizer.php)"]:::business
            ABTesting["A/B Testing Engine<br>(class-ab-testing.php)"]:::business
            MultiAccount["Multi-Account Selector<br>(class-multi-account.php)"]:::business
            SmartFeatures["Smart Features<br>(class-smart-features.php)"]:::business
        end

        %% External Adapter
        subgraph "Twitter API Adapter"
            direction TB
            TwitterAPI["REST Client over SSL<br>(class-twitter-api.php)"]:::external
        end

        %% Utilities: Error & Analytics & Engagement
        subgraph "Utilities"
            direction TB
            ErrorManager["Error Manager & Retry Logic<br>(class-error-manager.php)"]:::utility
            Analytics["Analytics Engine<br>(class-analytics.php)"]:::utility
            Engagement["Engagement Module<br>(class-engagement.php)"]:::utility
        end
    end

    %% External Service
    Twitter["Twitter REST API"]:::external

    %% Data flows and relationships
    AdminUI -->|stores settings| WP_DB
    WP_EventDispatcher -->|publish_post event| Scheduler
    Scheduler -->|invoke| Retweeter
    Retweeter -->|optimize| ContentOptimizer
    Retweeter -->|AB test| ABTesting
    Retweeter -->|select account| MultiAccount
    Retweeter -->|apply smart features| SmartFeatures
    Retweeter -->|assemble payload| TwitterAPI
    TwitterAPI -->|HTTPS request/response| Twitter
    Twitter -->|response| TwitterAPI
    TwitterAPI -->|success/failure| Retweeter
    TwitterAPI -->|error| ErrorManager
    ErrorManager -->|retry enqueued| Scheduler
    ErrorManager -->|log error| Analytics
    Retweeter -->|log success| Analytics
    Engagement -->|poll metrics| TwitterAPI
    Engagement -->|record engagement| Analytics

    %% Hook registrations
    Bootstrap -->|register hooks| WP_EventDispatcher
    Bootstrap -->|register cron| WP_CronEngine
    Bootstrap -->|initialize modules| AdminUI
    Bootstrap -->|initialize modules| Scheduler
    Bootstrap -->|initialize modules| Retweeter
    Bootstrap -->|initialize modules| ErrorManager
    Bootstrap -->|initialize modules| Analytics
    Bootstrap -->|initialize modules| Engagement

    %% Click events
    click Bootstrap "https://github.com/kreatifsayfa/wordpress-twitter-auto-shared/blob/main/turknews-twitter-auto.php"
    click Scheduler "https://github.com/kreatifsayfa/wordpress-twitter-auto-shared/blob/main/includes/class-scheduler.php"
    click Retweeter "https://github.com/kreatifsayfa/wordpress-twitter-auto-shared/blob/main/includes/class-tweet-retweeter.php"
    click ContentOptimizer "https://github.com/kreatifsayfa/wordpress-twitter-auto-shared/blob/main/includes/class-content-optimizer.php"
    click ABTesting "https://github.com/kreatifsayfa/wordpress-twitter-auto-shared/blob/main/includes/class-ab-testing.php"
    click MultiAccount "https://github.com/kreatifsayfa/wordpress-twitter-auto-shared/blob/main/includes/class-multi-account.php"
    click SmartFeatures "https://github.com/kreatifsayfa/wordpress-twitter-auto-shared/blob/main/includes/class-smart-features.php"
    click TwitterAPI "https://github.com/kreatifsayfa/wordpress-twitter-auto-shared/blob/main/includes/class-twitter-api.php"
    click ErrorManager "https://github.com/kreatifsayfa/wordpress-twitter-auto-shared/blob/main/includes/class-error-manager.php"
    click Analytics "https://github.com/kreatifsayfa/wordpress-twitter-auto-shared/blob/main/includes/class-analytics.php"
    click Engagement "https://github.com/kreatifsayfa/wordpress-twitter-auto-shared/blob/main/includes/class-engagement.php"

    %% Styles
    classDef admin fill:#D0E8FF,stroke:#0366D6,color:#012A4A
    classDef scheduler fill:#FFE5B4,stroke:#FF8C00,color:#663C00
    classDef business fill:#E2F0D9,stroke:#2E8B57,color:#114B08
    classDef external fill:#F8D7DA,stroke:#C82333,color:#721C24
    classDef data fill:#E2E3E5,stroke:#6C757D,color:#343A40
    classDef hooks fill:#FFF3CD,stroke:#856404,color:#533F03
    classDef bootstrap fill:#F0F0F0,stroke:#888888,color:#333333
    classDef utility fill:#FFF7E6,stroke:#D4A017,color:#5A3E02
```
