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
3 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
3 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
3 years ago
gutenberg-available-extensions.php
4 years ago
hello.php
4 years ago
memberships.php
3 years ago
publicize-connection-test-results.php
3 years ago
publicize-connections.php
3 years ago
publicize-services.php
3 years ago
service-api-keys.php
3 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
publicize-connection-test-results.php
142 lines
| 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 | /** |
| 3 | * Fetch information about Publicize connections on a site, including tests and connection status. |
| 4 | * |
| 5 | * @package automattic/jetpack |
| 6 | */ |
| 7 | |
| 8 | require_once __DIR__ . '/publicize-connections.php'; |
| 9 | |
| 10 | /** |
| 11 | * Publicize: List Connection Test Result Data |
| 12 | * |
| 13 | * All the same data as the Publicize Connections Endpoint, plus test results. |
| 14 | * |
| 15 | * @since 6.8 |
| 16 | */ |
| 17 | class WPCOM_REST_API_V2_Endpoint_List_Publicize_Connection_Test_Results extends WPCOM_REST_API_V2_Endpoint_List_Publicize_Connections { |
| 18 | /** |
| 19 | * Constructor. |
| 20 | */ |
| 21 | public function __construct() { |
| 22 | $this->namespace = 'wpcom/v2'; |
| 23 | $this->rest_base = 'publicize/connection-test-results'; |
| 24 | |
| 25 | add_action( 'rest_api_init', array( $this, 'register_routes' ) ); |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Called automatically on `rest_api_init()`. |
| 30 | */ |
| 31 | public function register_routes() { |
| 32 | register_rest_route( |
| 33 | $this->namespace, |
| 34 | '/' . $this->rest_base, |
| 35 | array( |
| 36 | array( |
| 37 | 'methods' => WP_REST_Server::READABLE, |
| 38 | 'callback' => array( $this, 'get_items' ), |
| 39 | 'permission_callback' => array( $this, 'get_items_permission_check' ), |
| 40 | ), |
| 41 | 'schema' => array( $this, 'get_public_item_schema' ), |
| 42 | ) |
| 43 | ); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Adds the test results properties to the Connection schema. |
| 48 | * |
| 49 | * @return array |
| 50 | */ |
| 51 | public function get_item_schema() { |
| 52 | $schema = array( |
| 53 | '$schema' => 'http://json-schema.org/draft-04/schema#', |
| 54 | 'title' => 'jetpack-publicize-connection-test-results', |
| 55 | 'type' => 'object', |
| 56 | 'properties' => $this->get_connection_schema_properties() + array( |
| 57 | 'test_success' => array( |
| 58 | 'description' => __( 'Did the Jetpack Social connection test pass?', 'jetpack' ), |
| 59 | 'type' => 'boolean', |
| 60 | ), |
| 61 | 'error_code' => array( |
| 62 | 'description' => __( 'Jetpack Social connection error code', 'jetpack' ), |
| 63 | 'type' => 'string', |
| 64 | ), |
| 65 | 'test_message' => array( |
| 66 | 'description' => __( 'Jetpack Social connection success or error message', 'jetpack' ), |
| 67 | 'type' => 'string', |
| 68 | ), |
| 69 | 'can_refresh' => array( |
| 70 | 'description' => __( 'Can the current user refresh the Jetpack Social connection?', 'jetpack' ), |
| 71 | 'type' => 'boolean', |
| 72 | ), |
| 73 | 'refresh_text' => array( |
| 74 | 'description' => __( 'Message instructing the user to refresh their Connection to the Jetpack Social service', 'jetpack' ), |
| 75 | 'type' => 'string', |
| 76 | ), |
| 77 | 'refresh_url' => array( |
| 78 | 'description' => __( 'URL for refreshing the Connection to the Jetpack Social service', 'jetpack' ), |
| 79 | 'type' => 'string', |
| 80 | 'format' => 'uri', |
| 81 | ), |
| 82 | ), |
| 83 | ); |
| 84 | |
| 85 | return $this->add_additional_fields_schema( $schema ); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Get list of Publicize Connections. |
| 90 | * |
| 91 | * @param WP_REST_Request $request Full details about the request. |
| 92 | * |
| 93 | * @see Publicize::get_publicize_conns_test_results() |
| 94 | * @return WP_REST_Response suitable for 1-page collection |
| 95 | */ |
| 96 | public function get_items( $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 97 | global $publicize; |
| 98 | |
| 99 | $items = $this->get_connections(); |
| 100 | |
| 101 | $test_results = $publicize->get_publicize_conns_test_results(); |
| 102 | $test_results_by_unique_id = array(); |
| 103 | foreach ( $test_results as $test_result ) { |
| 104 | $test_results_by_unique_id[ $test_result['connectionID'] ] = $test_result; |
| 105 | } |
| 106 | |
| 107 | $mapping = array( |
| 108 | 'test_success' => 'connectionTestPassed', |
| 109 | 'test_message' => 'connectionTestMessage', |
| 110 | 'error_code' => 'connectionTestErrorCode', |
| 111 | 'can_refresh' => 'userCanRefresh', |
| 112 | 'refresh_text' => 'refreshText', |
| 113 | 'refresh_url' => 'refreshURL', |
| 114 | 'connection_id' => 'connectionID', |
| 115 | ); |
| 116 | |
| 117 | foreach ( $items as &$item ) { |
| 118 | $test_result = $test_results_by_unique_id[ $item['connection_id'] ]; |
| 119 | |
| 120 | foreach ( $mapping as $field => $test_result_field ) { |
| 121 | $item[ $field ] = $test_result[ $test_result_field ]; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | if ( |
| 126 | isset( $item['id'] ) |
| 127 | && 'linkedin' === $item['id'] |
| 128 | && 'must_reauth' === $test_result['connectionTestPassed'] |
| 129 | ) { |
| 130 | $item['test_success'] = 'must_reauth'; |
| 131 | } |
| 132 | |
| 133 | $response = rest_ensure_response( $items ); |
| 134 | |
| 135 | $response->header( 'X-WP-Total', count( $items ) ); |
| 136 | $response->header( 'X-WP-TotalPages', 1 ); |
| 137 | |
| 138 | return $response; |
| 139 | } |
| 140 | } |
| 141 | wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_List_Publicize_Connection_Test_Results' ); |
| 142 |