|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Posts collection file. |
| 4 | + * |
| 5 | + * @package Activitypub |
| 6 | + */ |
| 7 | + |
| 8 | +namespace Activitypub\Collection; |
| 9 | + |
| 10 | +use Activitypub\Sanitize; |
| 11 | + |
| 12 | +use function Activitypub\object_to_uri; |
| 13 | + |
| 14 | +/** |
| 15 | + * Posts collection. |
| 16 | + * |
| 17 | + * Provides methods to retrieve, create, update, and manage ActivityPub posts (articles, notes, media, etc.). |
| 18 | + */ |
| 19 | +class Posts { |
| 20 | + /** |
| 21 | + * The post type for the posts. |
| 22 | + * |
| 23 | + * @var string |
| 24 | + */ |
| 25 | + const POST_TYPE = 'ap_post'; |
| 26 | + |
| 27 | + /** |
| 28 | + * Add an object to the collection. |
| 29 | + * |
| 30 | + * @param array $activity The activity object data. |
| 31 | + * |
| 32 | + * @return \WP_Post|\WP_Error The object post or WP_Error on failure. |
| 33 | + */ |
| 34 | + public static function add( $activity ) { |
| 35 | + $activity_object = $activity['object']; |
| 36 | + $actor = Remote_Actors::fetch_by_uri( object_to_uri( $activity_object['attributedTo'] ) ); |
| 37 | + |
| 38 | + if ( \is_wp_error( $actor ) ) { |
| 39 | + return $actor; |
| 40 | + } |
| 41 | + |
| 42 | + $post_array = self::activity_to_post( $activity_object ); |
| 43 | + $post_id = \wp_insert_post( $post_array, true ); |
| 44 | + |
| 45 | + if ( \is_wp_error( $post_id ) ) { |
| 46 | + return $post_id; |
| 47 | + } |
| 48 | + |
| 49 | + \add_post_meta( $post_id, '_activitypub_remote_actor_id', $actor->ID ); |
| 50 | + |
| 51 | + self::add_taxonomies( $post_id, $activity_object ); |
| 52 | + |
| 53 | + return \get_post( $post_id ); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Get an object from the collection. |
| 58 | + * |
| 59 | + * @param int $id The object ID. |
| 60 | + * |
| 61 | + * @return \WP_Post|null The post object or null on failure. |
| 62 | + */ |
| 63 | + public static function get( $id ) { |
| 64 | + return \get_post( $id ); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Get an object by its GUID. |
| 69 | + * |
| 70 | + * @param string $guid The object GUID. |
| 71 | + * |
| 72 | + * @return \WP_Post|\WP_Error The object post or WP_Error on failure. |
| 73 | + */ |
| 74 | + public static function get_by_guid( $guid ) { |
| 75 | + global $wpdb; |
| 76 | + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 77 | + $post_id = $wpdb->get_var( |
| 78 | + $wpdb->prepare( |
| 79 | + "SELECT ID FROM $wpdb->posts WHERE guid=%s AND post_type=%s", |
| 80 | + \esc_url( $guid ), |
| 81 | + self::POST_TYPE |
| 82 | + ) |
| 83 | + ); |
| 84 | + |
| 85 | + if ( ! $post_id ) { |
| 86 | + return new \WP_Error( |
| 87 | + 'activitypub_object_not_found', |
| 88 | + \__( 'Object not found', 'activitypub' ), |
| 89 | + array( 'status' => 404 ) |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + return \get_post( $post_id ); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Update an object in the collection. |
| 98 | + * |
| 99 | + * @param array $activity The activity object data. |
| 100 | + * |
| 101 | + * @return \WP_Post|\WP_Error The updated object post or WP_Error on failure. |
| 102 | + */ |
| 103 | + public static function update( $activity ) { |
| 104 | + $post = self::get_by_guid( $activity['object']['id'] ); |
| 105 | + if ( \is_wp_error( $post ) ) { |
| 106 | + return $post; |
| 107 | + } |
| 108 | + |
| 109 | + $post_array = self::activity_to_post( $activity['object'] ); |
| 110 | + $post_array['ID'] = $post->ID; |
| 111 | + $post_id = \wp_update_post( $post_array, true ); |
| 112 | + |
| 113 | + if ( \is_wp_error( $post_id ) ) { |
| 114 | + return $post_id; |
| 115 | + } |
| 116 | + |
| 117 | + self::add_taxonomies( $post_id, $activity['object'] ); |
| 118 | + |
| 119 | + return \get_post( $post_id ); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Convert an activity to a post array. |
| 124 | + * |
| 125 | + * @param array $activity The activity array. |
| 126 | + * |
| 127 | + * @return array|\WP_Error The post array or WP_Error on failure. |
| 128 | + */ |
| 129 | + private static function activity_to_post( $activity ) { |
| 130 | + if ( ! is_array( $activity ) ) { |
| 131 | + return new \WP_Error( 'invalid_activity', __( 'Invalid activity format', 'activitypub' ) ); |
| 132 | + } |
| 133 | + |
| 134 | + return array( |
| 135 | + 'post_title' => isset( $activity['name'] ) ? \wp_strip_all_tags( $activity['name'] ) : '', |
| 136 | + 'post_content' => isset( $activity['content'] ) ? Sanitize::content( $activity['content'] ) : '', |
| 137 | + 'post_excerpt' => isset( $activity['summary'] ) ? \wp_strip_all_tags( $activity['summary'] ) : '', |
| 138 | + 'post_status' => 'publish', |
| 139 | + 'post_type' => self::POST_TYPE, |
| 140 | + 'guid' => isset( $activity['id'] ) ? \esc_url_raw( $activity['id'] ) : '', |
| 141 | + ); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Add taxonomies to the object post. |
| 146 | + * |
| 147 | + * @param int $post_id The post ID. |
| 148 | + * @param array $activity_object The activity object data. |
| 149 | + */ |
| 150 | + private static function add_taxonomies( $post_id, $activity_object ) { |
| 151 | + // Save Object Type as Taxonomy item. |
| 152 | + \wp_set_post_terms( $post_id, array( $activity_object['type'] ), 'ap_object_type' ); |
| 153 | + |
| 154 | + $tags = array(); |
| 155 | + |
| 156 | + // Save the Hashtags as Taxonomy items. |
| 157 | + if ( ! empty( $activity_object['tag'] ) && \is_array( $activity_object['tag'] ) ) { |
| 158 | + foreach ( $activity_object['tag'] as $tag ) { |
| 159 | + if ( isset( $tag['type'] ) && 'Hashtag' === $tag['type'] && isset( $tag['name'] ) ) { |
| 160 | + $tags[] = \wp_strip_all_tags( ltrim( $tag['name'], '#' ) ); |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + \wp_set_post_terms( $post_id, $tags, 'ap_tag' ); |
| 166 | + } |
| 167 | +} |
0 commit comments