Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add title to content if post is a Note #958

Merged
merged 18 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions activitypub.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: ActivityPub
* Plugin URI: https://github.com/pfefferle/wordpress-activitypub/
* Description: The ActivityPub protocol is a decentralized social networking protocol based upon the ActivityStreams 2.0 data format.
* Version: 4.0.2
* Version: 4.1.0
* Author: Matthias Pfefferle & Automattic
* Author URI: https://automattic.com/
* License: MIT
Expand Down Expand Up @@ -35,7 +35,7 @@
\defined( 'ACTIVITYPUB_HASHTAGS_REGEXP' ) || \define( 'ACTIVITYPUB_HASHTAGS_REGEXP', '(?:(?<=\s)|(?<=<p>)|(?<=<br>)|^)#([A-Za-z0-9_]+)(?:(?=\s|[[:punct:]]|$))' );
\defined( 'ACTIVITYPUB_USERNAME_REGEXP' ) || \define( 'ACTIVITYPUB_USERNAME_REGEXP', '(?:([A-Za-z0-9\._-]+)@((?:[A-Za-z0-9_-]+\.)+[A-Za-z]+))' );
\defined( 'ACTIVITYPUB_URL_REGEXP' ) || \define( 'ACTIVITYPUB_URL_REGEXP', '(https?:|www\.)\S+[\w\/]' );
\defined( 'ACTIVITYPUB_CUSTOM_POST_CONTENT' ) || \define( 'ACTIVITYPUB_CUSTOM_POST_CONTENT', "<h2>[ap_title]</h2>\n\n[ap_content]\n\n[ap_hashtags]\n\n[ap_shortlink]" );
\defined( 'ACTIVITYPUB_CUSTOM_POST_CONTENT' ) || \define( 'ACTIVITYPUB_CUSTOM_POST_CONTENT', "[ap_title type=\"html\"]\n\n[ap_content]\n\n[ap_hashtags]" );
\defined( 'ACTIVITYPUB_AUTHORIZED_FETCH' ) || \define( 'ACTIVITYPUB_AUTHORIZED_FETCH', false );
\defined( 'ACTIVITYPUB_DISABLE_REWRITES' ) || \define( 'ACTIVITYPUB_DISABLE_REWRITES', false );
\defined( 'ACTIVITYPUB_DISABLE_INCOMING_INTERACTIONS' ) || \define( 'ACTIVITYPUB_DISABLE_INCOMING_INTERACTIONS', false );
Expand All @@ -44,7 +44,7 @@
\defined( 'ACTIVITYPUB_DISABLE_OUTGOING_INTERACTIONS' ) || \define( 'ACTIVITYPUB_DISABLE_OUTGOING_INTERACTIONS', false );
\defined( 'ACTIVITYPUB_SHARED_INBOX_FEATURE' ) || \define( 'ACTIVITYPUB_SHARED_INBOX_FEATURE', false );
\defined( 'ACTIVITYPUB_SEND_VARY_HEADER' ) || \define( 'ACTIVITYPUB_SEND_VARY_HEADER', false );
\defined( 'ACTIVITYPUB_DEFAULT_OBJECT_TYPE' ) || \define( 'ACTIVITYPUB_DEFAULT_OBJECT_TYPE', 'note' );
\defined( 'ACTIVITYPUB_DEFAULT_OBJECT_TYPE' ) || \define( 'ACTIVITYPUB_DEFAULT_OBJECT_TYPE', 'wordpress-post-format' );

