WP_REST_Comments_Controller::prepare_links()protectedWP 4.7.0

Prepares links for the request.

Метод класса: WP_REST_Comments_Controller{}

Хуков нет.

Возвращает

Массив. Links for the given comment.

Использование

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->prepare_links( $comment );
$comment(WP_Comment) (обязательный)
Comment object.

Список изменений

С версии 4.7.0 Введена.

Код WP_REST_Comments_Controller::prepare_links() WP 6.5.2

protected function prepare_links( $comment ) {
	$links = array(
		'self'       => array(
			'href' => rest_url( sprintf( '%s/%s/%d', $this->namespace, $this->rest_base, $comment->comment_ID ) ),
		),
		'collection' => array(
			'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
		),
	);

	if ( 0 !== (int) $comment->user_id ) {
		$links['author'] = array(
			'href'       => rest_url( 'wp/v2/users/' . $comment->user_id ),
			'embeddable' => true,
		);
	}

	if ( 0 !== (int) $comment->comment_post_ID ) {
		$post       = get_post( $comment->comment_post_ID );
		$post_route = rest_get_route_for_post( $post );

		if ( ! empty( $post->ID ) && $post_route ) {
			$links['up'] = array(
				'href'       => rest_url( $post_route ),
				'embeddable' => true,
				'post_type'  => $post->post_type,
			);
		}
	}

	if ( 0 !== (int) $comment->comment_parent ) {
		$links['in-reply-to'] = array(
			'href'       => rest_url( sprintf( '%s/%s/%d', $this->namespace, $this->rest_base, $comment->comment_parent ) ),
			'embeddable' => true,
		);
	}

	// Only grab one comment to verify the comment has children.
	$comment_children = $comment->get_children(
		array(
			'count'   => true,
			'orderby' => 'none',
		)
	);

	if ( ! empty( $comment_children ) ) {
		$args = array(
			'parent' => $comment->comment_ID,
		);

		$rest_url = add_query_arg( $args, rest_url( $this->namespace . '/' . $this->rest_base ) );

		$links['children'] = array(
			'href'       => $rest_url,
			'embeddable' => true,
		);
	}

	return $links;
}