André Menrath
1e6e7a505e
Some checks failed
Unit Testing / phpunit (5.6, 6.2) (push) Waiting to run
Unit Testing / phpunit (7.0) (push) Waiting to run
Unit Testing / phpunit (7.2) (push) Waiting to run
Unit Testing / phpunit (7.3) (push) Waiting to run
Unit Testing / phpunit (7.4) (push) Waiting to run
Unit Testing / phpunit (8.0) (push) Waiting to run
Unit Testing / phpunit (8.1) (push) Waiting to run
Unit Testing / phpunit (8.2) (push) Waiting to run
Unit Testing / phpunit (latest) (push) Waiting to run
PHP_CodeSniffer / phpcs (push) Has been cancelled
179 lines
4.5 KiB
PHP
179 lines
4.5 KiB
PHP
<?php
|
|
namespace Activitypub\Transformer;
|
|
|
|
use WP_Post;
|
|
use Activitypub\Collection\Users;
|
|
use Activitypub\Model\Blog_User;
|
|
use Activitypub\Activity\Base_Object;
|
|
use Activitypub\Shortcodes;
|
|
use Activitypub\Transformer\Base;
|
|
|
|
use function Activitypub\esc_hashtag;
|
|
use function Activitypub\is_single_user;
|
|
use function Activitypub\get_rest_url_by_path;
|
|
use function Activitypub\site_supports_blocks;
|
|
|
|
/**
|
|
* WordPress Post Transformer
|
|
* The Post Transformer is responsible for transforming a WP_Post object into different othe
|
|
* Object-Types.
|
|
*
|
|
* Currently supported are:
|
|
* - Activitypub\Activity\Base_Object
|
|
*/
|
|
class Post extends Base {
|
|
/**
|
|
* Getter function for the name of the transformer.
|
|
*
|
|
* @return string name
|
|
*/
|
|
public function get_name() {
|
|
return 'activitypub/default';
|
|
}
|
|
|
|
/**
|
|
* Getter function for the display name (label/title) of the transformer.
|
|
*
|
|
* @return string name
|
|
*/
|
|
public function get_label() {
|
|
return 'Built-In';
|
|
}
|
|
|
|
/**
|
|
* Returns the ActivityStreams 2.0 Object-Type for a Post based on the
|
|
* settings and the Post-Type.
|
|
*
|
|
* @see https://www.w3.org/TR/activitystreams-vocabulary/#activity-types
|
|
*
|
|
* @return string The Object-Type.
|
|
*/
|
|
protected function get_object_type() {
|
|
if ( 'wordpress-post-format' !== \get_option( 'activitypub_object_type', 'note' ) ) {
|
|
return \ucfirst( \get_option( 'activitypub_object_type', 'note' ) );
|
|
}
|
|
|
|
// Default to Article.
|
|
$object_type = 'Article';
|
|
$post_type = \get_post_type( $this->wp_post );
|
|
switch ( $post_type ) {
|
|
case 'post':
|
|
$post_format = \get_post_format( $this->wp_post );
|
|
switch ( $post_format ) {
|
|
case 'aside':
|
|
case 'status':
|
|
case 'quote':
|
|
case 'note':
|
|
$object_type = 'Note';
|
|
break;
|
|
case 'gallery':
|
|
case 'image':
|
|
$object_type = 'Image';
|
|
break;
|
|
case 'video':
|
|
$object_type = 'Video';
|
|
break;
|
|
case 'audio':
|
|
$object_type = 'Audio';
|
|
break;
|
|
default:
|
|
$object_type = 'Article';
|
|
break;
|
|
}
|
|
break;
|
|
case 'page':
|
|
$object_type = 'Page';
|
|
break;
|
|
case 'attachment':
|
|
$mime_type = \get_post_mime_type();
|
|
$media_type = \preg_replace( '/(\/[a-zA-Z]+)/i', '', $mime_type );
|
|
switch ( $media_type ) {
|
|
case 'audio':
|
|
$object_type = 'Audio';
|
|
break;
|
|
case 'video':
|
|
$object_type = 'Video';
|
|
break;
|
|
case 'image':
|
|
$object_type = 'Image';
|
|
break;
|
|
}
|
|
break;
|
|
default:
|
|
$object_type = 'Article';
|
|
break;
|
|
}
|
|
|
|
return $object_type;
|
|
}
|
|
|
|
/**
|
|
* Gets the template to use to generate the content of the activitypub item.
|
|
*
|
|
* @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 = "[ap_title]\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 = \get_option( 'activitypub_custom_post_content', ACTIVITYPUB_CUSTOM_POST_CONTENT );
|
|
break;
|
|
}
|
|
|
|
return apply_filters( 'activitypub_object_content_template', $template, $this->wp_post );
|
|
}
|
|
|
|
/**
|
|
* Transforms the WP_Post object to an ActivityPub Object
|
|
*
|
|
* @see \Activitypub\Activity\Base_Object
|
|
* @param WP_Post $wp_post The WordPress Post
|
|
*
|
|
* @return \Activitypub\Activity\Base_Object The ActivityPub Object
|
|
*/
|
|
public function transform() {
|
|
$object = new Base_Object();
|
|
$wp_post = $this->get_wp_post();
|
|
|
|
$object->set_id( $this->get_id() );
|
|
$object->set_url( $this->get_url() );
|
|
$object->set_type( $this->get_object_type() );
|
|
|
|
$published = \strtotime( $wp_post->post_date_gmt );
|
|
|
|
$object->set_published( \gmdate( 'Y-m-d\TH:i:s\Z', $published ) );
|
|
|
|
$updated = \strtotime( $wp_post->post_modified_gmt );
|
|
|
|
if ( $updated > $published ) {
|
|
$object->set_updated( \gmdate( 'Y-m-d\TH:i:s\Z', $updated ) );
|
|
}
|
|
|
|
$object->set_attributed_to( $this->get_attributed_to() );
|
|
$object->set_content( $this->get_content() );
|
|
$object->set_content_map( $this->get_content_map );
|
|
$path = sprintf( 'users/%d/followers', intval( $wp_post->post_author ) );
|
|
|
|
$object->set_to(
|
|
array(
|
|
'https://www.w3.org/ns/activitystreams#Public',
|
|
get_rest_url_by_path( $path ),
|
|
)
|
|
);
|
|
$object->set_cc( $this->get_cc() );
|
|
$object->set_attachment( $this->get_attachments() );
|
|
$object->set_tag( $this->get_tags() );
|
|
|
|
return $object;
|
|
}
|
|
}
|