PluginProbe
ActivityPub / 2.0.1
ActivityPub v2.0.1
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.0.1, at includes/class-admin.php

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