Skip to content

Commit 87e1904

Browse files
smithjw1claude
andauthored
Limit who can remove a post from its workflow (#211)
* fix: limit who can remove a post from its workflow Removing a post from its workflow, replacing its workflow, or re-seating it in the same one is now limited to the Workflow override roles (`bypass_workflow_roles`, administrators by default). Giving an unassigned post its first workflow stays open to anyone who can edit it, and a write with no logged-in user is still treated as a system actor. A post with any stored sequence value, including one naming a deleted workflow or one that is not a number, counts as already assigned. The limit lives in StatusManager::remove_sequence() and assign_sequence(), so the REST route, the remove-from-workflow ability (and so MCP), the editor and extensions all get it. Anyone else gets a 403 `vip_workflows_removal_restricted` that names the roles to ask, and nothing is written or audited. Removals stay audited as before. The publish-veto messages, server and client, now name who to ask instead of implying removal is available, and the deleted-workflow message says who can remove the post. Settings::get_override_role_names() gives the display names, served to the clients as `guard.override_roles`. The editor's Workflow row no longer offers the picker or Remove from workflow to a user outside the override roles on a post that already has a workflow; it says who can change it instead. The Workflow override setting description lists the new grant. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * docs: describe the workflow removal limit in SPEC.md Edits the bypass-list glossary line, N2, the sequence route and remove-from-workflow ability rows, the Bypass and publish boundary guard notes, Sequences' assignment rule and strand footgun, and Publish boundary's "Two ways through", Always true, footgun and code lines. Also updates the Workflow row description in docs/reference/admin-ui.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * test: locate the MCP adapter from the plugins directory The MCP refusal test found the adapter's autoloader with dirname( __DIR__, 4 ), which only reaches wp-content/plugins because of how wp-env mounts the plugin. From a repository checkout it points at the repo root, which reads as a path that can never exist. Use WP_PLUGIN_DIR, as GuidelineContextProviderKnowledgeTest does, and say why the autoloader is required here: the tests environment installs the adapter but never activates it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * test: cover the save guard's ask sentence with override roles The WorkflowSaveGuard veto tests used a guard payload without override_roles, so the sentence naming who to ask was never reached through the component. Add cases for a refused publish with one role, a refused status change with two, and a post in a deleted workflow, each asserting the full message. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix: check the workflow change limit before the target sequence assign_sequence() checked the override-role limit only after it had looked up the target sequence and found the post's entry stage. A user outside the override roles replacing a post's workflow could get a 400 about a missing sequence or a region the sequence has no stage for, fix it, and then get the 403 naming who to ask. The limit now runs right after the post is resolved, whenever the post already has a workflow. A first assignment of an unassigned post is unchanged: it is open to anyone with edit_post and still gets the stage check's 400. The REST route and the remove-from-workflow ability had no check ahead of StatusManager that needed moving. Tests: integration cases for an author replacing with an unseatable or missing sequence (403), an override user with the same unseatable sequence (400), and a first assignment with it (400). Unit fixtures updated for the new lookup order. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix: show a workflow the user can't change as plain text For a post that has a workflow (or a deleted one), a user outside the Workflow override roles now sees the Workflow row's value as plain text, the way core's Status row reads for someone who can't publish: no trigger, no popover, no picker and no Remove button. The popover that named the override roles is gone, along with the client copy of that sentence, which nothing else used. Users in the override roles keep the picker, and a post with no workflow stays selectable for everyone. The server's refusal and its message are unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * docs: describe the read-only Workflow row Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent fea3fab commit 87e1904

30 files changed

Lines changed: 2108 additions & 186 deletions

‎docs/SPEC.md‎

Lines changed: 12 additions & 11 deletions
Large diffs are not rendered by default.

‎docs/reference/admin-ui.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,10 @@ which work whether or not the sidebar or the panel is open.
106106
Workflow row → metadata rows. The Workflow row's popover holds the sequence
107107
list and, below it, Remove from workflow (withheld when the workflow is
108108
enforced). A post whose sequence was deleted is drawn as the same row in its
109-
invalid state, whose popover offers only removal. The slot is a render-prop
109+
invalid state, whose popover offers only removal. For a post that has a
110+
workflow (or a deleted one), a user outside the workflow override roles
111+
(`guard.can_bypass` false) sees the row's value as plain text, like core's
112+
read-only Status row: no trigger, no popover. The slot is a render-prop
110113
slot, so the rows take nothing from React context around the fill; they read
111114
through `wp.data` and `useEntityProp`. The whole run is behind
112115
`! isEditorPanelRemoved( 'post-status' )`, and some experimental DataForm

‎vip-workflows/includes/abilities/tools/get-available-transitions.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ function build_transition_guard_context( $status_manager, int $post_id, ?array $
9595
'current_region' => $current_region,
9696
'can_bypass' => \VIPWorkflows\Admin\Settings::can_user_bypass_workflow( get_current_user_id() ),
9797
'agent_pending' => $status_manager->has_pending_agent_job( $post_id, $stage_key ),
98+
'override_roles' => \VIPWorkflows\Admin\Settings::get_override_role_names(),
9899
);
99100
}
100101

