Skip to content

Commit

Permalink
Merge pull request #164 from mailjet/feature/30162_Handle_api_timeout
Browse files Browse the repository at this point in the history
Feature/30162 handle api timeout
  • Loading branch information
goshon authored Feb 11, 2019
2 parents c1f511a + a896227 commit 0aea4c2
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 88 deletions.
66 changes: 57 additions & 9 deletions src/includes/MailjetActivator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,67 @@

namespace MailjetPlugin\Includes;

/**
* Triggered on mailjet plugin activation
* Check if server php version is higher enough
*/
class MailjetActivator
{

/**
* Short Description. (use period)
*
* Long Description.
*
* @since 5.0.0
*/
public static function activate()
function __construct()
{
add_action('admin_init', array($this, 'check_version'));

// Don't run anything else in the plugin, if we're on an incompatible WordPress version
if (!self::compatible_version()) {
return;
}
}

}
// The primary sanity check, automatically disable the plugin on activation if it doesn't
// meet minimum requirements.
function activation_check()
{
if (!self::compatible_version()) {
deactivate_plugins(plugin_basename(__FILE__));
$phpVersion = phpversion();
$message = sprintf(esc_html__('Mailjet for WordPress requires PHP 5.5 or later. Your server currently runs on PHP %s. Please upgrade your PHP and activate the plugin again.', 'mailjet-for-wordpress'), $phpVersion);
wp_die($message);
}
}

// The backup sanity check, in case the plugin is activated in a weird way,
// or the versions change after activation.
function check_version()
{
if (!self::compatible_version()) {
if (is_plugin_active(plugin_basename(__FILE__))) {
deactivate_plugins(plugin_basename(__FILE__));
add_action('admin_notices', array($this, 'disabled_notice'));
if (isset($_GET['activate'])) {
unset($_GET['activate']);
}
}
}
}

function disabled_notice()
{
$phpVersion = phpversion();
$message = sprintf(esc_html__('Mailjet for WordPress requires PHP 5.5 or later. Your server currently runs on PHP %s. Please upgrade your PHP and activate the plugin again.', 'mailjet-for-wordpress'), $phpVersion);
echo '<strong>' . $message . '</strong>';
}

static function compatible_version()
{
$phpVersion = phpversion();
if (version_compare($phpVersion, '5.5', '<')) {
return false;
}

// Add sanity checks for other version requirements here

return true;
}

}
74 changes: 59 additions & 15 deletions src/includes/MailjetApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ public static function getMailjetContactLists()
'Limit' => '0',
'Sort' => 'Name ASC'
];
$response = $mjApiClient->get(Resources::$Contactslist, ['filters' => $filters]);
try {
$response = $mjApiClient->get(Resources::$Contactslist, ['filters' => $filters]);
} catch (\GuzzleHttp\Exception\ConnectException $e) {
return false;
}
if ($response->success()) {
return $response->getData();
} else {
Expand All @@ -83,7 +87,11 @@ public static function createMailjetContactList($listName)
$body = [
'Name' => $listName
];
$response = $mjApiClient->post(Resources::$Contactslist, ['body' => $body]);
try {
$response = $mjApiClient->post(Resources::$Contactslist, ['body' => $body]);
} catch (\GuzzleHttp\Exception\ConnectException $e) {
return false;
}
return $response;
}

