From a1823f2f13085d995c195bdfdf2142b219a6c44b Mon Sep 17 00:00:00 2001 From: Max Lyuchin Date: Fri, 29 Apr 2022 09:20:28 +0300 Subject: [PATCH 1/6] Filters in request --- includes/classes/SiteAutomation/Request.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/includes/classes/SiteAutomation/Request.php b/includes/classes/SiteAutomation/Request.php index 62e03b09..e91d7efc 100644 --- a/includes/classes/SiteAutomation/Request.php +++ b/includes/classes/SiteAutomation/Request.php @@ -206,6 +206,16 @@ private function request( $timeout ) { ], ]; + /** + * Filters the arguments used in Sophi HTTP request. + * + * @since 1.0.14 + * + * @param array $args An array of HTTP request arguments. + * @param string $url The request URL. + */ + $args = apply_filters( 'sophi_request_args', $args, $this->api_url ); + if ( function_exists( 'vip_safe_wp_remote_get' ) ) { $request = vip_safe_wp_remote_get( $this->api_url, '', 3, $timeout, 20, $args ); } else { @@ -213,6 +223,17 @@ private function request( $timeout ) { $request = wp_remote_get( $this->api_url, $args ); // phpcs:ignore } + /** + * Filters a Sophi HTTP request immediately after the response is received. + * + * @since 2.9.0 + * + * @param array|WP_Error $response HTTP response. + * @param array $args HTTP request arguments. + * @param string $url The request URL. + */ + $request = apply_filters( 'sophi_request_result', $request, $args, $this->api_url ); + if ( is_wp_error( $request ) ) { return $request; } From 44e60dbd8a89f48de67aea7c907b49cff2fdaed3 Mon Sep 17 00:00:00 2001 From: Max Lyuchin Date: Fri, 29 Apr 2022 09:20:58 +0300 Subject: [PATCH 2/6] Filters in request auth token --- includes/classes/SiteAutomation/Auth.php | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/includes/classes/SiteAutomation/Auth.php b/includes/classes/SiteAutomation/Auth.php index cddc97fe..a2c147a4 100644 --- a/includes/classes/SiteAutomation/Auth.php +++ b/includes/classes/SiteAutomation/Auth.php @@ -64,19 +64,26 @@ public function refresh_access_token() { * @return array|\WP_Error */ public function request_access_token( $client_id, $client_secret ) { - $body = [ + $body = [ 'client_id' => $client_id, 'client_secret' => $client_secret, 'audience' => $this->get_audience(), 'grant_type' => 'client_credentials', ]; - $request = wp_remote_post( - $this->get_auth_url(), - [ - 'headers' => [ 'Content-Type' => 'application/json' ], - 'body' => wp_json_encode( $body ), - ] - ); + $args = [ + 'headers' => [ 'Content-Type' => 'application/json' ], + 'body' => wp_json_encode( $body ), + ]; + + $auth_url = $this->get_auth_url(); + + /** This filter is documented in includes/classes/SiteAutomation/Request.php */ + $args = apply_filters( 'sophi_request_args', $args, $auth_url ); + + $request = wp_remote_post( $auth_url, $args ); + + /** This filter is documented in includes/classes/SiteAutomation/Request.php */ + $request = apply_filters( 'sophi_request_result', $request, $args, $this->api_url ); if ( is_wp_error( $request ) ) { return $request; From c943d3feee4a0c6211a8d47af56cfa83ed0a584f Mon Sep 17 00:00:00 2001 From: Max Lyuchin Date: Fri, 29 Apr 2022 11:14:42 +0300 Subject: [PATCH 3/6] Hooks for event tracker --- includes/functions/content-sync.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/includes/functions/content-sync.php b/includes/functions/content-sync.php index ab6bfb77..053fbe56 100644 --- a/includes/functions/content-sync.php +++ b/includes/functions/content-sync.php @@ -131,6 +131,17 @@ function send_track_event( $tracker, $post, $action ) { delete_transient( 'sophi_content_sync_pending_' . $post->ID ); } + /** + * Filters the data used in Sophi track event request. + * + * @since 1.0.14 + * + * @param array $data Tracking data. + * @param Tracker $tracker Tracker being used. + * @param string $url Post object. + * @param string $action Publishing action. + */ + $data = apply_filters_ref_array( 'sophi_tracking_data', array( $data, &$tracker, $post, $action ) ); $tracker->trackUnstructEvent( [ 'schema' => 'iglu:com.sophi/content_update/jsonschema/2-0-3', @@ -146,6 +157,18 @@ function send_track_event( $tracker, $post, $action ) { ], ] ); + + /** + * Fires after tracker sends the request. + * + * @since 1.0.14 + * + * @param array $data Tracked data. + * @param Tracker $tracker Tracker object. + * @param WP_Post $post Post object. + * @param string $action Publishing action. + */ + do_action_ref_array( 'sophi_tracking_result', array( $data, &$tracker, $post, $action ) ); } /** From 68c3e8359e77790124899c2ae5a85a22a2aed5b1 Mon Sep 17 00:00:00 2001 From: Max Lyuchin Date: Fri, 29 Apr 2022 11:15:04 +0300 Subject: [PATCH 4/6] Hook for tracker emitter debug mode --- includes/functions/content-sync.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/includes/functions/content-sync.php b/includes/functions/content-sync.php index 053fbe56..f4cc87cc 100644 --- a/includes/functions/content-sync.php +++ b/includes/functions/content-sync.php @@ -194,8 +194,17 @@ function init_tracker() { ); } + /** + * Turns on emitter debug + * + * @since 1.0.14 + * + * @param bool $debug Debug is active. + */ + $debug = apply_filters( 'sophi_tracker_emitter_debug', false ); + $app_id = sprintf( '%s:cms', $tracker_client_id ); - $emitter = new SyncEmitter( $collector_url, 'https', 'POST', 1, false ); + $emitter = new SyncEmitter( $collector_url, 'https', 'POST', 1, $debug ); $subject = new Subject(); return new Tracker( $emitter, $subject, 'sophiTag', $app_id, false ); } From 4cc848542b6ff248477caeb7492ed826c69b8f9a Mon Sep 17 00:00:00 2001 From: Max Lyuchin Date: Fri, 29 Apr 2022 11:46:53 +0300 Subject: [PATCH 5/6] Since version fix --- includes/classes/SiteAutomation/Request.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/classes/SiteAutomation/Request.php b/includes/classes/SiteAutomation/Request.php index e91d7efc..e8488ef2 100644 --- a/includes/classes/SiteAutomation/Request.php +++ b/includes/classes/SiteAutomation/Request.php @@ -226,7 +226,7 @@ private function request( $timeout ) { /** * Filters a Sophi HTTP request immediately after the response is received. * - * @since 2.9.0 + * @since 1.0.14 * * @param array|WP_Error $response HTTP response. * @param array $args HTTP request arguments. From 79ddc034c5cc3b4f6112f15de59cc09fbaaf124d Mon Sep 17 00:00:00 2001 From: Max Lyuchin Date: Fri, 29 Apr 2022 11:56:32 +0300 Subject: [PATCH 6/6] Update hook docs --- includes/classes/SiteAutomation/Request.php | 16 ++++++++---- includes/functions/content-sync.php | 27 +++++++++++++-------- 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/includes/classes/SiteAutomation/Request.php b/includes/classes/SiteAutomation/Request.php index e8488ef2..57f6bc71 100644 --- a/includes/classes/SiteAutomation/Request.php +++ b/includes/classes/SiteAutomation/Request.php @@ -210,9 +210,12 @@ private function request( $timeout ) { * Filters the arguments used in Sophi HTTP request. * * @since 1.0.14 + * @hook sophi_request_args * - * @param array $args An array of HTTP request arguments. - * @param string $url The request URL. + * @param {array} $args HTTP request arguments. + * @param {string} $url The request URL. + * + * @return {array} HTTP request arguments. */ $args = apply_filters( 'sophi_request_args', $args, $this->api_url ); @@ -227,10 +230,13 @@ private function request( $timeout ) { * Filters a Sophi HTTP request immediately after the response is received. * * @since 1.0.14 + * @hook sophi_request_result * - * @param array|WP_Error $response HTTP response. - * @param array $args HTTP request arguments. - * @param string $url The request URL. + * @param {array|WP_Error} $request Result of HTTP request. + * @param {array} $args HTTP request arguments. + * @param {string} $url The request URL. + * + * @return {array|WP_Error} Result of HTTP request. */ $request = apply_filters( 'sophi_request_result', $request, $args, $this->api_url ); diff --git a/includes/functions/content-sync.php b/includes/functions/content-sync.php index f4cc87cc..65b4b415 100644 --- a/includes/functions/content-sync.php +++ b/includes/functions/content-sync.php @@ -135,11 +135,14 @@ function send_track_event( $tracker, $post, $action ) { * Filters the data used in Sophi track event request. * * @since 1.0.14 + * @hook sophi_tracking_data * - * @param array $data Tracking data. - * @param Tracker $tracker Tracker being used. - * @param string $url Post object. - * @param string $action Publishing action. + * @param {array} $data Tracking data to send. + * @param {Tracker} $tracker Tracker being used. + * @param {string} $url Post object. + * @param {string} $action Publishing action. + * + * @return {array} Tracking data to send. */ $data = apply_filters_ref_array( 'sophi_tracking_data', array( $data, &$tracker, $post, $action ) ); $tracker->trackUnstructEvent( @@ -162,11 +165,12 @@ function send_track_event( $tracker, $post, $action ) { * Fires after tracker sends the request. * * @since 1.0.14 + * @hook sophi_tracking_result * - * @param array $data Tracked data. - * @param Tracker $tracker Tracker object. - * @param WP_Post $post Post object. - * @param string $action Publishing action. + * @param {array} $data Tracked data. + * @param {Tracker} $tracker Tracker object. + * @param {WP_Post} $post Post object. + * @param {string} $action Publishing action. */ do_action_ref_array( 'sophi_tracking_result', array( $data, &$tracker, $post, $action ) ); } @@ -195,11 +199,14 @@ function init_tracker() { } /** - * Turns on emitter debug + * Whether to turn on emitter debug * * @since 1.0.14 + * @hook sophi_tracker_emitter_debug + * + * @param {bool} $debug Debug is active. * - * @param bool $debug Debug is active. + * @return {bool} Whether to turn on emitter debug. */ $debug = apply_filters( 'sophi_tracker_emitter_debug', false );