Skip to content

Commit b9e9082

Browse files
committed
content fixes
* added option to switch between content and excerpt * removed html and duplicateded new-lines fixes #6
1 parent be3fc9b commit b9e9082

File tree

8 files changed

+145
-86
lines changed

8 files changed

+145
-86
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
**Tags:** OStatus, fediverse, activitypub, activitystream
55
**Requires at least:** 4.7
66
**Tested up to:** 5.0.2
7-
**Stable tag:** 0.1.1
7+
**Stable tag:** 0.2.0
88
**Requires PHP:** 5.6
99
**License:** MIT
1010
**License URI:** http://opensource.org/licenses/MIT
@@ -59,6 +59,11 @@ To implement:
5959

6060
Project maintained on github at [pfefferle/wordpress-activitypub](https://github.com/pfefferle/wordpress-activitypub).
6161

62+
### 0.2.0 ###
63+
64+
* added option to switch between content and excerpt
65+
* removed html and duplicateded new-lines
66+
6267
### 0.1.1 ###
6368

6469
* fixed "excerpt" in AS JSON

activitypub.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Plugin Name: ActivityPub
44
* Plugin URI: https://github.com/pfefferle/wordpress-activitypub/
55
* Description: The ActivityPub protocol is a decentralized social networking protocol based upon the ActivityStreams 2.0 data format.
6-
* Version: 0.1.1
6+
* Version: 0.2.0
77
* Author: Matthias Pfefferle
88
* Author URI: https://notiz.blog/
99
* License: MIT

includes/class-activitypub-admin.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,14 @@ public static function settings_page() {
3030
*/
3131
public static function register_settings() {
3232
register_setting(
33-
'activitypub', 'activitypub_add_summary', array(
34-
'type' => 'boolean',
35-
'description' => __( 'Adds a "summary" to the Activity-Objects', 'activitypub' ),
36-
'show_in_rest' => true,
33+
'activitypub', 'activitypub_post_content_type', array(
34+
'type' => 'string',
35+
'description' => __( 'Use summary or full content', 'activitypub' ),
36+
'show_in_rest' => array(
37+
'schema' => array(
38+
'enum' => array( 'excerpt', 'content' )
39+
),
40+
),
3741
'default' => 0,
3842
)
3943
);

includes/class-activitypub-post.php

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ 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' => get_option( 'activitypub_add_summary', false ) ? apply_filters( 'the_excerpt', activitypub_get_the_excerpt( $post->ID, 400 ) ) : null,
30+
'summary' => null,
3131
'inReplyTo' => null,
32-
'content' => apply_filters( 'the_content', get_post_field( 'post_content', $post->ID ) ),
32+
'content' => $this->get_the_content(),
3333
'contentMap' => array(
34-
strstr( get_locale(), '_', true ) => apply_filters( 'the_content', get_post_field( 'post_content', $post->ID ) ),
34+
strstr( get_locale(), '_', true ) => $this->get_the_content(),
3535
),
3636
'to' => array( 'https://www.w3.org/ns/activitystreams#Public' ),
3737
'cc' => array( 'https://www.w3.org/ns/activitystreams#Public' ),
@@ -138,7 +138,7 @@ public function get_tags() {
138138
* @return string the object-type
139139
*/
140140
public function get_object_type() {
141-
if ( get_option( 'activitypub_object_type', 'note' ) !== "wordpress-post-format" ) {
141+
if ( 'wordpress-post-format' !== get_option( 'activitypub_object_type', 'note' ) ) {
142142
return ucfirst( get_option( 'activitypub_object_type', 'note' ) );
143143
}
144144

@@ -193,4 +193,72 @@ public function get_object_type() {
193193

194194
return $object_type;
195195
}
196+
197+
public function get_the_content() {
198+
if ( 'excerpt' === get_option( 'activitypub_post_content_type', 'excerpt' ) ) {
199+
return $this->get_the_post_excerpt();
200+
}
201+
202+
return $this->get_the_post_content();
203+
}
204+
205+
/**
206+
* Get the excerpt for a post for use outside of the loop.
207+
*
208+
* @param int Optional excerpt length.
209+
*
210+
* @return string The excerpt.
211+
*/
212+
public function get_the_post_excerpt( $excerpt_length = 400 ) {
213+
$post = $this->post;
214+
215+
$excerpt = get_post_field( 'post_excerpt', $post );
216+
217+
if ( '' === $excerpt ) {
218+
219+
$content = get_post_field( 'post_content', $post );
220+
221+
// An empty string will make wp_trim_excerpt do stuff we do not want.
222+
if ( '' !== $content ) {
223+
224+
$excerpt = strip_shortcodes( $content );
225+
226+
/** This filter is documented in wp-includes/post-template.php */
227+
$excerpt = apply_filters( 'the_content', $excerpt );
228+
$excerpt = str_replace( ']]>', ']]>', $excerpt );
229+
230+
$excerpt_length = apply_filters( 'excerpt_length', $excerpt_length );
231+
232+
/** This filter is documented in wp-includes/formatting.php */
233+
$excerpt_more = apply_filters( 'excerpt_more', ' [...]' );
234+
235+
$excerpt = wp_trim_words( $excerpt, $excerpt_length, $excerpt_more );
236+
}
237+
}
238+
239+
$filtered_excerpt = apply_filters( 'the_excerpt', $excerpt );
240+
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>';
242+
243+
$allowed_html = apply_filters( 'activitypub_allowed_html', '<a>' );
244+
245+
return trim( preg_replace( '/[\r\n]{2,}/', "\n\n", strip_tags( $excerpt, $allowed_html ) ) );
246+
}
247+
248+
/**
249+
* Get the content for a post for use outside of the loop.
250+
*
251+
* @return string The content.
252+
*/
253+
public function get_the_post_content() {
254+
$post = $this->post;
255+
256+
$content = get_post_field( 'post_content', $post );
257+
258+
$filtered_content = apply_filters( 'the_content', $content );
259+
260+
$allowed_html = apply_filters( 'activitypub_allowed_html', '<a>' );
261+
262+
return trim( preg_replace( '/[\r\n]{2,}/', "\n\n", strip_tags( $filtered_content, $allowed_html ) ) );
263+
}
196264
}

includes/functions.php

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

184184
return array_unique( $inboxes );
185185
}
186-
187-
/**
188-
* Get the excerpt for a post for use outside of the loop.
189-
*
190-
* @param int|WP_Post $post ID or WP_Post object.
191-
* @param int Optional excerpt length.
192-
*
193-
* @return string The excerpt.
194-
*/
195-
function activitypub_get_the_excerpt( $post, $excerpt_length = 55 ) {
196-
197-
$excerpt = get_post_field( 'post_excerpt', $post );
198-
199-
if ( '' === $excerpt ) {
200-
201-
$content = get_post_field( 'post_content', $post );
202-
203-
// An empty string will make wp_trim_excerpt do stuff we do not want.
204-
if ( '' !== $content ) {
205-
206-
$excerpt = strip_shortcodes( $content );
207-
208-
/** This filter is documented in wp-includes/post-template.php */
209-
$excerpt = apply_filters( 'the_content', $excerpt );
210-
$excerpt = str_replace( ']]>', ']]>', $excerpt );
211-
212-
$excerpt_length = apply_filters( 'excerpt_length', $excerpt_length );
213-
214-
/** This filter is documented in wp-includes/formatting.php */
215-
$excerpt_more = apply_filters( 'excerpt_more', ' […]' );
216-
217-
$excerpt = wp_trim_words( $excerpt, $excerpt_length, $excerpt_more );
218-
}
219-
}
220-
221-
return apply_filters( 'the_excerpt', $excerpt );
222-
}

languages/activitypub.pot

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
1-
# Copyright (C) 2018 Matthias Pfefferle
1+
# Copyright (C) 2019 Matthias Pfefferle
22
# This file is distributed under the MIT.
33
msgid ""
44
msgstr ""
5-
"Project-Id-Version: ActivityPub 0.1.1\n"
5+
"Project-Id-Version: ActivityPub 0.2.0\n"
66
"Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/activitypub\n"
7-
"POT-Creation-Date: 2018-12-30 10:41:44+00:00\n"
7+
"POT-Creation-Date: 2019-01-04 18:55:11+00:00\n"
88
"MIME-Version: 1.0\n"
99
"Content-Type: text/plain; charset=utf-8\n"
1010
"Content-Transfer-Encoding: 8bit\n"
11-
"PO-Revision-Date: 2018-MO-DA HO:MI+ZONE\n"
11+
"PO-Revision-Date: 2019-MO-DA HO:MI+ZONE\n"
1212
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
1313
"Language-Team: LANGUAGE <[email protected]>\n"
1414
"X-Generator: grunt-wp-i18n1.0.2\n"
1515

1616
#: includes/class-activitypub-admin.php:35
17-
msgid "Adds a \"summary\" to the Activity-Objects"
17+
msgid "Use summary or full content"
1818
msgstr ""
1919

20-
#: includes/class-activitypub-admin.php:43
20+
#: includes/class-activitypub-admin.php:47
2121
msgid "The Activity-Object-Type"
2222
msgstr ""
2323

24-
#: includes/class-activitypub-admin.php:58
24+
#: includes/class-activitypub-admin.php:62
2525
msgid "Overview"
2626
msgstr ""
2727

28-
#: includes/class-activitypub-admin.php:60
28+
#: includes/class-activitypub-admin.php:64
2929
msgid ""
3030
"ActivityPub is a decentralized social networking protocol based on the "
3131
"ActivityStreams 2.0 data format. ActivityPub is an official W3C recommended "
@@ -35,25 +35,25 @@ msgid ""
3535
"subscribing to content."
3636
msgstr ""
3737

38-
#: includes/class-activitypub-admin.php:65
38+
#: includes/class-activitypub-admin.php:69
3939
msgid "For more information:"
4040
msgstr ""
4141

42-
#: includes/class-activitypub-admin.php:66
42+
#: includes/class-activitypub-admin.php:70
4343
msgid "<a href=\"https://activitypub.rocks/\">Test Suite</a>"
4444
msgstr ""
4545

46-
#: includes/class-activitypub-admin.php:67
46+
#: includes/class-activitypub-admin.php:71
4747
msgid "<a href=\"https://www.w3.org/TR/activitypub/\">W3C Spec</a>"
4848
msgstr ""
4949

50-
#: includes/class-activitypub-admin.php:68
50+
#: includes/class-activitypub-admin.php:72
5151
msgid ""
5252
"<a href=\"https://github.com/pfefferle/wordpress-activitypub/issues\">Give "
5353
"us feedback</a>"
5454
msgstr ""
5555

56-
#: includes/class-activitypub-admin.php:70
56+
#: includes/class-activitypub-admin.php:74
5757
msgid "<a href=\"https://notiz.blog/donate\">Donate</a>"
5858
msgstr ""
5959

@@ -108,7 +108,7 @@ msgstr ""
108108
msgid "Blog"
109109
msgstr ""
110110

111-
#: templates/json-author.php:58 templates/settings-page.php:45
111+
#: templates/json-author.php:58 templates/settings-page.php:49
112112
msgid "Profile"
113113
msgstr ""
114114

@@ -136,74 +136,84 @@ msgid "All activity related settings."
136136
msgstr ""
137137

138138
#: templates/settings-page.php:17
139-
msgid "Add the Post-Summary"
139+
msgid "Post-Content"
140140
msgstr ""
141141

142142
#: templates/settings-page.php:21
143-
msgid ""
144-
"Adds the Post-Summary to the activity. Be aware, that Mastodon seems to use "
145-
"the \"summary\" as the \"content warning\" label."
143+
msgid "Excerpt (default)"
146144
msgstr ""
147145

148-
#: templates/settings-page.php:26
149-
msgid "Activtity-Object-Type"
146+
#: templates/settings-page.php:21
147+
msgid "A content summary, shortened to 400 characters and without markup."
148+
msgstr ""
149+
150+
#: templates/settings-page.php:24
151+
msgid "Content"
152+
msgstr ""
153+
154+
#: templates/settings-page.php:24
155+
msgid "The full content."
150156
msgstr ""
151157

152158
#: templates/settings-page.php:30
159+
msgid "Activtity-Object-Type"
160+
msgstr ""
161+
162+
#: templates/settings-page.php:34
153163
msgid "Note (default)"
154164
msgstr ""
155165

156-
#: templates/settings-page.php:30
166+
#: templates/settings-page.php:34
157167
msgid "Should work with most plattforms."
158168
msgstr ""
159169

160-
#: templates/settings-page.php:33
170+
#: templates/settings-page.php:37
161171
msgid "Article"
162172
msgstr ""
163173

164-
#: templates/settings-page.php:33
174+
#: templates/settings-page.php:37
165175
msgid ""
166176
"The presentation of the \"Article\" might change on different plattforms. "
167177
"Mastodon for example shows the \"Article\" type as a simple link."
168178
msgstr ""
169179

170-
#: templates/settings-page.php:36
180+
#: templates/settings-page.php:40
171181
msgid "WordPress Post-Format"
172182
msgstr ""
173183

174-
#: templates/settings-page.php:36
184+
#: templates/settings-page.php:40
175185
msgid "Maps the WordPress Post-Format to the ActivityPub Object Type."
176186
msgstr ""
177187

178-
#: templates/settings-page.php:47
188+
#: templates/settings-page.php:51
179189
msgid "All profile related settings."
180190
msgstr ""
181191

182-
#: templates/settings-page.php:53
192+
#: templates/settings-page.php:57
183193
msgid "Profile identifier"
184194
msgstr ""
185195

186-
#: templates/settings-page.php:57
196+
#: templates/settings-page.php:61
187197
msgid "Try to follow \"@%s\" in the mastodon/friendi.ca search field."
188198
msgstr ""
189199

190-
#: templates/settings-page.php:65
200+
#: templates/settings-page.php:69
191201
msgid "Followers"
192202
msgstr ""
193203

194-
#: templates/settings-page.php:67
204+
#: templates/settings-page.php:71
195205
msgid "All follower related settings."
196206
msgstr ""
197207

198-
#: templates/settings-page.php:73
208+
#: templates/settings-page.php:77
199209
msgid "List of followers"
200210
msgstr ""
201211

202-
#: templates/settings-page.php:83
212+
#: templates/settings-page.php:87
203213
msgid "No followers yet"
204214
msgstr ""
205215

206-
#: templates/settings-page.php:98
216+
#: templates/settings-page.php:102
207217
msgid ""
208218
"If you like this plugin, what about a small <a "
209219
"href=\"https://notiz.blog/donate\">donation</a>?"

readme.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Donate link: https://notiz.blog/donate/
44
Tags: OStatus, fediverse, activitypub, activitystream
55
Requires at least: 4.7
66
Tested up to: 5.0.2
7-
Stable tag: 0.1.1
7+
Stable tag: 0.2.0
88
Requires PHP: 5.6
99
License: MIT
1010
License URI: http://opensource.org/licenses/MIT
@@ -59,6 +59,11 @@ To implement:
5959

6060
Project maintained on github at [pfefferle/wordpress-activitypub](https://github.com/pfefferle/wordpress-activitypub).
6161

62+
= 0.2.0 =
63+
64+
* added option to switch between content and excerpt
65+
* removed html and duplicateded new-lines
66+
6267
= 0.1.1 =
6368

6469
* fixed "excerpt" in AS JSON

0 commit comments

Comments
 (0)