PluginProbe
ActivityPub / 1.0.0
ActivityPub v1.0.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 1.0.0, at includes/class-admin.php

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