Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hooks to Sophi requests #257

Merged
merged 7 commits into from
Apr 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions includes/classes/SiteAutomation/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
27 changes: 27 additions & 0 deletions includes/classes/SiteAutomation/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,40 @@ private function request( $timeout ) {
],
];

/**
* Filters the arguments used in Sophi HTTP request.
*
* @since 1.0.14
* @hook sophi_request_args
*
* @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 );

if ( function_exists( 'vip_safe_wp_remote_get' ) ) {
$request = vip_safe_wp_remote_get( $this->api_url, '', 3, $timeout, 20, $args );
} else {
$args['timeout'] = $timeout;
$request = wp_remote_get( $this->api_url, $args ); // phpcs:ignore
}

/**
* Filters a Sophi HTTP request immediately after the response is received.
*
* @since 1.0.14
* @hook sophi_request_result
*
* @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 );

if ( is_wp_error( $request ) ) {
return $request;
}
Expand Down
41 changes: 40 additions & 1 deletion includes/functions/content-sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,20 @@ 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
* @hook sophi_tracking_data
*
* @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(
[
'schema' => 'iglu:com.sophi/content_update/jsonschema/2-0-3',
Expand All @@ -146,6 +160,19 @@ 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.
*/
do_action_ref_array( 'sophi_tracking_result', array( $data, &$tracker, $post, $action ) );
}

/**
Expand All @@ -171,8 +198,20 @@ function init_tracker() {
);
}

/**
* Whether to turn on emitter debug
*
* @since 1.0.14
* @hook sophi_tracker_emitter_debug
*
* @param {bool} $debug Debug is active.
*
* @return {bool} Whether to turn on emitter debug.
*/
$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 );
}
Expand Down