PluginProbe
ActivityPub / 7.2.0
ActivityPub v7.2.0
9.3.1 9.3.0 9.2.2 9.2.1 9.2.0 9.1.0 9.0.2 9.0.1 9.0.0 8.3.0 8.2.1 8.2.0 8.1.1 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.2.0 1.3.0 2.0.0 2.0.1 2.1.0 2.1.1 All 160 releases
activitypub / includes / class-cli.php

class-cli.php in ActivityPub 7.2.0, at includes/class-cli.php

261 lines 5.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WP-CLI file.
4 *
5 * @package Activitypub
6 */
7
8 namespace Activitypub;
9
10 use Activitypub\Collection\Actors;
11 use Activitypub\Collection\Followers;
12 use Activitypub\Collection\Outbox;
13
14 /**
15 * WP-CLI commands.
16 *
17 * @package Activitypub
18 */
19 class Cli extends \WP_CLI_Command {
20
21 /**
22 * Remove the entire blog from the Fediverse.
23 *
24 * ## EXAMPLES
25 *
26 * $ wp activitypub self-destruct
27 *
28 * @param array|null $args The arguments.
29 * @param array|null $assoc_args The associative arguments.
30 *
31 * @return void
32 */
33 public function self_destruct( $args, $assoc_args ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
34 \WP_CLI::warning( 'Self-Destructing is not implemented yet.' );
35 }
36
37 /**
38 * Delete or Update a Post, Page, Custom Post Type or Attachment.
39 *
40 * ## OPTIONS
41 *
42 * <action>
43 * : The action to perform. Either `delete` or `update`.
44 * ---
45 * options:
46 * - delete
47 * - update
48 * ---
49 *
50 * <id>
51 * : The id of the Post, Page, Custom Post Type or Attachment.
52 *
53 * ## EXAMPLES
54 *
55 * $ wp activitypub post delete 1
56 *
57 * @synopsis <action> <id>
58 *
59 * @param array $args The arguments.
60 */
61 public function post( $args ) {
62 $post = get_post( $args[1] );
63
64 if ( ! $post ) {
65 \WP_CLI::error( 'Post not found.' );
66 }
67
68 switch ( $args[0] ) {
69 case 'delete':
70 \WP_CLI::confirm( 'Do you really want to delete the (Custom) Post with the ID: ' . $args[1] );
71 add_to_outbox( $post, 'Delete', $post->post_author );
72 \WP_CLI::success( '"Delete" activity is queued.' );
73 break;
74 case 'update':
75 add_to_outbox( $post, 'Update', $post->post_author );
76 \WP_CLI::success( '"Update" activity is queued.' );
77 break;
78 default:
79 \WP_CLI::error( 'Unknown action.' );
80 }
81 }
82
83 /**
84 * Delete or Update a Comment.
85 *
86 * ## OPTIONS
87 *
88 * <action>
89 * : The action to perform. Either `delete` or `update`.
90 * ---
91 * options:
92 * - delete
93 * - update
94 * ---
95 *
96 * <id>
97 * : The id of the Comment.
98 *
99 * ## EXAMPLES
100 *
101 * $ wp activitypub comment delete 1
102 *
103 * @synopsis <action> <id>
104 *
105 * @param array $args The arguments.
106 */
107 public function comment( $args ) {
108 $comment = get_comment( $args[1] );
109
110 if ( ! $comment ) {
111 \WP_CLI::error( 'Comment not found.' );
112 }
113
114 if ( was_comment_received( $comment ) ) {
115 \WP_CLI::error( 'This comment was received via ActivityPub and cannot be deleted or updated.' );
116 }
117
118 switch ( $args[0] ) {
119 case 'delete':
120 \WP_CLI::confirm( 'Do you really want to delete the Comment with the ID: ' . $args[1] );
121 add_to_outbox( $comment, 'Delete', $comment->user_id );
122 \WP_CLI::success( '"Delete" activity is queued.' );
123 break;
124 case 'update':
125 add_to_outbox( $comment, 'Update', $comment->user_id );
126 \WP_CLI::success( '"Update" activity is queued.' );
127 break;
128 default:
129 \WP_CLI::error( 'Unknown action.' );
130 }
131 }
132
133 /**
134 * Undo an activity that was sent to the Fediverse.
135 *
136 * ## OPTIONS
137 *
138 * <outbox_item_id>
139 * The ID or URL of the outbox item to undo.
140 *
141 * ## EXAMPLES
142 *
143 * $ wp activitypub undo 123
144 * $ wp activitypub undo "https://example.com/?post_type=ap_outbox&p=123"
145 *
146 * @synopsis <outbox_item_id>
147 *
148 * @param array $args The arguments.
149 */
150 public function undo( $args ) {
151 $outbox_item_id = $args[0];
152 if ( ! is_numeric( $outbox_item_id ) ) {
153 $outbox_item_id = url_to_postid( $outbox_item_id );
154 }
155
156 $outbox_item_id = get_post( $outbox_item_id );
157 if ( ! $outbox_item_id ) {
158 \WP_CLI::error( 'Activity not found.' );
159 }
160
161 $undo_id = Outbox::undo( $outbox_item_id );
162 if ( ! $undo_id ) {
163 \WP_CLI::error( 'Failed to undo activity.' );
164 }
165 \WP_CLI::success( 'Undo activity scheduled.' );
166 }
167
168 /**
169 * Re-Schedule an activity that was sent to the Fediverse before.
170 *
171 * ## OPTIONS
172 *
173 * <outbox_item_id>
174 * The ID or URL of the outbox item to reschedule.
175 *
176 * ## EXAMPLES
177 *
178 * $ wp activitypub reschedule 123
179 * $ wp activitypub reschedule "https://example.com/?post_type=ap_outbox&p=123"
180 *
181 * @synopsis <outbox_item_id>
182 *
183 * @param array $args The arguments.
184 */
185 public function reschedule( $args ) {
186 $outbox_item_id = $args[0];
187 if ( ! is_numeric( $outbox_item_id ) ) {
188 $outbox_item_id = url_to_postid( $outbox_item_id );
189 }
190
191 $outbox_item_id = get_post( $outbox_item_id );
192 if ( ! $outbox_item_id ) {
193 \WP_CLI::error( 'Activity not found.' );
194 }
195
196 Outbox::reschedule( $outbox_item_id );
197
198 \WP_CLI::success( 'Rescheduled activity.' );
199 }
200
201 /**
202 * Move the blog to a new URL.
203 *
204 * ## OPTIONS
205 *
206 * <from>
207 * The current URL of the blog.
208 *
209 * <to>
210 * The new URL of the blog.
211 *
212 * ## EXAMPLES
213 *
214 * $ wp activitypub move https://example.com/ https://newsite.com/
215 *
216 * @synopsis <from> <to>
217 *
218 * @param array $args The arguments.
219 */
220 public function move( $args ) {
221 $from = $args[0];
222 $to = $args[1];
223
224 $outbox_item_id = Move::account( $from, $to );
225
226 if ( is_wp_error( $outbox_item_id ) ) {
227 \WP_CLI::error( $outbox_item_id->get_error_message() );
228 } else {
229 \WP_CLI::success( 'Move Scheduled.' );
230 }
231 }
232
233 /**
234 * Follow a user.
235 *
236 * ## OPTIONS
237 *
238 * <remote-user>
239 * The remote user to follow.
240 *
241 * ## EXAMPLES
242 *
243 * $ wp activitypub follow https://example.com/@user
244 * $ wp --user=pfefferle activitypub follow https://example.com/@user
245 *
246 * @synopsis <remote_user>
247 *
248 * @param array $args The arguments.
249 */
250 public function follow( $args ) {
251 $user_id = \get_current_user_id();
252 $follow = follow( $args[0], $user_id );
253
254 if ( is_wp_error( $follow ) ) {
255 \WP_CLI::error( $follow->get_error_message() );
256 } else {
257 \WP_CLI::success( 'Follow Scheduled.' );
258 }
259 }
260 }
261