Add Tribe organizer
All checks were successful
PHP Code Checker / PHP Code Checker (pull_request) Successful in 55s
PHPUnit / PHPUnit – PHP 7.4 (pull_request) Successful in 1m4s
PHPUnit / PHPUnit – PHP 8.0 (pull_request) Successful in 1m4s
PHPUnit / PHPUnit – PHP 8.1 (pull_request) Successful in 1m1s
PHPUnit / PHPUnit – PHP 8.2 (pull_request) Successful in 1m3s
PHPUnit / PHPUnit – PHP 8.3 (pull_request) Successful in 1m3s
PHPUnit / PHPUnit – PHP 8.4 (pull_request) Successful in 1m0s

This commit is contained in:
André Menrath 2024-12-24 11:55:06 +01:00
parent 633a5332d1
commit aee19e9606
2 changed files with 79 additions and 3 deletions

View file

@ -15,6 +15,7 @@ namespace Event_Bridge_For_ActivityPub\ActivityPub\Model;
use Activitypub\Activity\Actor;
use Event_Bridge_For_ActivityPub\ActivityPub\Collection\Event_Sources;
use WP_Error;
use WP_Post;
use function Activitypub\sanitize_url;
@ -61,11 +62,24 @@ class Event_Source extends Actor {
return array();
}
/**
* Getter for URL attribute.
*
* @return string The URL.
*/
public function get_url() {
if ( $this->url ) {
return $this->url;
}
return $this->id;
}
/**
* Get the WordPress post which stores the Event Source by the ActivityPub actor id of the event source.
*
* @param string $actor_id The ActivityPub actor ID.
* @return ?int The WordPress post ID if the actor is found, null if not.
* @return ?WP_Post The WordPress post if the actor is found, null if not.
*/
private static function get_wp_post_by_activitypub_actor_id( $actor_id ) {
global $wpdb;
@ -110,8 +124,8 @@ class Event_Source extends Actor {
$object->set_id( $post->guid );
$object->set_name( $post->post_title );
$object->set_summary( $post->post_excerpt );
$object->set_published( gmdate( 'Y-m-d H:i:s', strtotime( $post->post_date ) ) );
$object->set_updated( gmdate( 'Y-m-d H:i:s', strtotime( $post->post_modified ) ) );
$object->set_published( gmdate( 'Y-m-d H:i:s', strtotime( $post->post_date_gmt ) ) );
$object->set_updated( gmdate( 'Y-m-d H:i:s', strtotime( $post->post_modified_gmt ) ) );
$thumbnail_id = get_post_thumbnail_id( $post );
if ( $thumbnail_id ) {
$object->set_icon(

View file

@ -11,9 +11,11 @@
namespace Event_Bridge_For_ActivityPub\ActivityPub\Transmogrifier;
use Event_Bridge_For_ActivityPub\ActivityPub\Model\Event_Source;
use Tribe__Date_Utils;
use function Activitypub\sanitize_url;
use function Activitypub\object_to_uri;
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit; // @codeCoverageIgnore
@ -103,6 +105,59 @@ class The_Events_Calendar extends Base {
return $post_id;
}
/**
* Add organizer.
*
* @return int|bool $post_id The organizers post ID.
*/
private function add_organizer() {
// This might likely change, because of FEP-8a8e.
$actor = $this->activitypub_event->get_attributed_to();
if ( is_null( $actor ) ) {
return false;
}
$actor_id = object_to_uri( $actor );
$event_source = Event_Source::get_by_id( $actor_id );
// As long as we do not support announces, we expect the attributedTo to be an existing event source.
if ( ! $event_source ) {
return false;
}
$tribe_organizer = tribe_organizers()
->set_args(
array(
'organizer' => $event_source->get_name(),
'description' => $event_source->get_summary(),
'post_date_gmt' => $event_source->get_published(),
'website' => $event_source->get_id(),
'excerpt' => $event_source->get_summary(),
),
'publish',
true // This enables avoid_duplicates which includes exact matches of title, content, excerpt, and website.
)->create();
if ( ! $tribe_organizer ) {
return;
}
// Make a relationship between the event source WP_Post and the organizer WP_Post.
wp_update_post(
array(
'ID' => $tribe_organizer->ID,
'post_parent' => $event_source->get__id(),
)
);
// Add the thumbnail of the event source to the organizer.
if ( get_post_thumbnail_id( $event_source ) ) {
set_post_thumbnail( $tribe_organizer, get_post_thumbnail_id( $event_source ) );
}
return $tribe_organizer->ID;
}
/**
* Save the ActivityPub event object as GatherPress Event.
*
@ -118,6 +173,8 @@ class The_Events_Calendar extends Base {
$venue_id = $this->add_venue();
$organizer_id = $this->add_organizer();
$args = array(
'title' => sanitize_text_field( $this->activitypub_event->get_name() ),
'content' => wp_kses_post( $this->activitypub_event->get_content() ?? '' ),
@ -132,6 +189,11 @@ class The_Events_Calendar extends Base {
$args['VenueID'] = $venue_id;
}
if ( $organizer_id ) {
$args['organizer'] = $organizer_id;
$args['OrganizerID'] = $organizer_id;
}
$tribe_event = new The_Events_Calendar_Event_Repository();
if ( $post_id ) {