some progress on The Events Calendar Transmogrifier
Some checks failed
PHP Code Checker / PHP Code Checker (pull_request) Successful in 47s
PHPUnit / PHPUnit – PHP 7.4 (pull_request) Failing after 56s
PHPUnit / PHPUnit – PHP 8.0 (pull_request) Failing after 1m0s
PHPUnit / PHPUnit – PHP 8.1 (pull_request) Failing after 1m0s
PHPUnit / PHPUnit – PHP 8.2 (pull_request) Failing after 59s
PHPUnit / PHPUnit – PHP 8.3 (pull_request) Failing after 57s
PHPUnit / PHPUnit – PHP 8.4 (pull_request) Failing after 1m0s

This commit is contained in:
André Menrath 2024-12-16 00:58:47 +01:00
parent 33f1ccbc86
commit e12a0734a5
2 changed files with 91 additions and 5 deletions

View file

@ -0,0 +1,47 @@
<?php
/**
* Extending the Tribe Events API to allow setting of the guid.
*
* @package Event_Bridge_For_ActivityPub
* @since 1.0.0
* @license AGPL-3.0-or-later
*/
namespace Event_Bridge_For_ActivityPub\Activitypub\Transmogrifier;
use DateTime;
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit; // @codeCoverageIgnore
/**
* Extending the Tribe Events API to allow setting of the guid.
*
* @since 1.0.0
*/
class The_Events_Calendar_Event_Repository extends \Tribe__Events__Repositories__Event {
/**
* Override diff: allow setting of guid.
*
* @var array An array of keys that cannot be updated on this repository.
*/
protected static $blocked_keys = array(
'ID',
'post_type',
'post_modified',
'post_modified_gmt',
'comment_count',
);
/**
* Whether the current key can be updated by this repository or not.
*
* @since 4.7.19
*
* @param string $key The key.
* @return bool
*/
protected function can_be_updated( $key ) {
return ! in_array( $key, self::$blocked_keys, true );
}
}

View file

@ -18,8 +18,6 @@ use function Activitypub\sanitize_url;
// Exit if accessed directly. // Exit if accessed directly.
defined( 'ABSPATH' ) || exit; // @codeCoverageIgnore defined( 'ABSPATH' ) || exit; // @codeCoverageIgnore
use GatherPress\Core\Event as GatherPress_Event;
/** /**
* ActivityPub Transmogrifier for the GatherPress event plugin. * ActivityPub Transmogrifier for the GatherPress event plugin.
* *
@ -45,15 +43,56 @@ class The_Events_Calendar extends Base {
/** /**
* Save the ActivityPub event object as GatherPress Event. * Save the ActivityPub event object as GatherPress Event.
* *
* @return void * @return false|int
*/ */
public function save_event(): void { public function save_event() {
// Limit this as a safety measure. // Limit this as a safety measure.
add_filter( 'wp_revisions_to_keep', array( self::class, 'revisions_to_keep' ) ); add_filter( 'wp_revisions_to_keep', array( self::class, 'revisions_to_keep' ) );
$this->get_post_id_from_activitypub_id(); $post_id = $this->get_post_id_from_activitypub_id();
$duration = $this->get_duration();
$args = array(
'title' => sanitize_text_field( $this->activitypub_event->get_name() ),
'content' => wp_kses_post( $this->activitypub_event->get_content() ),
'start_date' => gmdate( 'Y-m-d H:i:s', strtotime( $this->activitypub_event->get_start_time() ) ),
'duration' => $duration,
'status' => 'publish',
'guid' => sanitize_url( $this->activitypub_event->get_id() ),
);
$tribe_event = new The_Events_Calendar_Event_Repository();
if ( $post_id ) {
$args['post_title'] = $args['title'];
$args['post_content'] = $args['content'];
// Update existing GatherPress event post.
$post = \Tribe__Events__API::updateEvent( $post_id, $args );
} else {
$post = $tribe_event->set_args( $args )->create();
}
if ( ! $post ) {
return false;
}
// Limit this as a safety measure. // Limit this as a safety measure.
remove_filter( 'wp_revisions_to_keep', array( self::class, 'revisions_to_keep' ) ); remove_filter( 'wp_revisions_to_keep', array( self::class, 'revisions_to_keep' ) );
return $post->ID;
}
/**
* Get the events duration in seconds.
*
* @return int
*/
private function get_duration() {
$end_time = $this->activitypub_event->get_end_time();
if ( ! $end_time ) {
return 2 * HOUR_IN_SECONDS;
}
return abs( strtotime( $end_time ) - strtotime( $this->activitypub_event->get_start_time() ) );
} }
} }