PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3
Jetpack – WP Security, Backup, Speed, & Growth v16.3
16.3 16.3-beta 16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 All 508 releases
← All changes | _inc/lib/core-api/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-related-posts.php +190 -0 16.2-beta → 16.3 View file →
@@ -1,0 +1,190 @@
1 +<?php
2 +/**
3 + * REST API endpoint for Related Posts
4 + *
5 + * @package automattic/jetpack
6 + * @since 12.6
7 + */
8 +
9 +if ( ! defined( 'ABSPATH' ) ) {
10 + exit( 0 );
11 +}
12 +
13 +/**
14 + * Class WPCOM_REST_API_V2_Endpoint_Related_Posts
15 + */
16 +class WPCOM_REST_API_V2_Endpoint_Related_Posts extends WP_REST_Controller {
17 + /**
18 + * Constructor.
19 + */
20 + public function __construct() {
21 + $this->base_api_path = 'wpcom';
22 + $this->version = 'v2';
23 + $this->namespace = $this->base_api_path . '/' . $this->version;
24 + $this->rest_base = '/related-posts';
25 +
26 + add_action( 'rest_api_init', array( $this, 'register_routes' ) );
27 + }
28 +
29 + /**
30 + * Register routes.
31 + */
32 + public function register_routes() {
33 + register_rest_route(
34 + $this->namespace,
35 + $this->rest_base,
36 + array(
37 + 'show_in_index' => true,
38 + 'methods' => WP_REST_Server::READABLE,
39 + 'callback' => array( $this, 'get_options' ),
40 + 'permission_callback' => function () {
41 + return current_user_can( 'edit_posts' );
42 + },
43 + )
44 + );
45 +
46 + register_rest_route(
47 + $this->namespace,
48 + $this->rest_base . '/enable',
49 + array(
50 + 'show_in_index' => true,
51 + 'methods' => WP_REST_Server::CREATABLE,
52 + 'callback' => array( $this, 'enable_rp' ),
53 + 'permission_callback' => function () {
54 + return current_user_can( 'manage_options' );
55 + },
56 + )
57 + );
58 +
59 + register_rest_route(
60 + $this->namespace,
61 + $this->rest_base . '/(?P<id>[\d]+)',
62 + array(
63 + 'args' => array(
64 + 'id' => array(
65 + 'description' => __( 'Unique identifier for the post.', 'jetpack' ),
66 + 'type' => 'integer',
67 + ),
68 + ),
69 + array(
70 + 'show_in_index' => true,
71 + 'methods' => WP_REST_Server::READABLE,
72 + 'callback' => array( $this, 'get_related_posts' ),
73 + 'permission_callback' => array( $this, 'get_related_posts_permissions_check' ),
74 + ),
75 + )
76 + );
77 + }
78 +
79 + /**
80 + * Gets the site's Related Posts Options.
81 + *
82 + * @return WP_REST_Response Array of information about the site's Related Posts options.
83 + * - enabled: Whether Related Posts is enabled.
84 + * - options: Array of options for Related Posts.
85 + */
86 + public function get_options() {
87 + $options = Jetpack_Options::get_option( 'relatedposts', array() );
88 + $enabled = isset( $options['enabled'] ) ? (bool) $options['enabled'] : false;
89 +
90 + return rest_ensure_response(
91 + array(
92 + 'enabled' => $enabled,
93 + 'options' => $options,
94 + )
95 + );
96 + }
97 +
98 + /**
99 + * Enables the site's Related Posts.
100 + *
101 + * @param WP_REST_Request $request Full data about the request.
102 + *
103 + * @return WP_REST_Response Array confirming the new status.
104 + */
105 + public function enable_rp( $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
106 + $old_relatedposts_options = Jetpack_Options::get_option( 'relatedposts', array() );
107 + $relatedposts_options_to_save = $old_relatedposts_options;
108 + $relatedposts_options_to_save['enabled'] = true;
109 +
110 + // Enable Related Posts.
111 + $enable = Jetpack_Options::update_option( 'relatedposts', $relatedposts_options_to_save );
112 +
113 + return rest_ensure_response(
114 + array(
115 + 'enabled' => (bool) $enable,
116 + )
117 + );
118 + }
119 +
120 + /**
121 + * Checks if a given request has access to get the related posts.
122 + *
123 + * @since 4.7.0
124 + *
125 + * @param WP_REST_Request $request Full details about the request.
126 + * @return bool|WP_Error True if the request has read access for the related posts, WP_Error object or false otherwise.
127 + */
128 + public function get_related_posts_permissions_check( $request ) {
129 + $post = $this->get_post( $request['id'] );
130 + if ( is_wp_error( $post ) ) {
131 + return $post;
132 + }
133 +
134 + if ( ! current_user_can( 'edit_post', $post->ID ) ) {
135 + return new WP_Error(
136 + 'rest_forbidden_context',
137 + __( 'Sorry, you are not allowed to get the related post.', 'jetpack' ),
138 + array( 'status' => rest_authorization_required_code() )
139 + );
140 + }
141 +
142 + return true;
143 + }
144 +
145 + /**
146 + * Get the related posts
147 + *
148 + * @param WP_REST_Request $request Full data about the request.
149 + * @return WP_REST_Response Array The related posts
150 + */
151 + public function get_related_posts( $request ) {
152 + $post = $this->get_post( $request['id'] );
153 + if ( is_wp_error( $post ) ) {
154 + return $post;
155 + }
156 +
157 + if ( ! class_exists( 'Jetpack_RelatedPosts' ) ) {
158 + require_once JETPACK__PLUGIN_DIR . 'modules/related-posts/jetpack-related-posts.php';
159 + }
160 + $related_posts = \Jetpack_RelatedPosts::init()->get_for_post_id( $post->ID, array( 'size' => 6 ) );
161 + return rest_ensure_response( $related_posts );
162 + }
163 +
164 + /**
165 + * Gets the post, if the ID is valid.
166 + *
167 + * @param int $id Supplied ID.
168 + * @return WP_Post|WP_Error Post object if ID is valid, WP_Error otherwise.
169 + */
170 + public function get_post( $id ) {
171 + $error = new WP_Error(
172 + 'rest_post_invalid_id',
173 + __( 'Invalid post ID.', 'jetpack' ),
174 + array( 'status' => 404 )
175 + );
176 +
177 + if ( (int) $id <= 0 ) {
178 + return $error;
179 + }
180 +
181 + $post = get_post( (int) $id );
182 + if ( empty( $post ) || empty( $post->ID ) ) {
183 + return $error;
184 + }
185 +
186 + return $post;
187 + }
188 +}
189 +
190 +wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Related_Posts' );