Skip to content
This repository was archived by the owner on Nov 29, 2026. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ WordPress (documentation CPT)

## Features

- **GitHub sync** — full and incremental sync via commit SHA comparison
- **GitHub sync** — deterministic Data Machine flows with a legacy commit-SHA sync fallback
- **Markdown processing** — GitHub Flavored Markdown with internal link resolution
- **Hierarchical organization** — `project` and `project_type` taxonomies with deep URL routing
- **REST API** — full CRUD under `docsync/v1` with markdown output for AI agents
- **WP-CLI** — `wp docsync docs {list, get, search, sync}` and `wp docsync project {ensure, tree}`
- **WP Abilities API** — AI agent capabilities for sync, search, and project inspection
- **Orphan cleanup** — docs removed from the repo are automatically deleted
- **Scheduled sync** — configurable WP-Cron intervals (hourly / twice daily / daily)
- **Scheduled sync** — daily Data Machine flows, or configurable WP-Cron intervals in fallback mode
- **Admin UI** — settings page with GitHub PAT configuration, connection diagnostics, per-project sync controls
- **Theme-agnostic** — works on any WordPress theme. Frontend templates are opt-in.

Expand All @@ -45,27 +45,35 @@ WordPress (documentation CPT)

1. Upload the `docsync` directory to `wp-content/plugins/`
2. Activate the plugin
3. Go to **Documentation → Settings** and enter your GitHub Personal Access Token
4. Create a project term under **Documentation → Projects**, set its GitHub URL
5. Sync: click "Sync" in the admin, use `wp docsync docs sync --all`, or wait for cron
3. Create a top-level project term under **Documentation → Projects** and set its GitHub URL
4. Activate Data Machine and the `data-machine-github` extension, configure GitHub credentials there if needed, then provision daily sync flows:

```bash
wp docsync project flows ensure --dry-run
wp docsync project flows ensure
```

5. To queue an immediate run, click **Sync Now** on the project or use `wp docsync docs sync <term_id>`.

Use `--project=<slug>` to provision a single project. Re-run the command after changing a project's GitHub URL or docs path. See the [Sync Guide](docs/sync-guide.md#data-machine-sync) for setup and monitoring. If Data Machine or its GitHub handler is unavailable, configure a GitHub PAT under **Documentation → Settings** to use DocSync's legacy WP-Cron sync.

## Configuration

### GitHub PAT