// Define Actor-Modes for the plugin.
\define( 'ACTIVITYPUB_ACTOR_MODE', 'actor' );
Expand Down
2 changes: 1 addition & 1 deletion includes/class-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public static function register_settings() {
),
),
),
'default' => 'note',
'default' => ACTIVITYPUB_DEFAULT_OBJECT_TYPE,
)
);
\register_setting(
Expand Down
36 changes: 36 additions & 0 deletions includes/class-migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ public static function maybe_migrate() {
if ( \version_compare( $version_from_db, '4.0.0', '<' ) ) {
self::migrate_to_4_0_0();
}
if ( \version_compare( $version_from_db, '4.1.0', '<' ) ) {
self::migrate_to_4_1_0();
}

/**
* Fires when the system has to be migrated.
Expand Down Expand Up @@ -321,6 +324,39 @@ private static function migrate_to_4_0_0() {
self::migrate_actor_mode();
}

/**
* Upate to 4.1.0
*
* * Migrate the `activitypub_post_content_type` to only use `activitypub_custom_post_content`.
*/
public static function migrate_to_4_1_0() {
$content_type = \get_option( 'activitypub_post_content_type' );

switch ( $content_type ) {
case 'excerpt':
$template = "[ap_excerpt]\n\n[ap_permalink type=\"html\"]";
break;
case 'title':
$template = "[ap_title type=\"html\"]\n\n[ap_permalink type=\"html\"]";
break;
case 'content':
$template = "[ap_content]\n\n[ap_permalink type=\"html\"]\n\n[ap_hashtags]";
break;
default:
$template = ACTIVITYPUB_CUSTOM_POST_CONTENT;
break;
}

\update_option( 'activitypub_custom_post_content', $template );

\delete_option( 'activitypub_post_content_type' );

$object_type = \get_option( 'activitypub_object_type', false );
if ( ! $object_type ) {
\update_option( 'activitypub_object_type', 'note' );
}
}

/**
* Set the defaults needed for the plugin to work.
*
Expand Down
28 changes: 24 additions & 4 deletions includes/class-shortcodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,36 @@ public static function hashtags() {
/**
* Generates output for the 'ap_title' Shortcode
*
* @param array $atts The Shortcode attributes.
* @param string $content The ActivityPub post-content.
* @param string $tag The tag/name of the Shortcode.
*
* @return string The post title.
*/
public static function title() {
public static function title( $atts, $content, $tag ) {
$item = self::get_item();

if ( ! $item ) {
return '';
}

return \wp_strip_all_tags( \get_the_title( $item->ID ), true );
$title = \wp_strip_all_tags( \get_the_title( $item->ID ), true );

if ( ! $title ) {
return '';
}

$atts = shortcode_atts(
array( 'type' => 'plain' ),
$atts,
$tag
);

if ( 'html' !== $atts['type'] ) {
return $title;
}

return sprintf( '<h2>%s</h2>', $title );
}

/**
Expand Down Expand Up @@ -191,7 +211,7 @@ public static function permalink( $atts, $content, $tag ) {
$tag
);

if ( 'url' === $atts['type'] ) {
if ( 'html' !== $atts['type'] ) {
return \esc_url( \get_permalink( $item->ID ) );
}

Expand Down Expand Up @@ -225,7 +245,7 @@ public static function shortlink( $atts, $content, $tag ) {
$tag
);

if ( 'url' === $atts['type'] ) {
if ( 'html' !== $atts['type'] ) {
return \esc_url( \wp_get_shortlink( $item->ID ) );
}

Expand Down
27 changes: 9 additions & 18 deletions includes/transformer/class-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -898,28 +898,19 @@ protected function get_content() {
* @return string The Template.
*/
protected function get_post_content_template() {
$type = \get_option( 'activitypub_post_content_type', 'content' );

switch ( $type ) {
case 'excerpt':
$template = "[ap_excerpt]\n\n[ap_permalink type=\"html\"]";
break;
case 'title':
$template = "<h2>[ap_title]</h2>\n\n[ap_permalink type=\"html\"]";
break;
case 'content':
$template = "[ap_content]\n\n[ap_permalink type=\"html\"]\n\n[ap_hashtags]";
break;
default:
$content = \get_option( 'activitypub_custom_post_content', ACTIVITYPUB_CUSTOM_POST_CONTENT );
$template = empty( $content ) ? ACTIVITYPUB_CUSTOM_POST_CONTENT : $content;
break;
}
$content = \get_option( 'activitypub_custom_post_content', ACTIVITYPUB_CUSTOM_POST_CONTENT );
$template = $content ?? ACTIVITYPUB_CUSTOM_POST_CONTENT;

$post_format_setting = \get_option( 'activitypub_object_type', ACTIVITYPUB_DEFAULT_OBJECT_TYPE );

if ( 'wordpress-post-format' === $post_format_setting ) {
$template = '[ap_content]';
$template = '';

if ( 'Note' === $this->get_type() ) {
$template .= "[ap_title type=\"html\"]\n\n";
}

$template .= '[ap_content]';
}

return apply_filters( 'activitypub_object_content_template', $template, $this->wp_object );
Expand Down
57 changes: 8 additions & 49 deletions templates/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,26 +75,25 @@
</th>
<td>
<p>
<label for="activitypub_object_type_note">
<input type="radio" name="activitypub_object_type" id="activitypub_object_type_note" value="note" <?php echo \checked( 'note', \get_option( 'activitypub_object_type', ACTIVITYPUB_DEFAULT_OBJECT_TYPE ) ); ?> />
<?php \esc_html_e( 'Note (default)', 'activitypub' ); ?>
<label>
<input type="radio" name="activitypub_object_type" id="activitypub_object_type" value="wordpress-post-format" <?php echo \checked( 'wordpress-post-format', \get_option( 'activitypub_object_type', ACTIVITYPUB_DEFAULT_OBJECT_TYPE ) ); ?> />
<?php \esc_html_e( 'Automatic (default)', 'activitypub' ); ?>
-
<span class="description">
<?php \esc_html_e( 'Should work with most platforms.', 'activitypub' ); ?>
<?php \esc_html_e( 'Let the plugin choose the best possible format for you.', 'activitypub' ); ?>
</span>
</label>
</p>
<p>
<label>
<input type="radio" name="activitypub_object_type" id="activitypub_object_type" value="wordpress-post-format" <?php echo \checked( 'wordpress-post-format', \get_option( 'activitypub_object_type', ACTIVITYPUB_DEFAULT_OBJECT_TYPE ) ); ?> />
<?php \esc_html_e( 'WordPress Post-Format', 'activitypub' ); ?>
<label for="activitypub_object_type_note">
<input type="radio" name="activitypub_object_type" id="activitypub_object_type_note" value="note" <?php echo \checked( 'note', \get_option( 'activitypub_object_type', ACTIVITYPUB_DEFAULT_OBJECT_TYPE ) ); ?> />
<?php \esc_html_e( 'Note', 'activitypub' ); ?>
-
<span class="description">
<?php \esc_html_e( 'Maps the WordPress Post-Format to the ActivityPub Object Type.', 'activitypub' ); ?>
<?php \esc_html_e( 'Should work with most platforms.', 'activitypub' ); ?>
</span>
</label>
</p>

</td>
</tr>
<tr <?php echo 'wordpress-post-format' === \get_option( 'activitypub_object_type', ACTIVITYPUB_DEFAULT_OBJECT_TYPE ) ? 'style="display: none"' : ''; ?>>
Expand All @@ -103,46 +102,6 @@
</th>
<td>
<p><strong><?php \esc_html_e( 'These settings only apply if you use the "Note" Object-Type setting above.', 'activitypub' ); ?></strong></p>
<p>
<label for="activitypub_post_content_type_title_link">
<input type="radio" name="activitypub_post_content_type" id="activitypub_post_content_type_title_link" value="title" <?php echo \checked( 'title', \get_option( 'activitypub_post_content_type', 'content' ) ); ?> />
<?php \esc_html_e( 'Title and link', 'activitypub' ); ?>
-
<span class="description">
<?php \esc_html_e( 'Only the title and a link.', 'activitypub' ); ?>
</span>
</label>
</p>
<p>
<label for="activitypub_post_content_type_excerpt">
<input type="radio" name="activitypub_post_content_type" id="activitypub_post_content_type_excerpt" value="excerpt" <?php echo \checked( 'excerpt', \get_option( 'activitypub_post_content_type', 'content' ) ); ?> />
<?php \esc_html_e( 'Excerpt', 'activitypub' ); ?>
-
<span class="description">
<?php \esc_html_e( 'A content summary without markup (truncated if no excerpt is provided).', 'activitypub' ); ?>
</span>
</label>
</p>
<p>
<label for="activitypub_post_content_type_content">
<input type="radio" name="activitypub_post_content_type" id="activitypub_post_content_type_content" value="content" <?php echo \checked( 'content', \get_option( 'activitypub_post_content_type', 'content' ) ); ?> />
<?php \esc_html_e( 'Content (default)', 'activitypub' ); ?>
-
<span class="description">
<?php \esc_html_e( 'The full content.', 'activitypub' ); ?>
</span>
</label>
</p>
<p>
<label for="activitypub_post_content_type_custom">
<input type="radio" name="activitypub_post_content_type" id="activitypub_post_content_type_custom" value="custom" <?php echo \checked( 'custom', \get_option( 'activitypub_post_content_type', 'content' ) ); ?> />
<?php \esc_html_e( 'Custom', 'activitypub' ); ?>
-
<span class="description">
<?php \esc_html_e( 'Use the text area below, to customize your activities.', 'activitypub' ); ?>
</span>
</label>
</p>
<p>
<textarea name="activitypub_custom_post_content" id="activitypub_custom_post_content" rows="10" cols="50" class="large-text" placeholder="<?php echo wp_kses( ACTIVITYPUB_CUSTOM_POST_CONTENT, 'post' ); ?>"><?php echo esc_textarea( wp_kses( \get_option( 'activitypub_custom_post_content', ACTIVITYPUB_CUSTOM_POST_CONTENT ), 'post' ) ); ?></textarea>
<details>
Expand Down
37 changes: 37 additions & 0 deletions tests/test-class-activitypub-migrate.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
<?php
class Test_Activitypub_Migrate extends ActivityPub_TestCase_Cache_HTTP {

public function tear_down() {
\delete_option( 'activitypub_object_type' );
\delete_option( 'activitypub_custom_post_content' );
\delete_option( 'activitypub_post_content_type' );
}

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

Expand Down Expand Up @@ -48,4 +54,35 @@ public function test_migrate_actor_mode() {

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

public function test_migrate_to_4_1_0() {
$template = \get_option( 'activitypub_custom_post_content', ACTIVITYPUB_CUSTOM_POST_CONTENT );
$object_type = \get_option( 'activitypub_object_type', ACTIVITYPUB_DEFAULT_OBJECT_TYPE );

$this->assertEquals( ACTIVITYPUB_CUSTOM_POST_CONTENT, $template );
$this->assertEquals( ACTIVITYPUB_DEFAULT_OBJECT_TYPE, $object_type );

\update_option( 'activitypub_post_content_type', 'title' );

\Activitypub\Migration::migrate_to_4_1_0();

$template = \get_option( 'activitypub_custom_post_content' );
$content_type = \get_option( 'activitypub_post_content_type' );
$object_type = \get_option( 'activitypub_object_type' );

$this->assertEquals( "[ap_title type=\"html\"]\n\n[ap_permalink type=\"html\"]", $template );
$this->assertFalse( $content_type );
$this->assertEquals( 'note', $object_type );

\update_option( 'activitypub_post_content_type', 'content' );
\update_option( 'activitypub_custom_post_content', '[ap_content]' );

\Activitypub\Migration::migrate_to_4_1_0();

$template = \get_option( 'activitypub_custom_post_content' );
$content_type = \get_option( 'activitypub_post_content_type' );

$this->assertEquals( "[ap_content]\n\n[ap_permalink type=\"html\"]\n\n[ap_hashtags]", $template );
$this->assertFalse( $content_type );
}
}
Loading