PluginProbe
ActivityPub / 2.1.0
ActivityPub v2.1.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-admin.php

class-admin.php in ActivityPub 2.1.0, at includes/class-admin.php

351 lines 9.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Activitypub;
3
4 use WP_User_Query;
5 use Activitypub\Model\Blog_User;
6
7 use function Activitypub\is_user_disabled;
8 use function Activitypub\was_comment_received;
9 use function Activitypub\is_comment_federatable;
10
11 /**
12 * ActivityPub Admin Class
13 *
14 * @author Matthias Pfefferle
15 */
16 class Admin {
17 /**
18 * Initialize the class, registering WordPress hooks
19 */
20 public static function init() {
21 \add_action( 'admin_menu', array( self::class, 'admin_menu' ) );
22 \add_action( 'admin_init', array( self::class, 'register_settings' ) );
23 \add_action( 'load-comment.php', array( self::class, 'edit_comment' ) );
24 \add_action( 'personal_options_update', array( self::class, 'save_user_description' ) );
25 \add_action( 'admin_enqueue_scripts', array( self::class, 'enqueue_scripts' ) );
26 \add_action( 'admin_notices', array( self::class, 'admin_notices' ) );
27 \add_filter( 'comment_row_actions', array( self::class, 'comment_row_actions' ), 10, 2 );
28
29 if ( ! is_user_disabled( get_current_user_id() ) ) {
30 \add_action( 'show_user_profile', array( self::class, 'add_profile' ) );
31 }
32 }
33
34 /**
35 * Add admin menu entry
36 */
37 public static function admin_menu() {
38 $settings_page = \add_options_page(
39 'Welcome',
40 'ActivityPub',
41 'manage_options',
42 'activitypub',
43 array( self::class, 'settings_page' )
44 );
45
46 \add_action( 'load-' . $settings_page, array( self::class, 'add_settings_help_tab' ) );
47
48 // user has to be able to publish posts
49 if ( ! is_user_disabled( get_current_user_id() ) ) {
50 $followers_list_page = \add_users_page( \__( 'Followers', 'activitypub' ), \__( 'Followers', 'activitypub' ), 'read', 'activitypub-followers-list', array( self::class, 'followers_list_page' ) );
51
52 \add_action( 'load-' . $followers_list_page, array( self::class, 'add_followers_list_help_tab' ) );
53 }
54 }
55
56 /**
57 * Display admin menu notices about configuration problems or conflicts.
58 *
59 * @return void
60 */
61 public static function admin_notices() {
62 $permalink_structure = \get_option( 'permalink_structure' );
63 if ( empty( $permalink_structure ) ) {
64 $admin_notice = \__( 'You are using the ActivityPub plugin without setting a permalink structure. This will prevent ActivityPub from working. Please set a permalink structure.', 'activitypub' );
65 self::show_admin_notice( $admin_notice, 'error' );
66 }
67 }
68
69 /**
70 * Display one admin menu notice about configuration problems or conflicts.
71 *
72 * @param string $admin_notice The notice to display.
73 * @param string $level The level of the notice (error, warning, success, info).
74 *
75 * @return void
76 */
77 private static function show_admin_notice( $admin_notice, $level ) {
78 ?>
79
80 <div class="notice notice-<?php echo esc_attr( $level ); ?>">
81 <p><?php echo wp_kses( $admin_notice, 'data' ); ?></p>
82 </div>
83
84 <?php
85 }
86
87 /**
88 * Load settings page
89 */
90 public static function settings_page() {
91 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
92 if ( empty( $_GET['tab'] ) ) {
93 $tab = 'welcome';
94 } else {
95 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
96 $tab = sanitize_key( $_GET['tab'] );
97 }
98
99 switch ( $tab ) {
100 case 'settings':
101 \load_template( ACTIVITYPUB_PLUGIN_DIR . 'templates/settings.php' );
102 break;
103 case 'followers':
104 \load_template( ACTIVITYPUB_PLUGIN_DIR . 'templates/blog-user-followers-list.php' );
105 break;
106 case 'welcome':
107 default:
108 wp_enqueue_script( 'plugin-install' );
109 add_thickbox();
110 wp_enqueue_script( 'updates' );
111
112 \load_template( ACTIVITYPUB_PLUGIN_DIR . 'templates/welcome.php' );
113 break;
114 }
115 }
116
117 /**
118 * Load user settings page
119 */
120 public static function followers_list_page() {
121 // user has to be able to publish posts
122 if ( ! is_user_disabled( get_current_user_id() ) ) {
123 \load_template( ACTIVITYPUB_PLUGIN_DIR . 'templates/user-followers-list.php' );
124 }
125 }
126
127 /**
128 * Register ActivityPub settings
129 */
130 public static function register_settings() {
131 \register_setting(
132 'activitypub',
133 'activitypub_post_content_type',
134 array(
135 'type' => 'string',
136 'description' => \__( 'Use title and link, summary, full or custom content', 'activitypub' ),
137 'show_in_rest' => array(
138 'schema' => array(
139 'enum' => array(
140 'title',
141 'excerpt',
142 'content',
143 ),
144 ),
145 ),
146 'default' => 'content',
147 )
148 );
149 \register_setting(
150 'activitypub',
151 'activitypub_custom_post_content',
152 array(
153 'type' => 'string',
154 'description' => \__( 'Define your own custom post template', 'activitypub' ),
155 'show_in_rest' => true,
156 'default' => ACTIVITYPUB_CUSTOM_POST_CONTENT,
157 )
158 );
159 \register_setting(
160 'activitypub',
161 'activitypub_max_image_attachments',
162 array(
163 'type' => 'integer',
164 'description' => \__( 'Number of images to attach to posts.', 'activitypub' ),
165 'default' => ACTIVITYPUB_MAX_IMAGE_ATTACHMENTS,
166 )
167 );
168 \register_setting(
169 'activitypub',
170 'activitypub_object_type',
171 array(
172 'type' => 'string',
173 'description' => \__( 'The Activity-Object-Type', 'activitypub' ),
174 'show_in_rest' => array(
175 'schema' => array(
176 'enum' => array(
177 'note',
178 'article',
179 'wordpress-post-format',
180 ),
181 ),
182 ),
183 'default' => 'note',
184 )
185 );
186 \register_setting(
187 'activitypub',
188 'activitypub_use_hashtags',
189 array(
190 'type' => 'boolean',
191 'description' => \__( 'Add hashtags in the content as native tags and replace the #tag with the tag-link', 'activitypub' ),
192 'default' => '0',
193 )
194 );
195 \register_setting(
196 'activitypub',
197 'activitypub_support_post_types',
198 array(
199 'type' => 'string',
200 'description' => \esc_html__( 'Enable ActivityPub support for post types', 'activitypub' ),
201 'show_in_rest' => true,
202 'default' => array( 'post' ),
203 )
204 );
205 \register_setting(
206 'activitypub',
207 'activitypub_blog_user_identifier',
208 array(
209 'type' => 'string',
210 'description' => \esc_html__( 'The Identifier of the Blog-User', 'activitypub' ),
211 'show_in_rest' => true,
212 'default' => Blog_User::get_default_username(),
213 'sanitize_callback' => function ( $value ) {
214 // hack to allow dots in the username
215 $parts = explode( '.', $value );
216 $sanitized = array();
217
218 foreach ( $parts as $part ) {
219 $sanitized[] = \sanitize_title( $part );
220 }
221
222 $sanitized = implode( '.', $sanitized );
223
224 // check for login or nicename.
225 $user = new WP_User_Query(
226 array(
227 'search' => $sanitized,
228 'search_columns' => array( 'user_login', 'user_nicename' ),
229 'number' => 1,
230 'hide_empty' => true,
231 'fields' => 'ID',
232 )
233 );
234
235 if ( $user->results ) {
236 add_settings_error(
237 'activitypub_blog_user_identifier',
238 'activitypub_blog_user_identifier',
239 \esc_html__( 'You cannot use an existing author\'s name for the blog profile ID.', 'activitypub' ),
240 'error'
241 );
242
243 return Blog_User::get_default_username();
244 }
245
246 return $sanitized;
247 },
248 )
249 );
250 \register_setting(
251 'activitypub',
252 'activitypub_enable_users',
253 array(
254 'type' => 'boolean',
255 'description' => \__( 'Every Author on this Blog (with the publish_posts capability) gets his own ActivityPub enabled Profile.', 'activitypub' ),
256 'default' => '1',
257 )
258 );
259 \register_setting(
260 'activitypub',
261 'activitypub_enable_blog_user',
262 array(
263 'type' => 'boolean',
264 'description' => \__( 'Your Blog becomes an ActivityPub compatible Profile.', 'activitypub' ),
265 'default' => '0',
266 )
267 );
268 }
269
270 public static function add_settings_help_tab() {
271 require_once ACTIVITYPUB_PLUGIN_DIR . 'includes/help.php';
272 }
273
274 public static function add_followers_list_help_tab() {
275 // todo
276 }
277
278 public static function add_profile( $user ) {
279 $description = get_user_meta( $user->ID, 'activitypub_user_description', true );
280
281 \load_template(
282 ACTIVITYPUB_PLUGIN_DIR . 'templates/user-settings.php',
283 true,
284 array(
285 'description' => $description,
286 )
287 );
288 }
289
290 public static function save_user_description( $user_id ) {
291 if ( ! isset( $_REQUEST['_apnonce'] ) ) {
292 return false;
293 }
294 $nonce = sanitize_text_field( wp_unslash( $_REQUEST['_apnonce'] ) );
295 if (
296 ! wp_verify_nonce( $nonce, 'activitypub-user-description' ) ||
297 ! current_user_can( 'edit_user', $user_id )
298 ) {
299 return false;
300 }
301 $description = ! empty( $_POST['activitypub-user-description'] ) ? sanitize_text_field( wp_unslash( $_POST['activitypub-user-description'] ) ) : false;
302 if ( $description ) {
303 update_user_meta( $user_id, 'activitypub_user_description', $description );
304 }
305 }
306
307 public static function enqueue_scripts( $hook_suffix ) {
308 if ( false !== strpos( $hook_suffix, 'activitypub' ) ) {
309 wp_enqueue_style( 'activitypub-admin-styles', plugins_url( 'assets/css/activitypub-admin.css', ACTIVITYPUB_PLUGIN_FILE ), array(), '1.0.0' );
310 wp_enqueue_script( 'activitypub-admin-styles', plugins_url( 'assets/js/activitypub-admin.js', ACTIVITYPUB_PLUGIN_FILE ), array( 'jquery' ), '1.0.0', false );
311 }
312 }
313
314 /**
315 * Hook into the edit_comment functionality
316 *
317 * * Disable the edit_comment capability for federated comments.
318 *
319 * @return void
320 */
321 public static function edit_comment() {
322 // Disable the edit_comment capability for federated comments.
323 \add_filter(
324 'user_has_cap',
325 function ( $allcaps, $caps, $arg ) {
326 if ( 'edit_comment' !== $arg[0] ) {
327 return $allcaps;
328 }
329
330 if ( was_comment_received( $arg[2] ) ) {
331 return false;
332 }
333
334 return $allcaps;
335 },
336 1,
337 3
338 );
339 }
340
341 public static function comment_row_actions( $actions, $comment ) {
342 if ( was_comment_received( $comment ) ) {
343 unset( $actions['edit'] );
344 unset( $actions['reply'] );
345 unset( $actions['quickedit'] );
346 }
347
348 return $actions;
349 }
350 }
351