admin
2 months ago
compat
2 months ago
class-bwt-api-handler.php
4 months ago
class-bwt-connect.php
7 months ago
class-gsc-api-handler.php
4 months ago
class-gsc-connect.php
4 months ago
class-gsc-oauth-handler.php
7 months ago
class-secret.php
7 months ago
class-sitemap-core.php
7 months ago
class-sitemap-news.php
3 months ago
class-sitemap-plugin.php
3 months ago
class-sitemap.php
2 months ago
class-sitemaps-provider-custom.php
7 months ago
class-sitemaps-provider-external.php
7 months ago
class-sitemaps-provider-news.php
7 months ago
class-xmlsitemapfeed.php
3 months ago
functions-debugging.php
1 year ago
functions-pluggable.php
1 year ago
functions.php
3 months ago
translations.php
4 months ago
class-bwt-api-handler.php
170 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Bing Webmaster Tools API Handler |
| 4 | * |
| 5 | * @package XML Sitemap & Google News |
| 6 | */ |
| 7 | |
| 8 | namespace XMLSF; |
| 9 | |
| 10 | use WP_Error; |
| 11 | |
| 12 | /** |
| 13 | * Bing Webmaster Tools API Handler class |
| 14 | * |
| 15 | * @since 5.7 |
| 16 | */ |
| 17 | class BWT_API_Handler { |
| 18 | |
| 19 | /** |
| 20 | * Remote request to submit the sitemap to Bing Webmaster Tools using an OAuth Access Token. |
| 21 | * |
| 22 | * @param string $sitemap The sitemap URL. |
| 23 | * @param string $access_token The OAuth 2.0 access token. |
| 24 | * |
| 25 | * @return array|WP_Error An array containing the sitemap data, WP_Error on failure. |
| 26 | */ |
| 27 | public static function get( $sitemap, $access_token ) { |
| 28 | $api_endpoint = \add_query_arg( |
| 29 | array( |
| 30 | 'siteUrl' => \rawurlencode( \home_url() ), |
| 31 | 'feedUrl' => \rawurlencode( $sitemap ), |
| 32 | 'apikey' => $access_token, |
| 33 | ), |
| 34 | 'https://ssl.bing.com/webmaster/api.svc/json/GetFeedDetails' |
| 35 | ); |
| 36 | |
| 37 | $api_request_args = array( |
| 38 | 'method' => 'GET', |
| 39 | 'headers' => array( |
| 40 | 'Content-Type' => 'application/json', |
| 41 | 'Content-Length' => '0', // GET request with no body. |
| 42 | ), |
| 43 | 'timeout' => 15, |
| 44 | 'sslverify' => false, |
| 45 | 'httpversion' => '1.1', |
| 46 | ); |
| 47 | |
| 48 | // Send the request. |
| 49 | $api_response = \wp_remote_request( $api_endpoint, $api_request_args ); |
| 50 | |
| 51 | if ( \is_wp_error( $api_response ) ) { |
| 52 | return $api_response; |
| 53 | } |
| 54 | |
| 55 | $api_response_code = \wp_remote_retrieve_response_code( $api_response ); |
| 56 | $api_response_body = \wp_remote_retrieve_body( $api_response ); |
| 57 | |
| 58 | // Bing Webmaster Tools API returns 200 OK on successful submission request. |
| 59 | if ( 200 === $api_response_code ) { |
| 60 | return \json_decode( $api_response_body, true ); |
| 61 | } else { |
| 62 | // Handle API errors. |
| 63 | return new WP_Error( |
| 64 | 'bwt_api_error', |
| 65 | self::handle_api_errors( $api_response_code, $api_response_body ), |
| 66 | array( |
| 67 | 'status' => $api_response_code, |
| 68 | ) |
| 69 | ); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Remote request to submit the sitemap to Bing Webmaster Tools using an OAuth Access Token. |
| 75 | * |
| 76 | * @param string $sitemap The sitemap URL. |
| 77 | * @param string $access_token The OAuth 2.0 access token. |
| 78 | * |
| 79 | * @return true|WP_Error True on success, WP_Error on failure. |
| 80 | */ |
| 81 | public static function submit( $sitemap, $access_token ) { |
| 82 | $api_endpoint = \add_query_arg( |
| 83 | array( |
| 84 | 'apikey' => $access_token, |
| 85 | ), |
| 86 | 'https://ssl.bing.com/webmaster/api.svc/json/SubmitFeed', |
| 87 | ); |
| 88 | |
| 89 | $body = \wp_json_encode( |
| 90 | array( |
| 91 | 'siteUrl' => \home_url(), |
| 92 | 'feedUrl' => $sitemap, |
| 93 | ) |
| 94 | ); |
| 95 | |
| 96 | $api_request_args = array( |
| 97 | 'method' => 'POST', |
| 98 | 'headers' => array( |
| 99 | 'Content-Type' => 'application/json', |
| 100 | ), |
| 101 | 'timeout' => 15, |
| 102 | 'body' => $body, |
| 103 | 'data_format' => 'body', |
| 104 | 'sslverify' => false, |
| 105 | 'httpversion' => '1.1', |
| 106 | ); |
| 107 | |
| 108 | // Send the request. |
| 109 | $api_response = \wp_remote_request( $api_endpoint, $api_request_args ); |
| 110 | |
| 111 | if ( \is_wp_error( $api_response ) ) { |
| 112 | return $api_response; |
| 113 | } |
| 114 | |
| 115 | $api_response_code = \wp_remote_retrieve_response_code( $api_response ); |
| 116 | $api_response_body = \wp_remote_retrieve_body( $api_response ); |
| 117 | |
| 118 | // Bing Webmaster Tools API returns 200 OK on successful submission request. |
| 119 | if ( 200 === $api_response_code ) { |
| 120 | return true; |
| 121 | } else { |
| 122 | // Handle API errors. |
| 123 | return new WP_Error( |
| 124 | 'bwt_api_error', |
| 125 | self::handle_api_errors( $api_response_code, $api_response_body ), |
| 126 | array( |
| 127 | 'status' => $api_response_code, |
| 128 | ) |
| 129 | ); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Handle API Errors |
| 135 | * |
| 136 | * @param int $api_response_code The HTTP response code. |
| 137 | * @param string $api_response_body The response body. |
| 138 | * @return array An array containing the success status and a message. |
| 139 | */ |
| 140 | public static function handle_api_errors( $api_response_code, $api_response_body ) { |
| 141 | // Handle API errors. |
| 142 | $error_message = \__( 'Unknown API error.', 'sitemap-notifier' ); |
| 143 | $response_data = \json_decode( $api_response_body, true ); |
| 144 | |
| 145 | if ( isset( $response_data['Message'] ) ) { |
| 146 | $error_message = $response_data['Message']; |
| 147 | } elseif ( ! empty( $api_response_body ) ) { |
| 148 | $error_message = \wp_strip_all_tags( $api_response_body ); |
| 149 | } |
| 150 | |
| 151 | // Provide more specific guidance for common errors relevant to OAuth. |
| 152 | $detailed_error_message = \sprintf( |
| 153 | /* translators: %1$s: HTTP status code, %2$s: Error message from Google API */ |
| 154 | \__( 'Bing API Error (HTTP %1$s): %2$s', 'xml-sitemap-feed' ), |
| 155 | $api_response_code, |
| 156 | \esc_html( $error_message ) |
| 157 | ); |
| 158 | |
| 159 | if ( 401 === $api_response_code ) { |
| 160 | $detailed_error_message .= ' ' . \__( 'The API key is invalid or it was removed or replaced in your Bing Webmaster Tools account.', 'xml-sitemap-feed' ) . ' ' . \sprintf( /* translators: %s: Bing Webmaster Tools */ __( 'Please try reconnecting to %s.', 'xml-sitemap-feed' ), \esc_html__( 'Bing Webmaster Tools', 'xml-sitemap-feed' ) ); |
| 161 | } elseif ( 403 === $api_response_code ) { |
| 162 | $detailed_error_message .= ' ' . \__( 'Please ensure the connected Microsoft account has at least Read-Write access to the site property in Bing Webmaster Tools.', 'xml-sitemap-feed' ); |
| 163 | } elseif ( 404 === $api_response_code ) { |
| 164 | $detailed_error_message .= ' ' . \__( 'This usually means the site property URL used in the API call was not found or does not match the property verified in Bing Webmaster Tools.', 'xml-sitemap-feed' ); |
| 165 | } |
| 166 | |
| 167 | return $detailed_error_message; |
| 168 | } |
| 169 | } |
| 170 |