wordpress-event-bridge-for-.../includes/activitypub/transmogrifier/class-gatherpress.php

125 lines
3.4 KiB
PHP
Raw Normal View History

<?php
/**
* ActivityPub Transmogrify for the GatherPress event plugin.
*
* Handles converting incoming external ActivityPub events to GatherPress Events.
*
* @package Event_Bridge_For_ActivityPub
* @since 1.0.0
* @license AGPL-3.0-or-later
*/
2024-12-10 19:34:15 +01:00
namespace Event_Bridge_For_ActivityPub\Activitypub\Transmogrifier;
use Activitypub\Activity\Extended_Object\Event;
use Activitypub\Activity\Extended_Object\Place;
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit; // @codeCoverageIgnore
use GatherPress\Core\Event as GatherPress_Event;
/**
2024-12-10 19:34:15 +01:00
* ActivityPub Transmogrifier for the GatherPress event plugin.
*
* Handles converting incoming external ActivityPub events to GatherPress Events.
*
* @since 1.0.0
*/
class GatherPress {
/**
* The current GatherPress Event object.
*
* @var Event
*/
protected $activitypub_event;
/**
* Extend the constructor, to also set the GatherPress objects.
*
* This is a special class object form The Events Calendar which
* has a lot of useful functions, we make use of our getter functions.
*
* @param array $activitypub_event The ActivityPub Event as associative array.
*/
public function __construct( $activitypub_event ) {
$activitypub_event = Event::init_from_array( $activitypub_event );
if ( is_wp_error( $activitypub_event ) ) {
return;
}
$this->activitypub_event = $activitypub_event;
}
2024-12-11 23:09:12 +01:00
/**
* Get post.
*/
private function get_post_id_from_activitypub_id() {
global $wpdb;
$id = $this->activitypub_event->get_id();
return $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE guid=%s AND post_type=%s", $id, 'gatherpress_event' ) );
}
/**
* Save the ActivityPub event object as GatherPress Event.
*/
2024-12-10 22:43:13 +01:00
public function create() {
2024-12-10 19:34:15 +01:00
// Insert new GatherPress Event post.
$post_id = wp_insert_post(
array(
'post_title' => $this->activitypub_event->get_name(),
'post_type' => 'gatherpress_event',
'post_content' => $this->activitypub_event->get_content(),
'post_excerpt' => $this->activitypub_event->get_summary(),
'post_status' => 'publish',
2024-12-11 23:09:12 +01:00
'guid' => $this->activitypub_event->get_id(),
)
);
if ( ! $post_id || is_wp_error( $post_id ) ) {
return;
}
$event = new \GatherPress\Core\Event( $post_id );
$params = array(
'datetime_start' => $this->activitypub_event->get_start_time(),
'datetime_end' => $this->activitypub_event->get_end_time(),
'timezone' => $this->activitypub_event->get_timezone(),
);
$event->save_datetimes( $params );
}
/**
* Save the ActivityPub event object as GatherPress Event.
*/
public function update() {
$post_id = $this->get_post_id_from_activitypub_id();
// Insert new GatherPress Event post.
$post_id = wp_update_post(
array(
'ID' => $post_id,
'post_title' => $this->activitypub_event->get_name(),
'post_type' => 'gatherpress_event',
'post_content' => $this->activitypub_event->get_content(),
'post_excerpt' => $this->activitypub_event->get_summary(),
'post_status' => 'publish',
'guid' => $this->activitypub_event->get_id(),
2024-12-10 19:34:15 +01:00
)
);
if ( ! $post_id || is_wp_error( $post_id ) ) {
return;
}
$event = new \GatherPress\Core\Event( $post_id );
$params = array(
'datetime_start' => $this->activitypub_event->get_start_time(),
'datetime_end' => $this->activitypub_event->get_end_time(),
'timezone' => $this->activitypub_event->get_timezone(),
);
$event->save_datetimes( $params );
}
}