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