-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore-content.php
More file actions
2679 lines (2459 loc) · 92.9 KB
/
Copy pathcore-content.php
File metadata and controls
2679 lines (2459 loc) · 92.9 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Post, page, and media abilities (Saddle v0.1 content scope).
*
* Twenty-three abilities, all namespaced `saddle/` with DASH-separated ids (e.g.
* `saddle/list-posts`) — core rejects underscores in ability names. The MCP
* Adapter further exposes them to agents with the slash sanitized to a dash
* (tool name `saddle-list-posts`). Each ability declares an explicit access
* tier (via its permission_callback) and an explicit destructive flag (via
* meta.annotations + the approval gate). Do not infer either from the callback
* body — see CLAUDE.md.
*
* AUTHORIZATION IS TWO LAYERS, AND ONLY THE FIRST IS IN THE permission_callback.
*
* `Saddle_Capabilities::permission( $tier, $cap, $tool )` answers "may this
* caller use this tool at all": an authenticated user, the site's access tier,
* the generic WordPress capability, the per-tool switch and the global pause.
* It deliberately does NOT answer "may this caller read THIS object", because
* the read tier's capability is `read` — which every logged-in user holds,
* a Subscriber included.
*
* That second question is `Saddle_Abilities::require_readable_post()` (below),
* the single funnel every id-taking read calls FIRST, before touching the row:
* `read_post` on the resolved object, plus a refusal for password-protected
* content because Saddle returns raw `post_content`, which core only ever hands
* out behind `edit_post`. Listings use `Saddle_Abilities::collection()`, which
* drops rows the caller cannot `read_post`. Anything that reaches a specific
* post another way is a bug.
*
* It lives on the execute path rather than in the gate on purpose. Core does
* pass `$input` to `WP_Ability::check_permissions()` and accepts a `WP_Error`
* back, but `Saddle_Capabilities::denial_reason()` and `is_callable_now()` —
* which build `tools/list` and every refusal message an agent reads — are
* input-free by construction, and a gate that answered "no" without naming
* which of the two layers said so is what puts an agent into a retry loop.
*
* @package Saddle
*/
defined( 'ABSPATH' ) || exit;
/**
* Register the Saddle ability category. Hooked to
* `wp_abilities_api_categories_init` (must run before abilities register).
*/
function saddle_register_ability_category() {
wp_register_ability_category(
'saddle',
array(
'label' => __( 'Saddle', 'saddle' ),
'description' => __( 'Approval-gated content operations exposed to AI agents by Saddle.', 'saddle' ),
)
);
}
/**
* Register all content abilities. Hooked to `wp_abilities_api_init`.
*/
function saddle_register_abilities() {
/*
* ---------------------------------------------------------------------
* General (read)
* ---------------------------------------------------------------------
*/
wp_register_ability(
'saddle/get-site-info',
array(
'label' => __( 'Get site info', 'saddle' ),
'description' => __( 'Returns orientation data about this WordPress site: name, tagline, URL, WordPress version, language, content counts (posts, pages, media), the current Saddle access tier, and the connected user. Read-only. Call this first to understand what the site contains before other operations.', 'saddle' ),
'category' => 'saddle',
'input_schema' => array(
'type' => 'object',
'default' => (object) array(),
'properties' => (object) array(),
),
'execute_callback' => array( 'Saddle_Abilities', 'get_site_info' ),
'permission_callback' => Saddle_Capabilities::permission( 'read', 'read', 'get-site-info' ),
'meta' => saddle_ability_meta( true, false, true, 'read' ),
)
);
wp_register_ability(
'saddle/get-instructions',
array(
'label' => __( 'Get instructions', 'saddle' ),
'description' => __( 'Returns important context about this site and the site owner\'s instructions for AI agents. Call this first, before other actions, and follow the guidance it returns. The installed-plugin inventory is included only for admin-level credentials. Read-only.', 'saddle' ),
'category' => 'saddle',
'input_schema' => array(
'type' => 'object',
'default' => (object) array(),
'properties' => (object) array(),
),
'execute_callback' => array( 'Saddle_Abilities', 'get_instructions' ),
'permission_callback' => Saddle_Capabilities::permission( 'read', 'read', 'get-instructions' ),
'meta' => saddle_ability_meta( true, false, true, 'read' ),
)
);
// Listings are authorized in two places, both in Saddle_Abilities: the
// requested status is gated on the post type's edit_posts, and collection()
// then drops every row the caller cannot read_post. See the two-layer note
// at the top of this file.
wp_register_ability(
'saddle/search-content',
array(
'label' => __( 'Search content', 'saddle' ),
'description' => __( 'Full-text search across posts and pages by keyword. Returns matching items as summaries (id, type, title, status, link, excerpt). Read-only. Use post_type to restrict to "post", "page", or "any".', 'saddle' ),
'category' => 'saddle',
'input_schema' => array(
'type' => 'object',
'default' => (object) array(),
'required' => array( 'query' ),
'properties' => array(
'query' => array(
'type' => 'string',
'description' => __( 'Search keywords.', 'saddle' ),
),
'post_type' => array(
'type' => 'string',
'enum' => array( 'post', 'page', 'any' ),
'default' => 'any',
'description' => __( 'Restrict results to this type.', 'saddle' ),
),
'per_page' => saddle_per_page_schema(),
'page' => saddle_page_schema(),
),
),
'execute_callback' => array( 'Saddle_Abilities', 'search_content' ),
'permission_callback' => Saddle_Capabilities::permission( 'read', 'read', 'search-content' ),
'meta' => saddle_ability_meta( true, false, true, 'read' ),
)
);
/*
* ---------------------------------------------------------------------
* Posts
* ---------------------------------------------------------------------
*/
// Listings are authorized in two places, both in Saddle_Abilities: the
// requested status is gated on the post type's edit_posts, and collection()
// then drops every row the caller cannot read_post. See the two-layer note
// at the top of this file.
wp_register_ability(
'saddle/list-posts',
array(
'label' => __( 'List posts', 'saddle' ),
'description' => __( 'Lists posts as summaries (id, title, status, author, date, link, excerpt). Read-only. Supports filtering by status, author, category, and search term, plus pagination via per_page/page.', 'saddle' ),
'category' => 'saddle',
'input_schema' => array(
'type' => 'object',
'default' => (object) array(),
'properties' => array(
'status' => saddle_status_schema(),
'search' => array(
'type' => 'string',
'description' => __( 'Optional keyword filter.', 'saddle' ),
),
'author' => array(
'type' => 'integer',
'description' => __( 'Filter by author user ID.', 'saddle' ),
),
'category_id' => array(
'type' => 'integer',
'description' => __( 'Filter by category term ID.', 'saddle' ),
),
'orderby' => array(
'type' => 'string',
'enum' => array( 'date', 'modified', 'title', 'ID' ),
'default' => 'date',
),
'order' => array(
'type' => 'string',
'enum' => array( 'ASC', 'DESC' ),
'default' => 'DESC',
),
'per_page' => saddle_per_page_schema(),
'page' => saddle_page_schema(),
),
),
'execute_callback' => array( 'Saddle_Abilities', 'list_posts' ),
'permission_callback' => Saddle_Capabilities::permission( 'read', 'read', 'list-posts' ),
'meta' => saddle_ability_meta( true, false, true, 'read' ),
)
);
// The permission_callback below gates the TOOL. The TARGET is authorized by
// require_readable_post(), called first in get_post(). See the two-layer
// note at the top of this file.
wp_register_ability(
'saddle/get-post',
array(
'label' => __( 'Get post', 'saddle' ),
'description' => __( 'Returns a single post in full: title, content, excerpt, status, slug, author, dates, categories, tags, featured image, discussion settings, and custom fields (meta). Read-only.', 'saddle' ),
'category' => 'saddle',
'input_schema' => saddle_id_schema( __( 'The post ID.', 'saddle' ) ),
'execute_callback' => array( 'Saddle_Abilities', 'get_post' ),
'permission_callback' => Saddle_Capabilities::permission( 'read', 'read', 'get-post' ),
'meta' => saddle_ability_meta( true, false, true, 'read' ),
)
);
wp_register_ability(
'saddle/create-post',
array(
'label' => __( 'Create post', 'saddle' ),
'description' => __( 'Creates a new post and returns its id and summary. Additive (non-destructive). Accepts title, content, excerpt, status (draft/publish/pending/private), slug, author, date, category_ids, tags, featured_media, discussion settings, and custom fields via "meta". Defaults to draft status.', 'saddle' ),
'category' => 'saddle',
'input_schema' => saddle_writable_schema( 'post', false ),
'execute_callback' => array( 'Saddle_Abilities', 'create_post' ),
'permission_callback' => Saddle_Capabilities::permission( 'write', 'edit_posts', 'create-post' ),
'meta' => saddle_ability_meta( false, false, false, 'write' ),
)
);
wp_register_ability(
'saddle/update-post',
array(
'label' => __( 'Update post', 'saddle' ),
'description' => __( 'Updates an existing post by id. Only the fields you pass are changed; omitted fields are left untouched. Accepts the same fields as create-post, including slug, author, and custom fields via "meta" (null deletes a key). WordPress stores a revision, so changes are recoverable (non-destructive). Returns the updated summary.', 'saddle' ),
'category' => 'saddle',
'input_schema' => saddle_writable_schema( 'post', true ),
'execute_callback' => array( 'Saddle_Abilities', 'update_post' ),
'permission_callback' => Saddle_Capabilities::permission( 'write', 'edit_posts', 'update-post' ),
'meta' => saddle_ability_meta( false, false, true, 'write' ),
)
);
wp_register_ability(
'saddle/delete-post',
array(
'label' => __( 'Delete post', 'saddle' ),
'description' => __( 'Deletes a post. DESTRUCTIVE — runs through a two-step confirmation: the first call returns a preview and a confirm_token without changing anything; call again with confirm_token to execute. Without "force" the post is trashed (recoverable); with force=true it is permanently deleted (not recoverable).', 'saddle' ),
'category' => 'saddle',
'input_schema' => saddle_delete_schema( __( 'The post ID to delete.', 'saddle' ), true ),
'execute_callback' => array( 'Saddle_Abilities', 'delete_post' ),
'permission_callback' => Saddle_Capabilities::permission( 'write', 'delete_posts', 'delete-post' ),
'meta' => saddle_ability_meta( false, true, false, 'write' ),
)
);
// The permission_callback below gates the TOOL. The TARGET is authorized by
// require_readable_post(), called first in list_post_revisions(), which then
// additionally requires edit_post — revisions are the edit history, not the
// published artifact. See the two-layer note at the top of this file.
wp_register_ability(
'saddle/list-post-revisions',
array(
'label' => __( 'List post revisions', 'saddle' ),
'description' => __( 'Lists the stored revisions of a post or page (id, author, date, title). Read-only. Restoring a revision is not available in this version.', 'saddle' ),
'category' => 'saddle',
'input_schema' => saddle_id_schema( __( 'The post or page ID.', 'saddle' ) ),
'execute_callback' => array( 'Saddle_Abilities', 'list_post_revisions' ),
'permission_callback' => Saddle_Capabilities::permission( 'read', 'read', 'list-post-revisions' ),
'meta' => saddle_ability_meta( true, false, true, 'read' ),
)
);
/*
* ---------------------------------------------------------------------
* Pages
* ---------------------------------------------------------------------
*/
// Listings are authorized in two places, both in Saddle_Abilities: the
// requested status is gated on the post type's edit_posts, and collection()
// then drops every row the caller cannot read_post. See the two-layer note
// at the top of this file.
wp_register_ability(
'saddle/list-pages',
array(
'label' => __( 'List pages', 'saddle' ),
'description' => __( 'Lists pages as summaries (id, title, status, parent, menu order, link). Read-only. Supports status filtering and pagination.', 'saddle' ),
'category' => 'saddle',
'input_schema' => array(
'type' => 'object',
'default' => (object) array(),
'properties' => array(
'status' => saddle_status_schema(),
'search' => array(
'type' => 'string',
'description' => __( 'Optional keyword filter.', 'saddle' ),
),
'parent' => array(
'type' => 'integer',
'description' => __( 'Filter by parent page ID.', 'saddle' ),
),
'per_page' => saddle_per_page_schema(),
'page' => saddle_page_schema(),
),
),
'execute_callback' => array( 'Saddle_Abilities', 'list_pages' ),
'permission_callback' => Saddle_Capabilities::permission( 'read', 'read', 'list-pages' ),
'meta' => saddle_ability_meta( true, false, true, 'read' ),
)
);
// The permission_callback below gates the TOOL. The TARGET is authorized by
// require_readable_post(), called first in get_page(). See the two-layer
// note at the top of this file.
wp_register_ability(
'saddle/get-page',
array(
'label' => __( 'Get page', 'saddle' ),
'description' => __( 'Returns a single page in full: title, content, excerpt, status, slug, author, dates, page template, parent, menu order, featured image, discussion settings, and custom fields (meta). Read-only.', 'saddle' ),
'category' => 'saddle',
'input_schema' => saddle_id_schema( __( 'The page ID.', 'saddle' ) ),
'execute_callback' => array( 'Saddle_Abilities', 'get_page' ),
'permission_callback' => Saddle_Capabilities::permission( 'read', 'read', 'get-page' ),
'meta' => saddle_ability_meta( true, false, true, 'read' ),
)
);
wp_register_ability(
'saddle/create-page',
array(
'label' => __( 'Create page', 'saddle' ),
'description' => __( 'Creates a new page and returns its id and summary. Additive (non-destructive). Accepts title, content, excerpt, status, slug, author, date, page template, parent, menu_order, featured_media, discussion settings, and custom fields via "meta". Defaults to draft status.', 'saddle' ),
'category' => 'saddle',
'input_schema' => saddle_writable_schema( 'page', false ),
'execute_callback' => array( 'Saddle_Abilities', 'create_page' ),
'permission_callback' => Saddle_Capabilities::permission( 'write', 'edit_pages', 'create-page' ),
'meta' => saddle_ability_meta( false, false, false, 'write' ),
)
);
wp_register_ability(
'saddle/update-page',
array(
'label' => __( 'Update page', 'saddle' ),
'description' => __( 'Updates an existing page by id. Only the fields you pass are changed. Accepts the same fields as create-page, including slug, author, and custom fields via "meta" (null deletes a key). WordPress stores a revision, so changes are recoverable (non-destructive). Returns the updated summary.', 'saddle' ),
'category' => 'saddle',
'input_schema' => saddle_writable_schema( 'page', true ),
'execute_callback' => array( 'Saddle_Abilities', 'update_page' ),
'permission_callback' => Saddle_Capabilities::permission( 'write', 'edit_pages', 'update-page' ),
'meta' => saddle_ability_meta( false, false, true, 'write' ),
)
);
wp_register_ability(
'saddle/delete-page',
array(
'label' => __( 'Delete page', 'saddle' ),
'description' => __( 'Deletes a page. DESTRUCTIVE — two-step confirmation required (preview + confirm_token). Without "force" the page is trashed (recoverable); with force=true it is permanently deleted.', 'saddle' ),
'category' => 'saddle',
'input_schema' => saddle_delete_schema( __( 'The page ID to delete.', 'saddle' ), true ),
'execute_callback' => array( 'Saddle_Abilities', 'delete_page' ),
'permission_callback' => Saddle_Capabilities::permission( 'write', 'delete_pages', 'delete-page' ),
'meta' => saddle_ability_meta( false, true, false, 'write' ),
)
);
/*
* ---------------------------------------------------------------------
* Media
* ---------------------------------------------------------------------
*/
// Listings are authorized in two places, both in Saddle_Abilities: the
// requested status is gated on the post type's edit_posts, and collection()
// then drops every row the caller cannot read_post. See the two-layer note
// at the top of this file.
wp_register_ability(
'saddle/list-media',
array(
'label' => __( 'List media', 'saddle' ),
'description' => __( 'Lists media library items as summaries (id, title, mime type, URL, date, attached post, and media tags when assigned). Read-only. Supports filtering by mime type, parent, source ("unsplash" lists everything imported from Unsplash, with the total count), and tag (a media-tag name — Unsplash imports are auto-tagged), plus pagination.', 'saddle' ),
'category' => 'saddle',
'input_schema' => array(
'type' => 'object',
'default' => (object) array(),
'properties' => array(
'search' => array(
'type' => 'string',
'description' => __( 'Optional keyword filter.', 'saddle' ),
),
'mime_type' => array(
'type' => 'string',
'description' => __( 'Filter by MIME type or prefix, e.g. "image" or "image/png".', 'saddle' ),
),
'parent' => array(
'type' => 'integer',
'description' => __( 'Filter by the post the media is attached to.', 'saddle' ),
),
'source' => array(
'type' => 'string',
'enum' => array( 'unsplash' ),
'description' => __( 'Only media imported from this source. "unsplash" answers what was downloaded from Unsplash and how many.', 'saddle' ),
),
'tag' => array(
'type' => 'string',
'description' => __( 'Only media assigned this media tag (matched case-insensitively). Unsplash imports are auto-tagged with the photo\'s Unsplash tags plus any tags passed at import.', 'saddle' ),
),
'per_page' => saddle_per_page_schema(),
'page' => saddle_page_schema(),
),
),
'execute_callback' => array( 'Saddle_Abilities', 'list_media' ),
'permission_callback' => Saddle_Capabilities::permission( 'read', 'read', 'list-media' ),
'meta' => saddle_ability_meta( true, false, true, 'read' ),
)
);
// The permission_callback below gates the TOOL. The TARGET is authorized by
// require_readable_post(), called first in get_media() — read_post against
// the attachment, which resolves an `inherit` status through post_parent, so
// an upload on a draft or private post is refused. See the two-layer note at
// the top of this file.
wp_register_ability(
'saddle/get-media',
array(
'label' => __( 'Get media', 'saddle' ),
'description' => __( 'Returns a single media item in full: URL, MIME type, alt text, caption, description, available image sizes, and the post it is attached to. Read-only.', 'saddle' ),
'category' => 'saddle',
'input_schema' => saddle_id_schema( __( 'The attachment ID.', 'saddle' ) ),
'execute_callback' => array( 'Saddle_Abilities', 'get_media' ),
'permission_callback' => Saddle_Capabilities::permission( 'read', 'read', 'get-media' ),
'meta' => saddle_ability_meta( true, false, true, 'read' ),
)
);
wp_register_ability(
'saddle/upload-media',
array(
'label' => __( 'Upload media from URL', 'saddle' ),
'description' => __( 'Downloads a file from a source URL you provide and adds it to the media library, returning the new attachment id and URL. Additive (non-destructive). This is the only ability that fetches an external URL, and it fetches only the URL you pass. Optionally set title, alt text, caption, description, and the post to attach it to.', 'saddle' ),
'category' => 'saddle',
'input_schema' => array(
'type' => 'object',
'required' => array( 'source_url' ),
'properties' => array(
'source_url' => array(
'type' => 'string',
'format' => 'uri',
'description' => __( 'Publicly reachable URL of the file to download.', 'saddle' ),
),
'filename' => array(
'type' => 'string',
'description' => __( 'Override the stored filename (with extension).', 'saddle' ),
),
'title' => array( 'type' => 'string' ),
'alt' => array( 'type' => 'string' ),
'caption' => array( 'type' => 'string' ),
'description' => array( 'type' => 'string' ),
'post_id' => array(
'type' => 'integer',
'description' => __( 'Attach the media to this post/page ID.', 'saddle' ),
),
),
),
'execute_callback' => array( 'Saddle_Abilities', 'upload_media' ),
'permission_callback' => Saddle_Capabilities::permission( 'write', 'upload_files', 'upload-media' ),
'meta' => saddle_ability_meta( false, false, false, 'write' ),
)
);
wp_register_ability(
'saddle/update-media',
array(
'label' => __( 'Update media metadata', 'saddle' ),
'description' => __( 'Updates a media item\'s metadata (title, alt text, caption, description) by id. Does not replace the file. Non-destructive. Returns the updated summary.', 'saddle' ),
'category' => 'saddle',
'input_schema' => array(
'type' => 'object',
'required' => array( 'id' ),
'properties' => array(
'id' => array(
'type' => 'integer',
'minimum' => 1,
'description' => __( 'The attachment ID.', 'saddle' ),
),
'title' => array( 'type' => 'string' ),
'alt' => array( 'type' => 'string' ),
'caption' => array( 'type' => 'string' ),
'description' => array( 'type' => 'string' ),
),
),
'execute_callback' => array( 'Saddle_Abilities', 'update_media' ),
'permission_callback' => Saddle_Capabilities::permission( 'write', 'upload_files', 'update-media' ),
'meta' => saddle_ability_meta( false, false, true, 'write' ),
)
);
wp_register_ability(
'saddle/delete-media',
array(
'label' => __( 'Delete media', 'saddle' ),
'description' => __( 'Deletes a media item and its files. DESTRUCTIVE and NOT RECOVERABLE — attachments have no trash state in WordPress, so deletion is permanent. Two-step confirmation required (preview + confirm_token).', 'saddle' ),
'category' => 'saddle',
'input_schema' => saddle_delete_schema( __( 'The attachment ID to delete.', 'saddle' ), false ),
'execute_callback' => array( 'Saddle_Abilities', 'delete_media' ),
'permission_callback' => Saddle_Capabilities::permission( 'write', 'delete_posts', 'delete-media' ),
'meta' => saddle_ability_meta( false, true, false, 'write' ),
)
);
/*
* ---------------------------------------------------------------------
* Taxonomies (categories & tags)
* ---------------------------------------------------------------------
*/
wp_register_ability(
'saddle/list-categories',
array(
'label' => __( 'List categories', 'saddle' ),
'description' => __( 'Lists post categories as summaries (id, name, slug, parent, post count, description). Read-only. Supports search and pagination.', 'saddle' ),
'category' => 'saddle',
'input_schema' => saddle_list_terms_schema(),
'execute_callback' => array( 'Saddle_Abilities', 'list_categories' ),
'permission_callback' => Saddle_Capabilities::permission( 'read', 'read', 'list-categories' ),
'meta' => saddle_ability_meta( true, false, true, 'read' ),
)
);
wp_register_ability(
'saddle/create-category',
array(
'label' => __( 'Create category', 'saddle' ),
'description' => __( 'Creates a post category and returns it. Additive (non-destructive). Accepts name (required), slug, parent category id, and description.', 'saddle' ),
'category' => 'saddle',
'input_schema' => saddle_create_term_schema( true ),
'execute_callback' => array( 'Saddle_Abilities', 'create_category' ),
'permission_callback' => Saddle_Capabilities::permission( 'write', 'manage_categories', 'create-category' ),
'meta' => saddle_ability_meta( false, false, false, 'write' ),
)
);
wp_register_ability(
'saddle/list-tags',
array(
'label' => __( 'List tags', 'saddle' ),
'description' => __( 'Lists post tags as summaries (id, name, slug, post count, description). Read-only. Supports search and pagination.', 'saddle' ),
'category' => 'saddle',
'input_schema' => saddle_list_terms_schema(),
'execute_callback' => array( 'Saddle_Abilities', 'list_tags' ),
'permission_callback' => Saddle_Capabilities::permission( 'read', 'read', 'list-tags' ),
'meta' => saddle_ability_meta( true, false, true, 'read' ),
)
);
wp_register_ability(
'saddle/create-tag',
array(
'label' => __( 'Create tag', 'saddle' ),
'description' => __( 'Creates a post tag and returns it. Additive (non-destructive). Accepts name (required), slug, and description.', 'saddle' ),
'category' => 'saddle',
'input_schema' => saddle_create_term_schema( false ),
'execute_callback' => array( 'Saddle_Abilities', 'create_tag' ),
'permission_callback' => Saddle_Capabilities::permission( 'write', 'manage_categories', 'create-tag' ),
'meta' => saddle_ability_meta( false, false, false, 'write' ),
)
);
}
/*
=========================================================================
* Schema helpers
* =========================================================================
*/
/**
* Build the `meta` array for an ability with explicit annotations.
*
* The tier is stored explicitly under meta.saddle.tier so the capability
* catalog (and the admin UI's Capability Map) can introspect it without parsing
* the permission_callback closure. Per CLAUDE.md, tier is declared, not inferred.
*
* @param bool $is_readonly Whether the ability only reads.
* @param bool $destructive Whether the ability may destroy data.
* @param bool $idempotent Whether repeated identical calls are no-ops.
* @param string $tier Minimum access tier ('read'|'write'|'admin').
* @return array
*/
function saddle_ability_meta( $is_readonly, $destructive, $idempotent, $tier = 'read' ) {
return array(
'show_in_rest' => true,
'mcp' => array( 'public' => true ),
'saddle' => array( 'tier' => $tier ),
'annotations' => array(
'readonly' => (bool) $is_readonly,
'destructive' => (bool) $destructive,
'idempotent' => (bool) $idempotent,
),
);
}
/**
* Schema for a term-listing ability (categories/tags).
*
* @return array
*/
function saddle_list_terms_schema() {
return array(
'type' => 'object',
'default' => (object) array(),
'properties' => array(
'search' => array(
'type' => 'string',
'description' => __( 'Optional keyword filter.', 'saddle' ),
),
'hide_empty' => array(
'type' => 'boolean',
'default' => false,
'description' => __( 'Exclude terms that have no posts.', 'saddle' ),
),
'per_page' => saddle_per_page_schema(),
'page' => saddle_page_schema(),
),
);
}
/**
* Schema for a term-creation ability.
*
* @param bool $with_parent Whether a parent term id applies (categories).
* @return array
*/
function saddle_create_term_schema( $with_parent ) {
$props = array(
'name' => array(
'type' => 'string',
'description' => __( 'The term name (required).', 'saddle' ),
),
'slug' => array( 'type' => 'string' ),
'description' => array( 'type' => 'string' ),
);
if ( $with_parent ) {
$props['parent'] = array(
'type' => 'integer',
'description' => __( 'Parent category term ID.', 'saddle' ),
);
}
return array(
'type' => 'object',
'required' => array( 'name' ),
'properties' => $props,
);
}
/**
* Reusable per_page schema fragment.
*
* @return array
*/
function saddle_per_page_schema() {
return array(
'type' => 'integer',
'minimum' => 1,
'maximum' => 100,
'default' => 20,
'description' => __( 'Results per page (1–100).', 'saddle' ),
);
}
/**
* Reusable page schema fragment.
*
* @return array
*/
function saddle_page_schema() {
return array(
'type' => 'integer',
'minimum' => 1,
'default' => 1,
'description' => __( 'Page number of results.', 'saddle' ),
);
}
/**
* Reusable status-filter schema fragment.
*
* @return array
*/
function saddle_status_schema() {
return array(
'type' => 'string',
'enum' => array( 'publish', 'draft', 'pending', 'private', 'future', 'trash', 'any' ),
'default' => 'any',
'description' => __( 'Filter by post status.', 'saddle' ),
);
}
/**
* Schema for an ability that needs only a required integer `id`.
*
* @param string $description Description for the id field.
* @return array
*/
function saddle_id_schema( $description ) {
return array(
'type' => 'object',
'required' => array( 'id' ),
'properties' => array(
'id' => array(
'type' => 'integer',
'minimum' => 1,
'description' => $description,
),
),
);
}
/**
* Schema for a destructive delete ability (id + confirm_token, optional force).
*
* @param string $id_description Description for the id field.
* @param bool $supports_force Whether a `force` (permanent) flag applies.
* @return array
*/
function saddle_delete_schema( $id_description, $supports_force ) {
$props = array(
'id' => array(
'type' => 'integer',
'minimum' => 1,
'description' => $id_description,
),
'confirm_token' => array(
'type' => 'string',
'description' => __( 'The single-use token returned by the preview call. Omit on the first call to receive a preview.', 'saddle' ),
),
);
if ( $supports_force ) {
$props['force'] = array(
'type' => 'boolean',
'default' => false,
'description' => __( 'If true, permanently delete instead of moving to trash.', 'saddle' ),
);
}
return array(
'type' => 'object',
'required' => array( 'id' ),
'properties' => $props,
);
}
/**
* Schema for create/update abilities on posts or pages.
*
* @param string $type 'post' or 'page'.
* @param bool $is_update Whether this is an update (id required) vs create.
* @return array
*/
function saddle_writable_schema( $type, $is_update ) {
$props = array(
'title' => array(
'type' => 'string',
'description' => __( 'The title.', 'saddle' ),
),
'content' => array(
'type' => 'string',
'description' => __( 'The body content (HTML or blocks).', 'saddle' ),
),
'excerpt' => array( 'type' => 'string' ),
'status' => array(
'type' => 'string',
'enum' => array( 'draft', 'publish', 'pending', 'private', 'future' ),
'description' => __( 'Publication status. Defaults to draft on create.', 'saddle' ),
),
'slug' => array( 'type' => 'string' ),
'author' => array(
'type' => 'integer',
'description' => __( 'Author user ID.', 'saddle' ),
),
'date' => array(
'type' => 'string',
'description' => __( 'Publish date in site time, "YYYY-MM-DD HH:MM:SS".', 'saddle' ),
),
'comment_status' => array(
'type' => 'string',
'enum' => array( 'open', 'closed' ),
),
'ping_status' => array(
'type' => 'string',
'enum' => array( 'open', 'closed' ),
),
'featured_media' => array(
'type' => 'integer',
'description' => __( 'Attachment ID to set as the featured image.', 'saddle' ),
),
'meta' => array(
'type' => 'object',
'additionalProperties' => true,
'description' => __( 'Custom fields (post meta) as key/value pairs. Values may be strings, numbers, booleans, or arrays; pass null as a value to delete that key. Keys starting with an underscore are protected — writable only when the owning plugin registered them as editable. Denied keys are skipped and reported back in "meta_denied"; the rest of the request still applies.', 'saddle' ),
),
);
if ( 'post' === $type ) {
$props['category_ids'] = array(
'type' => 'array',
'items' => array( 'type' => 'integer' ),
'description' => __( 'Category term IDs to assign.', 'saddle' ),
);
$props['tags'] = array(
'type' => 'array',
'items' => array( 'type' => 'string' ),
'description' => __( 'Tag names to assign (created if missing).', 'saddle' ),
);
} else {
$props['template'] = array(
'type' => 'string',
'description' => __( 'Page template filename, e.g. "template-full.php".', 'saddle' ),
);
$props['parent'] = array(
'type' => 'integer',
'description' => __( 'Parent page ID.', 'saddle' ),
);
$props['menu_order'] = array(
'type' => 'integer',
'description' => __( 'Ordering value among sibling pages.', 'saddle' ),
);
}
$schema = array(
'type' => 'object',
'properties' => $props,
);
if ( $is_update ) {
$props['id'] = array(
'type' => 'integer',
'minimum' => 1,
'description' => __( 'The ID to update.', 'saddle' ),
);
$schema['properties'] = $props;
$schema['required'] = array( 'id' );
}
return $schema;
}
/*
=========================================================================
* Execute callbacks
*
* Tier and capability enforcement happen in each ability's permission_callback
* (see Saddle_Capabilities). Destructive callbacks route their mutation through
* Saddle_Approval::gate(). These methods assume permission has already passed.
* =========================================================================
*/
/**
* Implementations for the content abilities.
*/
class Saddle_Abilities {
/**
* Normalize ability input to an array.
*
* @param mixed $input Raw input.
* @return array
*/
private static function args( $input ) {
return is_array( $input ) ? $input : array();
}
/**
* Validate and extract a required positive integer ID.
*
* @param array $input Input array.
* @param string $key Key to read.
* @return int|WP_Error
*/
private static function require_id( array $input, $key = 'id' ) {
if ( ! isset( $input[ $key ] ) || ! is_numeric( $input[ $key ] ) ) {
return new WP_Error(
'saddle_missing_' . $key,
sprintf(
/* translators: %s: field name. */
__( 'A numeric "%s" is required.', 'saddle' ),
$key
),
array( 'status' => 400 )
);
}
$id = (int) $input[ $key ];
if ( $id < 1 ) {
return new WP_Error(
'saddle_invalid_' . $key,
sprintf(
/* translators: %s: field name. */
__( 'The "%s" must be a positive integer.', 'saddle' ),
$key
),
array( 'status' => 400 )
);
}
return $id;
}
/**
* Standard 403 for per-object authorization failures.
*
* @param string|null $msg Optional message.
* @return WP_Error
*/
private static function forbidden( $msg = null ) {
return new WP_Error(
'saddle_forbidden',
$msg ? $msg : __( 'You do not have permission to do that with this item.', 'saddle' ),
array( 'status' => 403 )
);
}
/**
* Enforce the meta capabilities that wp_insert_post()/wp_update_post() do
* not check themselves: editing a specific object, publishing, and assigning
* content to another author. The tier + generic-cap check in the
* permission_callback is necessary but not sufficient.
*
* @param string $type 'post'|'page'.
* @param array $input Writable input.
* @param int $current_owner Author compared against (existing author on
* update; current user on create).
* @param int $id Post id being edited, or 0 on create.
* @return true|WP_Error
*/
private static function authorize_write( $type, array $input, $current_owner, $id ) {
$is_page = ( 'page' === $type );
if ( $id > 0 && ! current_user_can( 'edit_post', $id ) ) {
return self::forbidden( __( 'You do not have permission to edit this item.', 'saddle' ) );
}
if ( isset( $input['status'] ) && 'publish' === sanitize_key( (string) $input['status'] ) ) {
$publish_cap = $is_page ? 'publish_pages' : 'publish_posts';
if ( ! current_user_can( $publish_cap ) ) {
return self::forbidden( __( 'You do not have permission to publish content.', 'saddle' ) );
}
}
if ( isset( $input['author'] ) && (int) $input['author'] !== (int) $current_owner ) {
$others_cap = $is_page ? 'edit_others_pages' : 'edit_others_posts';
if ( ! current_user_can( $others_cap ) ) {
return self::forbidden( __( 'You do not have permission to assign content to another user.', 'saddle' ) );
}
}
return true;
}
/**
* Drafts-only policy for a brand-new post/page (see
* Saddle_Capabilities::DRAFTS_ONLY_OPTION). There is no existing object to
* protect on create, so an explicit status=publish is rewritten to draft
* before insert rather than gated — nothing public exists yet either way.
*
* @param array $input Create input, mutated in place.
* @return bool Whether the requested status was overridden.
*/
private static function apply_drafts_only_to_create( array &$input ) {
if ( ! Saddle_Capabilities::is_drafts_only() ) {
return false;
}
if ( ! isset( $input['status'] ) || 'publish' !== sanitize_key( (string) $input['status'] ) ) {
return false;
}
$input['status'] = 'draft';
return true;
}
/**
* Drafts-only policy for an update that would flip an EXISTING post/page
* to publish: unlike create, something real is already sitting at its
* current status, so this is gated the same way delete_of_type() gates a
* deletion — a preview + confirm_token round trip — rather than silently
* downgraded. Not triggered when the item is already published, since
* nothing is being "flipped".
*
* @param string $type 'post'|'page'.
* @param int $id Post id being updated.
* @param WP_Post $existing The post before this update.
* @param array $input Update input (read for status + confirm_token).
* @return array|WP_Error|null Gate result to return immediately, or null
* when the update should proceed unguarded.
*/
private static function guard_publish_transition( $type, $id, $existing, array $input ) {
if ( ! Saddle_Capabilities::is_drafts_only() ) {
return null;
}
if ( ! isset( $input['status'] ) || 'publish' !== sanitize_key( (string) $input['status'] ) ) {
return null;
}
if ( 'publish' === $existing->post_status ) {
return null;
}
// Fold the rest of the payload into the token identity: a preview
// shown for one edit must not be confirmable into publishing a
// different one (same principle as delete's trash-vs-permanent bind).
$bind = md5( (string) wp_json_encode( array_diff_key( $input, array( 'confirm_token' => true ) ) ) );
return Saddle_Approval::gate(
array(
'action' => 'publish_' . $type,