PluginProbe
ElasticPress / 5.0.0
ElasticPress v5.0.0
5.3.5 5.3.4 3.6.5 3.6.6 4.0.0 4.0.1 4.1.0 4.2.0 4.2.1 4.2.2 4.3.0 4.3.1 4.4.0 4.4.1 4.5.0 4.5.1 4.5.2 4.6.0 4.6.1 4.7.0 4.7.1 4.7.2 5.0.0 5.0.1 5.0.2 All 108 releases
elasticpress / includes / classes / REST / RelatedPosts.php

RelatedPosts.php in ElasticPress 5.0.0, at includes/classes/REST/RelatedPosts.php

108 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Related Posts REST API Controller
4 *
5 * @since 5.0.0
6 * @package elasticpress
7 */
8
9 namespace ElasticPress\REST;
10
11 use ElasticPress\Features;
12
13 /**
14 * Related Posts API controller class.
15 *
16 * @since 5.0.0
17 * @package elasticpress
18 */
19 class RelatedPosts {
20
21 /**
22 * Register routes.
23 *
24 * Registers the route using its own endpoint and the previous core
25 * endpoint, for backwards compatibility.
26 *
27 * @return void
28 */
29 public function register_routes() {
30 $args = [
31 'args' => $this->get_args(),
32 'callback' => [ $this, 'get_posts' ],
33 'methods' => 'GET',
34 'permission_callback' => '__return_true',
35 ];
36
37 register_rest_route( 'elasticpress/v1', 'related-posts/(?P<id>[0-9]+)', $args );
38 register_rest_route( 'wp/v2', 'posts/(?P<id>[0-9]+)/related', $args );
39 }
40
41
42 /**
43 * Get args schema.
44 *
45 * @return array
46 */
47 public function get_args() {
48 return [
49 'id' => [
50 'description' => __( 'ID of the post to get related posts for.', 'elasticpress' ),
51 'required' => true,
52 'type' => 'integer',
53 ],
54 'number' => [
55 'default' => 5,
56 'description' => __( 'Number of related posts to return.', 'elasticpress' ),
57 'required' => false,
58 'type' => 'integer',
59 ],
60 ];
61 }
62
63 /**
64 * Get posts.
65 *
66 * @param \WP_REST_Request $request Full details about the request.
67 * @return \WP_REST_Response
68 */
69 public function get_posts( \WP_REST_Request $request ) {
70 $id = $request->get_param( 'id' );
71 $number = $request->get_param( 'number' );
72 $posts = Features::factory()->get_registered_feature( 'related_posts' )->find_related( $id, $number );
73
74 $prepared_posts = [];
75
76 if ( ! empty( $posts ) ) {
77 foreach ( $posts as $post ) {
78 $prepared_post = [];
79
80 $prepared_post['id'] = $post->ID;
81 $prepared_post['link'] = get_permalink( $post->ID );
82 $prepared_post['status'] = $post->post_status;
83 $prepared_post['title'] = [
84 'raw' => $post->post_title,
85 'rendered' => get_the_title( $post->ID ),
86 ];
87 $prepared_post['author'] = (int) $post->post_author;
88 $prepared_post['parent'] = (int) $post->post_parent;
89 $prepared_post['menu_order'] = (int) $post->menu_order;
90 $prepared_post['content'] = [
91 'rendered' => post_password_required( $post ) ? '' : apply_filters( 'the_content', $post->post_content ),
92 ];
93 $prepared_post['date'] = $post->post_date;
94 $prepared_post['date_gmt'] = $post->post_date_gmt;
95 $prepared_post['modified'] = $post->post_modified;
96 $prepared_post['modified_gmt'] = $post->post_modified_gmt;
97
98 $prepared_posts[] = $prepared_post;
99 }
100 }
101
102 $response = new \WP_REST_Response();
103 $response->set_data( $prepared_posts );
104
105 return $response;
106 }
107 }
108