2018-09-30 22:51:22 +02:00
|
|
|
<?php
|
2023-11-22 13:59:39 +01:00
|
|
|
namespace Activitypub\Transformer;
|
2023-07-03 17:59:42 +02:00
|
|
|
|
|
|
|
use WP_Post;
|
2023-07-03 19:25:49 +02:00
|
|
|
use Activitypub\Collection\Users;
|
2023-07-10 15:14:37 +02:00
|
|
|
use Activitypub\Model\Blog_User;
|
2023-07-03 19:25:49 +02:00
|
|
|
use Activitypub\Activity\Base_Object;
|
2023-10-19 21:46:31 +02:00
|
|
|
use Activitypub\Shortcodes;
|
2023-11-22 13:59:39 +01:00
|
|
|
use Activitypub\Transformer\Base;
|
2019-02-24 12:07:41 +01:00
|
|
|
|
2023-08-09 13:07:30 +02:00
|
|
|
use function Activitypub\esc_hashtag;
|
2023-07-10 10:57:06 +02:00
|
|
|
use function Activitypub\is_single_user;
|
2023-05-12 22:31:53 +02:00
|
|
|
use function Activitypub\get_rest_url_by_path;
|
2023-09-22 09:21:49 +02:00
|
|
|
use function Activitypub\site_supports_blocks;
|
2023-05-12 22:31:53 +02:00
|
|
|
|
2018-09-30 22:51:22 +02:00
|
|
|
/**
|
2023-07-03 17:59:42 +02:00
|
|
|
* 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
|
2018-09-30 22:51:22 +02:00
|
|
|
*/
|
2023-11-22 13:59:39 +01:00
|
|
|
class Post extends Base {
|
2023-10-27 22:55:44 +02:00
|
|
|
/**
|
2023-11-20 18:15:54 +01:00
|
|
|
* Getter function for the name of the transformer.
|
2023-10-27 22:55:44 +02:00
|
|
|
*
|
2023-11-20 18:15:54 +01:00
|
|
|
* @return string name
|
2023-09-22 09:21:49 +02:00
|
|
|
*/
|
2023-11-20 18:15:54 +01:00
|
|
|
public function get_name() {
|
|
|
|
return 'activitypub/default';
|
2023-09-22 09:21:49 +02:00
|
|
|
}
|
|
|
|
|
2023-01-27 10:21:51 +01:00
|
|
|
/**
|
2023-11-20 18:15:54 +01:00
|
|
|
* Getter function for the display name (label/title) of the transformer.
|
2023-01-27 10:21:51 +01:00
|
|
|
*
|
2023-11-20 18:15:54 +01:00
|
|
|
* @return string name
|
2023-01-27 10:21:51 +01:00
|
|
|
*/
|
2023-11-20 18:15:54 +01:00
|
|
|
public function get_label() {
|
|
|
|
return 'Built-In';
|
2023-04-25 10:54:21 +02:00
|
|
|
}
|
|
|
|
|
2023-01-27 10:21:51 +01:00
|
|
|
/**
|
2023-07-03 17:59:42 +02:00
|
|
|
* Returns the ActivityStreams 2.0 Object-Type for a Post based on the
|
|
|
|
* settings and the Post-Type.
|
2023-01-27 10:21:51 +01:00
|
|
|
*
|
2023-07-03 17:59:42 +02:00
|
|
|
* @see https://www.w3.org/TR/activitystreams-vocabulary/#activity-types
|
2018-09-30 22:51:22 +02:00
|
|
|
*
|
2023-07-03 17:59:42 +02:00
|
|
|
* @return string The Object-Type.
|
2018-09-30 22:51:22 +02:00
|
|
|
*/
|
2023-07-03 17:59:42 +02:00
|
|
|
protected function get_object_type() {
|
2019-09-27 10:12:59 +02:00
|
|
|
if ( 'wordpress-post-format' !== \get_option( 'activitypub_object_type', 'note' ) ) {
|
|
|
|
return \ucfirst( \get_option( 'activitypub_object_type', 'note' ) );
|
2018-12-28 22:40:57 +01:00
|
|
|
}
|
|
|
|
|
2023-10-25 08:44:04 +02:00
|
|
|
// Default to Article.
|
|
|
|
$object_type = 'Article';
|
2023-11-22 13:59:39 +01:00
|
|
|
$post_type = \get_post_type( $this->wp_post );
|
2018-09-30 22:51:22 +02:00
|
|
|
switch ( $post_type ) {
|
|
|
|
case 'post':
|
2023-07-03 17:59:42 +02:00
|
|
|
$post_format = \get_post_format( $this->wp_post );
|
2018-09-30 22:51:22 +02:00
|
|
|
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':
|
2023-11-22 13:59:39 +01:00
|
|
|
$mime_type = \get_post_mime_type();
|
2019-09-27 10:12:59 +02:00
|
|
|
$media_type = \preg_replace( '/(\/[a-zA-Z]+)/i', '', $mime_type );
|
2018-09-30 22:51:22 +02:00
|
|
|
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;
|
|
|
|
}
|
2023-11-29 23:46:13 +01:00
|
|
|
|
2023-01-16 16:27:27 +01:00
|
|
|
/**
|
|
|
|
* Gets the template to use to generate the content of the activitypub item.
|
|
|
|
*
|
2023-07-03 17:59:42 +02:00
|
|
|
* @return string The Template.
|
2023-01-16 16:27:27 +01:00
|
|
|
*/
|
2023-07-03 17:59:42 +02:00
|
|
|
protected function get_post_content_template() {
|
2023-12-12 13:58:44 +01:00
|
|
|
$type = \get_option( 'activitypub_post_content_type', 'content' );
|
2019-02-02 23:56:05 +01:00
|
|
|
|
2023-12-12 13:58:44 +01:00
|
|
|
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;
|
2023-01-13 21:47:13 +01:00
|
|
|
}
|
|
|
|
|
2023-12-12 13:58:44 +01:00
|
|
|
return apply_filters( 'activitypub_object_content_template', $template, $this->wp_post );
|
2019-02-02 23:56:05 +01:00
|
|
|
}
|
2023-07-03 17:59:42 +02:00
|
|
|
|
2023-11-30 08:46:15 +01:00
|
|
|
/**
|
2023-11-29 23:46:13 +01:00
|
|
|
* 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() {
|
2023-12-08 14:09:52 +01:00
|
|
|
$object = new Base_Object();
|
2023-11-30 08:46:15 +01:00
|
|
|
$wp_post = $this->get_wp_post();
|
2023-11-29 23:46:13 +01:00
|
|
|
|
|
|
|
$object->set_id( $this->get_id() );
|
|
|
|
$object->set_url( $this->get_url() );
|
|
|
|
$object->set_type( $this->get_object_type() );
|
|
|
|
|
2023-11-30 08:46:15 +01:00
|
|
|
$published = \strtotime( $wp_post->post_date_gmt );
|
2023-11-29 23:46:13 +01:00
|
|
|
|
|
|
|
$object->set_published( \gmdate( 'Y-m-d\TH:i:s\Z', $published ) );
|
|
|
|
|
2023-11-30 08:46:15 +01:00
|
|
|
$updated = \strtotime( $wp_post->post_modified_gmt );
|
2023-11-29 23:46:13 +01:00
|
|
|
|
|
|
|
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 );
|
2023-11-30 08:46:15 +01:00
|
|
|
$path = sprintf( 'users/%d/followers', intval( $wp_post->post_author ) );
|
2023-11-29 23:46:13 +01:00
|
|
|
|
|
|
|
$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;
|
|
|
|
}
|
2018-09-30 22:51:22 +02:00
|
|
|
}
|