jetpack
/
_inc
/
lib
/
core-api
/
wpcom-endpoints
/
class-wpcom-rest-api-v2-endpoint-related-posts.php
business-hours.php
4 years ago
class-wpcom-rest-api-v2-endpoint-admin-menu.php
3 years ago
class-wpcom-rest-api-v2-endpoint-ai.php
2 years ago
class-wpcom-rest-api-v2-endpoint-external-media.php
2 years ago
class-wpcom-rest-api-v2-endpoint-following.php
2 years ago
class-wpcom-rest-api-v2-endpoint-google-docs.php
4 years ago
class-wpcom-rest-api-v2-endpoint-instagram-gallery.php
3 years ago
class-wpcom-rest-api-v2-endpoint-mailchimp.php
3 years ago
class-wpcom-rest-api-v2-endpoint-newsletter-categories-list.php
2 years ago
class-wpcom-rest-api-v2-endpoint-podcast-player.php
2 years ago
class-wpcom-rest-api-v2-endpoint-publicize-share-post.php
3 years ago
class-wpcom-rest-api-v2-endpoint-related-posts.php
2 years ago
class-wpcom-rest-api-v2-endpoint-resolve-redirect.php
2 years ago
class-wpcom-rest-api-v2-endpoint-search.php
4 years ago
class-wpcom-rest-api-v2-endpoint-send-email-preview.php
2 years ago
class-wpcom-rest-api-v2-endpoint-template-loader.php
3 years ago
class-wpcom-rest-api-v2-endpoint-transient.php
5 years ago
class-wpcom-rest-api-v2-endpoint-tweetstorm-gather.php
3 years ago
class-wpcom-rest-api-v2-endpoint-tweetstorm-parse.php
3 years ago
class-wpcom-rest-api-v3-endpoint-blogging-prompts.php
2 years ago
gutenberg-available-extensions.php
2 years ago
hello.php
4 years ago
memberships.php
2 years ago
publicize-connection-test-results.php
3 years ago
publicize-connections.php
2 years ago
publicize-services.php
3 years ago
service-api-keys.php
2 years ago
sites-posts-featured-media-url.php
4 years ago
subscribers.php
3 years ago
trait-wpcom-rest-api-proxy-request-trait.php
2 years ago
class-wpcom-rest-api-v2-endpoint-related-posts.php
99 lines
| 1 | <?php |
| 2 | /** |
| 3 | * REST API endpoint for Related Posts |
| 4 | * |
| 5 | * @package automattic/jetpack |
| 6 | * @since 12.6 |
| 7 | */ |
| 8 | |
| 9 | /** |
| 10 | * Class WPCOM_REST_API_V2_Endpoint_Related_Posts |
| 11 | */ |
| 12 | class WPCOM_REST_API_V2_Endpoint_Related_Posts extends WP_REST_Controller { |
| 13 | /** |
| 14 | * Constructor. |
| 15 | */ |
| 16 | public function __construct() { |
| 17 | $this->base_api_path = 'wpcom'; |
| 18 | $this->version = 'v2'; |
| 19 | $this->namespace = $this->base_api_path . '/' . $this->version; |
| 20 | $this->rest_base = '/related-posts'; |
| 21 | |
| 22 | add_action( 'rest_api_init', array( $this, 'register_routes' ) ); |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * Register routes. |
| 27 | */ |
| 28 | public function register_routes() { |
| 29 | register_rest_route( |
| 30 | $this->namespace, |
| 31 | $this->rest_base, |
| 32 | array( |
| 33 | 'show_in_index' => true, |
| 34 | 'methods' => WP_REST_Server::READABLE, |
| 35 | 'callback' => array( $this, 'get_options' ), |
| 36 | 'permission_callback' => function () { |
| 37 | return current_user_can( 'manage_options' ); |
| 38 | }, |
| 39 | ) |
| 40 | ); |
| 41 | |
| 42 | register_rest_route( |
| 43 | $this->namespace, |
| 44 | $this->rest_base . '/enable', |
| 45 | array( |
| 46 | 'show_in_index' => true, |
| 47 | 'methods' => WP_REST_Server::CREATABLE, |
| 48 | 'callback' => array( $this, 'enable_rp' ), |
| 49 | 'permission_callback' => function () { |
| 50 | return current_user_can( 'manage_options' ); |
| 51 | }, |
| 52 | ) |
| 53 | ); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Gets the site's Related Posts Options. |
| 58 | * |
| 59 | * @return WP_REST_Response Array of information about the site's Related Posts options. |
| 60 | * - enabled: Whether Related Posts is enabled. |
| 61 | * - options: Array of options for Related Posts. |
| 62 | */ |
| 63 | public function get_options() { |
| 64 | $options = Jetpack_Options::get_option( 'relatedposts', array() ); |
| 65 | $enabled = isset( $options['enabled'] ) ? (bool) $options['enabled'] : false; |
| 66 | |
| 67 | return rest_ensure_response( |
| 68 | array( |
| 69 | 'enabled' => $enabled, |
| 70 | 'options' => $options, |
| 71 | ) |
| 72 | ); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Enables the site's Related Posts. |
| 77 | * |
| 78 | * @param WP_REST_Request $request Full data about the request. |
| 79 | * |
| 80 | * @return WP_REST_Response Array confirming the new status. |
| 81 | */ |
| 82 | public function enable_rp( $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 83 | $old_relatedposts_options = Jetpack_Options::get_option( 'relatedposts', array() ); |
| 84 | $relatedposts_options_to_save = $old_relatedposts_options; |
| 85 | $relatedposts_options_to_save['enabled'] = true; |
| 86 | |
| 87 | // Enable Related Posts. |
| 88 | $enable = Jetpack_Options::update_option( 'relatedposts', $relatedposts_options_to_save ); |
| 89 | |
| 90 | return rest_ensure_response( |
| 91 | array( |
| 92 | 'enabled' => (bool) $enable, |
| 93 | ) |
| 94 | ); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Related_Posts' ); |
| 99 |