Skip to content

Commit

Permalink
Merge pull request #369 from mailjet/DE-1270-php-warnings-in-debug-lo…
Browse files Browse the repository at this point in the history
…g-wp-mailjet-plugin

Some code improvements
  • Loading branch information
oleksandr-mykhailenko authored Mar 20, 2024
2 parents e086284 + 1f92dc7 commit 2d6eea0
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 63 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## Changelog

##### 5.5.4
* Code improvements. Fix possible warnings and notices.

##### 5.5.0
* Fixed bug with redirect problem during initial step
* Fixed links in the readme file and in the plugin
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
- Tags: email, marketing, signup, newsletter, widget, smtp, woocommerce, contact form 7
- Requires at least: 4.4
- Tested up to: 6.4.3
- Stable tag: 5.5.3
- Stable tag: 5.5.4
- Requires PHP: 7.4
- License: GPLv2 or later
- License URI: http://www.gnu.org/licenses/gpl-2.0.html
Expand Down
2 changes: 1 addition & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Contributors: Mailjet
Tags: email, marketing, signup, newsletter, widget, smtp, woocommerce, contact form 7
Requires at least: 4.4
Tested up to: 6.4.3
Stable tag: 5.5.3
Stable tag: 5.5.4
Requires PHP: 7.4
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Expand Down
4 changes: 2 additions & 2 deletions src/includes/Mailjet.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function __construct()
if (\defined('MAILJET_VERSION')) {
$this->version = MAILJET_VERSION;
} else {
$this->version = '5.5.1';
$this->version = '5.5.4';
}
$this->plugin_name = 'mailjet';
$this->load_dependencies();
Expand All @@ -75,7 +75,7 @@ public function __construct()
$this->addMailjetPHPMailer();
$this->registerMailjetWidget();

add_shortcode('mailjet_form_builder', array($this, 'display_mailjet_form_builder_widget'));
add_shortcode('mailjet_form_builder', [$this, 'display_mailjet_form_builder_widget']);
}

