Skip to content

Commit

Permalink
Merge pull request #105 from newfold-labs/fix/wp-function-namespacing
Browse files Browse the repository at this point in the history
add slash to namespace wp functions
  • Loading branch information
circlecube authored Nov 7, 2024
2 parents 002cc3d + 3787f5a commit da6d2ea
Showing 1 changed file with 44 additions and 32 deletions.
76 changes: 44 additions & 32 deletions includes/HiiveConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class HiiveConnection implements SubscriberInterface {
private $throttled;

/**
* The throttle
*
* @var bool
*/
protected $throttle;
Expand Down Expand Up @@ -88,7 +90,7 @@ public function ajax_verify() {
'token' => $_REQUEST['token'],
'valid' => $valid,
);
wp_send_json( $data, $status );
\wp_send_json( $data, $status );
}

/**
Expand Down Expand Up @@ -124,6 +126,10 @@ public static function is_connected(): bool {
*
* @used-by Data::init()
* @used-by HiiveConnection::reconnect()
*
* @param string $path the path
* @param string $authorization the authorization
* @return Boolean success
*/
public function connect( string $path = '/sites/v2/connect', ?string $authorization = null ): bool {

Expand All @@ -133,15 +139,15 @@ public function connect( string $path = '/sites/v2/connect', ?string $authorizat

$this->throttle();

$token = md5( wp_generate_password() );
$token = md5( \wp_generate_password() );
Transient::set( 'nfd_data_verify_token', $token, 5 * constant( 'MINUTE_IN_SECONDS' ) );

$data = $this->get_core_data();
$data['verify_token'] = $token;
$data['plugins'] = PluginHelper::collect_installed();

$args = array(
'body' => wp_json_encode( $data ),
'body' => \wp_json_encode( $data ),
'headers' => array(
'Content-Type' => 'application/json',
'Accept' => 'application/json',
Expand All @@ -155,18 +161,18 @@ public function connect( string $path = '/sites/v2/connect', ?string $authorizat
}

$attempts = intval( get_option( 'nfd_data_connection_attempts', 0 ) );
update_option( 'nfd_data_connection_attempts', $attempts + 1 );
\update_option( 'nfd_data_connection_attempts', $attempts + 1 );

$response = wp_remote_post( $this->api . $path, $args );
$status = wp_remote_retrieve_response_code( $response );
$response = \wp_remote_post( $this->api . $path, $args );
$status = \wp_remote_retrieve_response_code( $response );

// Created = 201; Updated = 200
if ( 201 === $status || 200 === $status ) {
$body = json_decode( wp_remote_retrieve_body( $response ) );
$body = json_decode( \wp_remote_retrieve_body( $response ) );
if ( ! empty( $body->token ) ) {

// Token is auto-encrypted using the `pre_update_option_nfd_data_token` hook.
update_option( 'nfd_data_token', $body->token );
\update_option( 'nfd_data_token', $body->token );
return true;
}
}
Expand Down Expand Up @@ -202,7 +208,7 @@ public function throttle() {
*/
public function get_throttle_interval() {

$attempts = intval( get_option( 'nfd_data_connection_attempts', 0 ) );
$attempts = intval( \get_option( 'nfd_data_connection_attempts', 0 ) );

// Throttle intervals step-up:
// Hourly for 4 hours
Expand Down Expand Up @@ -239,7 +245,7 @@ public function is_throttled() {
*
* @used-by Events::create_item()
*
* @param Event $event
* @param Event $event the event
*
* @phpstan-type Notification_Array array{id:string,locations:array,query:string|null,expiration:int,content:string}
* @return array<Notification_Array>|WP_Error
Expand All @@ -258,14 +264,18 @@ public function send_event( Event $event ) {
return $hiive_response;
}

$status_code = wp_remote_retrieve_response_code( $hiive_response );
$status_code = \wp_remote_retrieve_response_code( $hiive_response );

if ( ! in_array( $status_code, array( 200, 201 ), true ) ) {
return new \WP_Error( $status_code, wp_remote_retrieve_response_message( $hiive_response ) );
return new \WP_Error( $status_code, \wp_remote_retrieve_response_message( $hiive_response ) );
}

/** @var array{data:array{id:string,locations:array,query:string|null,expiration:int,content:string}} $response_payload */
$response_payload = json_decode( wp_remote_retrieve_body( $hiive_response ), true );
/**
* Sample shape.
*
* @var array{data:array{id:string,locations:array,query:string|null,expiration:int,content:string}} $response_payload
* */
$response_payload = json_decode( \wp_remote_retrieve_body( $hiive_response ), true );

return $response_payload['data'] ?? array();
}
Expand All @@ -289,12 +299,12 @@ public function notify( $events ) {

$hiive_response = $this->hiive_request( 'sites/v2/events', $payload );

if ( is_wp_error( ( $hiive_response ) ) ) {
if ( \is_wp_error( ( $hiive_response ) ) ) {
return $hiive_response;
}

if ( ! in_array( wp_remote_retrieve_response_code( $hiive_response ), array( 200, 201, 500 ) ) ) {
return new WP_Error( wp_remote_retrieve_response_code( $hiive_response ), wp_remote_retrieve_response_message( $hiive_response ) );
if ( ! in_array( \wp_remote_retrieve_response_code( $hiive_response ), array( 200, 201, 500 ), true ) ) {
return new WP_Error( \wp_remote_retrieve_response_code( $hiive_response ), \wp_remote_retrieve_response_message( $hiive_response ) );
}

$response_body = json_decode( wp_remote_retrieve_body( $hiive_response ), true );
Expand All @@ -315,14 +325,16 @@ public function notify( $events ) {
* Defaults to POST. Override with `$args = array('method' => 'GET')`.
*
* @param string $path The Hiive api path (after /api/).
* @param array|null $payload
* @param array|null $args
* @param array|null $payload the payload
* @param array|null $args and args for the request
*
* @return array|WP_Error The response array or a WP_Error when no Hiive connection, no network connection, network requests disabled.
*/
public function hiive_request( string $path, ?array $payload = array(), ?array $args = array() ) {

/**
* Add plugin name/version to user agent
*
* @see \WP_Http::request()
* @see https://developer.wordpress.org/reference/hooks/http_headers_useragent/
*/
Expand All @@ -341,19 +353,19 @@ public function hiive_request( string $path, ?array $payload = array(), ?array $
'Accept' => 'application/json',
'Authorization' => 'Bearer ' . self::get_auth_token(),
),
'timeout' => wp_is_serving_rest_request() ? 15 : 60, // If we're responding to the frontend, we need to be quick.
'timeout' => \wp_is_serving_rest_request() ? 15 : 60, // If we're responding to the frontend, we need to be quick.
);

$parsed_args = wp_parse_args( $args ?? array(), $defaults );
$parsed_args = \wp_parse_args( $args ?? array(), $defaults );

if ( ! empty( $payload ) ) {
$parsed_args['body'] = wp_json_encode( $payload );
$parsed_args['body'] = \wp_json_encode( $payload );
}

$request_response = wp_remote_request( "{$this->api}/{$path}", $parsed_args );
$request_response = \wp_remote_request( "{$this->api}/{$path}", $parsed_args );

// E.g. Hiive is down, or the site has disabled HTTP requests.
if ( is_wp_error( $request_response ) ) {
if ( \is_wp_error( $request_response ) ) {
return $request_response;
}

Expand All @@ -369,7 +381,7 @@ public function hiive_request( string $path, ?array $payload = array(), ?array $
}
}

remove_filter( 'http_headers_useragent', array( $this, 'add_plugin_name_version_to_user_agent' ) );
\remove_filter( 'http_headers_useragent', array( $this, 'add_plugin_name_version_to_user_agent' ) );

return $request_response;
}
Expand All @@ -382,7 +394,7 @@ public function hiive_request( string $path, ?array $payload = array(), ?array $
* @return string|false The decrypted token if it's set
*/
public static function get_auth_token() {
return get_option( 'nfd_data_token' );
return \get_option( 'nfd_data_token' );
}

/**
Expand All @@ -395,17 +407,17 @@ public function get_core_data() {
$container = container();

$data = array(
'brand' => sanitize_title( $container->plugin()->brand ),
'cache_level' => intval( get_option( 'newfold_cache_level', 2 ) ),
'cloudflare' => get_option( 'newfold_cloudflare_enabled', false ),
'brand' => \sanitize_title( $container->plugin()->brand ),
'cache_level' => intval( \get_option( 'newfold_cache_level', 2 ) ),
'cloudflare' => \get_option( 'newfold_cloudflare_enabled', false ),
'data' => defined( 'NFD_DATA_MODULE_VERSION' ) ? constant( 'NFD_DATA_MODULE_VERSION' ) : '0.0',
'email' => get_option( 'admin_email' ),
'email' => \get_option( 'admin_email' ),
'hostname' => gethostname(),
'mysql' => $wpdb->db_version(),
'origin' => $container->plugin()->get( 'id', 'error' ),
'php' => phpversion(),
'plugin' => $container->plugin()->get( 'version', '0' ),
'url' => get_site_url(),
'url' => \get_site_url(),
'username' => get_current_user(),
'wp' => $wp_version,
'server_path' => defined( 'ABSPATH' ) ? constant( 'ABSPATH' ) : '',
Expand All @@ -424,7 +436,7 @@ public function get_core_data() {
*/
public function add_plugin_name_version_to_user_agent( string $user_agent, string $url ): string {
$container = container();
$plugin_brand = sanitize_title( $container->plugin()->brand );
$plugin_brand = \sanitize_title( $container->plugin()->brand );
$plugin_version = $container->plugin()->get( 'version', '0' );

$user_agent_parts = array_map( 'trim', explode( ';', $user_agent ) );
Expand Down

0 comments on commit da6d2ea

Please sign in to comment.