@@ -145,7 +146,7 @@ function register_get_available_transitions(): void {
145146
),
146147
'guard' => array(
147148
'type' => 'object',
148-
'description' => __( 'What performing one of these transitions would set off. `current_region` is the editorial region (draft/pending/private/publish) of the post’s stage, or null when it cannot be resolved; a move whose target region differs, with either side being publish, is refused for a user whose `can_bypass` is false. `agent_pending` is true when an AI agent is mid-run on this post, in which case any transition out of the stage stops it and must be confirmed with transition-post’s acknowledge_warnings.', 'vip-workflows' ),
149+
'description' => __( 'What performing one of these transitions would set off. `current_region` is the editorial region (draft/pending/private/publish) of the post’s stage, or null when it cannot be resolved; a move whose target region differs, with either side being publish, is refused for a user whose `can_bypass` is false. `agent_pending` is true when an AI agent is mid-run on this post, in which case any transition out of the stage stops it and must be confirmed with transition-post’s acknowledge_warnings. `override_roles` names the roles that can skip these rules and that alone can remove the post from its workflow or change its workflow: who to ask when `can_bypass` is false.', 'vip-workflows' ),
149150
),
150151
),
151152
),

‎vip-workflows/includes/abilities/tools/remove-from-workflow.php‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22
/**
33
* Remove From Workflow ability.
44
*
5-
* The Abilities-API half of the publish veto's escape hatch.
5+
* The Abilities-API counterpart of the editor's "Remove from workflow".
66
*
7-
* A non-bypass user's status change across the publish boundary is refused at
8-
* the save layer, and the refusal names exactly two ways through: move the post
9-
* through the workflow to a published stage, or take it out of the workflow —
10-
* which is recorded. Both were reachable from the UI; only the first was
11-
* reachable from an ability, so an agent asked to "remove this from its workflow
12-
* and publish it" could not do the thing the veto itself told it to do.
7+
* Removal is limited to the workflow override roles, the same people the
8+
* publish veto tells everyone else to ask. The limit is
9+
* StatusManager::remove_sequence()'s, not this ability's: any other user with
10+
* edit access gets that method's 403 naming who to ask, so an agent acting for
11+
* them learns the same thing the editor would tell them.
1312
*
1413
* @package VIPWorkflows
1514
*/
@@ -83,7 +82,7 @@ function register_remove_from_workflow(): void {
8382
'vip-workflows/remove-from-workflow',
8483
array(
8584
'label' => __( 'Remove from workflow', 'vip-workflows' ),
86-
'description' => __( 'Takes a post out of its workflow, leaving its published status exactly as it is. The removal is recorded in the workflow log and cannot be undone: re-assigning a workflow later starts the post at the first stage of its region. This is the audited escape from the publish guard — prefer moving the post through the workflow to a published stage where that is possible.', 'vip-workflows' ),
85+
'description' => __( 'Takes a post out of its workflow, leaving its published status exactly as it is. The removal is recorded in the workflow log and cannot be undone: re-assigning a workflow later starts the post at the first stage of its region. Only users in a workflow override role can remove a post; anyone else gets an error naming the roles to ask. Prefer moving the post through the workflow to a published stage.', 'vip-workflows' ),
8786
'category' => 'vip-workflows',
8887
'input_schema' => array(
8988
'type' => 'object',

‎vip-workflows/includes/admin/class-posts-columns.php‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -440,8 +440,11 @@ public function enqueue_quick_edit_assets( string $hook_suffix ): void {
440440
// the fail-closed message shown when the guard itself cannot answer.
441441
$guard = wp_json_encode(
442442
array(
443-
'canBypass' => Settings::can_user_bypass_workflow(),
444-
'strings' => array(
443+
'canBypass' => Settings::can_user_bypass_workflow(),
444+
// Who to ask: the shared module's veto sentences name these, and
445+
// must say what the server says.
446+
'overrideRoles' => Settings::get_override_role_names(),
447+
'strings' => array(
445448
// The publish boundary is symmetric, so the veto copy is
446449
// direction-aware: a user un-publishing was never trying to
447450
// publish, and telling them they can't would be a non-sequitur.
@@ -667,11 +670,15 @@ function quickEditAllowed(postId) {
667670
// was deleted, so there is no workflow left to move the post through
668671
// and no name to call it by. Same guard, different sentence.
669672
window.alert(data.dataset.orphaned
670-
? api.getOrphanedWorkflowMessage({ title: titleFor(row, postId) })
673+
? api.getOrphanedWorkflowMessage({
674+
title: titleFor(row, postId),
675+
overrideRoles: guard.overrideRoles
676+
})
671677
: api.getPublishVetoMessage({
672678
title: titleFor(row, postId),
673679
workflowName: data.dataset.sequenceName,
674-
targetStatus: targetStatus
680+
targetStatus: targetStatus,
681+
overrideRoles: guard.overrideRoles
675682
}));
676683
return false;
677684
}

‎vip-workflows/includes/admin/class-settings.php‎

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,44 @@ public static function get_bypass_workflow_roles(): array {
172172
return $settings['bypass_workflow_roles'] ?? array( 'administrator' );
173173
}
174174

175+
/**
176+
* Display names of the workflow override roles: who to ask.
177+
*
178+
* The one answer every "ask someone with…" sentence uses — the publish
179+
* veto, the refused removal or replacement, and the `override_roles` list
180+
* the editor and list-table clients build the same sentences from — so the
181+
* server and the client name the same people.
182+
*
183+
* Translated, in the configured order. A slug that names no registered
184+
* role on this site is skipped rather than shown raw; an empty list means
185+
* no role holds the override, and the sentences then name nobody.
186+
*
187+
* @return string[] Role display names.
188+
*/
189+
public static function get_override_role_names(): array {
190+
$role_names = wp_roles()->role_names;
191+
$names = array();
192+
193+
foreach ( self::get_bypass_workflow_roles() as $slug ) {
194+
if ( ! is_string( $slug ) || ! isset( $role_names[ $slug ] ) ) {
195+
continue;
196+
}
197+
198+
$names[] = translate_user_role( $role_names[ $slug ] );
199+
}
200+
201+
return $names;
202+
}
203+
175204
/**
176205
* Check if a user holds a workflow override role.
177206
*
178-
* The override grants three things: every transition is offered regardless
207+
* The override grants four things: every transition is offered regardless
179208
* of its `allowed_roles`, empty required fields do not block a move into
180-
* the publish region, and the publish-boundary veto on a direct status
181-
* change becomes a confirm.
209+
* the publish region, the publish-boundary veto on a direct status change
210+
* becomes a confirm, and only an override user can remove a post from its
211+
* workflow or replace the workflow it is in
212+
* (StatusManager::remove_sequence(), StatusManager::assign_sequence()).
182213
*
183214
* @param int|null $user_id User ID. Default current user.
184215
* @return bool

‎vip-workflows/includes/api/class-workflow-controller.php‎

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,12 @@ public function register_routes(): void {
232232

233233
// POST /workflow/post/{id}/sequence - Assign a sequence to a post.
234234
// DELETE /workflow/post/{id}/sequence - Remove workflow from a post.
235+
//
236+
// Both routes need only edit access to reach the handler, so 404 and the
237+
// 403 for a post the user cannot edit stay as they are. Removing a post's
238+
// workflow, or replacing the workflow of a post that already has one, is
239+
// further limited to the workflow override roles; StatusManager enforces
240+
// that and its 403 passes through. A first assignment is not limited.
235241
register_rest_route(
236242
$this->namespace,
237243
'/' . $this->rest_base . '/post/(?P<id>[\d]+)/sequence',
@@ -476,6 +482,9 @@ public function get_post_status( $request ) {
476482
'current_region' => null,
477483
'can_bypass' => \VIPWorkflows\Admin\Settings::can_user_bypass_workflow( get_current_user_id() ),
478484
'agent_pending' => false,
485+
// Who to ask: the client's refusal sentences name these, and
486+
// must say what the server says.
487+
'override_roles' => \VIPWorkflows\Admin\Settings::get_override_role_names(),
479488
);
480489
}
481490

@@ -595,6 +604,9 @@ public function get_post_status( $request ) {
595604
'current_region' => $current_region,
596605
'can_bypass' => \VIPWorkflows\Admin\Settings::can_user_bypass_workflow( get_current_user_id() ),
597606
'agent_pending' => $agent_pending,
607+
// Who to ask: the client's refusal sentences name these, and
608+
// must say what the server says.
609+
'override_roles' => \VIPWorkflows\Admin\Settings::get_override_role_names(),
598610
),
599611
'all_statuses' => $sequence->get_statuses(),
600612
// Served for an enrolled post too, not only for one with no
@@ -985,8 +997,9 @@ public function assign_sequence( $request ) {
985997
}
986998

987999
// Seating can be refused — the sequence models no stage in the post's
988-
// status region, the user may not cross into the named stage's region, or
989-
// a crossing failed at the core status write. Each arrives as a WP_Error
1000+
// status region, the post already has a workflow and the user is not in
1001+
// an override role, the user may not cross into the named stage's region,
1002+
// or a crossing failed at the core status write. Each arrives as a WP_Error
9901003
// already carrying the message and status code the author should see;
9911004
// surface it rather than falling through to success.
9921005
if ( is_wp_error( $result ) ) {
@@ -1013,14 +1026,15 @@ public function assign_sequence( $request ) {
10131026
}
10141027

10151028
/**
1016-
* Remove a post from its workflow — the escape hatch behind the publish veto.
1029+
* Remove a post from its workflow.
10171030
*
10181031
* Delegates wholly to StatusManager::remove_sequence(), the one authority
1019-
* for removal: it deletes the workflow identity (sequence + stage meta),
1020-
* lets any in-flight stage-agent job abandon itself, records the audited
1021-
* `workflow.removed` event, and writes NO post_status — the post stays where
1022-
* core has it and is simply no longer workflow-managed. Its WP_Error (post
1023-
* missing, or the post not in a workflow) propagates to the client as-is.
1032+
* for removal: it limits removal to the workflow override roles, deletes the
1033+
* workflow identity (sequence + stage meta), lets any in-flight stage-agent
1034+
* job abandon itself, records the audited `workflow.removed` event, and
1035+
* writes NO post_status — the post stays where core has it and is simply no
1036+
* longer workflow-managed. Its WP_Error (post missing, the post not in a
1037+
* workflow, or a 403 naming who to ask) propagates to the client as-is.
10241038
*
10251039
* @param WP_REST_Request $request Request object.
10261040
* @return bool|WP_Error True on success.

‎vip-workflows/includes/workflow/class-publish-boundary-guard.php‎

Lines changed: 95 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@
2929
* - `admin_notices` surfaces the same message on the classic editor (post.php).
3030
* Quick Edit and Bulk Edit cannot render it — see render_veto_notice().
3131
*
32-
* The veto changes capabilities in no way: it gates one action and always offers
33-
* a first-class, audited way through (StatusManager::remove_sequence(), or
34-
* moving the post through the workflow to a published stage).
32+
* The veto changes capabilities in no way: it gates one action, and its message
33+
* names the way through — moving the post through the workflow to a published
34+
* stage — and who to ask otherwise: the workflow override roles, the only users
35+
* it never applies to and the only ones who can remove a post from its
36+
* workflow (StatusManager::remove_sequence()).
3537
*/
3638
class PublishBoundaryGuard {
3739

@@ -377,41 +379,53 @@ private function is_system_context( int $post_id ): bool {
377379
}
378380

379381
/**
380-
* Build the veto message: the change can't be made directly, and the way through.
382+
* Build the veto message: the change can't be made directly, the way through, and who to ask.
381383
*
382384
* The way through is the workflow. Removing the post from its workflow is
383-
* not offered: the refusal is the most common thing a person sees here (it
384-
* is what a habitual Publish click produces), and a refusal that suggests
385-
* leaving the workflow teaches people to leave it. Removal stays a
386-
* deliberate act in the sidebar.
385+
* not offered: only the override roles can, and the person reading this is
386+
* never one of them (the veto does not apply to them). Instead a second
387+
* sentence names those roles (Settings::get_override_role_names()) as the
388+
* people to ask (ask_sentence()). With no override role configured there is
389+
* no one to name, and the message is the first sentence alone.
387390
*
388-
* The veto refuses a crossing in either direction, so the sentence follows
391+
* The veto refuses a crossing in either direction, so the sentences follow
389392
* the target: publishing names a published stage, and anything else
390393
* (unpublishing, making private) says the status can't be changed directly.
391394
*
395+
* The client builds the same message (getPublishVetoMessage() and
396+
* getOrphanedWorkflowMessage() in src/common/confirm-workflow-side-effect.js)
397+
* from the same role names, served as `guard.override_roles`. The two MUST
398+
* stay identical to the character; change them together.
399+
*
392400
* @param int $post_id Post ID.
393401
* @param string $post_title Post title.
394402
* @param string $target_status Core status the refused change would write.
395403
* @return string
396404
*/
397405
private function veto_message( int $post_id, string $post_title, string $target_status ): string {
398406
$sequence = $this->status_manager->get_sequence_for_post( $post_id );
407+
$roles = Settings::get_override_role_names();
399408

400409
if ( ! $sequence ) {
401410
// crosses_publish_boundary() reported a crossing, so the post carries
402411
// sequence meta; a sequence that will not resolve from it is a
403412
// data-integrity bug. The veto stands (fail closed). There is no
404-
// workflow to move the post through, so removal is the only way out
405-
// and is named. This is the orphaned post the client calls a deleted
406-
// workflow, so it gets the client's sentence word for word
407-
// (getOrphanedWorkflowMessage()): one string for one situation.
413+
// workflow to move the post through, so removal is the only way out,
414+
// and only the override roles can take it. This is the orphaned post
415+
// the client calls a deleted workflow, so it gets the client's
416+
// sentences word for word (getOrphanedWorkflowMessage()): one message
417+
// for one situation.
408418
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
409419
error_log( sprintf( '[VIP Workflows] Post %d is workflow-managed but its sequence could not be resolved while building the publish-veto message.', $post_id ) );
410420

411-
return sprintf(
412-
/* translators: %s: post title. */
413-
__( '“%s” is in a deleted workflow. Remove it from the workflow to change its status.', 'vip-workflows' ),
414-
$post_title
421+
return self::with_ask_sentence(
422+
sprintf(
423+
/* translators: %s: post title. */
424+
__( '“%s” is in a deleted workflow, so its status can’t be changed.', 'vip-workflows' ),
425+
$post_title
426+
),
427+
'remove',
428+
$roles
415429
);
416430
}
417431

@@ -423,20 +437,75 @@ private function veto_message( int $post_id, string $post_title, string $target_
423437
}
424438

425439
if ( 'publish' === $this->status_manager->status_to_region( $target_status ) ) {
426-
return sprintf(
440+
return self::with_ask_sentence(
441+
sprintf(
442+
/* translators: 1: post title, 2: workflow (sequence) name. */
443+
__( '“%1$s” is in the “%2$s” workflow, so it can’t be published directly. Move it through the workflow to a published stage.', 'vip-workflows' ),
444+
$post_title,
445+
$sequence->name
446+
),
447+
'publish',
448+
$roles
449+
);
450+
}
451+
452+
return self::with_ask_sentence(
453+
sprintf(
427454
/* translators: 1: post title, 2: workflow (sequence) name. */
428-
__( '“%1$s” is in the “%2$s” workflow, so it can’t be published directly. Move it through the workflow to a published stage.', 'vip-workflows' ),
455+
__( '“%1$s” is in the “%2$s” workflow, so its status can’t be changed directly. Move it through the workflow instead.', 'vip-workflows' ),
429456
$post_title,
430457
$sequence->name
431-
);
458+
),
459+
'change',
460+
$roles
461+
);
462+
}
463+
464+
/**
465+
* Append the sentence naming who to ask, when there is anyone to name.
466+
*
467+
* A separate sentence, not a clause of the first, so each translatable
468+
* string is a whole sentence and the message stays readable when no role
469+
* holds the override.
470+
*
471+
* @param string $message First sentence of the veto message.
472+
* @param string $action What to ask for: `publish`, `change` or `remove`.
473+
* @param string[] $roles Override role display names.
474+
* @return string
475+
*/
476+
private static function with_ask_sentence( string $message, string $action, array $roles ): string {
477+
if ( empty( $roles ) ) {
478+
return $message;
432479
}
433480

434-
return sprintf(
435-
/* translators: 1: post title, 2: workflow (sequence) name. */
436-
__( '“%1$s” is in the “%2$s” workflow, so its status can’t be changed directly. Move it through the workflow instead.', 'vip-workflows' ),
437-
$post_title,
438-
$sequence->name
439-
);
481+
$list = implode( ', ', $roles );
482+
$one = 1 === count( $roles );
483+
484+
switch ( $action ) {
485+
case 'publish':
486+
$ask = $one
487+
/* translators: %s: role name, e.g. "Administrator". */
488+
? sprintf( __( 'Ask someone with the %s role to publish it.', 'vip-workflows' ), $list )
489+
/* translators: %s: comma-separated role names, e.g. "Editor, Administrator". */
490+
: sprintf( __( 'Ask someone with one of these roles to publish it: %s.', 'vip-workflows' ), $list );
491+
break;
492+
case 'remove':
493+
$ask = $one
494+
/* translators: %s: role name, e.g. "Administrator". */
495+
? sprintf( __( 'Ask someone with the %s role to remove it from the workflow.', 'vip-workflows' ), $list )
496+
/* translators: %s: comma-separated role names, e.g. "Editor, Administrator". */
497+
: sprintf( __( 'Ask someone with one of these roles to remove it from the workflow: %s.', 'vip-workflows' ), $list );
498+
break;
499+
default:
500+
$ask = $one
501+
/* translators: %s: role name, e.g. "Administrator". */
502+
? sprintf( __( 'Ask someone with the %s role to change it.', 'vip-workflows' ), $list )
503+
/* translators: %s: comma-separated role names, e.g. "Editor, Administrator". */
504+
: sprintf( __( 'Ask someone with one of these roles to change it: %s.', 'vip-workflows' ), $list );
505+
break;
506+
}
507+
508+
return $message . ' ' . $ask;
440509
}
441510

442511
/**

0 commit comments

Comments
 (0)