move event related shortcodes to base event transformer
Some checks failed
PHP Code Checker / PHP Code Checker (pull_request) Failing after 54s
PHPUnit / PHPUnit – PHP 7.4 (pull_request) Failing after 1m0s
PHPUnit / PHPUnit – PHP 8.0 (pull_request) Failing after 1m1s
PHPUnit / PHPUnit – PHP 8.1 (pull_request) Failing after 1m2s
PHPUnit / PHPUnit – PHP 8.2 (pull_request) Failing after 56s
PHPUnit / PHPUnit – PHP 8.3 (pull_request) Failing after 54s

This commit is contained in:
André Menrath 2024-11-21 14:32:43 +01:00
parent 02a3eb7cef
commit b7b36d355e
2 changed files with 183 additions and 275 deletions

View file

@ -13,8 +13,8 @@ defined( 'ABSPATH' ) || exit; // @codeCoverageIgnore
use Activitypub\Activity\Extended_Object\Event as Event_Object;
use Activitypub\Activity\Extended_Object\Place;
use Activitypub\Shortcodes;
use Activitypub\Transformer\Post;
use ActivityPub_Event_Bridge\Event_Shortcodes;
use DateTime;
@ -196,7 +196,7 @@ abstract class Event extends Post {
*
* @param ?string $time The time which needs to be formatted.
*/
private static function format_time( $time ) {
protected static function format_time( $time ) {
if ( is_null( $time ) ) {
return '';
}
@ -207,24 +207,160 @@ abstract class Event extends Post {
}
/**
* Format a human readable address.
* Generates output for the 'ap_start_time' shortcode.
*
* @param ?array $atts The shortcode's attributes.
* @return string The formatted start date and time of the event.
*/
protected function format_address(): string {
public function shortcode_start_time( $atts ) {
$start_timestamp = $this->get_start_time();
return $this->generate_time_output( $start_timestamp, $atts, '🗓️', __( 'Start', 'activitypub-event-bridge' ) );
}
/**
* Generates output for the 'ap_end_time' shortcode.
*
* @param ?array $atts The shortcode's attributes.
* @return string The formatted end date and time of the event.
*/
public function shortcode_end_time( $atts ) {
$end_timestamp = $this->get_end_time();
return $this->generate_time_output( $end_timestamp, $atts, '⏳', __( 'End', 'activitypub-event-bridge' ) );
}
/**
* Generates the formatted time output for a shortcode.
*
* @param int|null $timestamp The timestamp for the event time.
* @param array $atts The shortcode attributes.
* @param string $icon The icon to display.
* @param string $label The label to display (e.g., 'Start', 'End').
* @return string The formatted date and time, or an empty string if the timestamp is invalid.
*/
private function generate_time_output( $timestamp, $atts, $icon, $label ) {
if ( ! $timestamp ) {
return '';
}
$args = shortcode_atts(
array(
'icon' => 'true',
'title' => 'true',
),
$atts
);
$args['icon'] = filter_var( $args['icon'], FILTER_VALIDATE_BOOLEAN );
$args['title'] = filter_var( $args['title'], FILTER_VALIDATE_BOOLEAN );
$output = array();
if ( $args['icon'] ) {
$output[] = $icon;
}
if ( $args['title'] ) {
$output[] = $label . ':';
}
$output[] = self::format_time( $timestamp );
return implode( ' ', $output );
}
/**
* Generates output for the 'ap_location' shortcode.
*
* @param ?array $atts The shortcode's attributes.
* @return string The formatted location/address of the event.
*/
public function shortcode_location( $atts ) {
$args = shortcode_atts(
array(
'icon' => 'true',
'title' => 'true',
'country' => 'true',
'zip' => 'true',
'city' => 'true',
'street' => 'true',
'name' => 'true',
),
$atts,
'ap_location'
);
// Convert attributes to booleans.
$args = array_map(
function ( $value ) {
return filter_var( $value, FILTER_VALIDATE_BOOLEAN );
},
$args
);
$location = $this->get_location();
if ( is_null( $location ) ) {
if ( ! $location ) {
return '';
}
$address = $location->get_address();
if ( ! $address ) {
return $location->get_name();
$output = array();
if ( $args['icon'] ) {
$output[] = '📍';
}
if ( $args['title'] ) {
$output[] = esc_html__( 'Location', 'activitypub-event-bridge' ) . ':';
}
$output[] = self::format_address( $location->get_address(), $args );
// Join output array into a single string with spaces and return.
return implode( ' ', array_filter( $output ) );
}
/**
* Formats the address based on provided arguments.
*
* @param mixed $address The address data, either as a string or an array.
* @param array $args The arguments for which components to include.
* @return string The formatted address.
*/
protected static function format_address( $address, $args = null) {
if ( is_string( $address ) ) {
return $address;
return esc_html( $address );
}
if ( ! is_array( $address ) ) {
return '';
if ( is_null( $args ) ) {
$args = array(
'icon' => 'true',
'title' => 'true',
'country' => 'true',
'zip' => 'true',
'city' => 'true',
'street' => 'true',
'name' => 'true',
);
}
return isset( $address['locality'] ) ? $address['locality'] : '';
if ( is_array( $address ) ) {
$address_parts = array();
$components = array(
'name' => 'name',
'street' => 'streetAddress',
'zip' => 'postalCode',
'city' => 'addressLocality',
'country' => 'addressCountry',
);
foreach ( $components as $arg_key => $address_key ) {
if ( $args[ $arg_key ] && ! empty( $address[ $address_key ] ) ) {
$address_parts[] = esc_html( $address[ $address_key ] );
}
}
return implode( ', ', $address_parts );
}
return '';
}
/**
@ -257,14 +393,34 @@ abstract class Event extends Post {
return '';
}
/**
* Register the shortcodes.
*/
public function register_shortcodes() {
foreach ( get_class_methods( self::class ) as $function ) {
if ( 'shortcode_' === substr( $function, 0, 10 ) ) {
add_shortcode( 'ap_' . substr( $function, 10, strlen( $function ) ), array( $this, $function ) );
}
}
}
/**
* Register the shortcodes.
*/
public function unregister_shortcodes() {
foreach ( get_class_methods( self::class ) as $function ) {
if ( 'shortcode_' === substr( $function, 0, 10 ) ) {
remove_shortcode( 'ap_' . substr( $function, 10, strlen( $function ) ), array( $this, $function ) );
}
}
}
/**
* Get the summary.
*/
public function get_summary(): ?string {
if ( 'preset' === get_option( 'activitypub_summary_type', 'preset' ) ) {
return $this->get_preset_summary();
return $this->format_preset_summary();
}
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
@ -277,7 +433,10 @@ abstract class Event extends Post {
}
// Register our shortcodes just in time.
Event_Shortcodes::register();
Shortcodes::register();
$this->register_shortcodes();
// Fill in the shortcodes.
\setup_postdata( $post );
$summary = \do_shortcode( $summary );
@ -289,8 +448,9 @@ abstract class Event extends Post {
$summary = \apply_filters( 'activitypub_event_bridge_the_summary', $summary, $post );
// Don't need these anymore, should never appear in a post.
Event_Shortcodes::unregister();
// Unregister the shortcodes.
Shortcodes::unregister();
$this->unregister_shortcodes();
return $summary;
}
@ -303,7 +463,7 @@ abstract class Event extends Post {
*
* @return string $summary The custom event summary.
*/
public function get_preset_summary(): ?string {
public function format_preset_summary(): ?string {
add_filter( 'activitypub_object_content_template', array( self::class, 'remove_ap_permalink_from_template' ), 2, 2 );
$excerpt = $this->retrieve_excerpt();
// BeforeFirstRelease: decide whether this should be a admin setting.
@ -314,9 +474,10 @@ abstract class Event extends Post {
remove_filter( 'activitypub_object_content_template', array( self::class, 'remove_ap_permalink_from_template' ) );
$category = $this->format_categories();
$start_time = $this->format_start_time();
$end_time = $this->format_end_time();
$address = $this->format_address();
$start_time = $this->get_start_time();
$end_time = $this->get_end_time();
$address = $this->format_address( $this->get_location() );
$time_atts = array( 'icon' => true, 'label' => true);
$formatted_items = array();
if ( ! empty( $category ) ) {
@ -324,11 +485,11 @@ abstract class Event extends Post {
}
if ( ! empty( $start_time ) ) {
$formatted_items[] = '🗓️ ' . __( 'Start', 'activitypub-event-bridge' ) . ': ' . $start_time;
$formatted_items[] = $this->generate_time_output( $start_time, $time_atts, '🗓️', __( 'Start', 'activitypub-event-bridge' ) );
}
if ( ! empty( $end_time ) ) {
$formatted_items[] = '⏳ ' . __( 'End', 'activitypub-event-bridge' ) . ': ' . $end_time;
$formatted_items[] = $this->generate_time_output( $end_time, $time_atts, '⏳', __( 'End', 'activitypub-event-bridge' ) );
}
if ( ! empty( $address ) ) {

View file

@ -1,253 +0,0 @@
<?php
/**
* File responsible for defining and registering custom shortcodes.
*
* @package ActivityPub_Event_Bridge
* @license AGPL-3.0-or-later
*/
namespace ActivityPub_Event_Bridge;
use Activitypub\Shortcodes;
use Activitypub\Transformer\Factory as Transformer_Factory;
use ActivityPub_Event_Bridge\Activitypub\Transformer\Event as Event_Transformer;
use DateTime;
/**
* Class responsible for defining and registering custom shortcodes.
*/
class Event_Shortcodes extends Shortcodes {
/**
* Register the shortcodes.
*/
public static function register() {
foreach ( get_class_methods( self::class ) as $shortcode ) {
if ( 'init' !== $shortcode ) {
add_shortcode( 'ap_' . $shortcode, array( self::class, $shortcode ) );
}
}
}
/**
* Unregister the shortcodes.
*/
public static function unregister() {
foreach ( get_class_methods( self::class ) as $shortcode ) {
if ( 'init' !== $shortcode ) {
remove_shortcode( 'ap_' . $shortcode );
}
}
}
/**
* Get the transformer of the current event post.
*
* @return ?Event_Transformer The Event Transformer.
*/
protected static function get_transformer(): ?Event_Transformer {
$post = self::get_item();
if ( ! $post ) {
return null;
}
$transformer = Transformer_Factory::get_transformer( $post );
if ( ! is_subclass_of( $transformer, Event_Transformer::class ) ) {
return null;
}
return $transformer;
}
/**
* Compose a human readable formatted time.
*
* @param ?string $time The time which needs to be formatted.
*/
protected static function format_time( $time ) {
if ( is_null( $time ) ) {
return '';
}
$start_datetime = new DateTime( $time );
$start_timestamp = $start_datetime->getTimestamp();
$datetime_format = get_option( 'date_format' ) . ' ' . get_option( 'time_format' );
return wp_date( $datetime_format, $start_timestamp );
}
/**
* Generates output for the 'apeb_start_time' shortcode.
*
* @param ?array $atts The shortcodes attributes.
*
* @return string The formatted start date and time of the event.
*/
public static function start_time( $atts ) {
$transformer = self::get_transformer();
if ( ! $transformer ) {
return '';
}
$args = shortcode_atts(
array(
'icon' => 'true',
'title' => 'true',
),
$atts,
'ap_start_time'
);
$args['icon'] = filter_var( $args['icon'], FILTER_VALIDATE_BOOLEAN );
$args['title'] = filter_var( $args['title'], FILTER_VALIDATE_BOOLEAN );
$start_timestamp = $transformer->get_start_time();
if ( ! $start_timestamp ) {
return '';
}
$start_time = array();
if ( $args['icon'] ) {
$start_time[] = '🗓️';
}
if ( $args['title'] ) {
$start_time[] = __( 'Start', 'activitypub-event-bridge' ) . ':';
}
$start_time[] = self::format_time( $start_timestamp );
$start_time = implode( ' ', $start_time );
return $start_time;
}
/**
* Generates output for the 'apeb_end_time' shortcode.
*
* @param ?array $atts The shortcodes attributes.
*
* @return string The formatted end date and time of the event.
*/
public static function end_time( $atts ) {
$transformer = self::get_transformer();
if ( ! $transformer ) {
return '';
}
$args = shortcode_atts(
array(
'icon' => 'true',
'title' => 'true',
),
$atts,
'ap_end_time'
);
$args['icon'] = filter_var( $args['icon'], FILTER_VALIDATE_BOOLEAN );
$args['title'] = filter_var( $args['title'], FILTER_VALIDATE_BOOLEAN );
$end_timestamp = $transformer->get_end_time();
if ( ! $end_timestamp ) {
return '';
}
$end_time = array();
if ( $args['icon'] ) {
$end_time[] = '⏳';
}
if ( $args['title'] ) {
$end_time[] = __( 'End', 'activitypub-event-bridge' ) . ':';
}
$end_time[] = self::format_time( $end_timestamp );
$end_time = implode( ' ', $end_time );
return $end_time;
}
/**
* Generates output for the 'apeb_location shortcode.
*
* @param ?array $atts The shortcodes attributes.
*
* @return string The formatted location/address of the event.
*/
public static function location( $atts ) {
$transformer = self::get_transformer();
if ( ! $transformer ) {
return '';
}
$args = shortcode_atts(
array(
'icon' => 'true',
'title' => 'true',
'country' => 'true',
'zip' => 'true',
'city' => 'true',
'street' => 'true',
'name' => 'true',
),
$atts,
'ap_location'
);
foreach ( $args as $arg => $value ) {
$args[ $arg ] = filter_var( $value, FILTER_VALIDATE_BOOLEAN );
}
$location = $transformer->get_location();
if ( ! $location ) {
return '';
}
$output = array();
if ( $args['icon'] ) {
$output[] = '📍';
}
if ( $args['title'] ) {
$output[] = __( 'Location', 'activitypub-event-bridge' ) . ':';
}
$address = $location->get_address();
if ( $address ) {
if ( is_string( $address ) ) {
$output[] = $address;
}
if ( is_array( $address ) ) {
if ( $args['name'] && array_key_exists( 'name', $address ) ) {
$output[] = $address['name'] . ',';
}
if ( $args['street'] && array_key_exists( 'streetAddress', $address ) ) {
$output[] = $address['streetAddress'] . ',';
}
if ( $args['zip'] && array_key_exists( 'postalCode', $address ) ) {
$output[] = $address['postalCode'];
}
if ( $args['city'] && array_key_exists( 'addressLocality', $address ) ) {
$output[] = $address['addressLocality'] . ',';
}
if ( $args['country'] && array_key_exists( 'addressCountry', $address ) ) {
$output[] = $address['addressCountry'];
}
}
}
$output = implode( ' ', $output );
return $output;
}
}