-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata-machine-editor.php
More file actions
160 lines (133 loc) · 4.91 KB
/
data-machine-editor.php
File metadata and controls
160 lines (133 loc) · 4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
/**
* Plugin Name: Data Machine Editor
* Plugin URI: https://github.com/Extra-Chill/data-machine-editor
* Description: Editor integration extension for Data Machine. Inline diff visualization, accept/reject review workflow, and Gutenberg editor tools for AI-powered content editing.
* Version: 0.2.0
* Author: Chris Huber
* Author URI: https://chubes.net
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: data-machine-editor
* Domain Path: /languages
* Requires Plugins: data-machine
* Requires PHP: 8.2
* Requires at least: 6.5
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
/**
* Plugin constants.
*/
define( 'DATAMACHINE_EDITOR_VERSION', '0.2.0' );
define( 'DATAMACHINE_EDITOR_PATH', plugin_dir_path( __FILE__ ) );
define( 'DATAMACHINE_EDITOR_URL', plugin_dir_url( __FILE__ ) );
/**
* PSR-4 autoloader.
*/
if ( file_exists( __DIR__ . '/vendor/autoload.php' ) ) {
require_once __DIR__ . '/vendor/autoload.php';
}
/**
* Bootstrap the plugin at plugins_loaded priority 20 (after DM core).
*/
add_action( 'plugins_loaded', function () {
// Guard: Data Machine core must be active.
if ( ! class_exists( 'DataMachine\Abilities\PermissionHelper' ) ) {
add_action( 'admin_notices', function () {
echo '<div class="notice notice-error"><p>';
echo '<strong>Data Machine Editor</strong> requires the <strong>Data Machine</strong> plugin to be active.';
echo '</p></div>';
} );
return;
}
// Register abilities.
new DataMachineEditor\Abilities\DiffAbilities();
// Register editor context memory file.
add_filter( 'datamachine_default_context_files', 'datamachine_editor_register_context' );
// Register the diff block.
add_action( 'init', 'datamachine_editor_register_blocks' );
// Enqueue editor assets.
add_action( 'enqueue_block_editor_assets', 'datamachine_editor_enqueue_assets' );
}, 20 );
/**
* Register editor context memory file.
*
* Scaffolds contexts/editor.md with diff workflow and content editing instructions.
* The file is written once — after that, the agent owns it.
*
* @param array $defaults Context slug => markdown content.
* @return array
*/
function datamachine_editor_register_context( array $defaults ): array {
$defaults['editor'] = <<<'MD'
# Editor Context
This context is active when you are editing post content through the Gutenberg block editor. You have tools for surgical block-level edits with inline diff visualization.
## Diff Workflow
When editing content, changes are presented as inline diffs using `<ins>` (additions) and `<del>` (removals) tags inside `datamachine/diff` blocks. The user reviews each change and accepts or rejects it individually.
Three diff modes are available:
- **edit** — Surgical text replacement within an existing block. Shows word-level changes.
- **write** — Full block replacement. Shows the complete before/after with word-level diff.
- **insert** — New content added between existing blocks. Shows the addition only.
## Review Protocol
- Present changes as diffs — never silently modify content.
- Each diff block can be accepted or rejected independently.
- When all diffs in a tool call are resolved, chat continuation fires automatically.
- Bulk accept/reject is available for batching multiple changes.
## Content Editing Principles
- Preserve the author's voice — suggest improvements, don't rewrite.
- Focus on clarity, grammar, and factual accuracy.
- When restructuring, explain why in the diff context.
- Never remove content without a clear reason.
MD;
return $defaults;
}
/**
* Register Gutenberg blocks.
*/
function datamachine_editor_register_blocks(): void {
register_block_type( DATAMACHINE_EDITOR_PATH . 'inc/Blocks/Diff' );
}
/**
* Enqueue editor scripts and styles.
*/
function datamachine_editor_enqueue_assets(): void {
// Diff block assets.
$diff_asset_file = DATAMACHINE_EDITOR_PATH . 'build/diff-block.asset.php';
if ( file_exists( $diff_asset_file ) ) {
$asset = require $diff_asset_file;
wp_enqueue_script(
'datamachine-diff-block',
DATAMACHINE_EDITOR_URL . 'build/diff-block.js',
$asset['dependencies'],
$asset['version'],
true
);
wp_enqueue_style(
'datamachine-diff-block',
DATAMACHINE_EDITOR_URL . 'build/style-diff-block.css',
array(),
$asset['version']
);
}
// Editor chat sidebar assets.
$sidebar_asset_file = DATAMACHINE_EDITOR_PATH . 'build/editor-sidebar.asset.php';
if ( file_exists( $sidebar_asset_file ) ) {
$sidebar_asset = require $sidebar_asset_file;
wp_enqueue_script(
'datamachine-editor-sidebar',
DATAMACHINE_EDITOR_URL . 'build/editor-sidebar.js',
$sidebar_asset['dependencies'],
$sidebar_asset['version'],
true
);
wp_enqueue_style(
'datamachine-editor-sidebar',
DATAMACHINE_EDITOR_URL . 'build/style-editor-sidebar.css',
array(),
$sidebar_asset['version']
);
}
}