From 42284c88a9009b0987b7181d7400a1a70208853c Mon Sep 17 00:00:00 2001 From: Evan Mullins Date: Wed, 6 Nov 2024 16:22:18 -0500 Subject: [PATCH 1/3] add slash to namespace wp functions --- includes/HiiveConnection.php | 56 ++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/includes/HiiveConnection.php b/includes/HiiveConnection.php index b8871c7..ec81e68 100644 --- a/includes/HiiveConnection.php +++ b/includes/HiiveConnection.php @@ -88,7 +88,7 @@ public function ajax_verify() { 'token' => $_REQUEST['token'], 'valid' => $valid, ); - wp_send_json( $data, $status ); + \wp_send_json( $data, $status ); } /** @@ -133,7 +133,7 @@ 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(); @@ -141,7 +141,7 @@ public function connect( string $path = '/sites/v2/connect', ?string $authorizat $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', @@ -155,18 +155,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; } } @@ -202,7 +202,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 @@ -258,14 +258,14 @@ 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 ); + $response_payload = json_decode( \wp_remote_retrieve_body( $hiive_response ), true ); return $response_payload['data'] ?? array(); } @@ -289,12 +289,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 ) ) ) { + 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 ); @@ -341,19 +341,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; } @@ -369,7 +369,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; } @@ -382,7 +382,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' ); } /** @@ -395,17 +395,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' ) : '', @@ -424,7 +424,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 ) ); From c38bc5433837cd3b8cc68b846d0d6bee45c17a8f Mon Sep 17 00:00:00 2001 From: Evan Mullins Date: Wed, 6 Nov 2024 16:31:50 -0500 Subject: [PATCH 2/3] lint updates --- includes/HiiveConnection.php | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/includes/HiiveConnection.php b/includes/HiiveConnection.php index ec81e68..1c1d49b 100644 --- a/includes/HiiveConnection.php +++ b/includes/HiiveConnection.php @@ -35,6 +35,8 @@ class HiiveConnection implements SubscriberInterface { private $throttled; /** + * The throttle + * * @var bool */ protected $throttle; @@ -124,6 +126,10 @@ public static function is_connected(): bool { * * @used-by Data::init() * @used-by HiiveConnection::reconnect() + * + * @param string $path the path + * @param $authorization the authorization + * @return Boolean success */ public function connect( string $path = '/sites/v2/connect', ?string $authorization = null ): bool { @@ -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|WP_Error @@ -264,7 +270,10 @@ public function send_event( Event $event ) { 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 */ + /** + * 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(); @@ -293,7 +302,7 @@ public function notify( $events ) { return $hiive_response; } - if ( ! in_array( \wp_remote_retrieve_response_code( $hiive_response ), array( 200, 201, 500 ) ) ) { + 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 ) ); } @@ -315,14 +324,15 @@ 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/ */ From 3787f5a689e32d432789caeff5b722dd9439e1a7 Mon Sep 17 00:00:00 2001 From: Evan Mullins Date: Wed, 6 Nov 2024 16:35:08 -0500 Subject: [PATCH 3/3] lint updates --- includes/HiiveConnection.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/includes/HiiveConnection.php b/includes/HiiveConnection.php index 1c1d49b..f9a7a77 100644 --- a/includes/HiiveConnection.php +++ b/includes/HiiveConnection.php @@ -35,7 +35,7 @@ class HiiveConnection implements SubscriberInterface { private $throttled; /** - * The throttle + * The throttle * * @var bool */ @@ -128,7 +128,7 @@ public static function is_connected(): bool { * @used-by HiiveConnection::reconnect() * * @param string $path the path - * @param $authorization the authorization + * @param string $authorization the authorization * @return Boolean success */ public function connect( string $path = '/sites/v2/connect', ?string $authorization = null ): bool { @@ -272,6 +272,7 @@ public function send_event( Event $event ) { /** * 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 ); @@ -333,6 +334,7 @@ public function hiive_request( string $path, ?array $payload = array(), ?array $ /** * Add plugin name/version to user agent + * * @see \WP_Http::request() * @see https://developer.wordpress.org/reference/hooks/http_headers_useragent/ */