-
Notifications
You must be signed in to change notification settings - Fork 2
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
Client usage tracking #15
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
<?php | ||
/** | ||
* Omnisend Client | ||
* | ||
* @package OmnisendClient | ||
*/ | ||
|
||
namespace Omnisend\Internal\V1; | ||
|
||
use Omnisend\Public\V1\Contact; | ||
use WP_Error; | ||
|
||
defined( 'ABSPATH' ) || die( 'no direct access' ); | ||
|
||
class Client implements \Omnisend\Public\V1\Client { | ||
|
||
private string $api_key; | ||
private string $plugin_name; | ||
private string $plugin_version; | ||
|
||
/** | ||
* @param string $plugin_name | ||
* @param string $plugin_version | ||
* @param string $api_key | ||
*/ | ||
public function __construct( string $api_key, string $plugin_name, string $plugin_version ) { | ||
$this->api_key = $api_key; | ||
$this->plugin_name = substr( $plugin_name, 0, 50 ); | ||
$this->plugin_version = substr( $plugin_version, 0, 50 ); | ||
} | ||
|
||
|
||
public function create_contact( $contact ): mixed { | ||
$error = new WP_Error(); | ||
|
||
if ( $contact instanceof Contact ) { | ||
$error->merge_from( $contact->validate() ); | ||
} else { | ||
$error->add( 'contact', 'Contact is not instance of Omnisend\Public\V1\Contact' ); | ||
} | ||
|
||
$error->merge_from( $this->check_setup() ); | ||
|
||
if ( $error->has_errors() ) { | ||
return $error; | ||
} | ||
|
||
$response = wp_remote_post( | ||
OMNISEND_CORE_API_V3 . '/contacts', | ||
array( | ||
'body' => wp_json_encode( $contact->to_array() ), | ||
'headers' => array( | ||
'Content-Type' => 'application/json', | ||
'X-API-Key' => $this->api_key, | ||
'X-INTEGRATION-NAME' => $this->plugin_name, | ||
'X-INTEGRATION-VERSION' => $this->plugin_version, | ||
), | ||
'timeout' => 10, | ||
) | ||
); | ||
|
||
if ( is_wp_error( $response ) ) { | ||
error_log('wp_remote_post error: ' . $response->get_error_message()); // phpcs:ignore | ||
return $response; | ||
} | ||
|
||
$http_code = wp_remote_retrieve_response_code( $response ); | ||
if ( $http_code >= 400 ) { | ||
$body = wp_remote_retrieve_body( $response ); | ||
$err_msg = "HTTP error: {$http_code} - " . wp_remote_retrieve_response_message( $response ) . " - {$body}"; | ||
$error->add( 'omnisend_api', $err_msg ); | ||
return $error; | ||
} | ||
|
||
$body = wp_remote_retrieve_body( $response ); | ||
if ( ! $body ) { | ||
$error->add( 'omnisend_api', 'empty response' ); | ||
return $error; | ||
} | ||
|
||
$arr = json_decode( $body, true ); | ||
|
||
if ( empty( $arr['contactID'] ) ) { | ||
$error->add( 'omnisend_api', 'contactID not found in response' ); | ||
return $error; | ||
} | ||
|
||
return (string) $arr['contactID']; | ||
} | ||
|
||
/** | ||
* @return WP_Error | ||
*/ | ||
private function check_setup(): WP_Error { | ||
$error = new WP_Error(); | ||
|
||
if ( ! $this->plugin_name ) { | ||
$error->add( 'initialisation', 'Client is created with empty plugin name' ); | ||
} | ||
|
||
if ( ! $this->plugin_version ) { | ||
$error->add( 'initialisation', 'Client is created with empty plugin version' ); | ||
} | ||
|
||
if ( ! $this->api_key ) { | ||
$error->add( 'api_key', 'Omnisend plugin is not connected' ); | ||
} | ||
|
||
return $error; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
/** | ||
* Omnisend Client | ||
* | ||
* @package OmnisendClient | ||
*/ | ||
|
||
namespace Omnisend\Public\V1; | ||
|
||
use WP_Error; | ||
|
||
defined( 'ABSPATH' ) || die( 'no direct access' ); | ||
|
||
/** | ||
* Client to interact with Omnisend. | ||
* | ||
*/ | ||
interface Client { | ||
|
||
/** | ||
* Create a contact in Omnisend. For it to succeed ensure that provided contact at least have email or phone number. | ||
* | ||
* @param Contact $contact | ||
* | ||
* @return string|WP_Error Created/updated contact identifier (ID) or WP_Error | ||
*/ | ||
public function create_contact( $contact ): mixed; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
/** | ||
* Omnisend Client | ||
* | ||
* @package OmnisendClient | ||
*/ | ||
|
||
namespace Omnisend\Public\V1; | ||
|
||
use Omnisend\Internal\Options; | ||
|
||
defined( 'ABSPATH' ) || die( 'no direct access' ); | ||
|
||
class Omnisend { | ||
|
||
/** | ||
* Factory to create Omnisend client. | ||
* | ||
* @param $plugin string plugin using client name | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add comment what this function does There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added |
||
* @param $version string plugin using client version | ||
* @return Client | ||
*/ | ||
public static function get_client( $plugin, $version ): Client { | ||
return new \Omnisend\Internal\V1\Client( Options::get_api_key(), (string) $plugin, (string) $version ); | ||
} | ||
|
||
/** | ||
* Check and return if plugin connected to Omnisend account. If connection does not exist, it will not be possible | ||
* to send data to Omnisend. | ||
* | ||
* @return bool | ||
*/ | ||
public static function is_connected(): bool { | ||
return Options::get_api_key() != ''; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe throw error here? User shouldn't encounter this error as dev will be forced to provide value
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I try not to break client website. Throwing error could take all website down, I think we should avoid this. This class will not be created directly. Only trough Factory, where we ask to provide values, I think this should be enough to inform about this requirement.