Skip to content

Commit

Permalink
Simplify Actor-Settings (#918)
Browse files Browse the repository at this point in the history
* Simplify Actor-Settings

The new version uses a radio button instead of checkboxes. This way it is better to explain the different modes.

* revert unnecessary changes

* remove old settings to not replace them by accident

* Update templates/settings.php

Co-authored-by: Konstantin Obenland <[email protected]>

* Update templates/settings.php

Co-authored-by: Alex Kirk <[email protected]>

* Update templates/settings.php

Co-authored-by: Alex Kirk <[email protected]>

* Update templates/settings.php

Co-authored-by: Alex Kirk <[email protected]>

* Update includes/class-admin.php

Co-authored-by: Konstantin Obenland <[email protected]>

* use constants

* Update templates/settings.php

Co-authored-by: Alex Kirk <[email protected]>

* Update templates/settings.php

Co-authored-by: Alex Kirk <[email protected]>

* add comment

* add output escaping

* fix escaping

* Update templates/settings.php

Co-authored-by: Matt Wiebe <[email protected]>

* Update templates/settings.php

Co-authored-by: Matt Wiebe <[email protected]>

* Update templates/settings.php

Co-authored-by: Matt Wiebe <[email protected]>

* call migration!

* add unit tests

* use descriptive consts

---------

Co-authored-by: Konstantin Obenland <[email protected]>
Co-authored-by: Alex Kirk <[email protected]>
Co-authored-by: Matt Wiebe <[email protected]>
  • Loading branch information
4 people authored Oct 21, 2024
1 parent 280e640 commit 418b8dc
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 28 deletions.
7 changes: 6 additions & 1 deletion activitypub.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
require_once __DIR__ . '/includes/compat.php';
require_once __DIR__ . '/includes/functions.php';

\define( 'ACTIVITYPUB_PLUGIN_VERSION', '3.3.3' );
\define( 'ACTIVITYPUB_PLUGIN_VERSION', '4.0.0' );

/**
* Initialize the plugin constants.
Expand All @@ -46,6 +46,11 @@
\defined( 'ACTIVITYPUB_SEND_VARY_HEADER' ) || \define( 'ACTIVITYPUB_SEND_VARY_HEADER', false );
\defined( 'ACTIVITYPUB_DEFAULT_OBJECT_TYPE' ) || \define( 'ACTIVITYPUB_DEFAULT_OBJECT_TYPE', 'note' );

// Define Actor-Modes for the plugin.
\define( 'ACTIVITYPUB_ACTOR_MODE', 'actor' );
\define( 'ACTIVITYPUB_BLOG_MODE', 'blog' );
\define( 'ACTIVITYPUB_ACTOR_AND_BLOG_MODE', 'actor_blog' );

// Post visibility constants.
\define( 'ACTIVITYPUB_CONTENT_VISIBILITY_PUBLIC', '' );
\define( 'ACTIVITYPUB_CONTENT_VISIBILITY_QUIET_PUBLIC', 'quiet_public' );
Expand Down
16 changes: 4 additions & 12 deletions includes/class-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,22 +269,14 @@ public static function register_settings() {
);
\register_setting(
'activitypub',
'activitypub_enable_users',
'activitypub_actor_mode',
array(
'type' => 'boolean',
'description' => \__( 'Every Author on this Blog (with the publish_posts capability) gets his own ActivityPub enabled Profile.', 'activitypub' ),
'type' => 'integer',
'description' => \__( 'Choose your preferred Actor-Mode.', 'activitypub' ),
'default' => '1',
)
);
\register_setting(
'activitypub',
'activitypub_enable_blog_user',
array(
'type' => 'boolean',
'description' => \__( 'Your Blog becomes an ActivityPub compatible Profile.', 'activitypub' ),
'default' => '0',
)
);

\register_setting(
'activitypub',
'activitypub_attribution_domains',
Expand Down
59 changes: 51 additions & 8 deletions includes/class-migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public static function is_locked() {
* @return bool True if the database structure is up to date, false otherwise.
*/
public static function is_latest_version() {
return (bool) version_compare(
return (bool) \version_compare(
self::get_version(),
self::get_target_version(),
'=='
Expand Down Expand Up @@ -120,23 +120,34 @@ public static function maybe_migrate() {
if ( ! \wp_next_scheduled( 'activitypub_migrate', $version_from_db ) ) {
\wp_schedule_single_event( \time(), 'activitypub_migrate', array( $version_from_db ) );
}
if ( version_compare( $version_from_db, '0.17.0', '<' ) ) {
if ( \version_compare( $version_from_db, '0.17.0', '<' ) ) {
self::migrate_from_0_16();
}
if ( version_compare( $version_from_db, '1.3.0', '<' ) ) {
if ( \version_compare( $version_from_db, '1.3.0', '<' ) ) {
self::migrate_from_1_2_0();
}
if ( version_compare( $version_from_db, '2.1.0', '<' ) ) {
if ( \version_compare( $version_from_db, '2.1.0', '<' ) ) {
self::migrate_from_2_0_0();
}
if ( version_compare( $version_from_db, '2.3.0', '<' ) ) {
if ( \version_compare( $version_from_db, '2.3.0', '<' ) ) {
self::migrate_from_2_2_0();
}
if ( version_compare( $version_from_db, '3.0.0', '<' ) ) {
if ( \version_compare( $version_from_db, '3.0.0', '<' ) ) {
self::migrate_from_2_6_0();
}
if ( \version_compare( $version_from_db, '4.0.0', '<' ) ) {
self::migrate_to_4_0_0();
}

/**
* Fires when the system has to be migrated.
*
* @param string $version_from_db The version from which to migrate.
* @param string $target_version The target version to migrate to.
*/
\do_action( 'activitypub_migrate', $version_from_db, self::get_target_version() );

update_option( 'activitypub_db_version', self::get_target_version() );
\update_option( 'activitypub_db_version', self::get_target_version() );

self::unlock();
}
Expand All @@ -147,7 +158,7 @@ public static function maybe_migrate() {
* @param string $version_from_db The version from which to migrate.
*/
public static function async_migration( $version_from_db ) {
if ( version_compare( $version_from_db, '1.0.0', '<' ) ) {
if ( \version_compare( $version_from_db, '1.0.0', '<' ) ) {
self::migrate_from_0_17();
}
}
Expand Down Expand Up @@ -262,6 +273,13 @@ private static function migrate_from_2_6_0() {
self::update_options_key( 'activitypub_blog_user_identifier', 'activitypub_blog_identifier' );
}

/**
* Update actor-mode settings.
*/
private static function migrate_to_4_0_0() {
self::migrate_actor_mode();
}

/**
* Set the defaults needed for the plugin to work.
*
Expand Down Expand Up @@ -323,4 +341,29 @@ private static function update_options_key( $old_key, $new_key ) {
array( '%s' )
);
}

/**
* Migrate the actor mode settings.
*/
public static function migrate_actor_mode() {
$blog_profile = \get_option( 'activitypub_enable_blog_user', '0' );
$author_profiles = \get_option( 'activitypub_enable_users', '0' );

if (
'1' === $blog_profile &&
'1' === $author_profiles
) {
\update_option( 'activitypub_actor_mode', ACTIVITYPUB_ACTOR_AND_BLOG_MODE );
} elseif (
'1' === $blog_profile &&
'1' !== $author_profiles
) {
\update_option( 'activitypub_actor_mode', ACTIVITYPUB_BLOG_MODE );
} elseif (
'1' !== $blog_profile &&
'1' === $author_profiles
) {
\update_option( 'activitypub_actor_mode', ACTIVITYPUB_ACTOR_MODE );
}
}
}
4 changes: 2 additions & 2 deletions includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ function is_user_type_disabled( $type ) {
break;
}

if ( '1' !== \get_option( 'activitypub_enable_blog_user', '0' ) ) {
if ( ACTIVITYPUB_ACTOR_MODE === \get_option( 'activitypub_actor_mode', ACTIVITYPUB_ACTOR_MODE ) ) {
$disabled = true;
break;
}
Expand All @@ -524,7 +524,7 @@ function is_user_type_disabled( $type ) {
break;
}

if ( '1' !== \get_option( 'activitypub_enable_users', '1' ) ) {
if ( ACTIVITYPUB_BLOG_MODE === \get_option( 'activitypub_actor_mode', ACTIVITYPUB_ACTOR_MODE ) ) {
$disabled = true;
break;
}
Expand Down
19 changes: 14 additions & 5 deletions templates/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
<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 Author-Profiles', 'activitypub' ); ?>
<input type="radio" name="activitypub_actor_mode" id="activitypub_actor_mode" value="<?php echo esc_attr( ACTIVITYPUB_ACTOR_MODE ); ?>" <?php echo \checked( ACTIVITYPUB_ACTOR_MODE, \get_option( 'activitypub_actor_mode', ACTIVITYPUB_ACTOR_MODE ) ); ?> />
<strong><?php \esc_html_e( 'Author Profiles Only', 'activitypub' ); ?></strong>
</label>
</p>
<p class="description">
Expand All @@ -41,12 +41,21 @@
</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-Profile', 'activitypub' ); ?>
<input type="radio" name="activitypub_actor_mode" id="activitypub_actor_mode" value="<?php echo esc_attr( ACTIVITYPUB_BLOG_MODE ); ?>" <?php echo \checked( ACTIVITYPUB_BLOG_MODE, \get_option( 'activitypub_actor_mode', ACTIVITYPUB_ACTOR_MODE ) ); ?> />
<strong><?php \esc_html_e( 'Blog profile only', 'activitypub' ); ?></strong>
</label>
</p>
<p class="description">
<?php \esc_html_e( 'Your blog becomes an ActivityPub profile.', 'activitypub' ); ?>
<?php \esc_html_e( 'Your blog becomes a single ActivityPub profile and every post will be published under this profile instead of the individual author profiles.', 'activitypub' ); ?>
</p>
<p>
<label>
<input type="radio" name="activitypub_actor_mode" id="activitypub_actor_mode" value="<?php echo esc_attr( ACTIVITYPUB_ACTOR_AND_BLOG_MODE ); ?>" <?php echo \checked( ACTIVITYPUB_ACTOR_AND_BLOG_MODE, \get_option( 'activitypub_actor_mode', ACTIVITYPUB_ACTOR_MODE ) ); ?> />
<strong><?php \esc_html_e( 'Both author and blog profiles', 'activitypub' ); ?></strong>
</label>
</p>
<p class="description">
<?php \esc_html_e( "This combines both modes. Users can be followed individually, while following the blog will show boosts of individual user's posts.", 'activitypub' ); ?>
</p>
</td>
</tr>
Expand Down
51 changes: 51 additions & 0 deletions tests/test-class-activitypub-migrate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
class Test_Activitypub_Migrate extends ActivityPub_TestCase_Cache_HTTP {

public function test_migrate_actor_mode() {
\delete_option( 'activitypub_actor_mode' );

\Activitypub\Migration::migrate_actor_mode();

$this->assertEquals( ACTIVITYPUB_ACTOR_MODE, \get_option( 'activitypub_actor_mode', ACTIVITYPUB_ACTOR_MODE ) );

\update_option( 'activitypub_enable_blog_user', '0' );
\update_option( 'activitypub_enable_users', '1' );
\delete_option( 'activitypub_actor_mode' );

\Activitypub\Migration::migrate_actor_mode();

$this->assertEquals( ACTIVITYPUB_ACTOR_MODE, \get_option( 'activitypub_actor_mode', ACTIVITYPUB_ACTOR_MODE ) );

\update_option( 'activitypub_enable_blog_user', '1' );
\update_option( 'activitypub_enable_users', '1' );
\delete_option( 'activitypub_actor_mode' );

\Activitypub\Migration::migrate_actor_mode();

$this->assertEquals( ACTIVITYPUB_ACTOR_AND_BLOG_MODE, \get_option( 'activitypub_actor_mode', ACTIVITYPUB_ACTOR_MODE ) );

\update_option( 'activitypub_enable_blog_user', '1' );
\update_option( 'activitypub_enable_users', '0' );
\delete_option( 'activitypub_actor_mode' );

\Activitypub\Migration::migrate_actor_mode();

$this->assertEquals( ACTIVITYPUB_BLOG_MODE, \get_option( 'activitypub_actor_mode', ACTIVITYPUB_ACTOR_MODE ) );

\delete_option( 'activitypub_enable_blog_user' );
\update_option( 'activitypub_enable_users', '0' );
\delete_option( 'activitypub_actor_mode' );

\Activitypub\Migration::migrate_actor_mode();

$this->assertEquals( ACTIVITYPUB_ACTOR_MODE, \get_option( 'activitypub_actor_mode', ACTIVITYPUB_ACTOR_MODE ) );

\update_option( 'activitypub_enable_blog_user', '0' );
\delete_option( 'activitypub_enable_users' );
\delete_option( 'activitypub_actor_mode' );

\Activitypub\Migration::migrate_actor_mode();

$this->assertEquals( ACTIVITYPUB_ACTOR_MODE, \get_option( 'activitypub_actor_mode', ACTIVITYPUB_ACTOR_MODE ) );
}
}

0 comments on commit 418b8dc

Please sign in to comment.