%PDF-
%PDF-
Mini Shell
Mini Shell
<?php
namespace WPaaS;
use \WP_Error;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
interface API_Interface {
public function get_disallowed_plugins();
public function is_valid_sso_hash( $hash );
public function user_changed_domain( $domain = '' );
public function get_woocommerce_products( $product_type );
public function refresh_blog_title( $blogname = null );
public function refresh_nextgen_compatibility( $is_nextgen_compat = false );
public function toggle_rum( $enabled = true );
public function flush_cdn();
public function get_failed_smart_updates();
public function smart_update_notice_dismiss( $dismiss_id );
public function hmt_register( $key );
public function get_experiment( $exp_name );
}
final class API implements API_Interface {
/**
* How long a failed disallowed-plugins fetch is remembered, in seconds.
*
* Short on purpose, and much shorter than the success TTL. An empty list is not a
* neutral value here: Disallowed_Plugins iterates it, so "no list" and "nothing is
* disallowed" are the same thing, and the plugins this exists to keep off the
* platform activate freely for as long as it is cached. An hour of that per failed
* request is too long; five minutes is the same trade the subscription-plans fetch
* makes.
*
* @since 4.302.0
*
* @return int
*/
private function disallowed_plugins_error_ttl() {
return 300;
}
/**
* Return an array of disallowed plugins.
*
* Note: The transient used here is persistent, meaning it
* will not be short-circuited by object cache and it will
* always be set to a non-false value regardless of the API
* response.
*
* @return array
*/
public function get_disallowed_plugins() {
$transient = Plugin::get_persistent_site_transient( 'gd_system_disallowed_plugins' );
if ( false !== $transient ) {
return (array) $transient;
}
add_filter( 'wpaas_api_url', [ $this, 'wp_public_api_url' ] );
$response = $this->call( 'disallowPlugins/' );
remove_filter( 'wpaas_api_url', [ $this, 'wp_public_api_url' ] );
if ( is_wp_error( $response ) || 200 !== (int) wp_remote_retrieve_response_code( $response ) ) {
// Previously indistinguishable from an empty list, and cached for an hour:
// wp_remote_retrieve_body() of a WP_Error is '', which json_decode()s to
// null. Fail closed for minutes instead.
Plugin::set_persistent_site_transient( 'gd_system_disallowed_plugins', [], $this->disallowed_plugins_error_ttl() );
return [];
}
$body = json_decode( wp_remote_retrieve_body( $response ), true );
if ( ! is_array( $body ) ) {
// A 200 carrying a truncated or HTML body is a failed fetch, not an empty
// allow-list, and must expire on the same short clock as the branch above.
Plugin::set_persistent_site_transient( 'gd_system_disallowed_plugins', [], $this->disallowed_plugins_error_ttl() );
return [];
}
$disallowed_plugins = ! empty( $body['disallowPlugins'] ) ? (array) $body['disallowPlugins'] : [];
Plugin::set_persistent_site_transient( 'gd_system_disallowed_plugins', $disallowed_plugins, HOUR_IN_SECONDS );
return $disallowed_plugins;
}
/**
* Validate an SSO hash.
*
* @param string $hash
*
* @return bool
*/
public function is_valid_sso_hash( $hash ) {
// sso_api_url(), not wp_public_api_url(): on Managed WordPress the token is
// redeemed by hosting. Falls back to the wpsecurity base when the platform
// has not delivered GD_SSO_API_URL, which is every site today.
add_filter( 'wpaas_api_url', [ $this, 'sso_api_url' ] );
add_filter( 'wpaas_api_http_post_body_json', '__return_true' );
$method_args = [
'token' => $hash,
'database' => DB_NAME
];
$response = $this->call(
$this->sso_allow_login_path(),
$method_args,
'POST'
);
$body = json_decode( wp_remote_retrieve_body( $response ), true );
remove_filter( 'wpaas_api_url', [ $this, 'sso_api_url' ] );
remove_filter( 'wpaas_api_http_post_body_json', '__return_true' );
return isset( $body['allow'] ) ? $body['allow'] : false;
}
/**
* Check if a user has changed their domain.
*
* It isn't reflected here yet because we're waiting on the
* DNS TTL to take effect.
*
* Note: The transient used here is persistent, meaning it
* will not be short-circuited by object cache and it will
* always be set to a non-false value regardless of the API
* response.
*
* @param string $cname (optional)
*
* @return bool
*/
public function user_changed_domain( $cname = '' ) {
$transient = Plugin::get_persistent_site_transient( 'gd_system_domain_changed' );
if ( false !== $transient ) {
return (
1 === (int) $transient
||
'Y' === $transient // Back compat
);
}
$cname = ( $cname ) ? $cname : Plugin::domain();
add_filter( 'wpaas_api_url', [ $this, 'wp_public_api_url' ] );
$response = $this->call( 'domains/' . $cname );
$body = json_decode( wp_remote_retrieve_body( $response ), true );
$changed = ! empty( $body['domainChanged'] ) ? 1 : 0;
$timeout = 300;
Plugin::set_persistent_site_transient( 'gd_system_domain_changed', $changed, absint( $timeout ) );
remove_filter( 'wpaas_api_url', [ $this, 'wp_public_api_url' ] );
return ( 1 === $changed );
}
/**
* Retreive WooCommerce product
*
* @param string $product_type The type of product to achieve.
*
* @return array $products The retreived products from the WooCommerce API.
*/
public function get_woocommerce_products( $product_type ) {
$product_cache = get_transient( 'wpaas_woocommerce_' . $product_type );
if ( ! WP_DEBUG && false !== $product_cache ) {
return $product_cache;
}
add_filter( 'wpaas_api_url', [ $this, 'wp_public_api_url' ] );
add_filter( 'wpaas_api_http_args', [ $this, 'v3_api_http_args' ] );
$request = $this->call( $this->wp_public_api_accountuid( "automattic/%s/woocommerce/info" ) );
remove_filter( 'wpaas_api_url', [ $this, 'wp_public_api_url' ] );
remove_filter( 'wpaas_api_http_args', [ $this, 'v3_api_http_args' ] );
if ( 200 !== wp_remote_retrieve_response_code( $request ) || is_wp_error( $request ) ) {
set_transient( 'wpaas_woocommerce_' . $product_type, [], 15 * MINUTE_IN_SECONDS );
return [];
}
$products = json_decode( wp_remote_retrieve_body( $request ), true );
if ( empty( $products ) || ! is_array( $products ) || ! isset( $products['products'] ) ) {
set_transient( 'wpaas_woocommerce_' . $product_type, [], 15 * MINUTE_IN_SECONDS );
return [];
}
$type = 'extensions' === $product_type ? 'plugin' : 'theme';
// Return the proper product type only.
$products = array_filter(
$products['products'],
function ( $extension ) use ( $type ) {
return $type === $extension['type'];
}
);
set_transient( 'wpaas_woocommerce_' . $product_type, $products, 8 * HOUR_IN_SECONDS );
return $products;
}
/**
* Request that the API refresh its copy of the blog title.
*
* @param string $blogname (optional)
*
* @return void
*/
public function refresh_blog_title( $blogname = null ) {
add_filter( 'wpaas_api_url', [ $this, 'wp_public_api_url' ] );
add_filter( 'wpaas_api_http_args', [ $this, 'v3_api_http_args' ] );
add_filter( 'wpaas_api_http_args', [ $this, 'non_blocking_http_args' ] );
add_filter( 'wpaas_api_http_post_body_json', '__return_true' );
$method_args = $blogname ? [ 'blogname' => htmlspecialchars_decode( $blogname, ENT_QUOTES ) ] : [];
$this->call( $this->wp_public_api_accountuid( "v3-proxy/%s/refreshBlogTitle" ), $method_args, 'POST' );
remove_filter( 'wpaas_api_url', [ $this, 'wp_public_api_url' ] );
remove_filter( 'wpaas_api_http_args', [ $this, 'v3_api_http_args' ] );
remove_filter( 'wpaas_api_http_args', [ $this, 'non_blocking_http_args' ] );
remove_filter( 'wpaas_api_http_post_body_json', '__return_true' );
}
/**
* Request that the API refresh wether or not the site is nextgen compatible.
*
* @param boolean $is_nextgen_compat (optional)
*
* @return void
*/
public function refresh_nextgen_compatibility( $is_nextgen_compat = false ) {
add_filter( 'wpaas_api_url', [ $this, 'wp_public_api_url' ] );
add_filter( 'wpaas_api_http_args', [ $this, 'v3_api_http_args' ] );
add_filter( 'wpaas_api_http_args', [ $this, 'non_blocking_http_args' ] );
add_filter( 'wpaas_api_http_post_body_json', '__return_true' );
$method_args = [ 'nextgenEnabled' => (bool) $is_nextgen_compat ];
$this->call( $this->wp_public_api_accountuid( "v3-proxy/%s/nextgen" ), $method_args, 'POST' );
remove_filter( 'wpaas_api_url', [ $this, 'wp_public_api_url' ] );
remove_filter( 'wpaas_api_http_args', [ $this, 'v3_api_http_args' ] );
remove_filter( 'wpaas_api_http_args', [ $this, 'non_blocking_http_args' ] );
remove_filter( 'wpaas_api_http_post_body_json', '__return_true' );
}
/**
* Send RAD data to the API.
*
* @param string $name
* @param array $metadata (optional)
*
* @return void
*/
public function log_rad_event( $name, $metadata = [] ) {
add_filter( 'wpaas_api_url', [ $this, 'wp_public_api_url' ] );
add_filter( 'wpaas_api_http_args', [ $this, 'v3_api_http_args' ] );
add_filter( 'wpaas_api_http_args', [ $this, 'non_blocking_http_args' ] );
add_filter( 'wpaas_api_http_post_body_json', '__return_true' );
$method_args = [
'datetime' => current_time( 'mysql', 1 ),
'metadata' => (array) $metadata,
'name' => $name,
];
$this->call( $this->wp_public_api_accountuid( "rad/%s/event" ), $method_args, 'POST' );
remove_filter( 'wpaas_api_url', [ $this, 'wp_public_api_url' ] );
remove_filter( 'wpaas_api_http_args', [ $this, 'v3_api_http_args' ] );
remove_filter( 'wpaas_api_http_args', [ $this, 'non_blocking_http_args' ] );
remove_filter( 'wpaas_api_http_post_body_json', '__return_true' );
}
/**
* Toggle RUM for this site.
*
* @param bool $enabled True or false
*
* @return void
*/
public function toggle_rum( $enabled = true ) {
add_filter( 'wpaas_api_url', [ $this, 'wp_public_api_url' ] );
add_filter( 'wpaas_api_http_args', [ $this, 'v3_api_http_args' ] );
add_filter( 'wpaas_api_http_args', [ $this, 'non_blocking_http_args' ] );
add_filter( 'wpaas_api_http_post_body_json', '__return_true' );
$method_args = [
'rumEnabled' => (bool) $enabled,
];
$this->call( $this->wp_public_api_accountuid( "v3-proxy/%s/rum" ), $method_args, 'POST' );
remove_filter( 'wpaas_api_url', [ $this, 'wp_public_api_url' ] );
remove_filter( 'wpaas_api_http_args', [ $this, 'v3_api_http_args' ] );
remove_filter( 'wpaas_api_http_args', [ $this, 'non_blocking_http_args' ] );
remove_filter( 'wpaas_api_http_post_body_json', '__return_true' );
}
/**
* Flush full page cdn
*
* On the site-signed lane the purge is requested from the hosting control plane with
* this app's own key, so the CDN credential never has to be reachable from a customer
* container. The returned id is a hosting request id rather than a CloudFront
* invalidation id; both are opaque to every caller, which is why the option and the
* poller need no change. An id minted by one lane and polled by the other reads as
* "not found", which clears the stored id and stops the poll.
*
* @return string|null
*/
public function flush_cdn() {
if ( Hosting_API::is_available() ) {
return Hosting_API::request_cache_flush();
}
add_filter( 'wpaas_api_url', [ $this, 'wp_public_api_url' ] );
add_filter( 'wpaas_api_http_args', [ $this, 'v3_api_http_args' ] );
$accountUid = defined( 'GD_ACCOUNT_UID' ) ? GD_ACCOUNT_UID : '';
try {
$response = $this->call( 'cdn/cache/' . $accountUid, [], 'DELETE' );
$body = json_decode( wp_remote_retrieve_body( $response ), true );
$invalidationId = ! empty( $body['invalidationId'] ) ? $body['invalidationId'] : null;
} catch ( WP_Error $e ) {
return null;
}
remove_filter( 'wpaas_api_url', [ $this, 'wp_public_api_url' ] );
remove_filter( 'wpaas_api_http_args', [ $this, 'v3_api_http_args' ] );
return $invalidationId;
}
/**
* Get CDN cache clear status
*
* @returns string|null
*/
public function flush_cache_cdn_status( $invalidation_id ) {
if ( Hosting_API::is_available() ) {
return Hosting_API::cache_flush_status( $invalidation_id );
}
add_filter( 'wpaas_api_url', [ $this, 'wp_public_api_url' ] );
add_filter( 'wpaas_api_http_args', [ $this, 'v3_api_http_args' ] );
try {
$accountUid = defined( 'GD_ACCOUNT_UID' ) ? GD_ACCOUNT_UID : '';
$response = $this->call( 'cdn/' . $accountUid . '/cache/' . $invalidation_id );
$body = json_decode( wp_remote_retrieve_body( $response ), true );
} catch ( WP_Error $e ) {
return null;
}
remove_filter( 'wpaas_api_url', [ $this, 'wp_public_api_url' ] );
remove_filter( 'wpaas_api_http_args', [ $this, 'v3_api_http_args' ] );
return $body;
}
/**
* Get feature flags
*
* @param string $api_url
*
* @returns array|null
*/
public function fetch_feature_flag( $api_url ) {
add_filter( 'wpaas_api_url', [ $this, 'wp_public_api_url' ] );
add_filter( 'wpaas_api_http_args', [ $this, 'v3_api_http_args' ] );
add_filter( 'wpaas_api_http_args', [ $this, 'timeout_3s_http_args' ] );
/** Logg user action */
$GLOBALS['wpaas_activity_logger']->log_sp_action( get_current_user_id(), 'Fetch feature flags from API' );
try {
$response = $this->call( $api_url );
$body = json_decode( wp_remote_retrieve_body( $response ), true );
} catch ( WP_Error $e ) {
return null;
}
remove_filter( 'wpaas_api_url', [ $this, 'wp_public_api_url' ] );
remove_filter( 'wpaas_api_http_args', [ $this, 'v3_api_http_args' ] );
remove_filter( 'wpaas_api_http_args', [ $this, 'timeout_3s_http_args' ] );
return $body;
}
/**
* Filter the API URL when using the V3 API.
*
* @param string $api_url
*
* @return string
*/
public function v3_api_url( $api_url ) {
if ( Plugin::is_mhp() ) {
// MHP has no V3 host (mwp.api.phx3.godaddy.com is decommissioned). Resolve
// to empty so API::call() bails at its wpaas_api_url guard with a WP_Error
// and never opens a connection. Empty string, never null — see the note
// below on poisoning later wpaas_api_url callbacks.
return '';
}
if ( ! defined( 'GD_ACCOUNT_UID' ) || ! GD_ACCOUNT_UID ) {
// Hand back what we were given. This is a filter callback, and returning
// null from one replaces the value for every later callback too.
return $api_url;
}
$env = Plugin::get_env();
$prefix = ( 'prod' === $env ) ? '' : "{$env}-";
return sprintf(
"https://mwp.api.phx3.{$prefix}godaddy.com/api/v1/mwp/sites/%s/",
GD_ACCOUNT_UID
);
}
/**
* Filter the API URL when using the WP Public API.
*
* @return string
*/
public function wp_public_api_url() {
if ( Plugin::is_mhp() ) {
// MHP has no wpsecurity Public API host (wp-api.wpsecurity.godaddy.com is
// decommissioned). This base is shared by disallowPlugins/, domain-change
// checks, smart-updates, feedback and more; resolving to empty makes every
// one of them bail at API::call()'s wpaas_api_url guard with a WP_Error
// instead of timing out against a dead host. SSO validation is unaffected —
// it resolves through sso_api_url() to the delivered platform base.
return '';
}
$env = Plugin::get_env();
if ( in_array( $env, array( 'myh.test' ) ) ) {
$env = 'test';
}
if ( in_array( $env, [ 'dev', 'test' ], true ) ) {
return "https://wp-api.wpsecurity.{$env}-godaddy.com/api/v1/";
}
return "https://wp-api.wpsecurity.godaddy.com/api/v1/";
}
/**
* Filter the API URL for the SSO login-token validation call.
*
* SSO validation is the one API call whose base URL is per-platform rather
* than per-environment. On Managed WordPress the login token is minted and
* redeemed by the hosting control plane rather than by wpsecurity, and the
* site is told where that is by the `GD_SSO_API_URL` constant delivered in
* `gd-config.php`.
*
* Deliberately NOT a repoint of wp_public_api_url(): that base is shared with
* disallowPlugins/, domain-change checks, RUM, CDN flushes and more, all of
* which still belong to wpsecurity on every platform.
*
* With nothing usable delivered this returns the wpsecurity base, so a site that
* has never heard of GD_SSO_API_URL validates exactly where it does today. That
* is what makes this safe to ship ahead of the control-plane side.
*
* @return string
*/
public function sso_api_url() {
$url = self::delivered_sso_api_url();
return $url ? $url : $this->wp_public_api_url();
}
/**
* The SSO validation base URL delivered by the platform, if any.
*
* Returns an empty string when the platform has not delivered a usable one,
* which is every site today. That emptiness is the one platform decision in
* SSO: sso_api_url(), sso_allow_login_path() and the entry hop in SSO all read
* this, so no two of them can disagree about which control plane a site is
* talking to.
*
* Not filterable, and not trusted on shape either. This is where a login token
* is redeemed, so whatever answers it decides whether a login is allowed; the
* constant is absent across the legacy estate, and an absent constant is
* definable by site code. A delivered value is therefore held to the shape a
* platform endpoint has — https, a hostname rather than an address literal, no
* credentials, default port — and anything else reads as not delivered, which
* leaves both hops exactly where they are today.
*
* @return string
*/
public static function delivered_sso_api_url() {
$url = defined( 'GD_SSO_API_URL' ) ? trim( (string) GD_SSO_API_URL ) : '';
if ( '' === $url || ! self::is_platform_endpoint( $url ) ) {
return '';
}
// call() joins this base to the endpoint path, so normalize it here rather
// than trusting the delivered value to carry a trailing slash.
return trailingslashit( $url );
}
/**
* Whether a delivered base has the shape of a platform API endpoint.
*
* Public because Hosting_API holds GD_HOSTING_API_URL to the same bound; both are
* delivered constants naming a control plane, so they must not disagree on shape.
*
* A bound on the damage, not an authorization: this rejects the shapes that
* turn a defined-by-accident constant into something worse than a broken login
* — cleartext, credentials in the authority, a non-default port, and an address
* literal, which is how the link-local metadata service and everything else
* inside the network gets named. A control plane renders a hostname.
*
* A query or fragment is rejected too. call() appends the endpoint path to this
* base, so either one can only produce a URL that is not the endpoint it looks
* like — `?x=1` becomes `?x=1sso/allow-login/`.
*
* It deliberately does not allow-list hosts. The hosting API host differs by
* environment and by cell, so a list maintained here would eventually be wrong
* about production and break SSO for every site at once — a worse outcome than
* the one it guards against, given that anything able to define this constant
* is already running in the process and can call wp_set_auth_cookie() directly.
*
* @param string $url
*
* @return bool
*/
public static function is_platform_endpoint( $url ) {
$parts = parse_url( $url ); // phpcs:ignore WordPress.WP.AlternativeFunctions
if ( ! is_array( $parts ) || empty( $parts['scheme'] ) || empty( $parts['host'] ) ) {
return false;
}
if ( 'https' !== strtolower( $parts['scheme'] ) ) {
return false;
}
if ( isset( $parts['user'] ) || isset( $parts['pass'] ) ) {
return false;
}
if ( isset( $parts['port'] ) && 443 !== (int) $parts['port'] ) {
return false;
}
if ( isset( $parts['query'] ) || isset( $parts['fragment'] ) ) {
return false;
}
return ! filter_var( trim( $parts['host'], '[]' ), FILTER_VALIDATE_IP );
}
/**
* The SSO validation endpoint path, which is per-platform like the base above.
*
* wpsecurity has served `sso/allowLogin/` for years and is not being renamed,
* so the legacy path is kept verbatim for every site that has no delivered
* base. Hosting is new and serves the kebab-case `sso/allow-login/`, matching
* the rest of its route surface.
*
* @return string
*/
private function sso_allow_login_path() {
return self::delivered_sso_api_url() ? 'sso/allow-login/' : 'sso/allowLogin/';
}
/**
* Filter the HTTP request args when using the V3 API.
*
* @param array $http_args
*
* @return array
*/
public function v3_api_http_args( $http_args ) {
$body = apply_filters( 'wpaas_v3_api_body_signage', [] );
$http_args['headers'] = array_merge( $http_args['headers'], Plugin::sign_http_request( wp_json_encode( $body ) ) );
return $http_args;
}
/**
* Filter the HTTP request args to use a 30s timeout.
*
* @param array $http_args
*
* @return array
*/
public function timeout_30s_http_args( $http_args ) {
$http_args['timeout'] = 30;
return $http_args;
}
/**
* Filter the HTTP request args to use a 3s timeout.
*
* @param array $http_args
*
* @return array
*/
public function timeout_3s_http_args( $http_args ) {
$http_args['timeout'] = 3;
return $http_args;
}
/**
* Filter the HTTP request args to make calls non-blocking.
*
* @param array $http_args
*
* @return array
*/
public function non_blocking_http_args( $http_args ) {
$http_args['blocking'] = false;
$http_args['timeout'] = 5;
return $http_args;
}
/**
* Make an API call.
*
* @param string $method
* @param array|string $method_args (optional)
* @param string $http_verb (optional)
*
* @return array|WP_Error
*/
private function call( $method, $method_args = [], $http_verb = 'GET' ) {
$api_url = (string) apply_filters( 'wpaas_api_url', '' );
if ( ! $api_url ) {
/** Logg user action */
$GLOBALS['wpaas_activity_logger']->log_sp_action( get_current_user_id(), sprintf( 'API call fail URL: %s', $api_url ) );
return new WP_Error( 'wpaas_api_url_not_found' );
}
add_filter( 'wpaas_v3_api_body_signage', function () use ( $method_args ) {
return $method_args;
} );
$http_args = (array) apply_filters(
'wpaas_api_http_args',
[
'headers' => [
'Content-Type' => 'application/json',
],
]
);
$url = trailingslashit( $api_url ) . $method;
$retries = 0;
$max_retries = 1;
while ( $retries <= $max_retries ) {
$retries ++;
switch ( $http_verb ) {
case 'GET':
if ( ! empty( $method_args ) ) {
$url .= '?' . build_query( $method_args );
}
$response = Plugin::remote_get( $url, $http_args );
break;
case 'POST':
$http_args['body'] = (bool) apply_filters( 'wpaas_api_http_post_body_json', false ) ? json_encode( $method_args, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ) : $method_args;
$response = Plugin::remote_post( $url, $http_args );
break;
case "DELETE":
$http_args['body'] = (bool) apply_filters( 'wpaas_api_http_post_body_json', false ) ? json_encode( $method_args, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ) : $method_args;
$response = Plugin::remote_request( $url, array_merge( $http_args, [
'method' => 'DELETE'
] ) );
break;
default:
return new WP_Error( 'wpaas_api_invalid_http_verb' );
}
$response_code = wp_remote_retrieve_response_code( $response );
// Check if we aren't on the last iteration and we can try the request again
if (
$retries <= $max_retries
&&
$this->is_retryable( $response, $response_code )
) {
// Give some time for the API to recover
sleep( (int) apply_filters( 'wpaas_api_retry_delay', 1 ) );
continue;
}
break;
}
if ( 200 !== $response_code && 204 !== $response_code ) {
/** Logg user action */
$GLOBALS['wpaas_activity_logger']->log_sp_action( get_current_user_id(),
sprintf( 'API call fail URL: %s status_code: %s', $url, $response_code )
);
return new WP_Error( 'wpaas_api_bad_status', $response_code );
}
/** Logg user action */
$GLOBALS['wpaas_activity_logger']->log_sp_action( get_current_user_id(),
sprintf( 'API call success URL: %s status_code: %s', $url, $response_code )
);
return $response;
}
/**
* Check if a response is an error and retryable.
*
* @param array|WP_Error $response
* @param int $response_code
*
* @return bool
*/
private function is_retryable( $response, $response_code ) {
if ( 200 === $response_code ) {
return false;
}
$body = json_decode( wp_remote_retrieve_body( $response ), true );
if (
isset( $body['status'], $body['type'], $body['code'] )
&&
503 === absint( $body['status'] )
&&
'error' === $body['type']
&&
'RetryRequest' === $body['code']
) {
return true;
}
return false;
}
public function get_failed_smart_updates() {
add_filter( 'wpaas_api_url', [ $this, 'wp_public_api_url' ] );
add_filter( 'wpaas_api_http_args', [ $this, 'v3_api_http_args' ] );
$response = $this->call( 'smart-updates' );
if ( is_wp_error( $response ) ) {
return [];
}
$body = json_decode( wp_remote_retrieve_body( $response ), true );
remove_filter( 'wpaas_api_url', [ $this, 'wp_public_api_url' ] );
remove_filter( 'wpaas_api_http_args', [ $this, 'v3_api_http_args' ] );
return $body;
}
public function get_trusted_site_sso_url() {
add_filter( 'wpaas_api_url', [ $this, 'wp_public_api_url' ] );
add_filter( 'wpaas_api_http_args', [ $this, 'v3_api_http_args' ] );
$response = $this->call( $this->wp_public_api_accountuid( "v3-proxy/%s/trustedsite" ) );
// Both filters come off before anything can return. The WP_Error branch used to
// return with them still attached, which repoints every later wpaas_api_url
// consumer in the request at the public API base and signs it for v3 — and that
// is the branch a platform with no v3-proxy route takes on every call.
remove_filter( 'wpaas_api_url', [ $this, 'wp_public_api_url' ] );
remove_filter( 'wpaas_api_http_args', [ $this, 'v3_api_http_args' ] );
if ( is_wp_error( $response ) || 200 !== (int) wp_remote_retrieve_response_code( $response ) ) {
return [];
}
$body = json_decode( wp_remote_retrieve_body( $response ), true );
// A non-JSON or scalar body would otherwise reach the REST route as `data`.
return is_array( $body ) ? $body : [];
}
public function smart_update_notice_dismiss( $dismiss_id ) {
add_filter( 'wpaas_api_url', [ $this, 'wp_public_api_url' ] );
add_filter( 'wpaas_api_http_args', [ $this, 'v3_api_http_args' ] );
add_filter( 'wpaas_api_http_post_body_json', '__return_true' );
$response = $this->call( 'smart-updates/dismiss', [ 'dismissId' => $dismiss_id ], 'POST' );
if ( is_wp_error( $response ) ) {
return false;
}
remove_filter( 'wpaas_api_url', [ $this, 'wp_public_api_url' ] );
remove_filter( 'wpaas_api_http_args', [ $this, 'v3_api_http_args' ] );
remove_filter( 'wpaas_api_http_post_body_json', '__return_true' );
return true;
}
public function hmt_register( $key ) {
add_filter( 'wpaas_api_url', [ $this, 'wp_public_api_url' ] );
add_filter( 'wpaas_api_http_args', [ $this, 'v3_api_http_args' ] );
add_filter( 'wpaas_api_http_post_body_json', '__return_true' );
$accountUid = defined( 'GD_ACCOUNT_UID' ) ? GD_ACCOUNT_UID : '';
$data = [
'mwp_potential_key' => $key
];
try {
$response = $this->call( 'backup/' . $accountUid . '/register', $data, 'POST' );
$body = json_decode( wp_remote_retrieve_body( $response ), true );
$success = ! empty( $body['success'] ) ? $body['success'] : null;
} catch ( WP_Error $e ) {
return null;
}
remove_filter( 'wpaas_api_url', [ $this, 'wp_public_api_url' ] );
remove_filter( 'wpaas_api_http_args', [ $this, 'v3_api_http_args' ] );
remove_filter( 'wpaas_api_http_post_body_json', '__return_true' );
return $success;
}
private function wp_public_api_accountuid( $path ) {
$accountUid = defined( 'GD_ACCOUNT_UID' ) ? GD_ACCOUNT_UID : '';
return sprintf(
$path,
$accountUid
);
}
/**
* Fetch an experiment result from the WP Public API.
*
* @param string $exp_name The experiment name to query.
*
* @return bool|null True if experiment should show, false if not, null on failure.
*/
public function get_experiment( $exp_name ) {
add_filter( 'wpaas_api_url', [ $this, 'wp_public_api_url' ] );
add_filter( 'wpaas_api_http_args', [ $this, 'v3_api_http_args' ] );
add_filter( 'wpaas_api_http_args', [ $this, 'timeout_3s_http_args' ] );
try {
$accountUid = defined( 'GD_ACCOUNT_UID' ) ? GD_ACCOUNT_UID : '';
$response = $this->call( 'experiment/' . $accountUid, [ 'exp_name' => $exp_name ] );
$body = json_decode( wp_remote_retrieve_body( $response ), true );
return ! empty( $body['show'] );
} catch ( WP_Error $e ) {
return null;
} finally {
remove_filter( 'wpaas_api_url', [ $this, 'wp_public_api_url' ] );
remove_filter( 'wpaas_api_http_args', [ $this, 'v3_api_http_args' ] );
remove_filter( 'wpaas_api_http_args', [ $this, 'timeout_3s_http_args' ] );
}
}
public function show_nps_form($npsFormId) {
add_filter( 'wpaas_api_url', [ $this, 'wp_public_api_url' ] );
add_filter( 'wpaas_api_http_args', [ $this, 'v3_api_http_args' ] );
try {
$accountUid = defined( 'GD_ACCOUNT_UID' ) ? GD_ACCOUNT_UID : '';
$response = $this->call( 'feedback-api-proxy/' . $accountUid . '/' . rawurlencode($npsFormId) . '/show');
$body = json_decode( wp_remote_retrieve_body( $response ), true );
return $body['show'];
} catch ( WP_Error $e ) {
return null;
} finally {
remove_filter( 'wpaas_api_url', [ $this, 'wp_public_api_url' ] );
remove_filter( 'wpaas_api_http_args', [ $this, 'v3_api_http_args' ] );
}
}
public function get_nps_form_configuration($nps_form_id) {
add_filter( 'wpaas_api_url', [ $this, 'wp_public_api_url' ] );
add_filter( 'wpaas_api_http_args', [ $this, 'v3_api_http_args' ] );
try {
$accountUid = defined( 'GD_ACCOUNT_UID' ) ? GD_ACCOUNT_UID : '';
$response = $this->call( 'feedback-api-proxy/' . $accountUid . '/' . rawurlencode($nps_form_id) . '/form-configuration');
// Validate response before processing
if ( 200 !== wp_remote_retrieve_response_code( $response ) || is_wp_error( $response ) ) {
return null;
}
$body = json_decode( wp_remote_retrieve_body( $response ), true );
return $body['config'];
} catch ( \Exception $e ) {
return null;
} finally {
remove_filter( 'wpaas_api_url', [ $this, 'wp_public_api_url' ] );
remove_filter( 'wpaas_api_http_args', [ $this, 'v3_api_http_args' ] );
}
return null;
}
public function nps_form_submit($form_data, $nps_form_id) {
add_filter( 'wpaas_api_url', [ $this, 'wp_public_api_url' ] );
add_filter( 'wpaas_api_http_args', [ $this, 'v3_api_http_args' ] );
add_filter( 'wpaas_api_http_post_body_json', '__return_true' );
try {
$accountUid = defined( 'GD_ACCOUNT_UID' ) ? GD_ACCOUNT_UID : '';
$data = [
'accountUid' => $accountUid,
'feedbackId' => $nps_form_id,
'data' => $form_data
];
$this->call( 'feedback-api-proxy/nps-submit', $data, 'POST' );
} catch ( WP_Error $e ) {
return null;
}
$nps_option_name = 'wpaas_nps_form_%s';
$option_name = sprintf( $nps_option_name, GD_ACCOUNT_UID );
$cached_data = [
'show_form' => false,
'timestamp' => time()
];
update_option( $option_name, $cached_data );
remove_filter( 'wpaas_api_url', [ $this, 'wp_public_api_url' ] );
remove_filter( 'wpaas_api_http_args', [ $this, 'v3_api_http_args' ] );
remove_filter( 'wpaas_api_http_post_body_json', '__return_true' );
return true;
}
public function nps_form_dismiss($nps_form_id) {
add_filter( 'wpaas_api_url', [ $this, 'wp_public_api_url' ] );
add_filter( 'wpaas_api_http_args', [ $this, 'v3_api_http_args' ] );
add_filter( 'wpaas_api_http_post_body_json', '__return_true' );
try {
$accountUid = defined( 'GD_ACCOUNT_UID' ) ? GD_ACCOUNT_UID : '';
$data = [
'accountUid' => $accountUid,
'feedbackId' => $nps_form_id
];
$response = $this->call( 'feedback-api-proxy/nps/dismiss', $data, 'POST' );
json_decode( wp_remote_retrieve_body( $response ), true );
} catch ( WP_Error $e ) {
return null;
}
$nps_option_name = 'wpaas_nps_form_%s';
$option_name = sprintf( $nps_option_name, GD_ACCOUNT_UID );
$cached_data = [
'show_form' => false,
'timestamp' => time()
];
update_option( $option_name, $cached_data );
remove_filter( 'wpaas_api_url', [ $this, 'wp_public_api_url' ] );
remove_filter( 'wpaas_api_http_args', [ $this, 'v3_api_http_args' ] );
remove_filter( 'wpaas_api_http_post_body_json', '__return_true' );
return true;
}
public function feedback_form_submit ($form_data, $feedback_form_id) {
add_filter( 'wpaas_api_url', [ $this, 'wp_public_api_url' ] );
add_filter( 'wpaas_api_http_args', [ $this, 'v3_api_http_args' ] );
add_filter( 'wpaas_api_http_post_body_json', '__return_true' );
try {
$accountUid = defined( 'GD_ACCOUNT_UID' ) ? GD_ACCOUNT_UID : '';
$data = [
'accountUid' => $accountUid,
'feedbackId' => $feedback_form_id,
'data' => $form_data
];
$this->call( 'feedback-api-proxy/submit', $data, 'POST' );
} catch ( WP_Error $e ) {
return null;
}
remove_filter( 'wpaas_api_url', [ $this, 'wp_public_api_url' ] );
remove_filter( 'wpaas_api_http_args', [ $this, 'v3_api_http_args' ] );
remove_filter( 'wpaas_api_http_post_body_json', '__return_true' );
return true;
}
}
Zerion Mini Shell 1.0
Page not found – Jefferson County Republican Party
Skip to content
Page load link