Skip to content

Commit e8d2508

Browse files
committed
nicer profile and customizable backlink
* customizable backlink URL #18 * fixes #12
1 parent b9e9082 commit e8d2508

File tree

5 files changed

+85
-31
lines changed

5 files changed

+85
-31
lines changed

activitypub.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ function activitypub_init() {
6464
require_once dirname( __FILE__ ) . '/includes/class-activitypub-admin.php';
6565
add_action( 'admin_menu', array( 'Activitypub_Admin', 'admin_menu' ) );
6666
add_action( 'admin_init', array( 'Activitypub_Admin', 'register_settings' ) );
67+
add_action( 'show_user_profile', array( 'Activitypub_Admin', 'add_fediverse_profile' ) );
6768
}
6869
add_action( 'plugins_loaded', 'activitypub_init' );
6970

includes/class-activitypub-admin.php

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,33 @@ public static function settings_page() {
3131
public static function register_settings() {
3232
register_setting(
3333
'activitypub', 'activitypub_post_content_type', array(
34-
'type' => 'string',
35-
'description' => __( 'Use summary or full content', 'activitypub' ),
34+
'type' => 'string',
35+
'description' => __( 'Use summary or full content', 'activitypub' ),
3636
'show_in_rest' => array(
3737
'schema' => array(
38-
'enum' => array( 'excerpt', 'content' )
38+
'enum' => array( 'excerpt', 'content' ),
3939
),
4040
),
41-
'default' => 0,
41+
'default' => 'excerpt',
4242
)
4343
);
4444
register_setting(
4545
'activitypub', 'activitypub_object_type', array(
46-
'type' => 'string',
47-
'description' => __( 'The Activity-Object-Type', 'activitypub' ),
46+
'type' => 'string',
47+
'description' => __( 'The Activity-Object-Type', 'activitypub' ),
4848
'show_in_rest' => array(
4949
'schema' => array(
50-
'enum' => array( 'note', 'article', 'wordpress-post-format' )
50+
'enum' => array( 'note', 'article', 'wordpress-post-format' ),
5151
),
5252
),
53-
'default' => 'note',
53+
'default' => 'note',
54+
)
55+
);
56+
register_setting(
57+
'activitypub', 'activitypub_use_shortlink', array(
58+
'type' => 'boolean',
59+
'description' => __( 'Use the Shortlink instead of the permalink', 'activitypub' ),
60+
'default' => 0,
5461
)
5562
);
5663
}
@@ -74,4 +81,11 @@ public static function add_help_tab() {
7481
'<p>' . __( '<a href="https://notiz.blog/donate">Donate</a>', 'activitypub' ) . '</p>'
7582
);
7683
}
84+
85+
public static function add_fediverse_profile( $user ) {
86+
?>
87+
<h2><?php esc_html_e( 'Fediverse', 'activitypub' ); ?></h2>
88+
<?php
89+
activitypub_get_identifier_settings( $user->ID );
90+
}
7791
}

