PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 4.9.0
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v4.9.0
4.9.1 4.9.0 4.8.2 4.8.1 4.8.0 4.7.0 4.6.2 4.6.1 4.6.0 4.5.6 4.5.5 4.5.4 4.5.3 4.5.2 4.5.1 4.5.0 4.4.1 4.4.0 3.3.4 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.5.2 All 199 releases
betterdocs / includes / Abilities / Docs / DeleteDoc.php

DeleteDoc.php in BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot 4.9.0, at includes/Abilities/Docs/DeleteDoc.php

157 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Delete a doc ability.
4 *
5 * @package BetterDocs
6 * @since 4.9.0
7 */
8
9 namespace WPDeveloper\BetterDocs\Abilities\Docs;
10
11 if ( ! defined( 'ABSPATH' ) ) {
12 exit; // Exit if accessed directly.
13 }
14
15 use WPDeveloper\BetterDocs\Abilities\AbilityBase;
16 use WPDeveloper\BetterDocs\Abilities\AbilityError;
17 use WPDeveloper\BetterDocs\Abilities\Traits\ResolvesTerms;
18 use WPDeveloper\BetterDocs\Abilities\Traits\ShapesDocs;
19
20 /**
21 * Trash a doc, or delete it for good.
22 *
23 * `force` defaults to **false**, so the ordinary call is reversible: the doc
24 * goes to the trash and a human can put it back. `force: true` is the one that
25 * cannot be undone, which is why `destructiveHint` is true on this tool and a
26 * client is expected to confirm before calling it.
27 *
28 * @since 4.9.0
29 */
30 class DeleteDoc extends AbilityBase {
31
32 use ResolvesTerms;
33 use ShapesDocs;
34
35 /**
36 * @since 4.9.0
37 */
38 public function __construct() {
39 $this->id = 'betterdocs/delete-doc';
40 $this->label = __( 'Delete doc', 'betterdocs' );
41 $this->description = __( 'Trash a BetterDocs doc, or delete it permanently with force:true. Trashing is reversible; force is not.', 'betterdocs' );
42 $this->capability = 'delete_docs';
43 }
44
45 /**
46 * @since 4.9.0
47 *
48 * @return array
49 */
50 public function get_annotations() {
51 return [
52 'readonly' => false,
53 'destructive' => true,
54 'idempotent' => false,
55 'priority' => 3.0,
56 'openWorldHint' => false
57 ];
58 }
59
60 /**
61 * @since 4.9.0
62 *
63 * @return array
64 */
65 public function get_input_schema() {
66 return [
67 'type' => 'object',
68 'additionalProperties' => false,
69 'required' => [ 'id' ],
70 'properties' => [
71 'id' => [
72 'type' => 'integer',
73 'description' => __( 'The doc id to delete. Required.', 'betterdocs' )
74 ],
75 'force' => [
76 'type' => 'boolean',
77 'default' => false,
78 'description' => __( 'false (default) moves the doc to the trash and can be undone. true deletes it permanently.', 'betterdocs' )
79 ]
80 ],
81 'default' => []
82 ];
83 }
84
85 /**
86 * @since 4.9.0
87 *
88 * @return array
89 */
90 public function get_output_schema() {
91 return [
92 'type' => 'object',
93 'properties' => [
94 'id' => [ 'type' => 'integer' ],
95 'deleted' => [ 'type' => 'boolean' ],
96 'trashed' => [ 'type' => 'boolean' ],
97 'title' => [ 'type' => 'string' ]
98 ]
99 ];
100 }
101
102 /**
103 * @since 4.9.0
104 *
105 * @param array $input Validated input.
106 * @return array|\WP_Error
107 */
108 public function execute( $input ) {
109 $post = $this->require_doc( isset( $input['id'] ) ? $input['id'] : 0 );
110
111 if ( is_wp_error( $post ) ) {
112 return $post;
113 }
114
115 $force = ! empty( $input['force'] );
116
117 if ( ! current_user_can( 'delete_post', $post->ID ) ) {
118 return AbilityError::capability_missing(
119 $this->deleting_capability( $post ),
120 sprintf(
121 /* translators: %d: doc id. */
122 __( 'delete doc #%d', 'betterdocs' ),
123 (int) $post->ID
124 )
125 );
126 }
127
128 $title = get_the_title( $post );
129
130 $deleted = $this->dispatch(
131 'DELETE',
132 '/docs/' . (int) $post->ID,
133 [ 'force' => $force ],
134 'wp/v2'
135 );
136
137 if ( is_wp_error( $deleted ) ) {
138 return $this->map_rest_error(
139 $deleted,
140 sprintf(
141 /* translators: %d: doc id. */
142 __( 'delete doc #%d', 'betterdocs' ),
143 (int) $post->ID
144 ),
145 $post
146 );
147 }
148
149 return [
150 'id' => (int) $post->ID,
151 'deleted' => true,
152 'trashed' => ! $force,
153 'title' => (string) $title
154 ];
155 }
156 }
157