/**
Expand Down
50 changes: 31 additions & 19 deletions src/includes/MailjetApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@

/**
* Define the internationalization functionality.
*
* Loads and defines the internationalization files for this plugin
* so that it is ready for translation.
*
* @since 5.0.0
* @package Mailjet
* @subpackage Mailjet/includes
Expand Down Expand Up @@ -117,9 +115,9 @@ public static function isContactListActive($contactListId)
} catch (Exception $e) {
return \false;
}
$filters = array('ID' => $contactListId);
$filters = ['ID' => $contactListId];
try {
$response = $mjApiClient->get(Resources::$Contactslist, array('filters' => $filters));
$response = $mjApiClient->get(Resources::$Contactslist, ['filters' => $filters]);
} catch (ConnectException $e) {
return \false;
}
Expand Down Expand Up @@ -147,9 +145,9 @@ public static function getContactListByID($contactListId)
} catch (Exception $e) {
return \false;
}
$filters = array('ID' => $contactListId);
$filters = ['ID' => $contactListId];
try {
$response = $mjApiClient->get(Resources::$Contactslist, array('filters' => $filters));
$response = $mjApiClient->get(Resources::$Contactslist, ['filters' => $filters]);
} catch (ConnectException $e) {
return \false;
}
Expand All @@ -174,17 +172,17 @@ public static function getSubscribersFromList($contactListId)
return \false;
}
$limit = 1000;
$dataArray = array();
$dataArray = [];
$offset = 0;
do {
$filters = array('ContactsList' => $contactListId, 'Unsub' => \false, 'Offset' => $offset, 'Limit' => $limit, 'Style' => 'Full');
$filters = ['ContactsList' => $contactListId, 'Unsub' => \false, 'Offset' => $offset, 'Limit' => $limit, 'Style' => 'Full'];
try {
$response = $mjApiClient->get(Resources::$Listrecipient, array('filters' => $filters));
$response = $mjApiClient->get(Resources::$Listrecipient, ['filters' => $filters]);
} catch (ConnectException $e) {
return \false;
}
if ($response->success()) {
\array_push($dataArray, $response->getData());
$dataArray[] = $response->getData();
} else {
return \false;
}
Expand All @@ -203,9 +201,9 @@ public static function getContactProperties()
} catch (Exception $e) {
return \false;
}
$filters = array('limit' => 0, 'Sort' => 'Name ASC');
$filters = ['limit' => 0, 'Sort' => 'Name ASC'];
try {
$response = $mjApiClient->get(Resources::$Contactmetadata, array('filters' => $filters));
$response = $mjApiClient->get(Resources::$Contactmetadata, ['filters' => $filters]);
} catch (ConnectException $e) {
return \false;
}
Expand All @@ -231,6 +229,7 @@ public static function getPropertyIdByName($name)
}
return \false;
}

public static function createMailjetContactProperty($name, $type = "str")
{
if (empty($name)) {
Expand Down Expand Up @@ -358,9 +357,9 @@ public static function isValidAPICredentials()

return false;
}

/**
* Add or Remove a contact to a Mailjet contact list - It can process many or single contact at once
*
* @param $contactListId - int - ID of the contact list to sync contacts
* @param $contacts - array('Email' => ContactEmail, 'Name' => ContactName, 'Properties' => array(propertyName1 => propertyValue1, ...));
* @param string $action - 'addforce', 'adnoforce', 'remove'
Expand All @@ -385,6 +384,7 @@ public static function syncMailjetContacts($contactListId, $contacts, $action =

return false;
}

/**
* Add a contact to a Mailjet contact list
*/
Expand All @@ -395,8 +395,11 @@ public static function syncMailjetContact($contactListId, $contact, $action = 'a
} catch (Exception $e) {
return \false;
}
$name = $contact['Properties']['firstname'] ?? '';
$body = ['Name' => $name, 'Action' => $action, 'Email' => $contact['Email'], 'Properties' => $contact['Properties']];
$name = '';
if (isset($contact['Properties'])) {
$name = $contact['Properties']['firstname'] ?? '';
}
$body = ['Name' => $name, 'Action' => $action, 'Email' => $contact['Email'], 'Properties' => $contact['Properties'] ?? []];
try {
$response = $mjApiClient->post(Resources::$ContactslistManagecontact, ['id' => $contactListId, 'body' => $body]);
} catch (ConnectException $e) {
Expand All @@ -408,11 +411,12 @@ public static function syncMailjetContact($contactListId, $contact, $action = 'a

return \false;
}

/**
* Return TRUE if a contact already subscribed to the list and FALSE if it is not, or is added to the list but Unsubscribed
*
* @param $email
* @param $listId
* @param bool $getContactId
* @return bool
*/
public static function checkContactSubscribedToList($email, $listId, $getContactId = \false)
Expand All @@ -432,8 +436,8 @@ public static function checkContactSubscribedToList($email, $listId, $getContact
}
if ($response->success() && $response->getCount() > 0) {
$data = $response->getData();
$exists = \true;
if (isset($data[0]['IsUnsubscribed']) && \false == $data[0]['IsUnsubscribed']) {
$exists = true;
if (isset($data[0]['IsUnsubscribed']) && !$data[0]['IsUnsubscribed']) {
$existsAndSubscribed = \true;
}
if ($getContactId && $exists && $existsAndSubscribed) {
Expand All @@ -442,6 +446,7 @@ public static function checkContactSubscribedToList($email, $listId, $getContact
}
return $exists && $existsAndSubscribed;
}

/**
* @throws Exception
*/
Expand All @@ -459,6 +464,7 @@ public static function isContactInList($email, $listId, $getContactId = \false)
}
return \true;
}

public static function getContactDataByEmail($contactEmail)
{
try {
Expand All @@ -472,15 +478,17 @@ public static function getContactDataByEmail($contactEmail)
}
return \false;
}

/**
* @throws Exception
*/
public static function updateContactData($contactEmail, $data)
{
$mjApiClient = self::getApiClient();
$body = ['Data' => $data];
$body = ['Data' => $data ?? []];
return $mjApiClient->put(['contactdata', $contactEmail], ['body' => $body]);
}

public static function getProfileName()
{
try {
Expand All @@ -502,6 +510,7 @@ public static function getProfileName()
}
return $name;
}

public static function getTemplateByName($templateName)
{
try {
Expand All @@ -520,6 +529,7 @@ public static function getTemplateByName($templateName)
return \false;
}
}

public static function getTemplateDetails($id)
{
try {
Expand All @@ -538,6 +548,7 @@ public static function getTemplateDetails($id)
}
return \false;
}

public static function createTemplate(array $arguments)
{
try {
Expand All @@ -560,6 +571,7 @@ public static function createTemplate(array $arguments)
}
return \false;
}

public static function createTemplateContent(array $content)
{
try {
Expand Down
6 changes: 3 additions & 3 deletions src/includes/MailjetLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function add_filter($hook, $component, $callback, $priority = 10, $accept
*/
private function add($hooks, $hook, $component, $callback, $priority, $accepted_args)
{
$hooks[] = array('hook' => $hook, 'component' => $component, 'callback' => $callback, 'priority' => $priority, 'accepted_args' => $accepted_args);
$hooks[] = ['hook' => $hook, 'component' => $component, 'callback' => $callback, 'priority' => $priority, 'accepted_args' => $accepted_args];
return $hooks;
}
/**
Expand All @@ -98,10 +98,10 @@ private function add($hooks, $hook, $component, $callback, $priority, $accepted_
public function run()
{
foreach ($this->filters as $hook) {
add_filter($hook['hook'], array($hook['component'], $hook['callback']), $hook['priority'], $hook['accepted_args']);
add_filter($hook['hook'], [$hook['component'], $hook['callback']], $hook['priority'], $hook['accepted_args']);
}
foreach ($this->actions as $hook) {
add_action($hook['hook'], array($hook['component'], $hook['callback']), $hook['priority'], $hook['accepted_args']);
add_action($hook['hook'], [$hook['component'], $hook['callback']], $hook['priority'], $hook['accepted_args']);
}
}
}
16 changes: 8 additions & 8 deletions src/includes/MailjetLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,63 +10,63 @@ public static function log($message, $level = null)
{
$mailjetActivateLogger = get_option('mailjet_activate_logger');
if (empty($mailjetActivateLogger) || $mailjetActivateLogger != 1) {
return;
return '';
}
return Analog::log($message, $level);
}
public static function urgent($message)
{
$mailjetActivateLogger = get_option('mailjet_activate_logger');
if (empty($mailjetActivateLogger) || $mailjetActivateLogger != 1) {
return;
return '';
}
return Analog::urgent($message);
}
public static function alert($message)
{
$mailjetActivateLogger = get_option('mailjet_activate_logger');
if (empty($mailjetActivateLogger) || $mailjetActivateLogger != 1) {
return;
return '';
}
return Analog::alert($message);
}
public static function error($message)
{
$mailjetActivateLogger = get_option('mailjet_activate_logger');
if (empty($mailjetActivateLogger) || $mailjetActivateLogger != 1) {
return;
return '';
}
return Analog::error($message);
}
public static function warning($message)
{
$mailjetActivateLogger = get_option('mailjet_activate_logger');
if (empty($mailjetActivateLogger) || $mailjetActivateLogger != 1) {
return;
return '';
}
return Analog::warning($message);
}
public static function notice($message)
{
$mailjetActivateLogger = get_option('mailjet_activate_logger');
if (empty($mailjetActivateLogger) || $mailjetActivateLogger != 1) {
return;
return '';
}
return Analog::notice($message);
}
public static function info($message)
{
$mailjetActivateLogger = get_option('mailjet_activate_logger');
if (empty($mailjetActivateLogger) || $mailjetActivateLogger != 1) {
return;
return '';
}
return Analog::info($message);
}
public static function debug($message)
{
$mailjetActivateLogger = get_option('mailjet_activate_logger');
if (empty($mailjetActivateLogger) || $mailjetActivateLogger != 1) {
return;
return '';
}
return Analog::debug($message);
}
Expand Down
16 changes: 7 additions & 9 deletions src/includes/MailjetMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*/
class MailjetMail
{
const MJ_HOST = 'in-v3.mailjet.com';
const MJ_MAILER = 'X-Mailer:WP-Mailjet/0.1';
public const MJ_HOST = 'in-v3.mailjet.com';
public const MJ_MAILER = 'X-Mailer:WP-Mailjet/0.1';
public function __construct()
{
if (\version_compare(get_bloginfo('version'), '5.5-alpha', '<')) {
Expand All @@ -29,7 +29,7 @@ public function __construct()
}
public function phpmailer_init_smtp($phpmailer)
{
if (!get_option('mailjet_enabled') || 0 == get_option('mailjet_enabled')) {
if (!get_option('mailjet_enabled') || 0 === (int)get_option('mailjet_enabled')) {
return;
}
$phpmailer->Mailer = 'smtp';
Expand Down Expand Up @@ -58,7 +58,7 @@ public function wp_mail_failed_cb($wpError)
public function wp_mail_failed_admin_notice()
{
global $pagenow;
if ($pagenow == 'index.php') {
if ($pagenow === 'index.php') {
$user = wp_get_current_user();
if ($user->exists()) {
echo '<div class="notice notice-error is-dismissible">' . __('Email sending failed. Please review your smtp configuration and try again later', 'mailjet-for-wordpress') . '</div>';
Expand All @@ -70,15 +70,13 @@ public static function sendTestEmail()
$testSent = \false;
$mailjetTestAddress = get_option('mailjet_test_address');
if (empty($mailjetTestAddress)) {
//MailjetLogger::error('[ Mailjet ] [ ' . __METHOD__ . ' ] [ Line #' . __LINE__ . ' ] [ Missing email address to send test email to ]');
return;
return $testSent;
}
// Send a test mail
add_filter('wp_mail_content_type', array('MailjetWp\\MailjetPlugin\\Includes\\MailjetMail', 'set_html_content_type'));
add_filter('wp_mail_content_type', ['MailjetWp\\MailjetPlugin\\Includes\\MailjetMail', 'set_html_content_type']);
$subject = __('Your test mail from Mailjet', 'mailjet-for-wordpress');
$message = \sprintf(__('Your Mailjet configuration is ok! <br /> Site URL: %s <br /> SSL: %s <br /> Port: %s', 'mailjet-for-wordpress'), get_home_url(), get_option('mailjet_ssl') ? 'On' : 'Off', get_option('mailjet_port'));
$testSent = wp_mail(get_option('mailjet_test_address'), $subject, $message);
return $testSent;
return wp_mail(get_option('mailjet_test_address'), $subject, $message);
}
public static function set_html_content_type()
{
Expand Down
Loading

0 comments on commit 2d6eea0

Please sign in to comment.