includes/class-activitypub-post.php

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function to_array() {
2727
'type' => $this->get_object_type(),
2828
'published' => date( 'Y-m-d\TH:i:s\Z', strtotime( $post->post_date ) ),
2929
'attributedTo' => get_author_posts_url( $post->post_author ),
30-
'summary' => null,
30+
'summary' => ( $this->get_object_type() == 'Article' ) ? $this->get_the_post_excerpt( 400, false ) : null,
3131
'inReplyTo' => null,
3232
'content' => $this->get_the_content(),
3333
'contentMap' => array(
@@ -209,7 +209,7 @@ public function get_the_content() {
209209
*
210210
* @return string The excerpt.
211211
*/
212-
public function get_the_post_excerpt( $excerpt_length = 400 ) {
212+
public function get_the_post_excerpt( $excerpt_length = 400, $with_link = true ) {
213213
$post = $this->post;
214214

215215
$excerpt = get_post_field( 'post_excerpt', $post );
@@ -238,25 +238,47 @@ public function get_the_post_excerpt( $excerpt_length = 400 ) {
238238

239239
$filtered_excerpt = apply_filters( 'the_excerpt', $excerpt );
240240

241-
$excerpt = $filtered_excerpt . "\n\n" . '<a rel="shortlink" href="' . esc_url( wp_get_shortlink( $this->post->ID ) ) . '">' . wp_get_shortlink( $this->post->ID ) . '</a>';
241+
if ( $with_link ) {
242+
$link = '';
243+
244+
if ( get_option( 'activitypub_use_shortlink', 0 ) ) {
245+
$link = esc_url( wp_get_shortlink( $this->post->ID ) );
246+
} else {
247+
$link = esc_url( get_permalink( $this->post->ID ) );
248+
}
249+
250+
$filtered_excerpt = $filtered_excerpt . "\n\n" . '<a href="' . $link . '">' . $link . '</a>';
251+
}
242252

243253
$allowed_html = apply_filters( 'activitypub_allowed_html', '<a>' );
244254

245-
return trim( preg_replace( '/[\r\n]{2,}/', "\n\n", strip_tags( $excerpt, $allowed_html ) ) );
255+
return trim( preg_replace( '/[\r\n]{2,}/', "\n\n", strip_tags( $filtered_excerpt, $allowed_html ) ) );
246256
}
247257

248258
/**
249259
* Get the content for a post for use outside of the loop.
250260
*
251261
* @return string The content.
252262
*/
253-
public function get_the_post_content() {
263+
public function get_the_post_content( $with_link = true ) {
254264
$post = $this->post;
255265

256266
$content = get_post_field( 'post_content', $post );
257267

258268
$filtered_content = apply_filters( 'the_content', $content );
259269

270+
if ( $with_link ) {
271+
$link = '';
272+
273+
if ( get_option( 'activitypub_use_shortlink', 0 ) ) {
274+
$link = esc_url( wp_get_shortlink( $this->post->ID ) );
275+
} else {
276+
$link = esc_url( get_permalink( $this->post->ID ) );
277+
}
278+
279+
$filtered_content = $filtered_content . "\n\n" . '<a href="' . $link . '">' . $link . '</a>';
280+
}
281+
260282
$allowed_html = apply_filters( 'activitypub_allowed_html', '<a>' );
261283

262284
return trim( preg_replace( '/[\r\n]{2,}/', "\n\n", strip_tags( $filtered_content, $allowed_html ) ) );

includes/functions.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,21 @@ function activitypub_get_follower_inboxes( $user_id, $followers ) {
183183

184184
return array_unique( $inboxes );
185185
}
186+
187+
function activitypub_get_identifier_settings( $user_id ) {
188+
?>
189+
<table class="form-table">
190+
<tbody>
191+
<tr>
192+
<th scope="row">
193+
<label><?php esc_html_e( 'Profile identifier', 'activitypub' ); ?></label>
194+
</th>
195+
<td>
196+
<p><code><?php echo activitypub_get_webfinger_resource( $user_id ); ?></code> or <code><?php echo get_author_posts_url( $user_id ); ?></code></p>
197+
<p class="description"><?php printf( __( 'Try to follow "@%s" in the mastodon/friendi.ca search field.', 'activitypub' ), activitypub_get_webfinger_resource( $user_id ) ); ?></p>
198+
</td>
199+
</tr>
200+
</tbody>
201+
</table>
202+
<?php
203+
}

templates/settings-page.php

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,21 @@
1818
</th>
1919
<td>
2020
<p>
21-
<label><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', 'excerpt' ) ); ?> /> <?php esc_html_e( 'Excerpt (default)', 'activitypub' ); ?> - <span class="description"><?php esc_html_e( 'A content summary, shortened to 400 characters and without markup.', 'activitypub' ); ?></span>
21+
<label><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', 'excerpt' ) ); ?> /> <?php esc_html_e( 'Excerpt (default)', 'activitypub' ); ?></label> - <span class="description"><?php esc_html_e( 'A content summary, shortened to 400 characters and without markup.', 'activitypub' ); ?></span>
2222
</p>
2323
<p>
24-
<label><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', 'excerpt' ) ); ?> /> <?php esc_html_e( 'Content', 'activitypub' ); ?> - <span class="description"><?php esc_html_e( 'The full content.', 'activitypub' ); ?></span>
24+
<label><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', 'excerpt' ) ); ?> /> <?php esc_html_e( 'Content', 'activitypub' ); ?></label> - <span class="description"><?php esc_html_e( 'The full content.', 'activitypub' ); ?></span>
25+
</p>
26+
</td>
27+
</tr>
28+
<tr>
29+
<th scope="row">
30+
<?php esc_html_e( 'Backlink', 'activitypub' ); ?>
31+
</th>
32+
<td>
33+
<p>
34+
<label><input type="checkbox" name="activitypub_use_shortlink" id="activitypub_use_shortlink" value="1" <?php echo checked( '1', get_option( 'activitypub_use_shortlink', '0' ) ); ?> /> <?php esc_html_e( 'Use the Shortlink instead of the permalink', 'activitypub' ); ?></label>
35+
<p class="description"><?php printf( esc_html( 'I can recommend %sHum%s, to prettify the Shortlinks', 'activitypub' ), '<a href="https://wordpress.org/plugins/hum/" target="_blank">', '</a>' ); ?></p>
2536
</p>
2637
</td>
2738
</tr>
@@ -31,13 +42,13 @@
3142
</th>
3243
<td>
3344
<p>
34-
<label><input type="radio" name="activitypub_object_type" id="activitypub_object_type_note" value="note" <?php echo checked( 'note', get_option( 'activitypub_object_type', 'note' ) ); ?> /> <?php esc_html_e( 'Note (default)', 'activitypub' ); ?> - <span class="description"><?php esc_html_e( 'Should work with most plattforms.', 'activitypub' ); ?></span>
45+
<label><input type="radio" name="activitypub_object_type" id="activitypub_object_type_note" value="note" <?php echo checked( 'note', get_option( 'activitypub_object_type', 'note' ) ); ?> /> <?php esc_html_e( 'Note (default)', 'activitypub' ); ?></label> - <span class="description"><?php esc_html_e( 'Should work with most plattforms.', 'activitypub' ); ?></span>
3546
</p>
3647
<p>
37-
<label><input type="radio" name="activitypub_object_type" id="activitypub_object_type_article" value="article" <?php echo checked( 'article', get_option( 'activitypub_object_type', 'note' ) ); ?> /> <?php esc_html_e( 'Article', 'activitypub' ); ?> - <span class="description"><?php esc_html_e( 'The presentation of the "Article" might change on different plattforms. Mastodon for example shows the "Article" type as a simple link.', 'activitypub' ); ?></span>
48+
<label><input type="radio" name="activitypub_object_type" id="activitypub_object_type_article" value="article" <?php echo checked( 'article', get_option( 'activitypub_object_type', 'note' ) ); ?> /> <?php esc_html_e( 'Article', 'activitypub' ); ?></label> - <span class="description"><?php esc_html_e( 'The presentation of the "Article" might change on different plattforms. Mastodon for example shows the "Article" type as a simple link.', 'activitypub' ); ?></span>
3849
</p>
3950
<p>
40-
<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', 'note' ) ); ?> /> <?php esc_html_e( 'WordPress Post-Format', 'activitypub' ); ?> - <span class="description"><?php esc_html_e( 'Maps the WordPress Post-Format to the ActivityPub Object Type.', 'activitypub' ); ?></span>
51+
<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', 'note' ) ); ?> /> <?php esc_html_e( 'WordPress Post-Format', 'activitypub' ); ?></label> - <span class="description"><?php esc_html_e( 'Maps the WordPress Post-Format to the ActivityPub Object Type.', 'activitypub' ); ?></span>
4152
</p>
4253
</td>
4354
</tr>
@@ -50,19 +61,7 @@
5061

5162
<p><?php esc_html_e( 'All profile related settings.', 'activitypub' ); ?></p>
5263

53-
<table class="form-table">
54-
<tbody>
55-
<tr>
56-
<th scope="row">
57-
<label><?php esc_html_e( 'Profile identifier', 'activitypub' ); ?></label>
58-
</th>
59-
<td>
60-
<p><code><?php echo activitypub_get_webfinger_resource( get_current_user_id() ); ?></code> or <code><?php echo get_author_posts_url( get_current_user_id() ); ?></code></p>
61-
<p class="description"><?php printf( __( 'Try to follow "@%s" in the mastodon/friendi.ca search field.', 'activitypub' ), activitypub_get_webfinger_resource( get_current_user_id() ) ); ?></p>
62-
</td>
63-
</tr>
64-
</tbody>
65-
</table>
64+
<?php activitypub_get_identifier_settings( get_current_user_id() ); ?>
6665

6766
<?php do_settings_fields( 'activitypub', 'profile' ); ?>
6867

0 commit comments

Comments
 (0)