| 1 |
<?php |
| 2 |
/** |
| 3 |
* ActivityPub Post REST Endpoints |
| 4 |
* |
| 5 |
* @package Activitypub |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Activitypub\Rest; |
| 9 |
|
| 10 |
use Activitypub\Comment; |
| 11 |
use Activitypub\Activity\Base_Object; |
| 12 |
use Activitypub\Collection\Replies; |
| 13 |
|
| 14 |
use function Activitypub\get_rest_url_by_path; |
| 15 |
|
| 16 |
/** |
| 17 |
* Class Post_Controller |
| 18 |
* |
| 19 |
* @package Activitypub\Rest |
| 20 |
*/ |
| 21 |
class Post_Controller extends \WP_REST_Controller { |
| 22 |
|
| 23 |
/** |
| 24 |
* The namespace of this controller's route. |
| 25 |
* |
| 26 |
* @var string |
| 27 |
*/ |
| 28 |
protected $namespace = ACTIVITYPUB_REST_NAMESPACE; |
| 29 |
|
| 30 |
/** |
| 31 |
* The base of this controller's route. |
| 32 |
* |
| 33 |
* @var string |
| 34 |
*/ |
| 35 |
protected $rest_base = 'posts/(?P<id>\d+)'; |
| 36 |
|
| 37 |
/** |
| 38 |
* Register routes. |
| 39 |
*/ |
| 40 |
public function register_routes() { |
| 41 |
\register_rest_route( |
| 42 |
$this->namespace, |
| 43 |
'/' . $this->rest_base . '/reactions', |
| 44 |
array( |
| 45 |
'args' => array( |
| 46 |
'id' => array( |
| 47 |
'required' => true, |
| 48 |
'type' => 'integer', |
| 49 |
), |
| 50 |
), |
| 51 |
array( |
| 52 |
'methods' => \WP_REST_Server::READABLE, |
| 53 |
'callback' => array( $this, 'get_reactions' ), |
| 54 |
'permission_callback' => '__return_true', |
| 55 |
), |
| 56 |
) |
| 57 |
); |
| 58 |
|
| 59 |
\register_rest_route( |
| 60 |
$this->namespace, |
| 61 |
'/' . $this->rest_base . '/context', |
| 62 |
array( |
| 63 |
'args' => array( |
| 64 |
'id' => array( |
| 65 |
'required' => true, |
| 66 |
'type' => 'integer', |
| 67 |
), |
| 68 |
), |
| 69 |
array( |
| 70 |
'methods' => \WP_REST_Server::READABLE, |
| 71 |
'callback' => array( $this, 'get_context' ), |
| 72 |
'permission_callback' => '__return_true', |
| 73 |
), |
| 74 |
) |
| 75 |
); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Get reactions for a post. |
| 80 |
* |
| 81 |
* @param \WP_REST_Request $request The request. |
| 82 |
* |
| 83 |
* @return \WP_REST_Response|\WP_Error Response object on success, or WP_Error object on failure. |
| 84 |
*/ |
| 85 |
public function get_reactions( $request ) { |
| 86 |
$post_id = $request->get_param( 'id' ); |
| 87 |
$post = \get_post( $post_id ); |
| 88 |
|
| 89 |
if ( ! $post ) { |
| 90 |
return new \WP_Error( 'activitypub_post_not_found', 'Post not found', array( 'status' => 404 ) ); |
| 91 |
} |
| 92 |
|
| 93 |
$reactions = array(); |
| 94 |
|
| 95 |
foreach ( Comment::get_comment_types() as $type_object ) { |
| 96 |
$comments = \get_comments( |
| 97 |
array( |
| 98 |
'post_id' => $post_id, |
| 99 |
'type' => $type_object['type'], |
| 100 |
'status' => 'approve', |
| 101 |
'parent' => 0, |
| 102 |
) |
| 103 |
); |
| 104 |
|
| 105 |
if ( empty( $comments ) ) { |
| 106 |
continue; |
| 107 |
} |
| 108 |
|
| 109 |
$count = \count( $comments ); |
| 110 |
// phpcs:disable WordPress.WP.I18n |
| 111 |
$label = \sprintf( |
| 112 |
\_n( |
| 113 |
$type_object['count_single'], |
| 114 |
$type_object['count_plural'], |
| 115 |
$count, |
| 116 |
'activitypub' |
| 117 |
), |
| 118 |
\number_format_i18n( $count ) |
| 119 |
); |
| 120 |
// phpcs:enable WordPress.WP.I18n |
| 121 |
|
| 122 |
$reactions[ $type_object['collection'] ] = array( |
| 123 |
'label' => $label, |
| 124 |
'items' => \array_map( |
| 125 |
function ( $comment ) { |
| 126 |
return array( |
| 127 |
'name' => html_entity_decode( $comment->comment_author ), |
| 128 |
'url' => $comment->comment_author_url, |
| 129 |
'avatar' => \get_avatar_url( $comment ), |
| 130 |
); |
| 131 |
}, |
| 132 |
$comments |
| 133 |
), |
| 134 |
); |
| 135 |
} |
| 136 |
|
| 137 |
return new \WP_REST_Response( $reactions ); |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Get the context for a post. |
| 142 |
* |
| 143 |
* @param \WP_REST_Request $request The request. |
| 144 |
* |
| 145 |
* @return \WP_REST_Response|\WP_Error Response object on success, or WP_Error object on failure. |
| 146 |
*/ |
| 147 |
public function get_context( $request ) { |
| 148 |
$post_id = $request->get_param( 'id' ); |
| 149 |
|
| 150 |
$collection = Replies::get_context_collection( $post_id ); |
| 151 |
|
| 152 |
if ( false === $collection ) { |
| 153 |
return new \WP_Error( 'activitypub_post_not_found', 'Post not found', array( 'status' => 404 ) ); |
| 154 |
} |
| 155 |
|
| 156 |
$response = array_merge( |
| 157 |
array( |
| 158 |
'@context' => Base_Object::JSON_LD_CONTEXT, |
| 159 |
'id' => get_rest_url_by_path( \sprintf( 'posts/%d/context', $post_id ) ), |
| 160 |
), |
| 161 |
$collection |
| 162 |
); |
| 163 |
|
| 164 |
$response = \rest_ensure_response( $response ); |
| 165 |
$response->header( 'Content-Type', 'application/activity+json; charset=' . \get_option( 'blog_charset' ) ); |
| 166 |
|
| 167 |
return $response; |
| 168 |
} |
| 169 |
} |
| 170 |
|