From ca940bfc4e0c1033a98a3ca5228cbb779247509a Mon Sep 17 00:00:00 2001 From: ruru4143 Date: Sat, 9 Dec 2023 02:01:13 +0100 Subject: [PATCH] smoother build process --- includes/activity/class-base-object.php | 16 ++++ includes/transformer/class-base.php | 103 +++++++++++++++++++++++- 2 files changed, 117 insertions(+), 2 deletions(-) diff --git a/includes/activity/class-base-object.php b/includes/activity/class-base-object.php index ded8f7b..cd3fb63 100644 --- a/includes/activity/class-base-object.php +++ b/includes/activity/class-base-object.php @@ -453,6 +453,10 @@ class Base_Object { return new WP_Error( 'invalid_key', __( 'Invalid key', 'activitypub' ), array( 'status' => 404 ) ); } + if ( $var === 'skip_next' ) { + return; // skip_next should never be retrieved publicly + } + return $this->$var; } @@ -522,6 +526,11 @@ class Base_Object { return new WP_Error( 'invalid_key', __( 'Invalid key', 'activitypub' ), array( 'status' => 404 ) ); } + if ( $this->skip_next ) { + $this->skip_next = false; + return $this; + } + $this->$key = $value; return $this; @@ -675,4 +684,11 @@ class Base_Object { return \wp_json_encode( $array, \JSON_HEX_TAG | \JSON_HEX_AMP | \JSON_HEX_QUOT ); } + + public function if( bool $b ) { + $this->skip_next = ! $b; + return $this; + } + + } diff --git a/includes/transformer/class-base.php b/includes/transformer/class-base.php index cb89ee2..248fb6d 100644 --- a/includes/transformer/class-base.php +++ b/includes/transformer/class-base.php @@ -33,6 +33,13 @@ abstract class Base { */ protected $wp_post; + /** + * Time format string for date or gmdate + * + * @var string + */ + protected $timeformat; + /** * Get the name used for registering the transformer with the ActivityPub plugin. * @@ -68,6 +75,48 @@ abstract class Base { */ abstract public function transform(); + /** + * Retrieves a post meta field for the configured post. + * + * @param string $key Optional. The meta key to retrieve. By default, + * returns data for all keys. Default empty. + * @param bool $single Optional. Whether to return a single value. + * This parameter has no effect if `$key` is not specified. + * Default false. + * @return mixed An array of values if `$single` is false. + * The value of the meta field if `$single` is true. + * False for an invalid `$post_id` (non-numeric, zero, or negative value). + * An empty string if a valid but non-existing post ID is passed. + */ + public function get_post_meta( string $key = '', $single = false, $default = null ) { + // todo test what if not present + // empty string or empty array, then return default + return \get_post_meta( $this->wp_post->ID, $key, $single ); + } + + /** + * Retrieves a post meta field for the configured post. + * + * @param string $key The meta key of the time value to retrieve. + * @return string formatted time + */ + public function get_post_meta_time( string $key ) { + $time = $this->get_post_meta( $key, true ); // todo what if not exists + $time = \strtotime( $time ); + return \gmdate( $this->timeformat, $time ); + } + + /** + * Retrieves if comments are enabled of the configured post. + * + * @return bool True if the comments are open. + */ + public function get_comments_open() { + return comments_open( $this->wp_post->ID ); + } + + + /** * Getter function for this wp_post. * @@ -98,6 +147,56 @@ abstract class Base { $this->wp_post = $wp_post; } + /** + * Setter for $timeformat. + * + * This configures a timeformat string for converting timestaps into dates + * + * @param string $timeformat The time format string. + * @return void + */ + public function set_timeformat( string $timeformat ) { + $this->timeformat = $timeformat; + } + + /** + * Get published time in configured format + * + * This queries the published time from the wp_object and returns it in the configured format + * + * @return string + */ + public function get_published() { + $published = \strtotime( $this->wp_post->post_date_gmt ); + return \gmdate( 'Y-m-d\TH:i:s\Z', $published ); + } + + + /** + * Get updated time in configured format + * + * This queries the update time from the wp_object and returns it in the configured format + * + * @return string + */ + public function get_updated() { + $updated = \strtotime( $this->wp_post->post_modified_gmt ); + return \gmdate( 'Y-m-d\TH:i:s\Z', $updated ); + } + + /** + * TODO review and write description + * + * @return array + */ + public function get_followers_stream() { + $path = sprintf( 'users/%d/followers', intval( $this->wp_post->post_author ) ); + return array( + 'https://www.w3.org/ns/activitystreams#Public', + get_rest_url_by_path( $path ), + ); + } + /** * Get the supported WP post types that the transformer can use as an input. * @@ -242,7 +341,7 @@ abstract class Base { * @return string The User-URL. */ protected function get_attributed_to() { - if ( is_single_user() ) { + if ( is_single_user() ) { // todo what is if this setting gets changed $user = new Blog_User(); return $user->get_url(); } @@ -590,7 +689,7 @@ abstract class Base { * * @return array the contenmap */ - protected function get_content_map() { + protected function get_basic_content_map() { return array( $this->get_locale() => $this->get_content(), );