Expand All @@ -100,7 +108,11 @@ public static function isContactListActive($contactListId)
$filters = array(
'ID' => $contactListId
);
$response = $mjApiClient->get(Resources::$Contactslist, array('filters' => $filters));
try{
$response = $mjApiClient->get(Resources::$Contactslist, array('filters' => $filters));
} catch (\GuzzleHttp\Exception\ConnectException $e) {
return false;
}
if ($response->success()) {
$data = $response->getData();
if (isset($data[0]['IsDeleted'])) {
Expand All @@ -113,12 +125,20 @@ public static function isContactListActive($contactListId)

public static function getContactProperties()
{

$mjApiClient = self::getApiClient();
$filters = array(
'limit' => 0,
'Sort' => 'Name ASC'
);
$response = $mjApiClient->get(Resources::$Contactmetadata, array('filters' => $filters));

try {
$response = $mjApiClient->get(Resources::$Contactmetadata, array('filters' => $filters));
} catch (\GuzzleHttp\Exception\ConnectException $e) {
return false;
}


if ($response->success()) {
return $response->getData();
} else {
Expand Down Expand Up @@ -159,7 +179,11 @@ public static function createMailjetContactProperty($name, $type = "str")
'Name' => $name,
'NameSpace' => "static"
];
$response = $mjApiClient->post(Resources::$Contactmetadata, ['body' => $body]);
try {
$response = $mjApiClient->post(Resources::$Contactmetadata, ['body' => $body]);
} catch (\GuzzleHttp\Exception\ConnectException $e) {
return false;
}
if ($response->success()) {
return $response->getData();
} else {
Expand All @@ -176,8 +200,11 @@ public static function getMailjetSenders()
'Limit' => '0',
'Sort' => 'ID DESC'
];

$response = $mjApiClient->get(Resources::$Sender, ['filters' => $filters]);
try {
$response = $mjApiClient->get(Resources::$Sender, ['filters' => $filters]);
} catch (\GuzzleHttp\Exception\ConnectException $e) {
return false;
}
if ($response->success()) {
return $response->getData();
} else {
Expand All @@ -197,8 +224,11 @@ public static function isValidAPICredentials()
$filters = [
'Limit' => '1'
];

$response = $mjApiClient->get(Resources::$Contactmetadata, ['filters' => $filters]);
try {
$response = $mjApiClient->get(Resources::$Contactmetadata, ['filters' => $filters]);
} catch (\GuzzleHttp\Exception\ConnectException $e) {
return false;
}
if ($response->success()) {
return true;
// return $response->getData();
Expand All @@ -224,8 +254,11 @@ public static function syncMailjetContacts($contactListId, $contacts, $action =
'Action' => $action,
'Contacts' => $contacts
];

$response = $mjApiClient->post(Resources::$ContactslistManagemanycontacts, ['id' => $contactListId, 'body' => $body]);
try {
$response = $mjApiClient->post(Resources::$ContactslistManagemanycontacts, ['id' => $contactListId, 'body' => $body]);
} catch (\GuzzleHttp\Exception\ConnectException $e) {
return false;
}
if ($response->success()) {
return $response->getData();
} else {
Expand All @@ -247,7 +280,11 @@ public static function syncMailjetContact($contactListId, $contact, $action = 'a
'Email' => $contact['Email'],
'Properties' => $contact['Properties']
];
$response = $mjApiClient->post(Resources::$ContactslistManagecontact, ['id' => $contactListId, 'body' => $body]);
try {
$response = $mjApiClient->post(Resources::$ContactslistManagecontact, ['id' => $contactListId, 'body' => $body]);
} catch (\GuzzleHttp\Exception\ConnectException $e) {
return false;
}
if ($response->success()) {
return $response->getData();
} else {
Expand All @@ -273,8 +310,11 @@ public static function checkContactSubscribedToList($email, $listId)
'ContactEmail' => $email,
'ContactsList' => $listId,
];

$response = $mjApiClient->get(Resources::$Listrecipient, ['filters' => $filters]);
try {
$response = $mjApiClient->get(Resources::$Listrecipient, ['filters' => $filters]);
} catch (\GuzzleHttp\Exception\ConnectException $e) {
return false;
}

if ($response->success() && $response->getCount() > 0) {
$data = $response->getData();
Expand All @@ -290,7 +330,11 @@ public static function checkContactSubscribedToList($email, $listId)
public static function getProfileName()
{
$mjApiClient = self::getApiClient();
$response = $mjApiClient->get(Resources::$Myprofile, []);
try {
$response = $mjApiClient->get(Resources::$Myprofile, []);
} catch (\GuzzleHttp\Exception\ConnectException $e) {
return false;
}
$name = "";
if ($response->success() && $response->getCount() > 0) {
$data = $response->getData();
Expand Down
65 changes: 1 addition & 64 deletions wp-mailjet.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
//use Analog\Analog;
use MailjetPlugin\Includes\Mailjet;
use MailjetPlugin\Includes\MailjetUpdate;
use MailjetPlugin\Includes\MailjetActivator;

/**
* Mailjet plugin version.
Expand Down Expand Up @@ -80,67 +81,3 @@ function run_mailjet()

run_mailjet();

/**
* Triggered on mailjet plugin activation
* Check if server php version is higher enough
*/
class MailjetActivator
{

function __construct()
{
add_action('admin_init', array($this, 'check_version'));

// Don't run anything else in the plugin, if we're on an incompatible WordPress version
if (!self::compatible_version()) {
return;
}
}

// The primary sanity check, automatically disable the plugin on activation if it doesn't
// meet minimum requirements.
function activation_check()
{
if (!self::compatible_version()) {
deactivate_plugins(plugin_basename(__FILE__));
$phpVersion = phpversion();
$message = sprintf(esc_html__('Mailjet for WordPress requires PHP 5.5 or later. Your server currently runs on PHP %s. Please upgrade your PHP and activate the plugin again.', 'mailjet-for-wordpress'), $phpVersion);
wp_die($message);
}
}

// The backup sanity check, in case the plugin is activated in a weird way,
// or the versions change after activation.
function check_version()
{
if (!self::compatible_version()) {
if (is_plugin_active(plugin_basename(__FILE__))) {
deactivate_plugins(plugin_basename(__FILE__));
add_action('admin_notices', array($this, 'disabled_notice'));
if (isset($_GET['activate'])) {
unset($_GET['activate']);
}
}
}
}

function disabled_notice()
{
$phpVersion = phpversion();
$message = sprintf(esc_html__('Mailjet for WordPress requires PHP 5.5 or later. Your server currently runs on PHP %s. Please upgrade your PHP and activate the plugin again.', 'mailjet-for-wordpress'), $phpVersion);
echo '<strong>' . $message . '</strong>';
}

static function compatible_version()
{
$phpVersion = phpversion();
if (version_compare($phpVersion, '5.5', '<')) {
return false;
}

// Add sanity checks for other version requirements here

return true;
}

}

0 comments on commit 0aea4c2

Please sign in to comment.