Merge pull request #365 from Automattic/fix/cleanup

This commit is contained in:
Matthias Pfefferle 2023-07-20 23:30:39 +02:00 committed by GitHub
commit dd1c0a3bb5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 164 additions and 35 deletions

View file

@ -29,9 +29,6 @@ function init() {
\defined( 'ACTIVITYPUB_CUSTOM_POST_CONTENT' ) || \define( 'ACTIVITYPUB_CUSTOM_POST_CONTENT', "<strong>[ap_title]</strong>\n\n[ap_content]\n\n[ap_hashtags]\n\n[ap_shortlink]" );
\defined( 'ACTIVITYPUB_SECURE_MODE' ) || \define( 'ACTIVITYPUB_SECURE_MODE', apply_filters( 'activitypub_secure_mode', $value = false ) );
\defined( 'ACTIVITYPUB_DISABLE_USER' ) || \define( 'ACTIVITYPUB_DISABLE_USER', false );
\defined( 'ACTIVITYPUB_DISABLE_BLOG_USER' ) || \define( 'ACTIVITYPUB_DISABLE_BLOG_USER', false );
\define( 'ACTIVITYPUB_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
\define( 'ACTIVITYPUB_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
\define( 'ACTIVITYPUB_PLUGIN_FILE', plugin_dir_path( __FILE__ ) . '/' . basename( __FILE__ ) );

View file

@ -39,7 +39,7 @@ class Activity_Dispatcher {
* @return void
*/
public static function send_activity_or_announce( WP_Post $wp_post, $type ) {
if ( is_user_disabled( Users::BLOG_USER_ID ) ) {
if ( is_user_type_disabled( 'blog' ) ) {
return;
}

View file

@ -83,7 +83,7 @@ class Activitypub {
$json_template = false;
// check if user can publish posts
if ( \is_author() && ! Users::get_by_id( \get_the_author_meta( 'ID' ) ) ) {
if ( \is_author() && is_wp_error( Users::get_by_id( \get_the_author_meta( 'ID' ) ) ) ) {
return $template;
}

View file

@ -13,9 +13,12 @@ class Admin {
public static function init() {
\add_action( 'admin_menu', array( self::class, 'admin_menu' ) );
\add_action( 'admin_init', array( self::class, 'register_settings' ) );
\add_action( 'show_user_profile', array( self::class, 'add_profile' ) );
\add_action( 'personal_options_update', array( self::class, 'save_user_description' ) );
\add_action( 'admin_enqueue_scripts', array( self::class, 'enqueue_scripts' ) );
if ( ! is_user_disabled( get_current_user_id() ) ) {
\add_action( 'show_user_profile', array( self::class, 'add_profile' ) );
}
}
/**
@ -74,11 +77,11 @@ class Admin {
* Load user settings page
*/
public static function followers_list_page() {
if ( ! current_user_can( 'publish_posts' ) ) {
return;
}
// user has to be able to publish posts
if ( ! is_user_disabled( get_current_user_id() ) ) {
\load_template( ACTIVITYPUB_PLUGIN_DIR . 'templates/user-followers-list.php' );
}
}
/**
* Register ActivityPub settings
@ -145,7 +148,7 @@ class Admin {
array(
'type' => 'boolean',
'description' => \__( 'Add hashtags in the content as native tags and replace the #tag with the tag-link', 'activitypub' ),
'default' => 0,
'default' => '0',
)
);
\register_setting(
@ -163,9 +166,9 @@ class Admin {
'activitypub_blog_user_identifier',
array(
'type' => 'string',
'description' => \esc_html__( 'The Identifier of th Blog-User', 'activitypub' ),
'description' => \esc_html__( 'The Identifier of the Blog-User', 'activitypub' ),
'show_in_rest' => true,
'default' => 'feed',
'default' => \Activitypub\Model\Blog_User::get_default_username(),
'sanitize_callback' => function( $value ) {
// hack to allow dots in the username
$parts = explode( '.', $value );
@ -179,6 +182,24 @@ class Admin {
},
)
);
\register_setting(
'activitypub',
'activitypub_enable_users',
array(
'type' => 'boolean',
'description' => \__( 'Every Author on this Blog (with the publish_posts capability) gets his own ActivityPub enabled Profile.', 'activitypub' ),
'default' => '1',
)
);
\register_setting(
'activitypub',
'activitypub_enable_blog_user',
array(
'type' => 'boolean',
'description' => \__( 'Your Blog becomes an ActivityPub compatible Profile.', 'activitypub' ),
'default' => '0',
)
);
}
public static function add_settings_help_tab() {

View file

@ -1,6 +1,7 @@
<?php
namespace Activitypub;
use WP_Error;
use Activitypub\Http;
use Activitypub\Activity\Activity;
use Activitypub\Collection\Followers;
@ -293,8 +294,8 @@ function is_user_disabled( $user_id ) {
break;
// if the user is the blog user, it's only enabled in single-user mode.
case \Activitypub\Collection\Users::BLOG_USER_ID:
if ( defined( 'ACTIVITYPUB_DISABLE_BLOG_USER' ) ) {
$return = ACTIVITYPUB_DISABLE_BLOG_USER;
if ( is_user_type_disabled( 'blog' ) ) {
$return = true;
break;
}
@ -307,8 +308,8 @@ function is_user_disabled( $user_id ) {
break;
}
if ( defined( 'ACTIVITYPUB_DISABLE_USER' ) ) {
$return = ACTIVITYPUB_DISABLE_USER;
if ( is_user_type_disabled( 'user' ) ) {
$return = true;
break;
}
@ -324,6 +325,66 @@ function is_user_disabled( $user_id ) {
return apply_filters( 'activitypub_is_user_disabled', $return, $user_id );
}
/**
* Checks if a User-Type is disabled for ActivityPub.
*
* This function is used to check if the 'blog' or 'user'
* type is disabled for ActivityPub.
*
* @param enum $type Can be 'blog' or 'user'.
*
* @return boolean True if the user type is disabled, false otherwise.
*/
function is_user_type_disabled( $type ) {
switch ( $type ) {
case 'blog':
if ( \defined( 'ACTIVITYPUB_SINGLE_USER_MODE' ) ) {
if ( ACTIVITYPUB_SINGLE_USER_MODE ) {
$return = false;
break;
}
}
if ( \defined( 'ACTIVITYPUB_DISABLE_BLOG_USER' ) ) {
$return = ACTIVITYPUB_DISABLE_BLOG_USER;
break;
}
if ( '1' !== \get_option( 'activitypub_enable_blog_user', '0' ) ) {
$return = true;
break;
}
$return = false;
break;
case 'user':
if ( \defined( 'ACTIVITYPUB_SINGLE_USER_MODE' ) ) {
if ( ACTIVITYPUB_SINGLE_USER_MODE ) {
$return = true;
break;
}
}
if ( \defined( 'ACTIVITYPUB_DISABLE_USER' ) ) {
$return = ACTIVITYPUB_DISABLE_USER;
break;
}
if ( '1' !== \get_option( 'activitypub_enable_users', '1' ) ) {
$return = true;
break;
}
$return = false;
break;
default:
$return = new WP_Error( 'activitypub_wrong_user_type', __( 'Wrong user type', 'activitypub' ) );
break;
}
return apply_filters( 'activitypub_is_user_type_disabled', $return, $type );
}
/**
* Check if the blog is in single-user mode.
*
@ -332,14 +393,18 @@ function is_user_disabled( $user_id ) {
function is_single_user() {
$return = false;
if (
false === ACTIVITYPUB_DISABLE_BLOG_USER &&
true === ACTIVITYPUB_DISABLE_USER
if ( \defined( 'ACTIVITYPUB_SINGLE_USER_MODE' ) ) {
if ( ACTIVITYPUB_SINGLE_USER_MODE ) {
$return = true;
}
} elseif (
false === is_user_type_disabled( 'blog' ) &&
true === is_user_type_disabled( 'user' )
) {
$return = true;
}
return apply_filters( 'activitypub_is_single_user', $return );
return $return;
}
if ( ! function_exists( 'get_self_link' ) ) {

View file

@ -1,6 +1,6 @@
<?php
\load_template(
\dirname( __FILE__ ) . '/admin-header.php',
__DIR__ . '/admin-header.php',
true,
array(
'settings' => 'active',
@ -28,12 +28,12 @@
<p><?php \esc_html_e( 'Customize your ActivityPub settings to suit your needs.', 'activitypub' ); ?></p>
<hr />
<form method="post" action="options.php">
<?php \settings_fields( 'activitypub' ); ?>
<?php if ( ! \Activitypub\is_user_disabled( \Activitypub\Collection\Users::BLOG_USER_ID ) ) : ?>
<h3><?php \esc_html_e( 'Blog-User', 'activitypub' ); ?></h3>
<h3 class="dashicons-before dashicons-admin-users"><?php \esc_html_e( 'Blog-User', 'activitypub' ); ?></h3>
<p><?php \esc_html_e( 'All settings for the Blog-User (Catch-All Account)', 'activitypub' ); ?></p>
@ -58,9 +58,45 @@
<?php \do_settings_fields( 'activitypub', 'blog-user' ); ?>
<?php endif; ?>
<hr />
<h3><?php \esc_html_e( 'Activities', 'activitypub' ); ?></h3>
<h3 class="dashicons-before dashicons-groups"><?php \esc_html_e( 'Users', 'activitypub' ); ?></h3>
<p><?php \esc_html_e( 'All user related settings.', 'activitypub' ); ?></p>
<table class="form-table">
<tbody>
<tr>
<th scope="row">
<?php \esc_html_e( 'Enable/disable Users by Type', 'activitypub' ); ?>
</th>
<td>
<p>
<label>
<input type="checkbox" name="activitypub_enable_users" id="activitypub_enable_users" value="1" <?php echo \checked( '1', \get_option( 'activitypub_enable_users', '1' ) ); ?> />
<?php \esc_html_e( 'Enable Authors', 'activitypub' ); ?>
</label>
</p>
<p class="description">
<?php echo \wp_kses( \__( 'Every Author on this Blog (with the <code>publish_posts</code> capability) gets his own ActivityPub enabled Profile.', 'activitypub' ), array( 'code' => array() ) ); ?>
</p>
<p>
<label>
<input type="checkbox" name="activitypub_enable_blog_user" id="activitypub_enable_blog_user" value="1" <?php echo \checked( '1', \get_option( 'activitypub_enable_blog_user', '0' ) ); ?> />
<?php \esc_html_e( 'Enable Blog-User', 'activitypub' ); ?>
</label>
</p>
<p class="description">
<?php \esc_html_e( 'Your Blog becomes an ActivityPub compatible Profile.', 'activitypub' ); ?>
</p>
</td>
</tr>
</tbody>
</table>
<?php \do_settings_fields( 'activitypub', 'user' ); ?>
<h3 class="dashicons-before dashicons-format-chat"><?php \esc_html_e( 'Activities', 'activitypub' ); ?></h3>
<p><?php \esc_html_e( 'All activity related settings.', 'activitypub' ); ?></p>
@ -222,7 +258,7 @@
<?php \do_settings_fields( 'activitypub', 'activity' ); ?>
<h3><?php \esc_html_e( 'Server', 'activitypub' ); ?></h3>
<h3 class="dashicons-before dashicons-database"><?php \esc_html_e( 'Server', 'activitypub' ); ?></h3>
<p><?php \esc_html_e( 'Server related settings.', 'activitypub' ); ?></p>

View file

@ -17,7 +17,7 @@
<?php if ( ! \Activitypub\is_user_disabled( \Activitypub\Collection\Users::BLOG_USER_ID ) ) : ?>
<h3><?php \esc_html_e( 'Blog Account', 'activitypub' ); ?></h3>
<h3 class="dashicons-before dashicons-admin-users"><?php \esc_html_e( 'Blog Account', 'activitypub' ); ?></h3>
<p>
<?php
$blog_user = new \Activitypub\Model\Blog_User();
@ -41,7 +41,7 @@
<?php if ( ! \Activitypub\is_user_disabled( get_current_user_id() ) ) : ?>
<h3><?php \esc_html_e( 'Personal Account', 'activitypub' ); ?></h3>
<h3 class="dashicons-before dashicons-groups"><?php \esc_html_e( 'Personal Account', 'activitypub' ); ?></h3>
<p>
<?php
$user = \Activitypub\Collection\Users::get_by_id( wp_get_current_user()->ID );
@ -63,7 +63,7 @@
<?php endif; ?>
<h3><?php \esc_html_e( 'Troubleshooting', 'activitypub' ); ?></h3>
<h3 class="dashicons-before dashicons-admin-tools"><?php \esc_html_e( 'Troubleshooting', 'activitypub' ); ?></h3>
<p>
<?php
echo wp_kses(
@ -82,7 +82,7 @@
<?php if ( ACTIVITYPUB_SHOW_PLUGIN_RECOMMENDATIONS ) : ?>
<hr />
<h3><?php \esc_html_e( 'Recommended Plugins', 'activitypub' ); ?></h3>
<h3 class="dashicons-before dashicons-admin-plugins"><?php \esc_html_e( 'Recommended Plugins', 'activitypub' ); ?></h3>
<p><?php \esc_html_e( 'ActivityPub works as is and there is no need for you to install additional plugins, nevertheless there are some plugins that extends the functionality of ActivityPub.', 'activitypub' ); ?></p>

View file

@ -95,6 +95,8 @@ class Test_Activitypub_Activity_Dispatcher extends ActivityPub_TestCase_Cache_HT
}
public function test_dispatch_announce() {
add_filter( 'activitypub_is_user_type_disabled', '__return_false' );
$followers = array( 'https://example.com/author/jon' );
foreach ( $followers as $follower ) {
@ -131,12 +133,20 @@ class Test_Activitypub_Activity_Dispatcher extends ActivityPub_TestCase_Cache_HT
$followers = array( 'https://example.com/author/jon' );
add_filter(
'activitypub_is_single_user',
function( $return ) {
'activitypub_is_user_type_disabled',
function( $value, $type ) {
if ( 'blog' === $type ) {
return false;
} else {
return true;
}
},
10,
2
);
$this->assertTrue( \Activitypub\is_single_user() );
foreach ( $followers as $follower ) {
\Activitypub\Collection\Followers::add_follower( \Activitypub\Collection\Users::BLOG_USER_ID, $follower );
}