From b9e90829afe11dcd22b4eed41bae329ac38b0722 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Fri, 4 Jan 2019 19:57:33 +0100 Subject: [PATCH] content fixes * added option to switch between content and excerpt * removed html and duplicateded new-lines fixes #6 --- README.md | 7 ++- activitypub.php | 2 +- includes/class-activitypub-admin.php | 12 +++-- includes/class-activitypub-post.php | 76 ++++++++++++++++++++++++++-- includes/functions.php | 37 -------------- languages/activitypub.pot | 76 ++++++++++++++++------------ readme.txt | 7 ++- templates/settings-page.php | 14 +++-- 8 files changed, 145 insertions(+), 86 deletions(-) diff --git a/README.md b/README.md index 04bb13d..bc45003 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ **Tags:** OStatus, fediverse, activitypub, activitystream **Requires at least:** 4.7 **Tested up to:** 5.0.2 -**Stable tag:** 0.1.1 +**Stable tag:** 0.2.0 **Requires PHP:** 5.6 **License:** MIT **License URI:** http://opensource.org/licenses/MIT @@ -59,6 +59,11 @@ To implement: Project maintained on github at [pfefferle/wordpress-activitypub](https://github.com/pfefferle/wordpress-activitypub). +### 0.2.0 ### + +* added option to switch between content and excerpt +* removed html and duplicateded new-lines + ### 0.1.1 ### * fixed "excerpt" in AS JSON diff --git a/activitypub.php b/activitypub.php index a067e43..5ecf1fe 100644 --- a/activitypub.php +++ b/activitypub.php @@ -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: 0.1.1 + * Version: 0.2.0 * Author: Matthias Pfefferle * Author URI: https://notiz.blog/ * License: MIT diff --git a/includes/class-activitypub-admin.php b/includes/class-activitypub-admin.php index 179ca12..2702ffc 100644 --- a/includes/class-activitypub-admin.php +++ b/includes/class-activitypub-admin.php @@ -30,10 +30,14 @@ class Activitypub_Admin { */ public static function register_settings() { register_setting( - 'activitypub', 'activitypub_add_summary', array( - 'type' => 'boolean', - 'description' => __( 'Adds a "summary" to the Activity-Objects', 'activitypub' ), - 'show_in_rest' => true, + 'activitypub', 'activitypub_post_content_type', array( + 'type' => 'string', + 'description' => __( 'Use summary or full content', 'activitypub' ), + 'show_in_rest' => array( + 'schema' => array( + 'enum' => array( 'excerpt', 'content' ) + ), + ), 'default' => 0, ) ); diff --git a/includes/class-activitypub-post.php b/includes/class-activitypub-post.php index 1d7bbad..e4e6dc7 100644 --- a/includes/class-activitypub-post.php +++ b/includes/class-activitypub-post.php @@ -27,11 +27,11 @@ class Activitypub_Post { 'type' => $this->get_object_type(), 'published' => date( 'Y-m-d\TH:i:s\Z', strtotime( $post->post_date ) ), 'attributedTo' => get_author_posts_url( $post->post_author ), - 'summary' => get_option( 'activitypub_add_summary', false ) ? apply_filters( 'the_excerpt', activitypub_get_the_excerpt( $post->ID, 400 ) ) : null, + 'summary' => null, 'inReplyTo' => null, - 'content' => apply_filters( 'the_content', get_post_field( 'post_content', $post->ID ) ), + 'content' => $this->get_the_content(), 'contentMap' => array( - strstr( get_locale(), '_', true ) => apply_filters( 'the_content', get_post_field( 'post_content', $post->ID ) ), + strstr( get_locale(), '_', true ) => $this->get_the_content(), ), 'to' => array( 'https://www.w3.org/ns/activitystreams#Public' ), 'cc' => array( 'https://www.w3.org/ns/activitystreams#Public' ), @@ -138,7 +138,7 @@ class Activitypub_Post { * @return string the object-type */ public function get_object_type() { - if ( get_option( 'activitypub_object_type', 'note' ) !== "wordpress-post-format" ) { + if ( 'wordpress-post-format' !== get_option( 'activitypub_object_type', 'note' ) ) { return ucfirst( get_option( 'activitypub_object_type', 'note' ) ); } @@ -193,4 +193,72 @@ class Activitypub_Post { return $object_type; } + + public function get_the_content() { + if ( 'excerpt' === get_option( 'activitypub_post_content_type', 'excerpt' ) ) { + return $this->get_the_post_excerpt(); + } + + return $this->get_the_post_content(); + } + + /** + * Get the excerpt for a post for use outside of the loop. + * + * @param int Optional excerpt length. + * + * @return string The excerpt. + */ + public function get_the_post_excerpt( $excerpt_length = 400 ) { + $post = $this->post; + + $excerpt = get_post_field( 'post_excerpt', $post ); + + if ( '' === $excerpt ) { + + $content = get_post_field( 'post_content', $post ); + + // An empty string will make wp_trim_excerpt do stuff we do not want. + if ( '' !== $content ) { + + $excerpt = strip_shortcodes( $content ); + + /** This filter is documented in wp-includes/post-template.php */ + $excerpt = apply_filters( 'the_content', $excerpt ); + $excerpt = str_replace( ']]>', ']]>', $excerpt ); + + $excerpt_length = apply_filters( 'excerpt_length', $excerpt_length ); + + /** This filter is documented in wp-includes/formatting.php */ + $excerpt_more = apply_filters( 'excerpt_more', ' [...]' ); + + $excerpt = wp_trim_words( $excerpt, $excerpt_length, $excerpt_more ); + } + } + + $filtered_excerpt = apply_filters( 'the_excerpt', $excerpt ); + + $excerpt = $filtered_excerpt . "\n\n" . '' . wp_get_shortlink( $this->post->ID ) . ''; + + $allowed_html = apply_filters( 'activitypub_allowed_html', '' ); + + return trim( preg_replace( '/[\r\n]{2,}/', "\n\n", strip_tags( $excerpt, $allowed_html ) ) ); + } + + /** + * Get the content for a post for use outside of the loop. + * + * @return string The content. + */ + public function get_the_post_content() { + $post = $this->post; + + $content = get_post_field( 'post_content', $post ); + + $filtered_content = apply_filters( 'the_content', $content ); + + $allowed_html = apply_filters( 'activitypub_allowed_html', '' ); + + return trim( preg_replace( '/[\r\n]{2,}/', "\n\n", strip_tags( $filtered_content, $allowed_html ) ) ); + } } diff --git a/includes/functions.php b/includes/functions.php index 20e4964..cf95860 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -183,40 +183,3 @@ function activitypub_get_follower_inboxes( $user_id, $followers ) { return array_unique( $inboxes ); } - -/** - * Get the excerpt for a post for use outside of the loop. - * - * @param int|WP_Post $post ID or WP_Post object. - * @param int Optional excerpt length. - * - * @return string The excerpt. - */ -function activitypub_get_the_excerpt( $post, $excerpt_length = 55 ) { - - $excerpt = get_post_field( 'post_excerpt', $post ); - - if ( '' === $excerpt ) { - - $content = get_post_field( 'post_content', $post ); - - // An empty string will make wp_trim_excerpt do stuff we do not want. - if ( '' !== $content ) { - - $excerpt = strip_shortcodes( $content ); - - /** This filter is documented in wp-includes/post-template.php */ - $excerpt = apply_filters( 'the_content', $excerpt ); - $excerpt = str_replace( ']]>', ']]>', $excerpt ); - - $excerpt_length = apply_filters( 'excerpt_length', $excerpt_length ); - - /** This filter is documented in wp-includes/formatting.php */ - $excerpt_more = apply_filters( 'excerpt_more', ' […]' ); - - $excerpt = wp_trim_words( $excerpt, $excerpt_length, $excerpt_more ); - } - } - - return apply_filters( 'the_excerpt', $excerpt ); -} diff --git a/languages/activitypub.pot b/languages/activitypub.pot index a4603ca..e4da6c5 100644 --- a/languages/activitypub.pot +++ b/languages/activitypub.pot @@ -1,31 +1,31 @@ -# Copyright (C) 2018 Matthias Pfefferle +# Copyright (C) 2019 Matthias Pfefferle # This file is distributed under the MIT. msgid "" msgstr "" -"Project-Id-Version: ActivityPub 0.1.1\n" +"Project-Id-Version: ActivityPub 0.2.0\n" "Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/activitypub\n" -"POT-Creation-Date: 2018-12-30 10:41:44+00:00\n" +"POT-Creation-Date: 2019-01-04 18:55:11+00:00\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=utf-8\n" "Content-Transfer-Encoding: 8bit\n" -"PO-Revision-Date: 2018-MO-DA HO:MI+ZONE\n" +"PO-Revision-Date: 2019-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" "X-Generator: grunt-wp-i18n1.0.2\n" #: includes/class-activitypub-admin.php:35 -msgid "Adds a \"summary\" to the Activity-Objects" +msgid "Use summary or full content" msgstr "" -#: includes/class-activitypub-admin.php:43 +#: includes/class-activitypub-admin.php:47 msgid "The Activity-Object-Type" msgstr "" -#: includes/class-activitypub-admin.php:58 +#: includes/class-activitypub-admin.php:62 msgid "Overview" msgstr "" -#: includes/class-activitypub-admin.php:60 +#: includes/class-activitypub-admin.php:64 msgid "" "ActivityPub is a decentralized social networking protocol based on the " "ActivityStreams 2.0 data format. ActivityPub is an official W3C recommended " @@ -35,25 +35,25 @@ msgid "" "subscribing to content." msgstr "" -#: includes/class-activitypub-admin.php:65 +#: includes/class-activitypub-admin.php:69 msgid "For more information:" msgstr "" -#: includes/class-activitypub-admin.php:66 +#: includes/class-activitypub-admin.php:70 msgid "Test Suite" msgstr "" -#: includes/class-activitypub-admin.php:67 +#: includes/class-activitypub-admin.php:71 msgid "W3C Spec" msgstr "" -#: includes/class-activitypub-admin.php:68 +#: includes/class-activitypub-admin.php:72 msgid "" "Give " "us feedback" msgstr "" -#: includes/class-activitypub-admin.php:70 +#: includes/class-activitypub-admin.php:74 msgid "Donate" msgstr "" @@ -108,7 +108,7 @@ msgstr "" msgid "Blog" msgstr "" -#: templates/json-author.php:58 templates/settings-page.php:45 +#: templates/json-author.php:58 templates/settings-page.php:49 msgid "Profile" msgstr "" @@ -136,74 +136,84 @@ msgid "All activity related settings." msgstr "" #: templates/settings-page.php:17 -msgid "Add the Post-Summary" +msgid "Post-Content" msgstr "" #: templates/settings-page.php:21 -msgid "" -"Adds the Post-Summary to the activity. Be aware, that Mastodon seems to use " -"the \"summary\" as the \"content warning\" label." +msgid "Excerpt (default)" msgstr "" -#: templates/settings-page.php:26 +#: templates/settings-page.php:21 +msgid "A content summary, shortened to 400 characters and without markup." +msgstr "" + +#: templates/settings-page.php:24 +msgid "Content" +msgstr "" + +#: templates/settings-page.php:24 +msgid "The full content." +msgstr "" + +#: templates/settings-page.php:30 msgid "Activtity-Object-Type" msgstr "" -#: templates/settings-page.php:30 +#: templates/settings-page.php:34 msgid "Note (default)" msgstr "" -#: templates/settings-page.php:30 +#: templates/settings-page.php:34 msgid "Should work with most plattforms." msgstr "" -#: templates/settings-page.php:33 +#: templates/settings-page.php:37 msgid "Article" msgstr "" -#: templates/settings-page.php:33 +#: templates/settings-page.php:37 msgid "" "The presentation of the \"Article\" might change on different plattforms. " "Mastodon for example shows the \"Article\" type as a simple link." msgstr "" -#: templates/settings-page.php:36 +#: templates/settings-page.php:40 msgid "WordPress Post-Format" msgstr "" -#: templates/settings-page.php:36 +#: templates/settings-page.php:40 msgid "Maps the WordPress Post-Format to the ActivityPub Object Type." msgstr "" -#: templates/settings-page.php:47 +#: templates/settings-page.php:51 msgid "All profile related settings." msgstr "" -#: templates/settings-page.php:53 +#: templates/settings-page.php:57 msgid "Profile identifier" msgstr "" -#: templates/settings-page.php:57 +#: templates/settings-page.php:61 msgid "Try to follow \"@%s\" in the mastodon/friendi.ca search field." msgstr "" -#: templates/settings-page.php:65 +#: templates/settings-page.php:69 msgid "Followers" msgstr "" -#: templates/settings-page.php:67 +#: templates/settings-page.php:71 msgid "All follower related settings." msgstr "" -#: templates/settings-page.php:73 +#: templates/settings-page.php:77 msgid "List of followers" msgstr "" -#: templates/settings-page.php:83 +#: templates/settings-page.php:87 msgid "No followers yet" msgstr "" -#: templates/settings-page.php:98 +#: templates/settings-page.php:102 msgid "" "If you like this plugin, what about a small donation?" diff --git a/readme.txt b/readme.txt index 6b9e5e0..adfd54c 100644 --- a/readme.txt +++ b/readme.txt @@ -4,7 +4,7 @@ Donate link: https://notiz.blog/donate/ Tags: OStatus, fediverse, activitypub, activitystream Requires at least: 4.7 Tested up to: 5.0.2 -Stable tag: 0.1.1 +Stable tag: 0.2.0 Requires PHP: 5.6 License: MIT License URI: http://opensource.org/licenses/MIT @@ -59,6 +59,11 @@ To implement: Project maintained on github at [pfefferle/wordpress-activitypub](https://github.com/pfefferle/wordpress-activitypub). += 0.2.0 = + +* added option to switch between content and excerpt +* removed html and duplicateded new-lines + = 0.1.1 = * fixed "excerpt" in AS JSON diff --git a/templates/settings-page.php b/templates/settings-page.php index cd20c27..8869210 100644 --- a/templates/settings-page.php +++ b/templates/settings-page.php @@ -14,11 +14,15 @@ - + - /> - +

+

+

+

@@ -27,10 +31,10 @@

-

-