Generate a [Personal Access Token](https://github.com/settings/tokens) with `repo` scope (for private repos) or no scope (for public repos only). Enter it at **Documentation → Settings**.
For the legacy fallback, generate a [Personal Access Token](https://github.com/settings/tokens) with `repo` scope (for private repos) or no scope (for public repos only). Enter it at **Documentation → Settings**.

### Adding a Repository

1. Go to **Documentation → Projects**
2. Create or edit a term
3. Set **GitHub URL** (e.g., `https://github.com/your-org/your-repo`)
4. Set **Docs Path** (default: `docs`) — the directory in the repo containing `.md` files
5. Click **Sync** to pull docs
5. If using Data Machine, run `wp docsync project flows ensure --project=<slug>` before clicking **Sync Now**

### Sync Intervals

Configure at **Documentation → Settings**:
Data Machine flows are provisioned on a daily schedule. In legacy WP-Cron mode, configure the interval at **Documentation → Settings**:
- **Hourly** — best for actively developed projects
- **Twice Daily** — good default
- **Daily** — for stable projects
Expand Down
65 changes: 39 additions & 26 deletions assets/js/admin-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* Handles sync operations via REST API on settings and term edit pages.
*/

/* global docSyncAdmin:readonly */

document.addEventListener( 'DOMContentLoaded', function() {
'use strict';

Expand All @@ -22,23 +24,23 @@ document.addEventListener( 'DOMContentLoaded', function() {
syncAllStatus.textContent = docSyncAdmin.strings.syncing;
syncAllStatus.className = '';

fetch( docSyncAdmin.restUrl + '/abilities/docsync/sync-docs-batch/run', {
fetch( docSyncAdmin.syncAbilityUrl, {
method: 'POST',
headers: headers,
body: JSON.stringify( {} ),
headers,
body: JSON.stringify( { input: {} } ),
credentials: 'same-origin',
} )
.then( response => response.json() )
.then( data => {
syncAllButton.disabled = false;

if ( data.errors && data.errors.length > 0 ) {
syncAllStatus.textContent = 'Synced ' + data.repos_synced + ' repos (' + data.errors.length + ' errors)';
syncAllStatus.className = 'sync-warning';
} else {
syncAllStatus.textContent = 'Synced ' + data.repos_synced + ' repos';
syncAllStatus.className = 'sync-success';
if ( ! data.success ) {
throw new Error( data.error || data.message || 'Sync failed.' );
}
const summary = data.repos_queued ? 'Queued ' + data.repos_queued + ' Data Machine flows' : 'Synced ' + data.repos_synced + ' repos';
const errors = data.errors || [];
syncAllStatus.textContent = errors.length ? summary + ': ' + errors.join( '; ' ) : summary;
syncAllStatus.className = errors.length ? 'sync-warning' : 'sync-success';
} )
.catch( error => {
syncAllButton.disabled = false;
Expand All @@ -59,7 +61,7 @@ document.addEventListener( 'DOMContentLoaded', function() {

fetch( docSyncAdmin.restUrl + '/sync/test-token', {
method: 'GET',
headers: headers,
headers,
credentials: 'same-origin',
} )
.then( response => response.json() )
Expand Down Expand Up @@ -106,36 +108,41 @@ document.addEventListener( 'DOMContentLoaded', function() {
}

termSyncButton.disabled = true;
if ( termTestButton ) termTestButton.disabled = true;
if ( termTestButton ) {
termTestButton.disabled = true;
}
termResults.innerHTML = '<span class="spinner is-active" style="float:none; margin:0 5px 0 0;"></span>' + docSyncAdmin.strings.syncing;

fetch( docSyncAdmin.restUrl + '/abilities/docsync/sync-docs/run', {
fetch( docSyncAdmin.syncAbilityUrl, {
method: 'POST',
headers: headers,
body: JSON.stringify( { term_id: parseInt( termId ) } ),
headers,
body: JSON.stringify( { input: { term_id: parseInt( termId, 10 ) } } ),
credentials: 'same-origin',
} )
.then( response => response.json() )
.then( data => {
termSyncButton.disabled = false;
if ( termTestButton ) termTestButton.disabled = false;
if ( termTestButton ) {
termTestButton.disabled = false;
}

if ( data.success ) {
if ( ! data.success ) {
termResults.textContent = docSyncAdmin.strings.error + ' ' + ( data.error || data.message || 'Sync failed.' );
} else if ( data.queued ) {
termResults.textContent = 'Data Machine sync queued (job ' + data.job_id + ').';
} else {
let html = '<div style="background:#f0faf0; border-left:4px solid #00a32a; padding:10px; margin-top:10px;">';
html += '<p style="color:#00a32a; margin:0 0 8px;"><strong>' + docSyncAdmin.strings.success + '</strong></p>';
html += '<p style="margin:0;">Added: ' + data.added.length + ', Updated: ' + data.updated.length + ', Removed: ' + data.removed.length + (data.renamed.length > 0 ? ', Renamed: ' + data.renamed.length : '') + '</p>';
html += '</div>';
termResults.innerHTML = html;
} else if ( data.error ) {
let html = '<div style="background:#fcf0f1; border-left:4px solid #d63638; padding:10px; margin-top:10px;">';
html += '<p style="color:#d63638; margin:0;"><strong>' + docSyncAdmin.strings.error + '</strong> ' + data.error + '</p>';
html += '</div>';
termResults.innerHTML = html;
}
} )
.catch( error => {
termSyncButton.disabled = false;
if ( termTestButton ) termTestButton.disabled = false;
if ( termTestButton ) {
termTestButton.disabled = false;
}
termResults.innerHTML = '<div style="color:#d63638;">Error: ' + error.message + '</div>';
} );
} );
Expand All @@ -151,19 +158,23 @@ document.addEventListener( 'DOMContentLoaded', function() {
}

termTestButton.disabled = true;
if ( termSyncButton ) termSyncButton.disabled = true;
if ( termSyncButton ) {
termSyncButton.disabled = true;
}
termResults.innerHTML = '<span class="spinner is-active" style="float:none; margin:0 5px 0 0;"></span>' + docSyncAdmin.strings.testingRepo;

fetch( docSyncAdmin.restUrl + '/sync/test-repo', {
method: 'POST',
headers: headers,
headers,
credentials: 'same-origin',
body: JSON.stringify( { repo_url: repoUrl } ),
} )
.then( response => response.json() )
.then( data => {
termTestButton.disabled = false;
if ( termSyncButton ) termSyncButton.disabled = false;
if ( termSyncButton ) {
termSyncButton.disabled = false;
}

if ( data.success || data.full_name ) {
let html = '<div style="background:#f0faf0; border-left:4px solid #00a32a; padding:10px; margin-top:10px;">';
Expand Down Expand Up @@ -195,7 +206,9 @@ document.addEventListener( 'DOMContentLoaded', function() {
} )
.catch( error => {
termTestButton.disabled = false;
if ( termSyncButton ) termSyncButton.disabled = false;
if ( termSyncButton ) {
termSyncButton.disabled = false;
}
termResults.innerHTML = '<div style="color:#d63638;">Error: ' + error.message + '</div>';
} );
} );
Expand Down
20 changes: 20 additions & 0 deletions docs/sync-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,26 @@ For exact request/response shapes and permissions, see the [API Reference](api-r

The sync system allows you to automatically import documentation from external repositories, convert Markdown to HTML, and organize content using the project taxonomy. Version 0.2.1 introduces enhanced project-based sync with improved term management and hierarchical subpath support.

## Data Machine sync

Activate **Data Machine** and **Data Machine GitHub** (`data-machine-github`). Configure GitHub credentials in Data Machine for private repositories or authenticated API limits. DocSync's own PAT is only used by the legacy sync and its diagnostic tools.

Set `project_github_url` on each top-level `project` term. Set `project_docs_path` to the docs directory (defaults to `docs`), or `.` to fetch from the repository root. Provision the flows with:

```bash
wp docsync project flows ensure --dry-run
wp docsync project flows ensure
wp docsync project flows ensure --project=my-project
```

The `docsync/ensure-sync-flows` ability takes the equivalent optional `project` (slug) and `dry_run` (boolean) inputs. DocSync provisions one **DocSync** pipeline and one daily flow per eligible project, storing the flow ID in `project_dm_flow_id`. Re-run ensure after changing a URL or path to update the existing flow. Changing the source does not automatically purge posts from the previous source.

The `github` fetch step reads `*.md` files from the default branch. The `docsync_apply_github_file` system task applies additions, edits, and removals without AI. Edits match existing posts by their docs-relative source path within the project, preserving post IDs and URLs. If the configured directory is missing, the fetch step fails rather than reporting a successful sync.

Use **Sync Now**, `wp docsync docs sync <term_id>`, or `docsync/sync-docs` to queue a run. Use `wp docsync docs sync --all` for all projects. A queued response means Data Machine accepted the run, not that the documentation has been updated. Check Data Machine's job history for fetch errors, and make sure the site's Action Scheduler queue is running. DocSync updates the project's sync metadata when its apply step runs.

When both Data Machine and the GitHub handler are available, DocSync disables its `docsync_github_sync` cron event. Provision the flows as part of enabling the extension; projects without a flow will not sync in this mode. Without the integration, DocSync retains its legacy WP-Cron runner, GitHub PAT, and interval settings under **Documentation → Settings**.

## Key Concepts

### Project Structure
Expand Down
63 changes: 31 additions & 32 deletions docsync.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* Text Domain: docsync
* Requires at least: 6.9
* Requires PHP: 8.0
* Requires Plugins: data-machine, data-machine-github
*/

// Prevent direct access
Expand Down Expand Up @@ -40,7 +41,14 @@
use DocSync\Templates\Homepage;
use DocSync\Templates\SearchBar;
use DocSync\Templates\TableOfContents;
use DocSync\Sync\CronSync;
use DataMachine\Core\Database\Flows\Flows;
use DataMachine\Core\Database\Pipelines\Pipelines;
use DataMachine\Engine\AI\System\Tasks\SystemTask;
use DataMachine\Engine\Scheduling\FlowRoutines;
use DataMachineGitHub\Handlers\GitHub\GitHubFetch;
use DocSync\Sync\DataMachineSync;
use DocSync\Sync\SyncUpgrade;
use DocSync\Abilities\SyncAbilities;
use DocSync\Sync\SyncNotifier;
use DocSync\Admin\SettingsPage;
use DocSync\Admin\ProjectColumns;
Expand All @@ -57,8 +65,6 @@
ProjectColumns::init();
} );

CronSync::init();
SyncNotifier::init();
SettingsPage::init();
DocumentationColumns::init();

Expand Down Expand Up @@ -117,41 +123,34 @@
return $post_types;
} );

register_activation_hook( __FILE__, function() {
docsync_maybe_migrate_options();
flush_rewrite_rules();
} );

/**
* Migrate options from the old chubes_docs_ prefix to docsync_ prefix.
*
* Runs on activation. Safe to call multiple times — only migrates
* if old options exist and new ones don't.
* Sync layer — Data Machine owns scheduling, fetch and job history.
*
* @since 0.1.0
* The content layer above works without it; syncing does not exist without it.
*/
function docsync_maybe_migrate_options() {
$migrations = [
'chubes_docs_github_pat' => 'docsync_github_pat',
'chubes_docs_sync_interval' => 'docsync_sync_interval',
];

foreach ( $migrations as $old_key => $new_key ) {
$old_value = get_option( $old_key );
if ( $old_value !== false && get_option( $new_key ) === false ) {
update_option( $new_key, $old_value );
delete_option( $old_key );
}
add_action( 'plugins_loaded', static function (): void {
if ( ! class_exists( SystemTask::class ) || ! class_exists( Flows::class ) || ! class_exists( Pipelines::class ) || ! class_exists( FlowRoutines::class ) || ! class_exists( GitHubFetch::class ) ) {
add_action( 'admin_notices', static function (): void {
echo '<div class="notice notice-error"><p>' . esc_html__( 'DocSync sync requires Data Machine and Data Machine GitHub to be active.', 'docsync' ) . '</p></div>';
} );
return;
}

// Migrate cron hook.
$old_hook = 'chubes_docs_github_sync';
$new_hook = 'docsync_github_sync';
$old_next = wp_next_scheduled( $old_hook );
if ( $old_next ) {
wp_unschedule_event( $old_next, $old_hook );
DataMachineSync::register_task();
SyncNotifier::init();
add_action( 'admin_init', array( SyncUpgrade::class, 'run' ) );
add_action( 'wp_abilities_api_init', array( SyncAbilities::class, 'register' ) );
add_action( 'rest_api_init', array( Routes::class, 'register_sync_trigger_route' ) );

if ( defined( 'WP_CLI' ) && WP_CLI ) {
add_action( 'init', array( SyncUpgrade::class, 'run' ) );
\DocSync\WPCLI\CLI::register_sync();
}
}
}, 30 );

register_activation_hook( __FILE__, function() {
flush_rewrite_rules();
} );

register_deactivation_hook( __FILE__, function() {
flush_rewrite_rules();
Expand Down
1 change: 0 additions & 1 deletion inc/Abilities/Abilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ class Abilities {

public static function init(): void {
add_action( 'wp_abilities_api_categories_init', [ __CLASS__, 'register_categories' ] );
add_action( 'wp_abilities_api_init', [ SyncAbilities::class, 'register' ] );
add_action( 'wp_abilities_api_init', [ ProjectAbilities::class, 'register' ] );
add_action( 'wp_abilities_api_init', [ SearchAbilities::class, 'register' ] );
add_action( 'wp_abilities_api_init', [ DocsAbilities::class, 'register' ] );
Expand Down
Loading